| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- <?php
- /**
- * 商品订单定时处理
- */
- namespace app\appapi\controller;
- use cmf\controller\HomeBaseController;
- use think\facade\Db;
- use think\db\Query;
- class ShoporderController extends HomebaseController {
- //定时更新商品订单状态
- public function checkOrder(){
- $lastid=$this->request->param('lastid',0,'intval');
- if(!$lastid){
- $lastid=0;
- }
- $limit=1000;
- $order_list=Db::name("shop_order")->where("status !=-1 and id>{$lastid} ")->order("id asc")->limit($limit)->select()->toArray();
- $now=time();
- $effective_time=getShopEffectiveTime();
-
- foreach ($order_list as $k => $v) {
- $now=time();
- if($v['status']==0){ //待付款要判断是否付款超时
- $pay_end=$v['addtime']+$effective_time['shop_payment_time']*60;
- if($pay_end<=$now){
- $data=array(
- 'status'=>-1,
- 'cancel_time'=>$now
- );
- changeShopOrderStatus($v['uid'],$v['id'],$data); //将订单关闭
- //商品规格库存回增
- changeShopGoodsSpecNum($v['goodsid'],$v['spec_id'],$v['nums'],1);
- //给买家发消息
- $title=lang('你购买的“{goods_name}”订单由于超时未付款,已自动关闭',['goods_name'=>$v['goods_name']]);
- $data1=array(
- 'uid'=>$v['uid'],
- 'orderid'=>$v['id'],
- 'title'=>$title,
- 'addtime'=>$now,
- 'type'=>'0'
- );
- addShopGoodsOrderMessage($data1);
- //发送腾讯云IM
- txMessageIM($title,$v['uid'],'goodsorder_admin');
- }
- }
- if($v['status']==1){ //买家已付款 判断卖家发货是否超时
- //如果买家没有申请退款
- if($v['refund_status']==0){
- $shipment_end=$v['paytime']+$effective_time['shop_shipment_time']*60*60*24;
-
- }else{ //买家申请了退款,判断时间超时,要根据退款最终的处理时间
-
- $shipment_end=$v['refund_endtime']+$effective_time['shop_shipment_time']*60*60*24;
- }
- if($shipment_end<=$now){
- $data=array(
- 'status'=>-1,
- 'cancel_time'=>$now
- );
- changeShopOrderStatus($v['uid'],$v['id'],$data); //将订单关闭
- //退还买家货款
- setUserBalance($v['uid'],1,$v['total']);
- //添加余额操作记录
- $data1=array(
- 'uid'=>$v['uid'],
- 'touid'=>$v['shop_uid'],
- 'balance'=>$v['total'],
- 'type'=>1,
- 'action'=>3, //卖家超时未发货,退款给买家
- 'orderid'=>$v['id'],
- 'addtime'=>$now
- );
- addBalanceRecord($data1);
- //店铺逾期发货记录+1
- Db::name("shop_apply")
- ->where("uid={$v['shop_uid']}")
- ->inc('shipment_overdue_num')
- ->update();
- //减去商品销量
- changeShopGoodsSaleNums($v['goodsid'],0,$v['nums']);
- //减去店铺销量
- changeShopSaleNums($v['shop_uid'],0,$v['nums']);
- //给买家发消息
- $title=lang('你购买的“{goods_name}”订单由于卖家超时未发货已自动关闭,货款已退还到余额账户中',['goods_name'=>$v['goods_name']]);
- $data2=array(
- 'uid'=>$v['uid'],
- 'orderid'=>$v['id'],
- 'title'=>$title,
- 'addtime'=>$now,
- 'type'=>'0'
- );
- addShopGoodsOrderMessage($data2);
- //发送腾讯云IM
- txMessageIM($title,$v['uid'],'goodsorder_admin');
- }
- }
- if($v['status']==2){ //待收货 判断自动确认收货时间是否已满足
- //如果买家没有申请退款
- if($v['refund_status']==0){
- $receive_end=$v['shipment_time']+$effective_time['shop_receive_time']*60*60*24;
- }else{
- $receive_end=$v['refund_endtime']+$effective_time['shop_receive_time']*60*60*24;
- }
- if($receive_end<=$now){
- $data=array(
- 'status'=>3,
- 'receive_time'=>$now
- );
- changeShopOrderStatus($v['uid'],$v['id'],$data); //将订单改为待评价
- //给买家发消息
- $title=lang('你购买的“{goods_name}”订单已自动确认收货',['goods_name'=>$v['goods_name']]);
- $data2=array(
- $data1=array(
- 'uid'=>$v['uid'],
- 'orderid'=>$v['id'],
- 'title'=>$title,
- 'addtime'=>$now,
- 'type'=>'0'
- );
- addShopGoodsOrderMessage($data1);
- //发送腾讯云IM
- txMessageIM($title,$v['uid'],'goodsorder_admin');
- }
- }
- if( ($v['status']==3||$v['status']==4) && $v['settlement_time']==0){ //待评价或已评价 且未结算
- //判断是否有过退货处理 判断确认收货后是否达到后台设置的给卖家打款的时间
- if($v['refund_status']==0){
- $settlement_end=$v['receive_time']+$effective_time['shop_settlement_time']*60*60*24;
- }else{
- $settlement_end=$v['refund_endtime']+$effective_time['shop_settlement_time']*60*60*24;
- }
-
- if($settlement_end<=$now){
- //判断自动结算记录是否存在
- $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();
- if(!$balance_record){
- //计算主播代售平台商品佣金
- if($v['commission']>0 && $v['liveuid']){
- //给主播增加余额
- setUserBalance($v['liveuid'],1,$v['commission']);
- //写入余额操作记录
- $data3=array(
- 'uid'=>$v['liveuid'], //主播ID
- 'touid'=>$v['uid'], //买家用户ID
- 'balance'=>$v['commission'],
- 'type'=>1,
- 'action'=>9, //代售平台商品佣金
- 'orderid'=>$v['id'],
- 'addtime'=>$now
- );
- addBalanceRecord($data3);
- //给主播发消息
- $title1=lang('买家购买的“{goods_name}”订单佣金{commission}已自动结算到你的账户',['goods_name'=>$v['goods_name'],'commission'=>$v['commission']]);
- $data4=array(
- 'uid'=>$v['liveuid'],
- 'orderid'=>$v['id'],
- 'title'=>$title1,
- 'addtime'=>$now,
- 'type'=>'1',
- 'is_commission'=>'1'
- );
- addShopGoodsOrderMessage($data4);
- //发送腾讯云IM
- txMessageIM($title1,$v['liveuid'],'goodsorder_admin');
- }
- //计算分享用户的分享佣金
- if($v['shareuid']>0 && $v['share_income']){
- //给用户增加余额
- setUserBalance($v['shareuid'],1,$v['share_income']);
- //写入余额操作记录
- $data5=array(
- 'uid'=>$v['shareuid'], //分享用户ID
- 'touid'=>$v['uid'], //买家用户ID
- 'balance'=>$v['share_income'],
- 'type'=>1,
- 'action'=>10, //分享商品给其他用户购买后获得佣金
- 'orderid'=>$v['id'],
- 'addtime'=>$now
- );
- addBalanceRecord($data5);
- }
- //给卖家增加余额
- $balance=$v['total']-$v['share_income'];
- if($v['order_percent']>0){
- $balance=$balance*(100-$v['order_percent'])/100;
- $balance=round($balance,2);
- }
-
- $res1=setUserBalance($v['shop_uid'],1,$balance);
- //更改订单信息
- $data=array(
- 'settlement_time'=>$now
- );
- changeShopOrderStatus($v['uid'],$v['id'],$data);
- //添加余额操作记录
- $data1=array(
- 'uid'=>$v['shop_uid'],
- 'touid'=>$v['uid'],
- 'balance'=>$balance,
- 'type'=>1,
- 'action'=>2, //系统自动结算货款给卖家
- 'orderid'=>$v['id'],
- 'addtime'=>$now
- );
- addBalanceRecord($data1);
- //主播才发送消息,平台自营不发消息
- if($v['shop_uid']>1){
- //给卖家发消息
- $title=lang('买家购买的“{goods_name}”订单已自动结算到你的账户',['goods_name'=>$v['goods_name']]);
- $data2=array(
- 'uid'=>$v['shop_uid'],
- 'orderid'=>$v['id'],
- 'title'=>$title,
- 'addtime'=>$now,
- 'type'=>'1'
- );
- addShopGoodsOrderMessage($data2);
- //发送腾讯云IM
- txMessageIM($title,$v['shop_uid'],'goodsorder_admin');
- }
-
- }
-
- }
- }
- if($v['status']==5&&$v['refund_status']==0){ //退款 判断等待卖家处理的时间是否超出后台设定的时间,如果超出,自动退款
- //获取退款申请信息
- $where=array(
- 'orderid'=>$v['id']
- );
- $refund_info=getShopOrderRefundInfo($where);
-
- if($refund_info['is_platform_interpose']==0&&$refund_info['shop_result']==0){ //平台未介入且店家未处理
- $refund_end=$refund_info['addtime']+$effective_time['shop_refund_time']*60*60*24;
- if($refund_end<=$now){
- //更改订单退款状态
- $data=array(
- 'refund_status'=>1,
- 'refund_endtime'=>$now
- );
- changeShopOrderStatus($v['uid'],$v['id'],$data);
- //更改订单退款记录信息
- $data1=array(
- 'system_process_time'=>$now,
- 'status'=>1,
- );
- changeGoodsOrderRefund($where,$data1);
-
- //退还买家货款
- setUserBalance($v['uid'],1,$v['total']);
- //添加余额操作记录
- $data1=array(
- 'uid'=>$v['uid'],
- 'touid'=>$v['shop_uid'],
- 'balance'=>$v['total'],
- 'type'=>1,
- 'action'=>4, //买家发起退款,卖家超时未处理,系统自动退款
- 'orderid'=>$v['id'],
- 'addtime'=>$now
- );
- addBalanceRecord($data1);
- //减去商品销量
- changeShopGoodsSaleNums($v['goodsid'],0,$v['nums']);
- //减去店铺销量
- changeShopSaleNums($v['shop_uid'],0,$v['nums']);
- //商品规格库存回增
- changeShopGoodsSpecNum($v['goodsid'],$v['spec_id'],$v['nums'],1);
- //给买家发消息
- $title=lang('你申请的“{goods_name}”订单退款卖家超时未处理,已自动退款到你的余额账户中',['goods_name'=>$v['goods_name']]);
- $data2=array(
- 'uid'=>$v['uid'],
- 'orderid'=>$v['id'],
- 'title'=>$title,
- 'addtime'=>$now,
- 'type'=>'0'
- );
- addShopGoodsOrderMessage($data2);
- //发送腾讯云IM
- txMessageIM($title,$v['uid'],'goodsorder_admin');
- }
-
- }
- if($refund_info['is_platform_interpose']==0&&$refund_info['shop_result']==-1){ //未申请平台介入且店家已拒绝
- //超时,退款自动完成,订单自动进入退款前状态
- $finish_endtime=$refund_info['shop_process_time']+$effective_time['shop_refund_finish_time']*60*60*24;
- if($finish_endtime<=$now){
- //更改退款订单状态
- $data=array(
- 'status'=>1,
- 'system_process_time'=>$now
- );
- changeGoodsOrderRefund($where,$data);
- //更改订单状态
- $data1=array(
- 'refund_endtime'=>$now,
- 'refund_status'=>-1
- );
- if($v['receive_time']>0){
- $data1['status']=3; //待评价
- }else{
- if($v['shipment_time']>0){
- $data1['status']=2; //待收货
- }else{
- $data1['status']=1; //待发货
- }
- }
- changeShopOrderStatus($v['uid'],$v['id'],$data1);
- //给买家发消息
- $title=lang('你购买的“{goods_name}”订单退款申请被卖家拒绝后,{shop_refund_finish_time}天内你没有进一步操作,系统自动处理结束',['goods_name'=>$v['goods_name'],'shop_refund_finish_time'=>$effective_time['shop_refund_finish_time']]);
- $data2=array(
- 'uid'=>$v['uid'],
- 'orderid'=>$v['id'],
- 'title'=>$title,
- 'addtime'=>$now,
- 'type'=>'0'
- );
- addShopGoodsOrderMessage($data2);
- //发送腾讯云IM
- txMessageIM($title,$v['uid'],'goodsorder_admin');
- }
- }
- }
- $lastid=$v['id'];
-
- }
- //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);
- $list_nums=count($order_list);
- if($list_nums<$limit){
- echo "NO";
- return;
- }
- echo 'OK-'.$lastid;
- }
- }
|