Profit.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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['status_desc'] = '';
  63. if ($info['status'] == 1) {
  64. $info['status_desc'] = '未到账原因:需要等到购物订单确认收货后,收益即可到账';
  65. } elseif ($info['status'] == 4) {
  66. $info['status_desc'] = '失败原因:该订单商品已全部完成退款';
  67. } elseif ($info['status'] == 3) {
  68. $info['status_desc'] = '部分到账原因:有部分商品已退款,所以仅发放未退款的那部分收益';
  69. }
  70. $info['cdate_string'] = date('Y-m-d H:i', $info['cdate']);
  71. if ($info['fdate']) {
  72. $info['fdate_string'] = date('Y-m-d H:i', $info['fdate']);
  73. }
  74. return $info;
  75. }
  76. # 入账
  77. public function up_commit($uid, $cash, $profit, $status, $source, $source_id, $name = '', $desc = '')
  78. {
  79. if ($cash == 0) {
  80. return Dever::alert('金额不能为空0');
  81. }
  82. if (!is_array($profit)) {
  83. $profit = Dever::db('account/config_profit')->find(array('key' => $profit));
  84. }
  85. if (!$profit) {
  86. return Dever::alert('收益类型不存在');
  87. }
  88. $config = Dever::db('account/config')->find($profit['config_id']);
  89. if (!$config) {
  90. return Dever::alert('账户信息不存在');
  91. }
  92. $project = Dever::db('account/config_project')->find($config['project_id']);
  93. $user = Dever::db($project['source'])->find($uid);
  94. if (!$user) {
  95. return Dever::alert('账户信息不存在');
  96. }
  97. $info = $this->get($uid, $profit['id'], $config['id'], $config['project_id']);
  98. # 开始处理入账
  99. $this->handle($profit, $config, $uid, $cash, $info['id'], $project['source'], $status, $source, $source_id, $name, $desc);
  100. return true;
  101. }
  102. # 生成订单号
  103. private function createOrderNum()
  104. {
  105. $where['order_num'] = Dever::order('S');
  106. $state = Dever::db('account/profit_log')->one($where);
  107. if (!$state) {
  108. return $where['order_num'];
  109. } else {
  110. return $this->createOrderNum();
  111. }
  112. }
  113. # 计算金额
  114. private function handle($profit, $config, $uid, $cash, $profit_id, $project_source, $status, $source, $source_id, $name, $desc)
  115. {
  116. $rule = Dever::db('account/config_profit_rule')->select(array('profit_id' => $profit['id']));
  117. if ($rule) {
  118. $child = array();
  119. foreach ($rule as $k => $v) {
  120. $child[$v['level']] = $this->rule($v, $child, $config, $uid, $cash, $profit_id, $project_source, $status, $source, $source_id, $name, $desc);
  121. }
  122. }
  123. return $cash;
  124. }
  125. private function rule($rule, $child, $config, $uid, $cash, $profit_id, $project_source, $status, $source, $source_id, $name, $desc)
  126. {
  127. if ($rule['level'] > 0) {
  128. $parent = Dever::load('invite/api')->getParent($uid, $rule['level']);
  129. if ($parent) {
  130. $uid = $parent;
  131. } else {
  132. return false;
  133. }
  134. }
  135. $replace = array();
  136. $replace['{a}'] = $cash;
  137. $replace['{b}'] = $uid;
  138. $user = Dever::db($project_source)->find($uid);
  139. if (isset($user['role']) && $user['role']) {
  140. $temp = explode(',', $user['role']);
  141. $replace['{c}'] = $temp[0];
  142. if (isset($temp[1]) && $temp[1]) {
  143. $level = $temp[1];
  144. $level = Dever::db('user/role_level')->find($level);
  145. if ($level) {
  146. $replace['{d}'] = $level['level'];
  147. }
  148. }
  149. }
  150. # 验证条件
  151. $condition = $rule['condition'];
  152. if ($condition) {
  153. $check = $this->run($condition, $replace);
  154. if (!$check) {
  155. return false;
  156. }
  157. }
  158. $level = $rule['level'] - 1;
  159. if ($child && isset($child[$level]) && $child[$level]) {
  160. $replace['{e}'] = $child[$level];
  161. }
  162. # 计算公式
  163. $exp = $rule['exp'];
  164. if ($exp) {
  165. $cash = $this->run($exp, $replace);
  166. }
  167. if ($cash > 0) {
  168. $this->add($config, $uid, $cash, $profit_id, $status, $source, $source_id, $name, $desc);
  169. return $cash;
  170. }
  171. return false;
  172. }
  173. private function run($string, $replace)
  174. {
  175. $keys = array_keys($replace);
  176. $values = array_values($replace);
  177. $string = str_replace($keys, $values, $string);
  178. $eval = '$value = ' . $string . ';';
  179. eval($eval);
  180. return $value;
  181. }
  182. # 入账
  183. private function add($config, $uid, $cash, $profit_id, $status, $source, $source_id, $name = '', $desc = '')
  184. {
  185. $data['clear'] = true;
  186. $data['profit_id'] = $profit_id;
  187. $data['source'] = $source;
  188. $data['source_id'] = $source_id;
  189. $log = Dever::db('account/profit_log')->one($data);
  190. $data['uid'] = $uid;
  191. if ($name) {
  192. $data['name'] = $name;
  193. }
  194. if ($desc) {
  195. $data['desc'] = $desc;
  196. }
  197. $data['status'] = $status;
  198. if ($log) {
  199. # 结算
  200. if ($status < 5 && $cash > 0 && $log['cash'] != $cash) {
  201. $status = 3;
  202. if ($cash > $log['cash']) {
  203. $cash = $log['cash'];
  204. }
  205. }
  206. if ($status == 1) {
  207. $data['cash'] = $cash;
  208. } elseif ($status == 2) {
  209. $data['ycash'] = $cash;
  210. } elseif ($status == 3) {
  211. $data['ycash'] = $log['ycash'] + $cash;
  212. } elseif ($status == 4) {
  213. $data['ycash'] = 0;
  214. } elseif ($status == 5) {
  215. if ($log['status'] == 4) {
  216. return;
  217. }
  218. $data['status'] = 3;
  219. if ($cash == $log['cash']) {
  220. $data['status'] = 4;
  221. }
  222. $data['tcash'] = $cash;
  223. }
  224. $id = $data['where_id'] = $log['id'];
  225. Dever::db('account/profit_log')->update($data);
  226. } else {
  227. if ($status >= 4) {
  228. return;
  229. }
  230. # 入账
  231. $data['cash'] = $cash;
  232. if ($status == 2) {
  233. $data['ycash'] = $cash;
  234. }
  235. $data['order_num'] = $this->createOrderNum();
  236. $id = Dever::db('account/profit_log')->insert($data);
  237. }
  238. if ($id && $id > 0) {
  239. if (isset($data['cash']) && $data['cash']) {
  240. $update['where_id'] = $profit_id;
  241. $update['set_cash'] = $data['cash'];
  242. Dever::db('account/profit')->incCash($update);
  243. }
  244. if (isset($data['ycash']) && $data['ycash']) {
  245. $update['where_id'] = $profit_id;
  246. $update['set_ycash'] = $data['ycash'];
  247. Dever::db('account/profit')->incYcash($update);
  248. # 同步钱包
  249. Dever::load('account/lib/info.up_commit', $uid, $data['ycash'], 'shouyi', $config, $name, $source, $source_id, 1);
  250. }
  251. if (isset($data['tcash']) && $data['tcash']) {
  252. $update['where_id'] = $profit_id;
  253. $update['set_tcash'] = $data['tcash'];
  254. Dever::db('account/profit')->incTcash($update);
  255. # 同步钱包
  256. Dever::load('account/lib/info.up_commit', $uid, $data['tcash'], 'shouyi_tuikuan', $config, $name, $source, $source_id, 1);
  257. }
  258. }
  259. }
  260. }