Profit.php 7.8 KB

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