MycService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace crmeb\services;
  3. use app\admin\model\system\SystemLevelEquity;
  4. use app\admin\model\user\User;
  5. use app\admin\model\user\UserBill;
  6. use app\models\store\StoreOrder;
  7. use MongoDB\Driver\Exception\Exception;
  8. class MycService
  9. {
  10. /**
  11. * 公司分红
  12. * @param $data
  13. */
  14. public static function all_stock_right_bonus($data)
  15. {
  16. if(intval($data['all_stock_right'])==0) return true;
  17. $pre_day = strtotime(date('Y-m-d', strtotime('-1 day')));//昨日
  18. $before_day = strtotime(date('Y-m-d', strtotime('-2 day')));//前天
  19. $rs = SystemLevelEquity::where('equity_type', 'all_stock_right_bonus')->where('is_show', 1)->select()->toArray();
  20. $all_stock_equity_list = [];
  21. foreach ($rs as $v) {
  22. $all_stock_equity_list[$v['levelid']] = $v;
  23. }
  24. try {
  25. $list = UserBill::where('type', 'all_stock_right')->where('category', 'right')->where('add_time', '<', $pre_day)
  26. ->group('uid')->field("uid,sum(number) as all_stock_right")->select()->toArray();
  27. foreach ($list as $v) {
  28. $level = User::where('uid', $v['uid'])->value('level');
  29. $profit = bcmul($data['all_profit'], $all_stock_equity_list[$level]['proportion'] / 100, 2);
  30. $profit_thigh = bcdiv($profit, $data['all_stock_right'], 2);
  31. $member = bcmul($profit_thigh, $v['all_stock_right'], 2);
  32. User::where('uid', $v['uid'])->inc('brokerage_price', $member)->inc('all_stock_right_bonus', $member)->update();
  33. $mark = "公司利润".$data['all_profit'].",总计分红:".$profit.",共".$data['all_stock_right']."股,每股分润".$profit_thigh.",".$all_stock_equity_list[$level]['title'] . ",获得分红:" . $member;
  34. UserBill::income($all_stock_equity_list[$level]['real_title'], $v['uid'], 'bonus', 'all_stock_right_bonus', $member, 0, $member, $mark);
  35. }
  36. }catch (Exception $e)
  37. {
  38. return false;
  39. }
  40. return true;
  41. }
  42. /**
  43. * 高级合伙人分红
  44. * @param $data
  45. * @return bool
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public static function senior_stock_right_bonus($data)
  51. {
  52. if(intval($data['senior_stock_right'])==0) return true;
  53. $pre_day = strtotime(date('Y-m-d', strtotime('-1 day')));//昨日
  54. $before_day = strtotime(date('Y-m-d', strtotime('-2 day')));//前天
  55. $rs = SystemLevelEquity::where('equity_type', 'senior_stock_right_bonus')->where('is_show', 1)->select()->toArray();
  56. $all_stock_equity_list = [];
  57. foreach ($rs as $v) {
  58. $all_stock_equity_list[$v['levelid']] = $v;
  59. }
  60. try {
  61. $list = UserBill::where('type', 'senior_stock_right')->where('category', 'right')->where('add_time', '<', $pre_day)
  62. ->group('uid')->field("uid,sum(number) as senior_stock_right")->select()->toArray();
  63. foreach ($list as $v) {
  64. $level = User::where('uid', $v['uid'])->value('level');
  65. if(isset($all_stock_equity_list[$level]['proportion'])) {
  66. $profit = bcmul($data['all_profit'], $all_stock_equity_list[$level]['proportion'] / 100, 2);
  67. $profit_thigh = bcdiv($profit, $data['senior_stock_right'], 2);
  68. $member = bcmul($profit_thigh, $v['senior_stock_right'], 2);
  69. User::where('uid', $v['uid'])->inc('brokerage_price', $member)->inc('all_stock_right_bonus', $member)->update();
  70. $mark = "公司利润" . $data['all_profit'] . ",总计分红:" . $profit . ",共" . $data['senior_stock_right'] . "股,每股分润" . $profit_thigh . "," . $all_stock_equity_list[$level]['title'] . ",获得分红:" . $member;
  71. UserBill::income($all_stock_equity_list[$level]['real_title'], $v['uid'], 'bonus', 'senior_stock_right_bonus', $member, 0, $member, $mark);
  72. }
  73. }
  74. }catch (Exception $e)
  75. {
  76. return false;
  77. }
  78. return true;
  79. }
  80. /**
  81. * 全民分红
  82. * @param $data
  83. */
  84. public static function code_stock_right_bonus($data)
  85. {
  86. $rs = SystemLevelEquity::where('equity_type', 'code_stock_right_bonus')->where('is_show', 1)->select()->toArray();
  87. $code_stock_equity_list = [];
  88. foreach ($rs as $v) {
  89. $code_stock_equity_list[$v['levelid']] = $v;
  90. }
  91. try {
  92. if(isset($data['code'])) {
  93. foreach ($data['code'] as $key => $v) {
  94. $code_stock_right_sum = User::where('code', $key == 'system' ? '' : $key)->whereExp('code_stock_right_bonus', '<monetary')->sum('code_stock_right');
  95. if ($code_stock_right_sum == 0) continue;
  96. $userlist = User::where('code', $key == 'system' ? '' : $key)->where('code_stock_right_bonus', 'exp', "<monetary")->field('uid,level,code_stock_right,monetary,code_stock_right_bonus')->select()->toarray();
  97. foreach ($userlist as $v1) {
  98. $level = $v1['level'];
  99. $profit = bcmul($v['order_profit_sum'], bcdiv($code_stock_equity_list[$level]['proportion'], 100, 2));
  100. $profit_thigh = bcdiv($profit, $code_stock_right_sum, 2);
  101. $member = bcmul($profit_thigh, $v1['code_stock_right'], 2);
  102. if (bcsub($v1['monetary'], $v1['code_stock_right_bonus'], 2) < $member) {
  103. if (bcsub($v1['monetary'], $v1['code_stock_right_bonus'], 2) > 0) {
  104. $member = bcsub($v1['monetary'], $v1['code_stock_right_bonus'], 2);
  105. } else {
  106. continue;
  107. }
  108. }
  109. User::where('uid', $v1['uid'])->inc('brokerage_price', $member)->inc('code_stock_right_bonus', $member)->update();
  110. $mark = User::where('code', $key)->where('code_top', 1)->value('nickname') . "总代利润" . $v['order_profit_sum'] . ",总计分红:" . $profit . ",共" . $code_stock_right_sum . "股,每股分润" . $profit_thigh . "," . $code_stock_equity_list[$level]['title'] . ",获得分红:" . $member;
  111. UserBill::income($code_stock_equity_list[$level]['real_title'], $v1['uid'], 'bonus', 'code_stock_right_bonus', $member, 0, $member, $mark);
  112. }
  113. }
  114. }
  115. }catch (Exception $e)
  116. {
  117. return false;
  118. }
  119. return true;
  120. }
  121. /**
  122. * 赠送分红
  123. * @param $sum
  124. */
  125. public static function give_stock_right_bonus($data)
  126. {
  127. $rs = SystemLevelEquity::where('equity_type', 'give_stock_right_bonus')->where('is_show', 1)->select()->toArray();
  128. $code_stock_equity_list = [];
  129. foreach ($rs as $v) {
  130. $code_stock_equity_list[$v['levelid']] = $v;
  131. }
  132. try {
  133. if(isset($data['code'])) {
  134. foreach ($data['code'] as $key => $v) {
  135. $userlist = User::alias('a')->join("system_user_level b", "a.level=b.id", "left")->where('a.code', $key == 'system' ? '' : $key)->where('a.give_stock_right', '>', 0)->where('a.give_stock_right_bonus', 'exp', "<if(a.monetary>b.up_amount,a.monetary,b.up_amount)")->field('a.uid,a.level,a.give_stock_right,b.up_amount,a.monetary,if(a.monetary>b.up_amount,a.monetary,b.up_amount) as monetary_ok,a.give_stock_right_bonus')->select()->toarray();
  136. $give_stock_right_sum = User::alias('a')->join("system_user_level b", "a.level=b.id", "left")->where('a.code', $key == 'system' ? '' : $key)->where('a.give_stock_right', '>', 0)->where('a.give_stock_right_bonus', 'exp', "<if(a.monetary>b.up_amount,a.monetary,b.up_amount)")->sum('give_stock_right');
  137. if ($give_stock_right_sum == 0) continue;
  138. foreach ($userlist as $v1) {
  139. $level = $v1['level'];
  140. if ($give_stock_right_sum > 0) {
  141. if (isset($code_stock_equity_list[$level]['proportion'])) {
  142. $profit = bcmul($v['order_profit_sum'], $code_stock_equity_list[$level]['proportion'] / 100, 2);
  143. $profit_thigh = bcdiv($profit, $give_stock_right_sum, 2);
  144. $member = bcmul($profit_thigh, $v1['give_stock_right'], 2);
  145. if (bcsub($v1['monetary_ok'], $v1['give_stock_right_bonus'], 2) < $member) {
  146. if (bcsub($v1['monetary'], $v1['give_stock_right_bonus'], 2) > 0) {
  147. $member = bcsub($v1['monetary'], $v1['give_stock_right_bonus'], 2);
  148. } else {
  149. continue;
  150. }
  151. }
  152. User::where('uid', $v1['uid'])->inc('brokerage_price', $member)->inc('give_stock_right_bonus', $member)->update();
  153. $mark = User::where('code', $key)->where('code_top', 1)->value('nickname') . "总代利润" . $v['order_profit_sum'] . ",总计分红:" . $profit . ",共" . $give_stock_right_sum . "股,每股分润" . $profit_thigh . "," . $code_stock_equity_list[$level]['title'] . ",获得分红:" . $member;
  154. UserBill::income($code_stock_equity_list[$level]['real_title'], $v1['uid'], 'bonus', 'give_stock_right_bonus', $member, 0, $member, $mark);
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }catch (Exception $e)
  161. {
  162. return false;
  163. }
  164. return true;
  165. }
  166. /**
  167. * 推荐分红
  168. * @param $data
  169. */
  170. public static function recommend_stock_right_bonus($data)
  171. {
  172. if(intval($data['recommend_stock_right'])==0) return true;
  173. $rs = SystemLevelEquity::where('equity_type', 'recommend_bonus')->where('is_show', 1)->select()->toArray();
  174. $all_stock_equity_list = [];
  175. foreach ($rs as $v) {
  176. $all_stock_equity_list[$v['levelid']] = $v;
  177. }
  178. try {
  179. $list = UserBill::where('type', 'recommend_stock_right')->where('category', 'right')->where('add_time', '<', strtotime(date('Y-m-d')))
  180. ->group('uid')->field("uid,sum(number) as all_stock_right")->select()->toArray();
  181. foreach ($list as $v) {
  182. $level = User::where('uid', $v['uid'])->value('level');
  183. if(isset($all_stock_equity_list[$level]['proportion'])) {
  184. $profit_thigh = bcdiv(bcmul($data['all_profit'], $all_stock_equity_list[$level]['proportion'] / 100, 2), $data['recommend_stock_right'], 2);
  185. $member = bcmul($profit_thigh, $v['all_stock_right'], 2);
  186. User::where('uid', $v['uid'])->inc('brokerage_price', $member)->inc('recommend_stock_right_bonus', $member)->update();
  187. $mark = "公司利润" . $data['all_profit'] . ",总计分红:" . bcmul($data['all_profit'], $all_stock_equity_list[$level]['proportion'] / 100, 2) . ",共" . $data['recommend_stock_right'] . "股,每股分润".$all_stock_equity_list[$level]['title'] . ",获得分红:" . $member;
  188. UserBill::income($all_stock_equity_list[$level]['real_title'], $v['uid'], 'bonus', 'recommend_stock_right_bonus', $member, 0, $member, $mark);
  189. }
  190. }
  191. }catch (Exception $e)
  192. {
  193. return false;
  194. }
  195. return true;
  196. }
  197. /**
  198. * 总代推荐分红
  199. * @param $data
  200. */
  201. public static function code_recommend_stock_right_bonus($data)
  202. {
  203. if(intval($data['code_recommend_stock_right'])==0) return true;
  204. $rs = SystemLevelEquity::where('equity_type', 'code_recommend_bonus')->where('is_show', 1)->select()->toArray();
  205. $all_stock_equity_list = [];
  206. foreach ($rs as $v) {
  207. $all_stock_equity_list[$v['levelid']] = $v;
  208. }
  209. try {
  210. $list = UserBill::where('type', 'code_recommend_stock_right')->where('category', 'right')->where('add_time', '<', strtotime(date('Y-m-d')))
  211. ->group('uid')->field("uid,sum(number) as code_recommend_stock_right")->select()->toArray();
  212. foreach ($list as $v) {
  213. $level = User::where('uid', $v['uid'])->value('level');
  214. if(isset($all_stock_equity_list[$level]['proportion'])) {
  215. $profit_thigh = bcdiv(bcmul($data['all_profit'], $all_stock_equity_list[$level]['proportion'] / 100, 2), $data['code_recommend_stock_right'], 2);
  216. $member = bcmul($profit_thigh, $v['code_recommend_stock_right'], 2);
  217. User::where('uid', $v['uid'])->inc('brokerage_price', $member)->inc('recommend_stock_right_bonus', $member)->update();
  218. $mark = "公司利润" . $data['all_profit'] . ",总计分红:" . bcmul($data['all_profit'], $all_stock_equity_list[$level]['proportion'] / 100, 2) . ",共" . $data['code_recommend_stock_right'] . "股,每股分润". $all_stock_equity_list[$level]['title'] . ",获得分红:" . $member;
  219. UserBill::income($all_stock_equity_list[$level]['real_title'], $v['uid'], 'bonus', 'code_recommend_stock_right_bonus', $member, 0, $member, $mark);
  220. }
  221. }
  222. }catch (Exception $e)
  223. {
  224. return false;
  225. }
  226. return true;
  227. }
  228. }