Profit.php 9.1 KB

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