Plantask.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use think\Db;
  5. use app\common\model\Wallet;
  6. class Plantask extends Controller
  7. {
  8. public function index()
  9. {
  10. exit;
  11. }
  12. //推荐奖励
  13. //订单支付回调之后的任务,每分钟运行
  14. //烧伤
  15. public function auto_order_paid(){
  16. $map = [
  17. 'status' => 1,
  18. 'have_paid' => ['gt',0],
  19. 'paidtasktime' => 0,
  20. ];
  21. $order_list = Db::name('unishop_order')->where($map)->order('id asc')->limit(10)->select();
  22. if(empty($order_list)){
  23. echo '没有数据';
  24. exit;
  25. }
  26. Db::startTrans();
  27. $walletmodel = new Wallet();
  28. foreach($order_list as $key => $order){
  29. //本金立即到账给入金者
  30. if($order['order_benjin'] > 0){
  31. $remark = '本金:'.$order['order_benjin'];
  32. $rs_wallet = model('wallet')->lockChangeAccountRemain($order['user_id'],'score',$order['order_benjin'],5,$remark,'unishop_order',$order['id'],$order['user_id']);
  33. if($rs_wallet['status'] === false){
  34. echo $rs_wallet['msg'];
  35. Db::rollback();
  36. exit;
  37. }
  38. }
  39. //直推代理商获益
  40. $intro_uid = Db::name('user')->where('id',$order['user_id'])->value('intro_uid');
  41. if($intro_uid){
  42. //直推的第一单金额,烧伤
  43. $map = [
  44. 'status' => 1,
  45. 'have_paid' => ['gt',0],
  46. 'user_id' => $intro_uid,
  47. ];
  48. $first_order = Db::name('unishop_order')->where($map)->order('have_paid asc')->value('order_price');
  49. $first_order = $first_order ?: 0;
  50. $jishu = $order['order_price'];
  51. $remark_shaoshang = '';
  52. if($first_order < $order['order_price']){
  53. $jishu = $first_order;
  54. $remark_shaoshang = '(烧伤)';
  55. }
  56. //奖励
  57. $bili = config('site.orderpaid_zhitui_bili') ?: 5;
  58. $score = bcdiv(bcmul($jishu,$bili,0),100,0);
  59. if($score > 0){
  60. $rs_wallet = $walletmodel->lockChangeAccountRemain($intro_uid,'score',$score,3,'直推代理奖励'.$remark_shaoshang,'unishop_order',$order['id'],$order['user_id']);
  61. if($rs_wallet['status'] === false){
  62. echo $rs_wallet['msg'];
  63. Db::rollback();
  64. exit;
  65. }
  66. $rs_wallet = $walletmodel->lockChangeAccountRemain($intro_uid,'shouyi',$score,31,'直推代理奖励'.$remark_shaoshang,'unishop_order',$order['id'],$order['user_id']);
  67. if($rs_wallet['status'] === false){
  68. echo $rs_wallet['msg'];
  69. Db::rollback();
  70. exit;
  71. }
  72. }
  73. }
  74. //订单完成
  75. $rs_order = Db::name('unishop_order')->where('id',$order['id'])->update(['paidtasktime'=>time()]);
  76. if($rs_order === false){
  77. echo '更新失败';
  78. Db::rollback();
  79. exit;
  80. }
  81. //循环结束
  82. }
  83. Db::commit();
  84. echo '成功'.count($order_list);
  85. }
  86. //总代奖励
  87. //自动结算10日前订单,每分钟运行
  88. public function auto_jiesuan_order(){
  89. $nowtime = time();
  90. /*$toweek = date('w',$nowtime);
  91. if($toweek == 0){
  92. //周日不结算(自动到下周一结算)
  93. echo '周日不结算';
  94. exit;
  95. }*/
  96. $tendays_ago = jiesuan_tendays_ago();
  97. // echo $tendays_ago;exit;
  98. //
  99. Db::startTrans();
  100. $order_map = [
  101. 'status' => 1,
  102. 'have_paid' => ['lt',$tendays_ago],
  103. 'jiesuantime' => 0,
  104. ];
  105. $order = Db::name('unishop_order')->where($order_map)->where('have_paid','gt',0)->order('id asc')->lock(true)->find();
  106. if(empty($order)){
  107. echo '没有数据';
  108. Db::rollback();
  109. exit;
  110. }
  111. // dump($order);
  112. //买家得到收益,两个数据同步加
  113. if($order['order_shouyi'] > 0){
  114. $shouyi = bcsub($order['order_shouyi'],$order['order_benjin']); //总收益 - 本金 = 可提现收益
  115. $remark = '收益:'.$shouyi;
  116. if($shouyi > 0){
  117. $rs_wallet = model('wallet')->lockChangeAccountRemain($order['user_id'],'score',$shouyi,50,$remark,'unishop_order',$order['id'],$order['user_id']);
  118. if($rs_wallet['status'] === false){
  119. echo $rs_wallet['msg'];
  120. Db::rollback();
  121. exit;
  122. }
  123. $rs_wallet = model('wallet')->lockChangeAccountRemain($order['user_id'],'shouyi',$shouyi,51,$remark,'unishop_order',$order['id'],$order['user_id']);
  124. if($rs_wallet['status'] === false){
  125. echo $rs_wallet['msg'];
  126. Db::rollback();
  127. exit;
  128. }
  129. }
  130. }
  131. //标记为结算
  132. $jiesuan = Db::name('unishop_order')->where('id',$order['id'])->update(['jiesuantime'=>$nowtime]);
  133. if($jiesuan === false){
  134. echo '修改状态失败';
  135. Db::rollback();
  136. exit;
  137. }
  138. //批发的都卖出
  139. $pifa = Db::name('unishop_order_product')->where('order_id',$order['id'])->update(['pifa_status'=>1]);
  140. if($pifa === false){
  141. echo '修改状态失败2';
  142. Db::rollback();
  143. exit;
  144. }
  145. Db::commit();
  146. echo '完成'.$order['id'];
  147. }
  148. }