Plantask.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. echo '周一不滑落';
  20. exit;
  21. }
  22. //最后买东西是11天前了,intro_num在这里使用,减少查到的数据量
  23. $tendays_ago = $this->hualuo_tendays_ago();
  24. // echo $tendays_ago;
  25. Db::startTrans();
  26. $user = Db::name('user')->where('last_paytime','lt',$tendays_ago)->where('intro_num','gt',0)->lock(true)->find();
  27. if(empty($user)){
  28. echo '没有数据';
  29. Db::rollback();
  30. exit;
  31. }
  32. dump($user);
  33. //我的多个下级,的推荐人,改成我的上级。也就是跳过了我
  34. $my_down = Db::name('user')->where('intro_uid',$user['id'])->select();
  35. if(!empty($my_down)){
  36. foreach($my_down as $key => $down_user){
  37. $rs_down = $this->updateIntro($down_user['id'],$user['intro_uid']);
  38. if($rs_down !== true){
  39. echo $rs_down;
  40. Db::rollback();
  41. exit;
  42. }
  43. }
  44. }
  45. //用不到了
  46. //我滑落了,我的上级保持不变,下级都没了
  47. /*$my_update = [
  48. 'intro_num' => 0,
  49. 'intro_num_all' => 0,
  50. ];
  51. $rs2 = Db::name('user')->where('id',$user['id'])->update($my_update);
  52. if($rs2 === false){
  53. Db::rollback();
  54. }*/
  55. //提交
  56. Db::rollback();
  57. }
  58. private function updateIntro($uid,$intro_uid){
  59. $db = Db::name("user");
  60. //验证
  61. if($uid == $intro_uid) $this->error("新邀请人不能是自己!");
  62. $rs_user = $db->where(array('id' => $uid))->find();
  63. if(!$rs_user) $this->error("会员 ".$uid.' 不存在! ');
  64. $rs_intro = $db->where(array('id' => $intro_uid))->find();
  65. if(!$rs_intro) $this->error("新邀请人 ".$intro_uid.' 不存在! ');
  66. if($rs_user['intro_uid'] == $intro_uid) $this->error("新邀请人不能是原来的邀请人!");
  67. //新推荐人不能是自己下级
  68. if($rs_intro['intro_ids']) {
  69. $ary = explode(',', $rs_intro['intro_ids']);
  70. if(in_array($rs_user['id'], $ary)) {
  71. $this->error("新邀请人不能是自己网体下级会员!");
  72. }
  73. }
  74. //更新此会员的上级
  75. $data = array();
  76. $data['intro_ids'] = $rs_intro['intro_ids'] ? $rs_intro['intro_ids'].','.$rs_intro['id'] : $rs_intro['id']; //新推荐人id序列
  77. $data['intro_level'] = $rs_intro['intro_level'] + 1; //新层数
  78. $data['intro_uid'] = $rs_intro['id']; //新推荐人id
  79. $db->where(['id' => $uid])->update($data);
  80. //更新此会员的下级会员
  81. $num_c = $rs_intro['intro_level'] - $rs_user['intro_level'] + 1;//变化的层数差
  82. $user_sub = $db->where("find_in_set('".$rs_user['id']."',intro_ids) > 0")->order("intro_level asc")->select(); //所有下级
  83. foreach($user_sub as $users) { //每个下级(因为是升序查询,所以每个的上级一定先更新完毕)
  84. $data_sub = array();
  85. $rs_tjr = $db->where(array('id' => $users['intro_uid']))->field('id,intro_ids')->find();
  86. $data_sub['intro_level'] = $users['intro_level'] + $num_c; //新层数
  87. $data_sub['intro_ids'] = $rs_tjr['intro_ids'].','.$rs_tjr['id']; //新推荐人id序列
  88. $db->where(array('id' => $users['id']))->update($data_sub);//更新移动网体的会员
  89. }
  90. //更新直推、团队数
  91. $team = count($user_sub) + 1;
  92. if($rs_user['intro_uid']) {
  93. $this->addIntroNum($rs_user['intro_uid'], -1, -$team); //有则 更新原上级
  94. }
  95. $this->addIntroNum($intro_uid, 1, $team); //更新新上级
  96. $db->commit();
  97. $this->success("更新成功");
  98. }
  99. /**
  100. * 更新邀请码数和推荐团队人数
  101. * @param string $tjrname 用户id
  102. * @param integer $num 直推增减人数
  103. * @param integer $team 团队增减人数
  104. */
  105. private function addIntroNum($tjrname, $num, $team)
  106. {
  107. //更新直推人数
  108. if($num < 0) {
  109. $rs1 = Db::name('user')->where(['id' => $tjrname])->setDec('intro_num', abs($num));
  110. } else {
  111. $rs1 = Db::name('user')->where(['id' => $tjrname])->setInc('intro_num', $num);
  112. }
  113. if($rs1 === false){
  114. return false;
  115. }
  116. //更新团队
  117. $rstjr = Db::name('user')->where(['id' => $tjrname])->field('id,intro_ids')->find();
  118. if($rstjr) {
  119. $tjstr = $rstjr['intro_ids'] ? ($rstjr['intro_ids'] . ',' . $rstjr['id']) : $rstjr['id'];
  120. $tjstr = trim($tjstr, ',');
  121. $arr_intro = explode(',', $tjstr);
  122. if($team < 0) {
  123. $rs2 = Db::name('user')->where(['id' => ['in', $arr_intro]])->setDec('intro_num_all', abs($team));
  124. } else {
  125. $rs2 = Db::name('user')->where(['id' => ['in', $arr_intro]])->setInc('intro_num_all', $team);
  126. }
  127. if($rs2 === false){
  128. return false;
  129. }
  130. }
  131. return true;
  132. }
  133. //滑落要按11天算
  134. private function hualuo_tendays_ago(){
  135. $nowtime = time();
  136. /*if(input('date','')){
  137. $nowtime = strtotime(input('date',''));
  138. }*/
  139. $today = strtotime(date('Y-m-d',$nowtime));
  140. $toweek = date('w',$nowtime);
  141. $enum = [
  142. 1 => 12,//这一天没人滑落
  143. 2 => 12,
  144. 3 => 12,
  145. 4 => 12,
  146. 5 => 11,
  147. 6 => 11,
  148. 0 => 11,
  149. ];
  150. $rs = $today - ($enum[$toweek] * 86400);
  151. // dump(date('Y-m-d',$rs));
  152. return $rs;
  153. }
  154. //订单支付回调之后的任务,每分钟运行
  155. public function auto_order_paid(){
  156. $map = [
  157. 'status' => 1,
  158. 'have_paid' => ['gt',0],
  159. 'paidtasktime' => 0,
  160. ];
  161. $order_list = Db::name('unishop_order')->where($map)->order('id asc')->limit(10)->select();
  162. if(empty($order_list)){
  163. echo '没有数据';
  164. exit;
  165. }
  166. Db::startTrans();
  167. $walletmodel = new Wallet();
  168. foreach($order_list as $key => $order){
  169. //买家立刻得到积分
  170. if($order['order_shouyi'] > 0){
  171. $rs_wallet = $walletmodel->lockChangeAccountRemain($order['user_id'],'score',$order['order_shouyi'],5,'下单收益','unishop_order',$order['id'],$order['user_id']);
  172. if($rs_wallet['status'] === false){
  173. echo $rs_wallet['msg'];
  174. Db::rollback();
  175. exit;
  176. }
  177. }
  178. //直推代理商获益
  179. $intro_uid = Db::name('user')->where('id',$order['user_id'])->value('intro_uid');
  180. if($intro_uid){
  181. $bili = config('site.orderpaid_zhitui_bili') ?: 5;
  182. $score = bcdiv(bcmul($order['order_price'],$bili,2),100,2);
  183. if($score > 0){
  184. $rs_wallet = $walletmodel->lockChangeAccountRemain($intro_uid,'score',$score,3,'直推代理奖励','unishop_order',$order['id'],$order['user_id']);
  185. if($rs_wallet['status'] === false){
  186. echo $rs_wallet['msg'];
  187. Db::rollback();
  188. exit;
  189. }
  190. }
  191. }
  192. $score = 0;
  193. //上上级
  194. $top_uid = Db::name('user')->where('id',$intro_uid)->value('intro_uid');
  195. if($top_uid){
  196. $bili = config('site.orderpaid_jiantui_bili') ?: 1;
  197. $score = bcdiv(bcmul($order['order_price'],$bili,2),100,2);
  198. if($score > 0){
  199. $rs_wallet = $walletmodel->lockChangeAccountRemain($top_uid,'score',$score,4,'间推代理奖励','unishop_order',$order['id'],$order['user_id']);
  200. if($rs_wallet['status'] === false){
  201. echo $rs_wallet['msg'];
  202. Db::rollback();
  203. exit;
  204. }
  205. }
  206. }
  207. //订单完成
  208. $rs_order = Db::name('unishop_order')->where('id',$order['id'])->update(['paidtasktime'=>time()]);
  209. if($rs_order === false){
  210. echo '更新失败';
  211. Db::rollback();
  212. exit;
  213. }
  214. //循环结束
  215. }
  216. Db::commit();
  217. echo '成功'.count($order_list);
  218. }
  219. //自动结算10日前订单
  220. public function auto_jiesuan_order(){
  221. $nowtime = time();
  222. $toweek = date('w',$nowtime);
  223. if($toweek == 0){
  224. //周日不结算(自动到下周一结算)
  225. echo '周日不结算';
  226. exit;
  227. }
  228. $tendays_ago = $this->jiesuan_tendays_ago();
  229. // echo $tendays_ago;exit;
  230. //
  231. Db::startTrans();
  232. $order_map = [
  233. 'status' => 1,
  234. 'have_paid' => ['lt',$tendays_ago],
  235. 'jiesuantime' => 0,
  236. ];
  237. $order = Db::name('unishop_order')->where($order_map)->where('have_paid','gt',0)->order('id asc')->lock(true)->find();
  238. if(empty($order)){
  239. echo '没有数据';
  240. Db::rollback();
  241. exit;
  242. }
  243. // dump($order);
  244. //为上级做贡献,找到上级
  245. $intro_uid = Db::name('user')->where('id',$order['user_id'])->value('intro_uid');
  246. if(empty($intro_uid)){
  247. echo '没有上级,结束';
  248. Db::name('unishop_order')->where('id',$order['id'])->update(['jiesuantime'=>time()]);
  249. Db::commit();
  250. exit;
  251. }
  252. //获取直推人数
  253. $intro_number = Db::name('user')->where('intro_uid',$intro_uid)->count();
  254. // dump($intro_number);
  255. //获取业绩
  256. $yeji = $this->jiesuan_yeji($intro_uid);
  257. // dump($yeji);
  258. //确定代理商等级,拿对应比例
  259. $rule = $this->jiesuan_daili_level($intro_number,$yeji);
  260. if($rule['bili'] == 0){
  261. echo '达不到第一级,结束';
  262. Db::name('unishop_order')->where('id',$order['id'])->update(['jiesuantime'=>time()]);
  263. Db::commit();
  264. exit;
  265. }
  266. // dump($rule);
  267. //给直推
  268. $score = bcdiv(bcmul($order['order_shouyi'],$rule['bili'],2),100,2);
  269. // dump($score);
  270. if($score > 0){
  271. $rs_wallet = model('wallet')->lockChangeAccountRemain($intro_uid,'score',$score,6,$rule['level'].'级代理','unishop_order',$order['id'],$order['user_id']);
  272. if($rs_wallet['status'] === false){
  273. echo $rs_wallet['msg'];
  274. Db::rollback();
  275. exit;
  276. }
  277. }
  278. //给间推
  279. if($rule['level'] == 1){ //一级代理要有间推
  280. $two_intro_uid = Db::name('user')->where('id',$intro_uid)->value('intro_uid');//上上级id
  281. if($two_intro_uid > 0){
  282. $two_intro_count = Db::name('user')->where('intro_uid',$two_intro_uid)->count();//上上级的直推
  283. // $two_yeji = $this->jiesuan_yeji($two_intro_uid);//上上级的业绩
  284. if($two_intro_count >= $rule['intronum']){//也要五个,且业绩不高于15万(极差)。这里很矛盾
  285. $score_2 = $score;//目前是一样的,不再次计算了
  286. if($score_2 > 0){
  287. $rs_wallet = model('wallet')->lockChangeAccountRemain($two_intro_uid,'score',$score_2,7,$rule['level'].'级代理(间推)','unishop_order',$order['id'],$order['user_id']);
  288. if($rs_wallet['status'] === false){
  289. echo $rs_wallet['msg'];
  290. Db::rollback();
  291. exit;
  292. }
  293. }
  294. }
  295. }
  296. }
  297. //标记为结算
  298. $jiesuan = Db::name('unishop_order')->where('id',$order['id'])->update(['jiesuantime'=>$nowtime]);
  299. if($jiesuan === false){
  300. echo '修改状态失败';
  301. Db::rollback();
  302. exit;
  303. }
  304. //批发的都卖出
  305. $pifa = Db::name('unishop_order_product')->where('order_id',$order['id'])->update(['pifa_status'=>1]);
  306. if($pifa === false){
  307. echo '修改状态失败2';
  308. Db::rollback();
  309. exit;
  310. }
  311. Db::commit();
  312. echo '完成'.$order['id'];
  313. }
  314. //获取业绩
  315. private function jiesuan_yeji($user_id){
  316. //找到所有下级
  317. $user_ids = Db::name('user')->where('find_in_set(:intro_ids,intro_ids)', ['intro_ids' => $user_id])->column('id');
  318. // dump($user_ids);
  319. if(empty($user_ids)){
  320. return 0;
  321. }
  322. $map = [
  323. 'status' => 1,
  324. 'have_paid' => ['gt',0],
  325. 'user_id' => ['IN',$user_ids],
  326. ];
  327. $yeji = Db::name('unishop_order')->where($map)->sum('order_price');
  328. return $yeji;
  329. }
  330. //确认代理等级及规则
  331. private function jiesuan_daili_level($intronum,$yeji){
  332. $data = Db::name('zongdai')->order('id asc')->select();
  333. // dump($data);
  334. $return = $data[0]; //默认第0个
  335. foreach($data as $key => $rule){
  336. if($intronum >= $rule['intronum'] && $yeji >= $rule['yeji']){
  337. $return = $rule;
  338. }
  339. }
  340. return $return;
  341. }
  342. //结算要按10天算
  343. private function jiesuan_tendays_ago(){
  344. $nowtime = time();
  345. /*if(input('date','')){
  346. $nowtime = strtotime(input('date',''));
  347. }*/
  348. $today = strtotime(date('Y-m-d',$nowtime));
  349. $toweek = date('w',$nowtime);
  350. $enum = [
  351. 1 => 11,
  352. 2 => 11,
  353. 3 => 11,
  354. 4 => 10,
  355. 5 => 10,
  356. 6 => 10,
  357. 0 => 10, //这一天没人结算
  358. ];
  359. $rs = $today - ($enum[$toweek] * 86400);
  360. // dump(date('Y-m-d',$rs));
  361. return $rs;
  362. }
  363. }