ShoporderController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. /**
  3. * 商品订单定时处理
  4. */
  5. namespace app\appapi\controller;
  6. use cmf\controller\HomeBaseController;
  7. use think\facade\Db;
  8. use think\db\Query;
  9. class ShoporderController extends HomebaseController {
  10. //定时更新商品订单状态
  11. public function checkOrder(){
  12. $lastid=$this->request->param('lastid',0,'intval');
  13. if(!$lastid){
  14. $lastid=0;
  15. }
  16. $limit=1000;
  17. $order_list=Db::name("shop_order")->where("status !=-1 and id>{$lastid} ")->order("id asc")->limit($limit)->select()->toArray();
  18. $now=time();
  19. $effective_time=getShopEffectiveTime();
  20. foreach ($order_list as $k => $v) {
  21. $now=time();
  22. if($v['status']==0){ //待付款要判断是否付款超时
  23. $pay_end=$v['addtime']+$effective_time['shop_payment_time']*60;
  24. if($pay_end<=$now){
  25. $data=array(
  26. 'status'=>-1,
  27. 'cancel_time'=>$now
  28. );
  29. changeShopOrderStatus($v['uid'],$v['id'],$data); //将订单关闭
  30. //商品规格库存回增
  31. changeShopGoodsSpecNum($v['goodsid'],$v['spec_id'],$v['nums'],1);
  32. //给买家发消息
  33. $title=lang('你购买的“{goods_name}”订单由于超时未付款,已自动关闭',['goods_name'=>$v['goods_name']]);
  34. $data1=array(
  35. 'uid'=>$v['uid'],
  36. 'orderid'=>$v['id'],
  37. 'title'=>$title,
  38. 'addtime'=>$now,
  39. 'type'=>'0'
  40. );
  41. addShopGoodsOrderMessage($data1);
  42. //发送腾讯云IM
  43. txMessageIM($title,$v['uid'],'goodsorder_admin');
  44. }
  45. }
  46. if($v['status']==1){ //买家已付款 判断卖家发货是否超时
  47. //如果买家没有申请退款
  48. if($v['refund_status']==0){
  49. $shipment_end=$v['paytime']+$effective_time['shop_shipment_time']*60*60*24;
  50. }else{ //买家申请了退款,判断时间超时,要根据退款最终的处理时间
  51. $shipment_end=$v['refund_endtime']+$effective_time['shop_shipment_time']*60*60*24;
  52. }
  53. if($shipment_end<=$now){
  54. $data=array(
  55. 'status'=>-1,
  56. 'cancel_time'=>$now
  57. );
  58. changeShopOrderStatus($v['uid'],$v['id'],$data); //将订单关闭
  59. //退还买家货款
  60. setUserBalance($v['uid'],1,$v['total']);
  61. //添加余额操作记录
  62. $data1=array(
  63. 'uid'=>$v['uid'],
  64. 'touid'=>$v['shop_uid'],
  65. 'balance'=>$v['total'],
  66. 'type'=>1,
  67. 'action'=>3, //卖家超时未发货,退款给买家
  68. 'orderid'=>$v['id'],
  69. 'addtime'=>$now
  70. );
  71. addBalanceRecord($data1);
  72. //店铺逾期发货记录+1
  73. Db::name("shop_apply")
  74. ->where("uid={$v['shop_uid']}")
  75. ->inc('shipment_overdue_num')
  76. ->update();
  77. //减去商品销量
  78. changeShopGoodsSaleNums($v['goodsid'],0,$v['nums']);
  79. //减去店铺销量
  80. changeShopSaleNums($v['shop_uid'],0,$v['nums']);
  81. //给买家发消息
  82. $title=lang('你购买的“{goods_name}”订单由于卖家超时未发货已自动关闭,货款已退还到余额账户中',['goods_name'=>$v['goods_name']]);
  83. $data2=array(
  84. 'uid'=>$v['uid'],
  85. 'orderid'=>$v['id'],
  86. 'title'=>$title,
  87. 'addtime'=>$now,
  88. 'type'=>'0'
  89. );
  90. addShopGoodsOrderMessage($data2);
  91. //发送腾讯云IM
  92. txMessageIM($title,$v['uid'],'goodsorder_admin');
  93. }
  94. }
  95. if($v['status']==2){ //待收货 判断自动确认收货时间是否已满足
  96. //如果买家没有申请退款
  97. if($v['refund_status']==0){
  98. $receive_end=$v['shipment_time']+$effective_time['shop_receive_time']*60*60*24;
  99. }else{
  100. $receive_end=$v['refund_endtime']+$effective_time['shop_receive_time']*60*60*24;
  101. }
  102. if($receive_end<=$now){
  103. $data=array(
  104. 'status'=>3,
  105. 'receive_time'=>$now
  106. );
  107. changeShopOrderStatus($v['uid'],$v['id'],$data); //将订单改为待评价
  108. //给买家发消息
  109. $title=lang('你购买的“{goods_name}”订单已自动确认收货',['goods_name'=>$v['goods_name']]);
  110. $data2=array(
  111. $data1=array(
  112. 'uid'=>$v['uid'],
  113. 'orderid'=>$v['id'],
  114. 'title'=>$title,
  115. 'addtime'=>$now,
  116. 'type'=>'0'
  117. );
  118. addShopGoodsOrderMessage($data1);
  119. //发送腾讯云IM
  120. txMessageIM($title,$v['uid'],'goodsorder_admin');
  121. }
  122. }
  123. if( ($v['status']==3||$v['status']==4) && $v['settlement_time']==0){ //待评价或已评价 且未结算
  124. //判断是否有过退货处理 判断确认收货后是否达到后台设置的给卖家打款的时间
  125. if($v['refund_status']==0){
  126. $settlement_end=$v['receive_time']+$effective_time['shop_settlement_time']*60*60*24;
  127. }else{
  128. $settlement_end=$v['refund_endtime']+$effective_time['shop_settlement_time']*60*60*24;
  129. }
  130. if($settlement_end<=$now){
  131. //判断自动结算记录是否存在
  132. $balance_record=Db::name("user_balance_record")->where("uid='{$v['shop_uid']}' and touid='{$v['uid']}' and type=1 and action=2 and orderid='{$v['id']}'")->find();
  133. if(!$balance_record){
  134. //计算主播代售平台商品佣金
  135. if($v['commission']>0 && $v['liveuid']){
  136. //给主播增加余额
  137. setUserBalance($v['liveuid'],1,$v['commission']);
  138. //写入余额操作记录
  139. $data3=array(
  140. 'uid'=>$v['liveuid'], //主播ID
  141. 'touid'=>$v['uid'], //买家用户ID
  142. 'balance'=>$v['commission'],
  143. 'type'=>1,
  144. 'action'=>9, //代售平台商品佣金
  145. 'orderid'=>$v['id'],
  146. 'addtime'=>$now
  147. );
  148. addBalanceRecord($data3);
  149. //给主播发消息
  150. $title1=lang('买家购买的“{goods_name}”订单佣金{commission}已自动结算到你的账户',['goods_name'=>$v['goods_name'],'commission'=>$v['commission']]);
  151. $data4=array(
  152. 'uid'=>$v['liveuid'],
  153. 'orderid'=>$v['id'],
  154. 'title'=>$title1,
  155. 'addtime'=>$now,
  156. 'type'=>'1',
  157. 'is_commission'=>'1'
  158. );
  159. addShopGoodsOrderMessage($data4);
  160. //发送腾讯云IM
  161. txMessageIM($title1,$v['liveuid'],'goodsorder_admin');
  162. }
  163. //计算分享用户的分享佣金
  164. if($v['shareuid']>0 && $v['share_income']){
  165. //给用户增加余额
  166. setUserBalance($v['shareuid'],1,$v['share_income']);
  167. //写入余额操作记录
  168. $data5=array(
  169. 'uid'=>$v['shareuid'], //分享用户ID
  170. 'touid'=>$v['uid'], //买家用户ID
  171. 'balance'=>$v['share_income'],
  172. 'type'=>1,
  173. 'action'=>10, //分享商品给其他用户购买后获得佣金
  174. 'orderid'=>$v['id'],
  175. 'addtime'=>$now
  176. );
  177. addBalanceRecord($data5);
  178. }
  179. //给卖家增加余额
  180. $balance=$v['total']-$v['share_income'];
  181. if($v['order_percent']>0){
  182. $balance=$balance*(100-$v['order_percent'])/100;
  183. $balance=round($balance,2);
  184. }
  185. $res1=setUserBalance($v['shop_uid'],1,$balance);
  186. //更改订单信息
  187. $data=array(
  188. 'settlement_time'=>$now
  189. );
  190. changeShopOrderStatus($v['uid'],$v['id'],$data);
  191. //添加余额操作记录
  192. $data1=array(
  193. 'uid'=>$v['shop_uid'],
  194. 'touid'=>$v['uid'],
  195. 'balance'=>$balance,
  196. 'type'=>1,
  197. 'action'=>2, //系统自动结算货款给卖家
  198. 'orderid'=>$v['id'],
  199. 'addtime'=>$now
  200. );
  201. addBalanceRecord($data1);
  202. //主播才发送消息,平台自营不发消息
  203. if($v['shop_uid']>1){
  204. //给卖家发消息
  205. $title=lang('买家购买的“{goods_name}”订单已自动结算到你的账户',['goods_name'=>$v['goods_name']]);
  206. $data2=array(
  207. 'uid'=>$v['shop_uid'],
  208. 'orderid'=>$v['id'],
  209. 'title'=>$title,
  210. 'addtime'=>$now,
  211. 'type'=>'1'
  212. );
  213. addShopGoodsOrderMessage($data2);
  214. //发送腾讯云IM
  215. txMessageIM($title,$v['shop_uid'],'goodsorder_admin');
  216. }
  217. }
  218. }
  219. }
  220. if($v['status']==5&&$v['refund_status']==0){ //退款 判断等待卖家处理的时间是否超出后台设定的时间,如果超出,自动退款
  221. //获取退款申请信息
  222. $where=array(
  223. 'orderid'=>$v['id']
  224. );
  225. $refund_info=getShopOrderRefundInfo($where);
  226. if($refund_info['is_platform_interpose']==0&&$refund_info['shop_result']==0){ //平台未介入且店家未处理
  227. $refund_end=$refund_info['addtime']+$effective_time['shop_refund_time']*60*60*24;
  228. if($refund_end<=$now){
  229. //更改订单退款状态
  230. $data=array(
  231. 'refund_status'=>1,
  232. 'refund_endtime'=>$now
  233. );
  234. changeShopOrderStatus($v['uid'],$v['id'],$data);
  235. //更改订单退款记录信息
  236. $data1=array(
  237. 'system_process_time'=>$now,
  238. 'status'=>1,
  239. );
  240. changeGoodsOrderRefund($where,$data1);
  241. //退还买家货款
  242. setUserBalance($v['uid'],1,$v['total']);
  243. //添加余额操作记录
  244. $data1=array(
  245. 'uid'=>$v['uid'],
  246. 'touid'=>$v['shop_uid'],
  247. 'balance'=>$v['total'],
  248. 'type'=>1,
  249. 'action'=>4, //买家发起退款,卖家超时未处理,系统自动退款
  250. 'orderid'=>$v['id'],
  251. 'addtime'=>$now
  252. );
  253. addBalanceRecord($data1);
  254. //减去商品销量
  255. changeShopGoodsSaleNums($v['goodsid'],0,$v['nums']);
  256. //减去店铺销量
  257. changeShopSaleNums($v['shop_uid'],0,$v['nums']);
  258. //商品规格库存回增
  259. changeShopGoodsSpecNum($v['goodsid'],$v['spec_id'],$v['nums'],1);
  260. //给买家发消息
  261. $title=lang('你申请的“{goods_name}”订单退款卖家超时未处理,已自动退款到你的余额账户中',['goods_name'=>$v['goods_name']]);
  262. $data2=array(
  263. 'uid'=>$v['uid'],
  264. 'orderid'=>$v['id'],
  265. 'title'=>$title,
  266. 'addtime'=>$now,
  267. 'type'=>'0'
  268. );
  269. addShopGoodsOrderMessage($data2);
  270. //发送腾讯云IM
  271. txMessageIM($title,$v['uid'],'goodsorder_admin');
  272. }
  273. }
  274. if($refund_info['is_platform_interpose']==0&&$refund_info['shop_result']==-1){ //未申请平台介入且店家已拒绝
  275. //超时,退款自动完成,订单自动进入退款前状态
  276. $finish_endtime=$refund_info['shop_process_time']+$effective_time['shop_refund_finish_time']*60*60*24;
  277. if($finish_endtime<=$now){
  278. //更改退款订单状态
  279. $data=array(
  280. 'status'=>1,
  281. 'system_process_time'=>$now
  282. );
  283. changeGoodsOrderRefund($where,$data);
  284. //更改订单状态
  285. $data1=array(
  286. 'refund_endtime'=>$now,
  287. 'refund_status'=>-1
  288. );
  289. if($v['receive_time']>0){
  290. $data1['status']=3; //待评价
  291. }else{
  292. if($v['shipment_time']>0){
  293. $data1['status']=2; //待收货
  294. }else{
  295. $data1['status']=1; //待发货
  296. }
  297. }
  298. changeShopOrderStatus($v['uid'],$v['id'],$data1);
  299. //给买家发消息
  300. $title=lang('你购买的“{goods_name}”订单退款申请被卖家拒绝后,{shop_refund_finish_time}天内你没有进一步操作,系统自动处理结束',['goods_name'=>$v['goods_name'],'shop_refund_finish_time'=>$effective_time['shop_refund_finish_time']]);
  301. $data2=array(
  302. 'uid'=>$v['uid'],
  303. 'orderid'=>$v['id'],
  304. 'title'=>$title,
  305. 'addtime'=>$now,
  306. 'type'=>'0'
  307. );
  308. addShopGoodsOrderMessage($data2);
  309. //发送腾讯云IM
  310. txMessageIM($title,$v['uid'],'goodsorder_admin');
  311. }
  312. }
  313. }
  314. $lastid=$v['id'];
  315. }
  316. //file_put_contents(CMF_ROOT.'data/shoporderauto/record_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' lastid:'.$lastid.PHP_EOL.PHP_EOL,FILE_APPEND);
  317. $list_nums=count($order_list);
  318. if($list_nums<$limit){
  319. echo "NO";
  320. return;
  321. }
  322. echo 'OK-'.$lastid;
  323. }
  324. }