StoreGp.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\models\store;
  3. use app\models\user\User;
  4. use app\models\user\UserBill;
  5. use crmeb\basic\BaseModel;
  6. use crmeb\traits\ModelTrait;
  7. use app\models\store\StoreMgp;
  8. use think\Exception;
  9. class StoreGp extends BaseModel
  10. {
  11. use ModelTrait;
  12. /**
  13. * 创建公排
  14. * @param $order
  15. */
  16. public static function createGp($data)
  17. {
  18. $order = $data['data'];
  19. @file_put_contents("job.txt",json_encode($order));
  20. try {
  21. $user = User::where('uid',$order['uid'])->find();
  22. $spuser = User::where('uid',$user['spread_uid'])->find();
  23. User::where('uid',$order['uid'])->update(['cycle_time'=>strtotime("+31 day")]);
  24. if($spuser)
  25. {
  26. User::where('uid',$spuser['uid'])->update(['cycle_time'=>strtotime("+31 day")]);
  27. $sp_uid = $spuser['uid'];
  28. }
  29. else
  30. {
  31. $sp_uid = 0;
  32. }
  33. $no = intval(self::value('max(no)') ?: 0) + 1;
  34. $rs = self::getpath($no);
  35. list($p_no, $no_path,$gp_id,$send,$gpinfo) = [$rs['p_no'],$rs['no_path'],$rs['gp_id'],$rs['send'],$rs['info']];
  36. @file_put_contents("job.txt","\r\n".json_encode($rs),8);
  37. $order_id = $order['id'];
  38. $add_time = time();
  39. $price = $order['total_price'];
  40. $uid = $order['uid'];
  41. $status = 0;
  42. self::create(compact('no', 'uid', 'order_id', 'add_time', 'price', 'p_no', 'no_path','status'));
  43. if($no>1) {
  44. self::where('no', $p_no)->inc('cts', 1)->update();
  45. self::where('no', $gp_id)->inc('team_cts', 1)->update();
  46. }
  47. /*
  48. if($send==1)
  49. {
  50. $p_user = User::find($gpinfo['uid']);
  51. $sp_brokeragePrice1 = 3000;
  52. $gp_balance = bcadd($p_user['gp_now_money'], $sp_brokeragePrice1, 2);
  53. $mark = "公排号:" . $no . "满3个加报单余额3000元,订单号:" . $order['order_id'];
  54. $res = UserBill::income('满3个报单余额', $p_user['uid'], 'now_money', 'brokerage', $sp_brokeragePrice1, $order['id'], $gp_balance, $mark, 1);
  55. User::where('uid', $p_user['uid'])->inc('gp_now_money', $sp_brokeragePrice1)->update();
  56. self::where('no', $p_no)->update(['send'=>1]);
  57. }*/
  58. $p_no_cts = self::where('no', $gp_id)->value('team_cts');
  59. if ($p_no_cts>0 && $p_no_cts % 3 == 0) {
  60. $info = self::where('no', $gp_id)->find();
  61. $sp_userInfo = User::find($info['uid']);
  62. if($sp_userInfo['cycle_time']>time()) {
  63. switch ($p_no_cts) {
  64. case 3:
  65. $sp_brokeragePrice = $order['out_amount'] - 3000;
  66. $balance = bcadd($sp_userInfo['brokerage_price'], $sp_brokeragePrice, 2);
  67. $mark = "公排号:" . $no . "满3个拥金3000元,订单号:" . $order['order_id'];
  68. $res = UserBill::income('满3个拥金', $sp_userInfo['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
  69. User::where('uid', $sp_userInfo['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
  70. break;
  71. case 6:
  72. case 9:
  73. $sp_brokeragePrice = $order['out_amount'];
  74. $balance = bcadd($sp_userInfo['brokerage_price'], $sp_brokeragePrice, 2);
  75. $mark = "公排号:" . $no . '满' . $p_no_cts . '个拥金' . $order['out_amount'] . "元,订单号:" . $order['order_id'];
  76. $res = UserBill::income('满' . $p_no_cts . '个拥金', $sp_userInfo['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
  77. User::where('uid', $sp_userInfo['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
  78. break;
  79. case 12:
  80. $sp_brokeragePrice = $order['out_amount'] - 1000;
  81. $balance = bcadd($sp_userInfo['brokerage_price'], $sp_brokeragePrice, 2);
  82. $mark = "公排号:" . $no . '满' . $p_no_cts . '个拥金' . $order['out_amount'] . "元,订单号:" . $order['order_id'];
  83. $res = UserBill::income('满' . $p_no_cts . '个拥金', $sp_userInfo['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
  84. User::where('uid', $sp_userInfo['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
  85. self::where('no', $gp_id)->update(['status' => 1]);
  86. break;
  87. }
  88. }
  89. }
  90. $user_path =null;
  91. if(!StoreMgp::be(['uid'=>$uid]))
  92. {
  93. $rs = StoreMgp::getpath($order['uid'],$sp_uid);
  94. $add_time = time();
  95. $status = 0;
  96. list($p_no,$no,$no_path,$user_path,$level) = [$rs['p_no'],$rs['no'],$rs['no_path'],$rs['user_path'],$rs['level']];
  97. StoreMgp::create(compact('no','p_no','order_id','no_path','user_path','level','price','uid','add_time','status'));
  98. StoreMgp::where('no',$p_no)->inc('cts',1)->update();
  99. }
  100. $no_path = explode(",",$user_path);
  101. if (sizeof($no_path) > 1) {
  102. $path = array_reverse($no_path);
  103. $n = 0;
  104. foreach ($path as $v) {
  105. $n++;
  106. if ($n <=1) continue;
  107. if($n>8) break;
  108. $sp_brokeragePrice = self::getbrokeragePrice($v);
  109. if($sp_brokeragePrice==0) continue;
  110. $user = User::find($v);
  111. $spuser = User::find($user['spread_uid']);
  112. if($user['cycle_time']>time()) {
  113. $balance = bcadd($user['brokerage_price'], $sp_brokeragePrice, 2);
  114. $mark = "公排号:" . $no . '团队奖' . floatval($sp_brokeragePrice) . "元,订单号:" . $order['order_id'];
  115. $res1 = UserBill::income('团队奖', $user['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
  116. User::where('uid', $user['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
  117. }
  118. if($spuser && $user['spread_uid']>0 && $spuser['cycle_time']>time())
  119. {
  120. $balance = bcadd($spuser['brokerage_price'], $sp_brokeragePrice, 2);
  121. $mark = "公排号:" . $no . '推荐奖' . $sp_brokeragePrice . "元,订单号:" . $order['order_id'];
  122. $res = UserBill::income('推荐奖', $spuser['uid'], 'now_money', 'brokerage', $sp_brokeragePrice, $order['id'], $balance, $mark, 1);
  123. User::where('uid', $spuser['uid'])->inc('brokerage_price', $sp_brokeragePrice)->update();
  124. }
  125. }
  126. }
  127. return true;
  128. }catch (Exception $e)
  129. {
  130. @file_put_contents("gp.txt","\r\n". $e->getFile().'--'.$e->getLine(),8);
  131. return false;
  132. }
  133. }
  134. public static function getbrokeragePrice($uid)
  135. {
  136. $num = User::where('spread_uid',$uid)->where('cycle_time','>',0)->count();
  137. if($uid==105) return sys_config('sp_one_num',100);
  138. if($num>2) return sys_config('sp_tree_num',200);
  139. if($num>0) return sys_config('sp_one_num',100);
  140. return 0;
  141. }
  142. /**
  143. * 获取上级及路径
  144. * @param $no
  145. * @return array
  146. * @throws \think\db\exception\DataNotFoundException
  147. * @throws \think\db\exception\DbException
  148. * @throws \think\db\exception\ModelNotFoundException
  149. */
  150. private static function getpath($no)
  151. {
  152. $no_path = [];$p_no=0;$send = 0;$info = null;
  153. if($no==1) {
  154. $no_path[] =1;
  155. $gp_id = 1;
  156. }
  157. else
  158. {
  159. $gp_id = self::where('team_cts','<',12)->value('no');
  160. $min = self::where('cts','<',3)->order("id asc")->find();
  161. $p_no = $min['no'];
  162. $no_path = explode(",",$min['no_path']);
  163. $no_path[] = $no;
  164. if($min['cts']==2)
  165. {
  166. $send = 1;
  167. }
  168. $info = $min;
  169. }
  170. $no_path = join(",",$no_path);
  171. return compact('p_no','no_path','gp_id','send','info');
  172. }
  173. }