Profit.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace Account\Lib;
  3. use Dever;
  4. class Profit
  5. {
  6. # 获取详情
  7. public function get($uid, $profit_id, $config_id, $project_id)
  8. {
  9. $data['uid'] = $uid;
  10. $data['config_id'] = $config_id;
  11. $data['project_id'] = $project_id;
  12. $data['config_profit_id'] = $profit_id;
  13. $data['clear'] = true;
  14. $info = Dever::db('account/profit')->find($data);
  15. if (!$info) {
  16. $info['id'] = Dever::db('account/profit')->insert($data);
  17. if (!$info['id']) {
  18. return Dever::alert('账户信息不存在');
  19. }
  20. $info['cash'] = '0.00';
  21. $info['ycash'] = '0.00';
  22. }
  23. return $info;
  24. }
  25. # 获取日志列表
  26. public function getList($uid, $profit_id)
  27. {
  28. $where['profit_id'] = $profit_id;
  29. $where['uid'] = $uid;
  30. $data = Dever::db('account/profit_log')->getData($where, function(&$info) {
  31. return $this->info($info);
  32. });
  33. return $data;
  34. }
  35. # 获取日志详情
  36. public function getInfo($id)
  37. {
  38. $data = Dever::db('account/profit_log')->find($id);
  39. if (!$data) {
  40. return false;
  41. }
  42. $data = $this->info($data);
  43. return $data;
  44. }
  45. # 获取详情
  46. private function info($info)
  47. {
  48. $info['username'] = '无';
  49. $profit = Dever::db('account/profit')->find($info['profit_id']);
  50. $status = Dever::db('account/profit_log')->config['status'];
  51. $config = Dever::db('account/config')->find($profit['config_id']);
  52. $project = Dever::db('account/config_project')->find($config['project_id']);
  53. //$source = Dever::db($project['source'])->find($info['uid']);
  54. $source = Dever::db($info['source'])->find($info['source_id']);
  55. if ($source) {
  56. $user = Dever::db($project['source'])->find($source['uid']);
  57. $info['username'] = $user['name'];
  58. $info['avatar'] = $user['avatar'];
  59. }
  60. $info['config_name'] = $config['name'];
  61. $info['status_name'] = Dever::status($status, $info['status']);
  62. $info['cdate_string'] = date('Y-m-d H:i', $info['cdate']);
  63. if ($info['fdate']) {
  64. $info['fdate_string'] = date('Y-m-d H:i', $info['fdate']);
  65. }
  66. return $info;
  67. }
  68. # 入账
  69. public function up_commit($uid, $cash, $profit, $status, $source, $source_id, $name = '', $desc = '')
  70. {
  71. if ($cash == 0) {
  72. return Dever::alert('金额不能为空0');
  73. }
  74. if (!is_array($profit)) {
  75. $profit = Dever::db('account/config_profit')->find(array('key' => $profit));
  76. }
  77. if (!$profit) {
  78. return Dever::alert('收益类型不存在');
  79. }
  80. $config = Dever::db('account/config')->find($profit['config_id']);
  81. if (!$config) {
  82. return Dever::alert('账户信息不存在');
  83. }
  84. $project = Dever::db('account/config_project')->find($config['project_id']);
  85. $user = Dever::db($project['source'])->find($uid);
  86. if (!$user) {
  87. return Dever::alert('账户信息不存在');
  88. }
  89. $info = $this->get($uid, $profit['id'], $config['id'], $config['project_id']);
  90. # 开始处理入账
  91. $this->handle($profit, $config, $uid, $cash, $info['id'], $project['source'], $status, $source, $source_id, $name, $desc);
  92. return true;
  93. }
  94. # 生成订单号
  95. private function createOrderNum()
  96. {
  97. $where['order_num'] = Dever::order('S');
  98. $state = Dever::db('account/profit_log')->one($where);
  99. if (!$state) {
  100. return $where['order_num'];
  101. } else {
  102. return $this->createOrderNum();
  103. }
  104. }
  105. # 计算金额
  106. private function handle($profit, $config, $uid, $cash, $profit_id, $project_source, $status, $source, $source_id, $name, $desc)
  107. {
  108. $rule = Dever::db('account/config_profit_rule')->select(array('profit_id' => $profit['id']));
  109. if ($rule) {
  110. $child = array();
  111. foreach ($rule as $k => $v) {
  112. $child[$v['level']] = $this->rule($v, $child, $config, $uid, $cash, $profit_id, $project_source, $status, $source, $source_id, $name, $desc);
  113. }
  114. }
  115. return $cash;
  116. }
  117. private function rule($rule, $child, $config, $uid, $cash, $profit_id, $project_source, $status, $source, $source_id, $name, $desc)
  118. {
  119. if ($rule['level'] > 0) {
  120. $parent = Dever::load('invite/api')->getParent($uid, $rule['level']);
  121. if ($parent) {
  122. $uid = $parent;
  123. } else {
  124. return false;
  125. }
  126. }
  127. $replace = array();
  128. $replace['{a}'] = $cash;
  129. $replace['{b}'] = $uid;
  130. $user = Dever::db($project_source)->find($uid);
  131. if (isset($user['role']) && $user['role']) {
  132. $temp = explode(',', $user['role']);
  133. $replace['{c}'] = $temp[0];
  134. if (isset($temp[1]) && $temp[1]) {
  135. $level = $temp[1];
  136. $level = Dever::db('user/role_level')->find($level);
  137. if ($level) {
  138. $replace['{d}'] = $level['level'];
  139. }
  140. }
  141. }
  142. # 验证条件
  143. $condition = $rule['condition'];
  144. if ($condition) {
  145. $check = $this->run($condition, $replace);
  146. if (!$check) {
  147. return false;
  148. }
  149. }
  150. $level = $rule['level'] - 1;
  151. if ($child && isset($child[$level]) && $child[$level]) {
  152. $replace['{e}'] = $child[$level];
  153. }
  154. # 计算公式
  155. $exp = $rule['exp'];
  156. if ($exp) {
  157. $cash = $this->run($exp, $replace);
  158. }
  159. if ($cash > 0) {
  160. $this->add($config, $uid, $cash, $profit_id, $status, $source, $source_id, $name, $desc);
  161. return $cash;
  162. }
  163. return false;
  164. }
  165. private function run($string, $replace)
  166. {
  167. $keys = array_keys($replace);
  168. $values = array_values($replace);
  169. $string = str_replace($keys, $values, $string);
  170. $eval = '$value = ' . $string . ';';
  171. eval($eval);
  172. return $value;
  173. }
  174. # 入账
  175. private function add($config, $uid, $cash, $profit_id, $status, $source, $source_id, $name = '', $desc = '')
  176. {
  177. $data['clear'] = true;
  178. $data['profit_id'] = $profit_id;
  179. $data['source'] = $source;
  180. $data['source_id'] = $source_id;
  181. $log = Dever::db('account/profit_log')->one($data);
  182. $data['uid'] = $uid;
  183. if ($name) {
  184. $data['name'] = $name;
  185. }
  186. if ($desc) {
  187. $data['desc'] = $desc;
  188. }
  189. $data['status'] = $status;
  190. if ($log) {
  191. # 结算
  192. if ($status < 5 && $cash > 0 && $log['cash'] != $cash) {
  193. $status = 3;
  194. if ($cash > $log['cash']) {
  195. $cash = $log['cash'];
  196. }
  197. }
  198. if ($status == 1) {
  199. $data['cash'] = $cash;
  200. } elseif ($status == 2) {
  201. $data['ycash'] = $cash;
  202. } elseif ($status == 3) {
  203. $data['ycash'] = $log['ycash'] + $cash;
  204. } elseif ($status == 4) {
  205. $data['ycash'] = 0;
  206. } elseif ($status == 5) {
  207. if ($log['status'] == 4) {
  208. return;
  209. }
  210. $data['status'] = 3;
  211. if ($cash == $log['cash']) {
  212. $data['status'] = 4;
  213. }
  214. $data['tcash'] = $cash;
  215. }
  216. $id = $data['where_id'] = $log['id'];
  217. Dever::db('account/profit_log')->update($data);
  218. } else {
  219. # 入账
  220. $data['cash'] = $cash;
  221. if ($status == 2) {
  222. $data['ycash'] = $cash;
  223. }
  224. $data['order_num'] = $this->createOrderNum();
  225. $id = Dever::db('account/profit_log')->insert($data);
  226. }
  227. if ($id && $id > 0) {
  228. if (isset($data['cash']) && $data['cash']) {
  229. $update['where_id'] = $profit_id;
  230. $update['set_cash'] = $data['cash'];
  231. Dever::db('account/profit')->incCash($update);
  232. }
  233. if (isset($data['ycash']) && $data['ycash']) {
  234. $update['where_id'] = $profit_id;
  235. $update['set_ycash'] = $data['ycash'];
  236. Dever::db('account/profit')->incYcash($update);
  237. # 同步钱包
  238. Dever::load('account/lib/info.up_commit', $uid, $data['ycash'], 'shouyi', $config, $name, $source, $source_id, 1);
  239. }
  240. if (isset($data['tcash']) && $data['tcash']) {
  241. $update['where_id'] = $profit_id;
  242. $update['set_tcash'] = $data['tcash'];
  243. Dever::db('account/profit')->incTcash($update);
  244. # 同步钱包
  245. Dever::load('account/lib/info.up_commit', $uid, $data['tcash'], 'shouyi_tuikuan', $config, $name, $source, $source_id, 1);
  246. }
  247. }
  248. }
  249. }