Plantask.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. //10天不买的用户,第10天结算完,第11天自动滑落,上下级自动衔接
  13. //该用户的所有下级,的推荐人,改成此人的上级。
  14. public function auto_hualuo(){
  15. $nowtime = time();
  16. $toweek = date('w',$nowtime);
  17. if($toweek == 1){
  18. //周日不结算(自动到下周一),所以,周一不滑落
  19. exit;
  20. }
  21. //最后买东西是11天前了,intro_num在这里使用,减少查到的数据量
  22. $tendays_ago = $this->hualuo_tendays_ago();
  23. $list = Db::name('user')->where('last_paytime','lt',$tendays_ago)->where('intro_num','gt',0)->select();
  24. if(!empty($list)){
  25. Db::startTrans();
  26. foreach($list as $key => $user){
  27. //我的下级,的推荐人,改成我的上级。也就是跳过了我
  28. $rs = Db::name('user')->where('intro_uid',$user['id'])->update(['intro_uid',$user['intro_uid']]);
  29. if($rs === false){
  30. Db::rollback();
  31. }
  32. //我的推荐人数变0
  33. $rs2 = Db::name('user')->where('id',$user['id'])->update(['intro_num',0]);
  34. if($rs2 === false){
  35. Db::rollback();
  36. }
  37. }
  38. Db::commit();
  39. }
  40. }
  41. //滑落要按11天算
  42. private function hualuo_tendays_ago(){
  43. $nowtime = time();
  44. /*if(input('date','')){
  45. $nowtime = strtotime(input('date',''));
  46. }*/
  47. $today = strtotime(date('Y-m-d',$nowtime));
  48. $toweek = date('w',$nowtime);
  49. $enum = [
  50. 1 => 12,//这一天没人滑落
  51. 2 => 12,
  52. 3 => 12,
  53. 4 => 12,
  54. 5 => 11,
  55. 6 => 11,
  56. 0 => 11,
  57. ];
  58. $rs = $today - ($enum[$toweek] * 86400);
  59. // dump(date('Y-m-d',$rs));
  60. return $rs;
  61. }
  62. //订单支付回调之后的任务,每分钟运行
  63. public function auto_order_paid(){
  64. $map = [
  65. 'status' => 1,
  66. 'have_paid' => ['gt',0],
  67. 'paidtasktime' => 0,
  68. ];
  69. $order_list = Db::name('unishop_order')->where($map)->limit(10)->select();
  70. if(empty($order_list)){
  71. echo '没有数据';
  72. exit;
  73. }
  74. Db::startTrans();
  75. $walletmodel = new Wallet();
  76. foreach($order_list as $key => $order){
  77. //买家立刻得到积分
  78. if($order['order_shouyi'] > 0){
  79. $rs_wallet = $walletmodel->lockChangeAccountRemain($order['user_id'],'score',$order['order_shouyi'],5,'下单收益','unishop_order',$order['id']);
  80. if($rs_wallet['status'] === false){
  81. echo $rs_wallet['msg'];
  82. Db::rollback();
  83. exit;
  84. }
  85. }
  86. //直推代理商获益
  87. $intro_uid = Db::name('user')->where('id',$order['user_id'])->value('intro_uid');
  88. if($intro_uid){
  89. $bili = config('site.orderpaid_zhitui_bili') ?: 5;
  90. $score = bcdiv(bcmul($order['order_price'],$bili,2),100,2);
  91. if($score > 0){
  92. $rs_wallet = $walletmodel->lockChangeAccountRemain($intro_uid,'score',$score,3,'直推代理奖励','unishop_order',$order['id']);
  93. if($rs_wallet['status'] === false){
  94. echo $rs_wallet['msg'];
  95. Db::rollback();
  96. exit;
  97. }
  98. }
  99. }
  100. $score = 0;
  101. //上上级
  102. $top_uid = Db::name('user')->where('id',$intro_uid)->value('intro_uid');
  103. if($top_uid){
  104. $bili = config('site.orderpaid_jiantui_bili') ?: 1;
  105. $score = bcdiv(bcmul($order['order_price'],$bili,2),100,2);
  106. if($score > 0){
  107. $rs_wallet = $walletmodel->lockChangeAccountRemain($top_uid,'score',$score,4,'间推代理奖励','unishop_order',$order['id']);
  108. if($rs_wallet['status'] === false){
  109. echo $rs_wallet['msg'];
  110. Db::rollback();
  111. exit;
  112. }
  113. }
  114. }
  115. //订单完成
  116. $rs_order = Db::name('unishop_order')->where('id',$order['id'])->update(['paidtasktime'=>time()]);
  117. if($rs_order === false){
  118. echo '更新失败';
  119. Db::rollback();
  120. exit;
  121. }
  122. //循环结束
  123. }
  124. Db::commit();
  125. echo '成功';
  126. }
  127. //自动结算10日前订单
  128. public function auto_jiesuan_order(){
  129. $nowtime = time();
  130. $toweek = date('w',$nowtime);
  131. if($toweek == 0){
  132. //周日不结算(自动到下周一结算)
  133. echo '周日不结算';
  134. exit;
  135. }
  136. $tendays_ago = $this->jiesuan_tendays_ago();
  137. //
  138. Db::startTrans();
  139. $order_map = [
  140. 'status' => 1,
  141. 'have_paid' => ['lt',$tendays_ago],
  142. 'jiesuantime' => 0,
  143. ];
  144. $order = Db::name('unishop_order')->where($order_map)->where('have_paid','gt',0)->order('id asc')->lock(true)->find();
  145. //为上级做贡献,找到上级
  146. $intro_uid = Db::name('user')->where('id',$order['user_id'])->value('intro_uid');
  147. if(empty($intro_uid)){
  148. //没有上级,结束
  149. Db::name('unishop_order')->where('id',$order['id'])->update(['jiesuantime'=>time()]);
  150. Db::commit();
  151. exit;
  152. }
  153. //获取直推人数
  154. $intro_number = Db::name('user')->where('intro_uid',$intro_uid)->count();
  155. //获取业绩
  156. $yeji = $this->jiesuan_yeji($intro_uid);
  157. //确定代理商等级,拿对应比例
  158. $rule = $this->jiesuan_daili_level($intro_number,$yeji);
  159. if($rule['bili'] == 0){
  160. //达不到第一级
  161. Db::name('unishop_order')->where('id',$order['id'])->update(['jiesuantime'=>time()]);
  162. Db::commit();
  163. exit;
  164. }
  165. //给直推
  166. $score = bcdiv(bcmul($order['order_shouyi'],$rule['bili'],2),100,2);
  167. if($score > 0){
  168. $rs_wallet = model('wallet')->lockChangeAccountRemain($intro_uid,'score',$score,6,$rule['level'].'级代理','unishop_order',$order['id']);
  169. if($rs_wallet['status'] === false){
  170. echo $rs_wallet['msg'];
  171. Db::rollback();
  172. exit;
  173. }
  174. }
  175. //给间推
  176. if($rule['level'] == 1){ //一级代理要有间推
  177. $two_intro_uid = Db::name('user')->where('id',$intro_uid)->value('intro_uid');//上上级id
  178. if($two_intro_uid > 0){
  179. $two_intro_count = Db::name('user')->where('intro_uid',$two_intro_uid)->count();//上上级的直推
  180. // $two_yeji = $this->jiesuan_yeji($two_intro_uid);//上上级的业绩
  181. if($two_intro_count >= $rule['intronum']){//也要五个,且业绩不高于15万(极差)。这里很矛盾
  182. $score_2 = $score;//目前是一样的,不再次计算了
  183. if($score_2 > 0){
  184. $rs_wallet = model('wallet')->lockChangeAccountRemain($two_intro_uid,'score',$score_2,7,$rule['level'].'级代理(间推)','unishop_order',$order['id']);
  185. if($rs_wallet['status'] === false){
  186. echo $rs_wallet['msg'];
  187. Db::rollback();
  188. exit;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. //标记为结算
  195. $jiesuan = Db::name('unishop_order')->where('id',$order['id'])->update(['jiesuantime'=>$nowtime]);
  196. if($jiesuan === false){
  197. echo '修改状态失败';
  198. Db::rollback();
  199. exit;
  200. }
  201. //批发的都卖出
  202. $pifa = Db::name('unishop_order_product')->where('order_id',$order['id'])->update(['pifa_status'=>1]);
  203. if($pifa === false){
  204. echo '修改状态失败2';
  205. Db::rollback();
  206. exit;
  207. }
  208. Db::commit();
  209. }
  210. //获取业绩
  211. private function jiesuan_yeji($user_id){
  212. //找到所有下级
  213. $user_ids = Db::name('user')->where('find_in_set(:intro_ids,intro_ids)', ['intro_ids' => $user_id])->column('id');
  214. if(empty($user_ids)){
  215. return 0;
  216. }
  217. $map = [
  218. 'status' => 1,
  219. 'have_paid' => ['gt',0],
  220. 'user_id' => ['IN',$user_ids],
  221. ];
  222. $yeji = Db::name('unishop_order')->where($map)->sum('order_price');
  223. return $yeji;
  224. }
  225. //确认代理等级及规则
  226. private function jiesuan_daili_level($intronum,$yeji){
  227. $data = Db::name('zongdai')->order('id asc')->select();
  228. $return = $data[0]; //默认第0个
  229. foreach($data as $key => $rule){
  230. if($intronum >= $rule['intronum'] && $yeji >= $rule['bili']){
  231. $return = $rule;
  232. }
  233. }
  234. return $return;
  235. }
  236. //结算要按10天算
  237. private function jiesuan_tendays_ago(){
  238. $nowtime = time();
  239. /*if(input('date','')){
  240. $nowtime = strtotime(input('date',''));
  241. }*/
  242. $today = strtotime(date('Y-m-d',$nowtime));
  243. $toweek = date('w',$nowtime);
  244. $enum = [
  245. 1 => 11,
  246. 2 => 11,
  247. 3 => 11,
  248. 4 => 10,
  249. 5 => 10,
  250. 6 => 10,
  251. 0 => 10, //这一天没人结算
  252. ];
  253. $rs = $today - ($enum[$toweek] * 86400);
  254. // dump(date('Y-m-d',$rs));
  255. return $rs;
  256. }
  257. }