where('type', 2) ->where('end_time<' . time())->select(); if ($uu) { $stoidarr = get_arr_column($uu, 'store_id'); M('store_distribut')->where('store_id', 'in', $stoidarr)->save(['distribut_num' => 0]); mlog('过期商家:' . json_encode($stoidarr),'timeset'); } }else{ $uu = M("store_module_endtime")->where('type', 2)->where('store_id',$stoid) ->where('end_time<' . time())->find(); if ($uu) { M('store_distribut')->where('store_id', $stoid)->save(['distribut_num' => 0]); mlog('过期商家:' . $stoid,'timeset'); } } $this->clear_order($stoid); $time=strtotime('-1 day'); M("storage_recharge")->where('billdate<'.$time) ->where('transaction_id','') ->where('recharge_state',0)->delete(); } //十分钟的工作任务,针对天天拼单的方法 public function time_minutes(){ $date=date('Y-m-d H:i:s'); $now_time=time(); mlog('时间是:'.$date."-".$now_time,'time_minutes'); //对于一些阶梯团,判断这些阶梯团是否成团, //同时计算尾款的金额 if(empty($stoid)) { $olist1 =M('teamgroup')->where('team_type',3)->where('state',2)->select(); foreach ($olist1 as $k=>$v){ //当这个团已经到时间了 if($v['kt_end_time']<=$now_time) { //查找成团人数 $ordarr = M('order')->where('is_zsorder', 4)->where('pt_listno', $v['listno']) ->where('pt_prom_id', $v['team_id'])->where('pt_status', 1) ->where('store_id',$v['store_id']) ->where('order_status=0 or order_status=1') ->field('order_id,order_sn,order_amount,user_id,store_id,pt_prom_id,user_money,shipping_price,pt_status') ->select(); $count=count($ordarr); $jsarr=json_decode($v['jt_json'],true); //如果阶梯团大于这个人数,就是成团了, if($count>=$jsarr[0]['rynum']){ $endtime=strtotime('+24 hour'); M('teamgroup')->where('id',$v['id']) ->save(['state'=>4,'wk_end_time'=>$endtime,'jt_ct_num'=>$count,'buyenddate'=>$now_time]); //要计算尾款的价钱 $price=0; foreach ($jsarr as $k=>$v){ if($count>=$v['rynum']) $price=$v['price'];} ///$oid=get_arr_column($ordarr,'order_id'); foreach ($ordarr as $kp=>$vp){ //更新订单商品表的单价 $dd['goods_price']=$price; $dd['member_goods_price']=$price; M('order_goods')->where('order_id',$vp['order_id'])->save($dd); //更新尾款,要加上物流费用的计算 $num= M('order_goods')->where('order_id',$vp['order_id'])->sum('goods_num'); M('order')->where('order_id',$vp['order_id']) ->save(['pt_status'=>2, 'pt_tail_money'=>$price*$num+$vp['shipping_price']-$vp['order_amount']-$vp['user_money'] ]); M('order_goods')->where('order_id',$vp['order_id']) ->save(['goods_price'=>$price,'member_goods_price'=>$price,'account'=>$price]); } //如果是满团,就要进行推送 $timer = new \app\timer\controller\Index(); $olist=$ordarr; if($olist){ foreach ($olist as $k=>$v){ $stoid=$v['store_id']; $title='拼团满团,等待支付尾款'; $remark='感谢您的参与~'; $usr=M('users')->where('user_id',$v['user_id'])->field('user_id')->find(); $erpid=tpCache('shop_info.ERPId',$stoid); $gd=M('order_goods')->where('order_id',$v['order_id'])->field('goods_name')->find(); $teamlist=M('teamlist')->where('id',$v['pt_prom_id'])->field('ct_num')->find(); $backurl=curHostURL(). "/Mobile/User/order_detail/id/".$v['order_id']."/stoid/".$stoid; $timer->post_msg($stoid,$erpid,$usr['user_id'],$title, $gd['goods_name'],$teamlist['ct_num'],$remark,$backurl); } } }else{ //未成团处理 //M('teamgroup')->where('id',$v['id'])->save(['buyenddate'=>time(),'state'=>1]); $this->out_tuan($v); } } } }else{ $this->deal_jt_pt($stoid); } //以下是拼单退款处理 include_once "plugins/payment/weixin/weixin.class.php"; $wx=new \weixin(); //对于拼单商品要进行退款。 //俩种,1中是未成团 2是尾款没有支付完成 if(empty($stoid)) { //-------处理30分钟都还没有成团的商家团------- $tt=strtotime("-30 minute"); $tgr_ten_mini_list=M('teamgroup') ->where('addtime<'.$tt) ->where('state',2) ->where('team_type',1)->order('store_id asc,team_id asc')->select(); //对所有的未成团的进行中的团,要进行拆解 if($tgr_ten_mini_list){ $tg_id=null; foreach ($tgr_ten_mini_list as $kk=>$vv){ $tg_id[$vv['team_id']][]=['tgr_id'=>$vv['id'],'listno'=>$vv['listno'],'stoid'=>$vv['store_id']]; } foreach($tg_id as $kh=>$vh){ //如果只有一个就重新循环 if(count($vh)==1) continue; //把订单ID收集起来 $ord_id=[]; foreach($vh as $kb=>$vb){ $ord=M("order")->where('pt_listno',$vb['listno'])->where('pt_status',1) ->where('store_id',$vb['stoid'])->field('order_id')->select(); if($ord){ foreach ($ord as $kj=>$vj){ $ord_id[]=$vj['order_id']; } } //删除团 M("teamgroup")->where('id',$vb['tgr_id'])->delete(); } //订单的listno都清空 M('order')->where("order_id","in",$ord_id)->save(['pt_listno'=>'']); //按ct_num分组 $tlist=M('teamlist')->where('id',$kh)->field('ct_num,store_id,kt_time')->find(); //分组成团 $this->fen_zu_treamgroup($ord_id, $tlist['ct_num'],$kh,$tlist['store_id'],$tlist['kt_time']); } } //处理未成团,并且已经过期的团,商家团 $tgrlist=M('teamgroup') ->where('kt_end_time<'.$now_time) ->where('state',2) ->where('team_type',1)->select(); if($tgrlist) { foreach ($tgrlist as $kl => $vl) { $tlist = M('teamlist')->where('store_id', $vl['store_id']) ->where('id', $vl['team_id']) ->field('goods_num,ct_num,buy_num')->find(); $olist = M('order')->where('store_id', $vl['store_id']) ->where('pt_prom_id', $vl['team_id']) ->where('pt_listno', $vl['listno']) ->where('order_status', 1) ->where('pt_status', 1) //->field('order_id,order_sn,order_amount,user_id,store_id,pt_listno,pt_prom_id,user_money,pt_status') ->select(); mlog("商家团:".$vl['listno']."-".json_encode($olist),"time_minutes/".$vl['store_id']); if ($olist) { $count = count($olist); $oidarr = get_arr_column($olist, 'order_id'); //最后一个人买完,且没有库存,虽然达不到成团人数,那么这个团让它成团 /* if ($tlist['goods_num'] - $tlist['buy_num']<=0) { M('teamgroup')->where('id', $vl['id'])->save(['state' => 3, 'buyenddate' => time()]); M('order')->where('store_id', $vl['store_id']) ->where('order_id', 'in', $oidarr) ->save(['pt_status' => 2, 'pay_status' => 1, 'order_status' => 1]); //如果是满团,就要进行推送 $timer = new \app\timer\controller\Index(); if ($olist) { foreach ($olist as $k => $v) { $stoid = $v['store_id']; $title = '拼团满团成功'; $remark = '感谢您的参与~'; $usr = M('users')->where('user_id', $v['user_id'])->field('user_id')->find(); $erpid = tpCache('shop_info.ERPId', $stoid); $gd = M('order_goods')->where('order_id', $v['order_id'])->field('goods_name')->find(); $teamlist = M('teamlist')->where('id', $v['pt_prom_id'])->field('ct_num')->find(); $backurl= curHostURL() ."/index.php/Mobile/team/team_success/orderno/".$v['order_sn']."/stoid/".$stoid; $timer->post_msg($stoid, $erpid, $usr['user_id'], $title, $gd['goods_name'], $teamlist['ct_num'], $remark,$backurl); } } } else { */ foreach ($olist as $k => $v) { if($v['pt_status']==1) $this->back_order_money($v,$wx); } //更新团的结束时间,对失效的团要进行处理 M('teamgroup')->where('id', $vl['id'])->save(['buyenddate' => $now_time, 'state' => 1]); //} }else{ //删除一些无效的商家团 M('teamgroup')->where('id',$vl['id'])->delete(); } } } //对于订单要进行退款,处理会员团 $tgrlist1=M('teamgroup') ->where('state',2) ->where('kt_end_time<'.$now_time) ->where('team_type=2') ->select(); if($tgrlist1) { $idarr = get_arr_column($tgrlist1, 'id'); //M('teamgroup')->where('id', 'in', $idarr)->save(['state' => 1, 'buyenddate' => time()]); foreach ($tgrlist1 as $kt=>$vt) { //对于订单要进行退款,处理会员团,阶梯团 $olist = M('order') ->where('store_id', $vt['store_id']) ->where('pt_prom_id', $vt['team_id']) ->where('pt_listno', $vt['listno']) ->where('order_status=0 or order_status=1') ->select(); mlog("会员团:".$vt['listno']."-".json_encode($olist),"time_minutes/".$vt['store_id']); if ($olist) { foreach ($olist as $k => $v) { //有支付的订单 if($v['pt_status']==1) { $this->back_order_money($v,$wx); } //未支付的订单 else if($v['pt_status']==0){ $this->cancel_order($v); } } } M('teamgroup')->where('id', $vt['id'])->save(['state' => 1, 'buyenddate' => $now_time]); } } //处理阶梯团,但是尾款未支付的订单 $olist1=M('order')->alias('a')->join('teamgroup b','b.listno=a.pt_listno and a.pt_prom_id=b.team_id') ->where('a.is_zsorder', 4) ->where('b.wk_end_time<'.$now_time) ->where('a.pt_status',2) ->where('b.state',4) ->where('a.pay_status=0')->field('a.*,b.id as tgid')->select(); if($olist1){ $idarr=get_arr_column($olist1,'tgid'); $idarr=array_unique($idarr); //更新订单状态 foreach ($olist1 as $k=>$v) { if($v['pt_status']<>4 && $v['pt_status']<>6 ){ //未支付尾款的订单,取消订单 $rw= M('order')->where('order_id', $v['order_id']) ->where('order_status<>3') ->save(['order_status' => 3, 'pt_status' => 6]); if($rw) { //M("teamgroup")->where('listno', $v['pt_listno']) //->where('team_id', $v['pt_prom_id']) //->save(['state' => 5]); mlog('pingtuan尾款过期的订单:' . json_encode($olist1), 'timeset'); $this->back_store_redis($v, '拼团未支付尾款失败'); } } M('teamgroup')->where('id',$v['tgid'])->save(['state'=>5]); } } } else{ $this->claer_pt_out($stoid); } //处理一些已经支付了的订单,余额支付的 $this->update_set_order($stoid); //十分钟处理赠送礼品的订单 $this->deal_lipin_order(); //十分钟处理推送 wechat_store_wxsend(); wechat_user_wxsend(); } //在进入拼单详情页时候要进行处理 public function claer_pt_out($stoid){ //以下是拼单退款处理 include_once "plugins/payment/weixin/weixin.class.php"; $wx=new \weixin(); //处理未成团,并且已经过期的团,商家团 $tgrlist=M('teamgroup') ->where('store_id',$stoid) ->where('kt_end_time<'.time()) ->where('state',2) ->where('team_type',1)->select(); if($tgrlist) { foreach ($tgrlist as $kl => $vl) { $tlist = M('teamlist')->where('store_id', $vl['store_id']) ->where('id', $vl['team_id']) ->field('goods_num,ct_num,buy_num')->find(); $olist = M('order')->where('store_id', $vl['store_id']) ->where('pt_prom_id', $vl['team_id']) ->where('pt_listno', $vl['listno']) ->where('order_status', 1) ->where('pt_status', 1) //->field('order_id,order_sn,order_amount,user_id,store_id,pt_listno,pt_prom_id,user_money') ->select(); if ($olist) { $count = count($olist); $oidarr = get_arr_column($olist, 'order_id'); //最后一个人买完,且没有库存,虽然达不到成团人数,那么这个团让它成团 if ($tlist['goods_num'] - $tlist['buy_num']<=0) { M('teamgroup')->where('id', $vl['id'])->save(['state' => 3, 'buyenddate' => time()]); M('order')->where('store_id', $vl['store_id']) ->where('order_id', 'in', $oidarr) ->save(['pt_status' => 2, 'pay_status' => 1, 'order_status' => 1]); //如果是满团,就要进行推送 $timer = new \app\timer\controller\Index(); if ($olist) { foreach ($olist as $k => $v) { $stoid = $v['store_id']; $title = '拼团满团成功'; $remark = '感谢您的参与~'; $usr = M('users')->where('user_id', $v['user_id'])->field('user_id')->find(); $erpid = tpCache('shop_info.ERPId', $stoid); $gd = M('order_goods')->where('order_id', $v['order_id'])->field('goods_name')->find(); $teamlist = M('teamlist')->where('id', $v['pt_prom_id'])->field('ct_num')->find(); $backurl= curHostURL() ."/index.php/Mobile/team/team_success/orderno/".$v['order_sn']."/stoid/".$stoid; $timer->post_msg($stoid, $erpid, $usr['user_id'], $title, $gd['goods_name'], $teamlist['ct_num'], $remark,$backurl); } } } else { //更新团的结束时间,对失效的团要进行处理 M('teamgroup')->where('id', $vl['id'])->save(['buyenddate' => time(), 'state' => 1]); foreach ($olist as $k => $v) { $this->back_order_money($v,$wx); } } } } } //对于订单要进行退款,处理会员团 $tgrlist1=M('teamgroup') ->where('store_id',$stoid) ->where('state',2) ->where('kt_end_time<'.time()) ->where('team_type=2') ->select(); if($tgrlist1) { $idarr = get_arr_column($tgrlist1, 'id'); M('teamgroup')->where('id', 'in', $idarr)->save(['state' => 1, 'buyenddate' => time()]); foreach ($tgrlist1 as $kt=>$vt) { //对于订单要进行退款,处理会员团,阶梯团 $olist = M('order') ->where('store_id', $vt['store_id']) ->where('pt_prom_id', $vt['team_id']) ->where('pt_listno', $vt['listno']) ->where('order_status=0 or order_status=1') ->select(); if ($olist) { foreach ($olist as $k => $v) { //有支付的订单 if($v['pt_status']==1) { $this->back_order_money($v,$wx); } //未支付的订单 else if($v['pt_status']==0){ $this->cancel_order($v); } } } } } //处理阶梯团,但是尾款未支付的订单 $olist1=M('order')->alias('a')->join('teamgroup b','b.listno=a.pt_listno and a.pt_prom_id=b.team_id') ->where('a.is_zsorder', 4)->where('a.store_id',$stoid) ->where('b.wk_end_time<'.time()) ->where('a.pt_status',2) ->where('b.state',4) ->where('a.pay_status=0')->field('a.*,b.id as tgid')->select(); if($olist1){ $idarr=get_arr_column($olist1,'tgid'); $idarr=array_unique($idarr); M('teamgroup')->where('id','in',$idarr)->save(['state'=>5]); //更新订单状态 foreach ($olist1 as $k=>$v) { if($v['pt_status']<>4){ //未支付尾款的订单,取消订单 $rw= M('order')->where('order_id', $v['order_id']) ->where('order_status<>3') ->save(['order_status' => 3, 'pt_status' => 6]); //M("teamgroup")->where('listno', $v['pt_listno']) //->where('team_id', $v['pt_prom_id']) //->save(['state' => 5]); if($rw){ mlog('pingtuan尾款过期的订单:' . json_encode($olist1), 'timeset'); $this->back_store_redis($v, '拼团未支付尾款失败'); } } } } } //对于一些阶梯团,判断这些阶梯团是否成团 public function deal_jt_pt($stoid){ $olist1 =M('teamgroup')->where('team_type',3)->where('state',2)->where('store_id',$stoid)->select(); foreach ($olist1 as $k=>$v){ //当这个团已经到时间了 if($v['kt_end_time']<=time()) { //查找成团人数 $ordarr = M('order')->where('is_zsorder', 4)->where('pt_listno', $v['listno']) ->where('pt_prom_id', $v['team_id'])->where('pt_status', 1)->where('store_id',$v['store_id']) ->where('order_status=0 or order_status=1') ->field('order_id,order_sn,order_amount,user_id,store_id,pt_prom_id,user_money,shipping_price') ->select(); $count=count($ordarr); $jsarr=json_decode($v['jt_json'],true); //如果阶梯团大于这个人数,就是成团了, if($count>=$jsarr[0]['rynum']){ $endtime=strtotime('+24 hour'); M('teamgroup')->where('id',$v['id']) ->save(['state'=>4,'wk_end_time'=>$endtime,'jt_ct_num'=>$count,'buyenddate'=>time()]); //要计算尾款的价钱 $price=0; foreach ($jsarr as $k=>$v){ if($count>=$v['rynum']) $price=$v['price'];} //$oid=get_arr_column($ordarr,'order_id'); foreach ($ordarr as $kp=>$vp){ //更新订单商品表的单价 $dd['goods_price']=$price; $dd['member_goods_price']=$price; M('order_goods')->where('order_id',$vp['order_id'])->save($dd); //更新尾款,要计算物流费用 $num= M('order_goods')->where('order_id',$vp['order_id'])->sum('goods_num'); M('order')->where('order_id',$vp['order_id']) ->save(['pt_status'=>2, 'pt_tail_money'=>$price*$num+$vp['shipping_price']-$vp['order_amount']-$vp['user_money'] ]); } //如果是满团,就要进行推送 $timer = new \app\timer\controller\Index(); $olist= $ordarr; if($olist){ foreach ($olist as $k=>$v){ $stoid=$v['store_id']; $title='拼团满团,等待支付尾款'; $remark='感谢您的参与~'; $usr=M('users')->where('user_id',$v['user_id'])->field('user_id')->find(); $erpid=tpCache('shop_info.ERPId',$stoid); $gd=M('order_goods')->where('order_id',$v['order_id'])->field('goods_name')->find(); $teamlist=M('teamlist')->where('id',$v['pt_prom_id'])->field('ct_num')->find(); $backurl=curHostURL(). "/Mobile/User/order_detail/id/".$v['order_id']."/stoid/".$stoid; $timer->post_msg($stoid,$erpid,$usr['user_id'], $title,$gd['goods_name'],$teamlist['ct_num'],$remark,$backurl); } } } else{ //未成团处理 M('teamgroup')->where('id',$v['id'])->save(['buyenddate'=>time(),'state'=>1]); $this->out_tuan($v); } } } } /*----公用函数---*/ public function back_store_redis($v,$title="拼团未满团到期失败"){ $stoid=$v['store_id']; $usr=M('users')->where('user_id',$v['user_id'])->field('user_id')->find(); $erpid=tpCache('shop_info.ERPId',$stoid); $gd=M('order_goods')->where('order_id',$v['order_id'])->field('goods_name')->find(); $teamlist=M('teamlist')->where('id',$v['pt_prom_id'])->field('ct_num')->find(); $remark='感谢您的参与~'; $backurl= curHostURL() ."/index.php/Mobile/team/team_success/orderno/".$v['order_sn']."/stoid/".$stoid; $this->post_msg($stoid,$erpid,$usr['user_id'],$title,$gd['goods_name'],$teamlist['ct_num'],$remark,$backurl); //退回库存 $ord_good=M('order_goods')->where('order_sn',$v['order_sn'])->find(); // 减少商品销售量 M('goods')->where('goods_id', $ord_good['goods_id'])->setDec('sales_sum', $ord_good['goods_num']); // 增加库存 M('goods')->where('goods_id', $ord_good['goods_id'])->setInc('store_count', $ord_good['goods_num']); //补回活动数量 M('teamlist')->where('id',$v['pt_prom_id'])->setDec('buy_num',$ord_good['goods_num']); M('teamlist')->where('id',$v['pt_prom_id'])->setDec('order_num'); //$redis=new \Redis(); //$redis->connect(redisip, 6379); $redis=get_redis_handle(); //补回redis //$name='pind'.$v['pt_prom_id'].'-'.$ord_good['store_id']; $name=get_redis_name($v['pt_prom_id'],6,$ord_good['store_id']); if(!doublefind($redis,$name,$ord_good['rec_id'])) { for ($i = 0; $i < $ord_good['goods_num']; $i++) { $redis->lPush($name, $ord_good['rec_id']. '_' . $i); $jsda['num'] =$ord_good['goods_num']; $jsda['order_sn'] = $ord_good['order_sn']; $jsda['type'] = 6; //10分退款补回 mlog(json_encode($jsda), 'pt_log/' . $stoid . '/' .$ord_good['prom_id']."_3"); } } if($v['is_zsorder']==3){ //$name2 = 'pind'.$v['pt_prom_id'].'-'.$v['pt_listno'].$ord_good['store_id']; $name2 = get_redis_name($v['pt_prom_id'],6,$ord_good['store_id']) .':'.$v['pt_listno']; //$redis->delete($name2); del_redis($redis,$name2); } } //推送消息 public function post_msg($stoid,$erpid,$user_id,$title,$goods_name,$num,$remark,$backurl=""){ $colorlist = urlencode("#FF0000|#173177|#173177|#173177|#173177|#FF0000|#FF0000|#ca003a"); //$url = curHostURL() . "/home/api/poswxcode?typeid=1023&Acc=" . $erpid . "&ErpVipid=" . $erpvipid . "&title=" . $title . "&key1=" . $goods_name . "&key2=" . $num . "人团&remark=" . $remark.'&colorlist='. $colorlist."&backurl=" . $backurl; //mlog($url,'poswxcode/'.$erpid); $data['typeid']='1023';//微信消息类型 1001至1010 $data['title']=$title; $data['remark']=$remark; $data['key1']=$goods_name; $data['key2']=$num.'人团'; $data['colorlist']=$colorlist; $data['backurl']=$backurl; $data['Acc']=$erpid; $data['user_id']=$user_id; $api = new \app\home\controller\Api(); //$resp=httpRequest($url, "GET"); $resp=$api->poswxcode2($data); return $resp; } //处理过期的阶梯团里的订单 public function out_tuan($vt){ //以下是拼单退款处理 include_once "plugins/payment/weixin/weixin.class.php"; $wx=new \weixin(); //对于订单要进行退款,处理会员团,阶梯团 $olist = M('order') ->where('store_id', $vt['store_id']) ->where('pt_prom_id', $vt['team_id']) ->where('pt_listno', $vt['listno']) ->where('order_status=0 or order_status=1') ->select(); if ($olist) { foreach ($olist as $k => $v) { if($v['pt_status']==1) { $this->back_order_money($v,$wx); }else if($v['pt_status']==0){ $this->cancel_order($v); } } M('teamgroup')->where('id',$vt['id'])->save(['buyenddate'=>time(),'state'=>1]); } } //清除不是当天未付款的订单 public function clear_order($stoid=0){ $where="1=1"; if(!empty($stoid)){ $where.=" and store_id=".$stoid ; } $time=strtotime('-1 day'); //清除不是当天未付款的订单 $delTempOrder1 = M('order') ->where('pay_status=0 and order_status in(0,1) and is_zsorder=0') ->where("add_time<".$time) ->where($where) ->field("user_id,user_money,coupon_no,store_id,order_id,is_zsorder,pt_prom_id,pt_listno,is_pt_tz,gift_receive_id") ->select(); $delTempOrder2 = M('order') ->where($where) ->where('pt_status=0 and order_status in(0,1) and is_zsorder>1') ->where("add_time<".$time) ->field("user_id,user_money,coupon_no,store_id,order_id,is_zsorder,pt_prom_id,pt_listno,is_pt_tz,gift_receive_id") ->select(); $delTempOrder=array_merge($delTempOrder1,$delTempOrder2); foreach ($delTempOrder as $k => $v) { $ur= M('order')->where(array('order_id' => $v['order_id'])) ->where('order_status<>3') ->save(['order_status' => 3]); if($ur) { $user_id = $v['user_id']; $stoid= $v['store_id']; // mlog($v['order_id'],"temporder"); // M('ordr_goods')->where(' order_id=') if ($v['user_money'] > 0) { //解冻余额 $rs = M('Users')->where("user_id", $v['user_id']) ->where('frozen_money>=' . $v['user_money'])->find(); if ($rs) M('Users')->where("user_id", $v['user_id'])->setDec('frozen_money', $v['user_money']); } /*---优惠券冻结---*/ $quanno = $v['coupon_no']; if (!empty($quanno)) { $dada = ['CashRepNo' => $quanno, 'user_id' => $user_id]; //冻结优惠券 M('frozen_quan')->where($dada)->delete(); } //$redis = new \Redis(); //$redis->connect(redisip, 6379); $redis=get_redis_handle(); $hh = M('order_goods')->where('order_id', $v['order_id']) ->where('prom_type',1)->where('store_id',$stoid) ->field('order_id,goods_num,prom_id,order_sn,rec_id')->select(); if($hh) { $odarr0 = get_arr_column($hh, 'order_id'); //增加队列 foreach ($hh as $k => $v) { //$name0 ='ms'.$v['prom_id'] . '-' . $stoid; $name0 =get_redis_name($v['prom_id'],1,$stoid); //不重复退回redis if(!doublefind($redis,$name0,$v['rec_id'])) { for ($i = 0; $i < $v['goods_num']; $i++) { $redis->lPush($name0, $v['rec_id'] . '_' . $i); } $jsda['num'] = $v['goods_num']; $jsda['order_sn'] = $v['order_sn']; $jsda['type'] = 1; //1是5分钟自动补回 mlog(json_encode($jsda), 'flash_log/' . $stoid . '/' . $v['prom_id']); } } } $hh2 = M('order_goods')->where('order_id', $v['order_id']) ->where('prom_type',2)->where('store_id',$stoid) ->field('order_id,goods_num,prom_id,order_sn,rec_id')->select(); if($hh2){ $odarr0 = get_arr_column($hh, 'order_id'); //增加队列 foreach ($hh2 as $k => $v) { //$name_g ='grb'.$v['prom_id'] . '-' . $stoid; $name_g =get_redis_name($v['prom_id'],2,$stoid); //不重复退回redis if(!doublefind($redis,$name_g,"2".$v['rec_id'])) { for ($i = 0; $i < $v['goods_num']; $i++) { $redis->lPush($name_g, "2".$v['rec_id'] . '_' . $i); } $jsda['num'] = $v['goods_num']; $jsda['order_sn'] = $v['order_sn']; $jsda['type'] = 1; //1是5分钟自动补回 mlog(json_encode($jsda), 'group_buy_log/' . $stoid . '/' . $v['prom_id']); } } } if ($v['is_zsorder'] == 3) { //$name2 = 'pind' . $v['pt_prom_id'] . '-' . $v['pt_listno'] . $stoid; $name2 = get_redis_name($v['pt_prom_id'],6,$stoid) . ':' . $v['pt_listno']; if (!doublefind($redis, $name2, $v['order_id'])) { $redis->lPush($name2, $v['order_id'] . '_0'); } //$len = $redis->lLen($name2); //如果这个团,全部补回,那这个单就没有意思了,要delete掉 //$teamnum = M('teamlist')->field('ct_num') // ->where('id', $v['pt_prom_id'])->find(); //if ($len == $teamnum['ct_num']) { if ($v['is_pt_tz']==1) { //$redis->delete($name2); del_redis($redis,$name2); //要取消这个团 $da['state'] = 1; $map['team_id'] = $v['pt_prom_id']; $map['listno'] = $v['pt_listno']; $map['store_id'] = $stoid; M('teamgroup')->where($map)->save($da); } } //补回购买的数量 $hh = M('order_goods')->where('order_id', $v['order_id']) ->where('prom_type',6)->where('store_id',$stoid) ->field('order_id,goods_num,prom_id,rec_id,order_sn')->select(); if($hh) { //增加队列 foreach ($hh as $k => $v) { //$name0 ='pind'.$v['prom_id'] . '-' . $stoid; $name0 =get_redis_name($v['prom_id'],6,$stoid); if(!doublefind($redis,$name0,$v['rec_id'])) { for ($i = 0; $i < $v['goods_num']; $i++) { $redis->lPush($name0, $v['rec_id'] . "_" . $i); $jsdac['num'] = $v['goods_num']; $jsdac['order_sn'] = $v['order_sn']; $jsdac['type'] = 1; //1是5分钟自动补回 mlog(json_encode($jsdac), 'pt_log/' . $stoid . '/' . $v['prom_id']."_c"); } } } } if (!empty($v['gift_receive_id'])) { //M('gift_receive')->where(['id' => ['in', $order['gift_receive_id']]])->delete();//去掉赠品领用记录 $org=M("order_goods")->where('order_id',$v['order_id']) ->where('is_gift',1)->where('store_id',$stoid) ->field('rec_id,gift_id,goods_num,prom_id')->select(); if($org){ foreach($org as $ko=>$vo){ //从表上删除记录,主表做标记,赠品被取消,同时补回活动的库存 //M('order_goods')->where('rec_id',$vo['rec_id'])->delete(); M("order")->where('order_id',$v['order_id'])->where('store_id',$stoid) ->save(['is_del_gift'=>1]); M("gift")->where('store_id',$stoid) ->where('id',$vo['gift_id']) ->setInc('goods_num',$vo['goods_num']); M('gift_receive')->where(["orderGoods_id"=>$vo['rec_id'],'user_id'=>$v['user_id']])->delete(); } } } } } } //退款 public function back_order_money($v,$wx){ $stoid = $v['store_id']; $row= M('order')->where('store_id', $stoid) ->where('order_id', $v['order_id']) ->where('order_status<>3') ->save(['order_status' => 3, 'pt_status' => 3]); if($row) { $dd=null; $refund_type = tpCache('basic.refund_type', $stoid); //退到余额 if ($refund_type == 0) { $user_money = $v['order_amount'] + $v['user_money']; $sql = "update wxd_users set user_money=user_money+" . $user_money . " where user_id=" . $v['user_id']; $rs1 = Db::execute($sql); if ($rs1) { if ($user_money > 0) { $odata['user_id'] = $v['user_id']; $odata['create_time'] = time(); $odata['money'] = $user_money; $odata['remark'] = "退款"; $odata['store_id'] = $v['store_id']; $odata['order_sn'] = $v['order_sn']; $odata['status'] = 1; $odata['type'] = 1; M('withdrawals')->save($odata); } } else { mlog($v['order_sn']."退款到余额失败,", 'refund/' . $stoid); $dd['user_money']=$user_money; } } //原路返回 else if ($refund_type == 1) { if ($v['order_amount'] > 0) { $ordno = $v['order_sn']; $total_fee = $v['order_amount']; $refund_fee = $total_fee; //$erpid = tpCache('shop_info.ERPId', $stoid); $path = BASE_PATH . 'public/cert/' . $stoid . '/'; try { //$wx->setCong($v['store_id']); $rs = $wx->refund2($ordno, $total_fee, $refund_fee, $path,$stoid,null,$v['source_type']); if ($rs['return_code'] == 'FAIL') { mlog($v['order_sn']."退款失败," . $rs['return_msg'], 'refund/' . $stoid); $dd['weixin_err']=$rs['return_msg']; $dd['weixin']=$v['order_amount']; } } catch (\Exception $e) { mlog($v['order_sn']."退款签名失败," . $e->getMessage(), 'refund/' . $stoid); $dd['weixin_err']=$e->getMessage(); $dd['weixin']=$v['order_amount']; } } if ($v['user_money'] > 0) { $user_money = $v['user_money']; $sql = "update wxd_users set user_money=user_money+" . $user_money . " where user_id=" . $v['user_id']; $rs1 = Db::execute($sql); if ($rs1) { if ($user_money > 0) { $odata['user_id'] = $v['user_id']; $odata['create_time'] = time(); $odata['money'] = $user_money; $odata['remark'] = "退款"; $odata['store_id'] = $v['store_id']; $odata['order_sn'] = $v['order_sn']; $odata['status'] = 1; $odata['type'] = 1; M('withdrawals')->save($odata); } } else { mlog($v['order_sn']."退款到余额失败,", 'refund/' . $stoid); $dd['user_money']=$v['user_money']; } } } //如果退款失败要进行记录 if($dd) M("order")->where('order_id',$v['order_id'])->save(['back_err_json'=>json_encode($dd)]); //M("teamgroup")->where('listno', $v['pt_listno']) // ->where('team_id', $v['pt_prom_id']) // ->save(['state' => 1]); mlog('pingtuan过期订单:' . json_encode($v), 'timeset'); $this->back_store_redis($v, '拼团未满团到期失败'); } } /*** 取消订单*/ public function cancel_order($v){ $order = $v; $order_id=$v['order_id']; $user_id=$v['user_id']; $row = M('order')->where(array('order_id' => $order_id, 'user_id' => $user_id)) ->where('order_status<>3') ->save(array('order_status' => 3)); if($row) { //获取记录表信息 //$log = M('account_log')->where(array('order_id'=>$order_id))->find(); //有余额支付的情况 if ($order['user_money'] > 0) { //解冻冻余额 $rs= M('Users')->where("user_id", $user_id) ->where('frozen_money>='.$order['user_money'])->find(); if($rs) { M('Users')->where("user_id", $user_id)->setDec('frozen_money', $order['user_money']); } } /*---优惠券冻结---*/ $quanno= $order['coupon_no']; if(!empty($quanno)) { $dada=['CashRepNo'=>$quanno,'user_id'=>$user_id]; //冻结优惠券 M('frozen_quan')->where($dada)->delete(); } //查一下拼团 $stoid = $order['store_id']; if ($order['is_zsorder'] >= 2) { //$redis = new \Redis(); //$redis->connect(redisip, 6379); $redis=get_redis_handle(); //查找会员团,删除团redis $v = $order; if ($v['is_zsorder'] == 3) { //$name2 = 'pind' . $v['pt_prom_id'] . '-' . $v['pt_listno'] . $stoid; $name2 = get_redis_name($v['pt_prom_id'],6,$stoid) . ':' . $v['pt_listno']; //$redis->delete($name2); del_redis($redis,$name2); } //补回购买的数量 $hh = M('order_goods')->where('order_id', $order_id) ->where('prom_type', 6)->where('store_id', $stoid) ->field('order_id,goods_num,prom_id,rec_id,order_sn')->select(); if ($hh) { //增加队列 foreach ($hh as $k => $v) { //$name0 = 'pind' . $v['prom_id'] . '-' . $stoid; $name0 = get_redis_name($v['prom_id'],6,$stoid); if (!doublefind($redis, $name2, $v['rec_id'])) { for ($i = 0; $i < $v['goods_num']; $i++) { $redis->lPush($name0, $v['rec_id'] . "_" . $i); $jsdad['num'] =$v['goods_num']; $jsdad['order_sn'] = $v['order_sn']; $jsdad['type'] = 7; //10分取消订单退回 mlog(json_encode($jsdad), 'pt_log/' . $stoid . '/' . $v['prom_id']."_4"); } } } } } } } //更新一些余额支付的订单 public function update_set_order($stoid=0){ $where="1=1"; if($stoid){$where.=" and store_id=".$stoid;} //循环更新订单状态,以及库存 $odr_arr=M('order') ->where($where) ->where('user_id',$this->user_id) ->where('order_amount',0) ->where('(pay_status=0 and pt_prom_id=0) or (pt_status=0 and pt_prom_id>0)') ->where('order_status=0 or order_status=1') ->field('order_sn')->select(); if($odr_arr) { foreach ($odr_arr as $k => $v) { update_pay_status2($v['order_sn'], 1); } } } //处理十分都未支付的礼品订单 public function deal_lipin_order() { //查找有促销活动的商家 $now0 = time(); $where0 = "start_time<=" . $now0 . " and " . $now0 . "<=end_time and is_end=0"; $rrs = M('prom_goods')->where($where0) ->field('store_id')->select(); mlog("11".json_encode($rrs),"deal_lipin_order"); if(!$rrs) return; $newarr = $this->clear_chongfu($rrs); $tt=strtotime("-10 minute"); if(!$newarr) return; //开始清理10分钟订单,未支付的订单 foreach ($newarr as $kv => $val) { $stoid=$val['store_id']; $order=M("order") ->where('order_status between 0 and 1') ->where("add_time<".$tt) ->where('pay_status',0) ->where('pay_time',0) ->where("store_id",$stoid) ->field("order_id,user_id")->select(); if(!$order) continue; mlog(json_encode($order),"deal_lipin_order/".$stoid); foreach($order as $k=>$v){ $org=M("order_goods")->where('order_id',$v['order_id']) ->where('is_gift',1)->where('store_id',$stoid) ->field('rec_id,gift_id,goods_num,prom_id')->select(); if(!$org) continue; foreach($org as $ko=>$vo){ //从表上删除记录,主表做标记,赠品被取消,同时补回活动的库存 M('order_goods')->where('rec_id',$vo['rec_id'])->delete(); M("order")->where('order_id',$v['order_id'])->where('store_id',$stoid) ->save(['is_del_gift'=>1]); M("gift")->where('store_id',$stoid) ->where('id',$vo['gift_id']) ->setInc('goods_num',$vo['goods_num']); M('gift_receive')->where(["orderGoods_id"=>$vo['rec_id'],'user_id'=>$v['user_id']])->delete(); } } } } //--去掉重复的数组-- function clear_chongfu($data){ $u=null; foreach ($data as $k=>$v){ if($u){ if(in_array($v,$u)) continue; $u[]=$v; }else{ $u[]=$v; } } return $u; } //time_minutes public function five_minutes(){ //找到所有有活动的商家 $now0=time(); $where0 = "(s_time<=" . $now0 . " or s_time<=" . $now0 . ") and " . $now0 . "<=e_time and is_end=0"; $where1="prom_type=1 or prom_type=2 or prom_type=6"; $rrs = M('activitylist')->where($where0) ->where($where1) ->field('store_id')->select(); if(!$rrs) return; $newarr= $this->clear_chongfu($rrs); if(!$newarr) return; //开始清理5分钟订单 foreach ($newarr as $kv=>$val){ clear_flash_Order($val['store_id']); clear_pind_Order($val['store_id']); } return "1"; } /*** * -30分钟自动成团- ***/ public function fen_zu_treamgroup($odls,$ct_num,$team_id,$stoid,$kt_time) { $num = count($odls); $i = $num / $ct_num; if (($num % $ct_num) > 0) { $i++; } for ($ii = 0; $ii < $i; $ii++) { $fir = $ii * $ct_num; $end = ($ii + 1) * $ct_num; if ($end > $num) $end = $num; $ud = null; for ($j = $fir; $j < $end; $j++) { $ud[] = $odls[$j]; } //$ud如果是空,就要跳出 if (empty($ud)) break; //生成期号 $qh = $team_id . "_" . date('ymdHis') . get_total_millisecond() . rand(1000, 9999); //如果数量是达到成团数量,就要重新分期 if (count($ud) == $ct_num) { $udata['pt_status'] = 2; $udata['pt_listno'] = $qh; $udata['pay_status'] = 1; //修改订单的状态 M('order')->where('order_id', 'in', $ud) ->save($udata); $t = time(); $grda['store_id'] = $stoid; $grda['team_id'] = $team_id; $grda['listno'] = $qh; $grda['buystartdate'] = $t; $grda['buyenddate'] = $t; $grda['state'] = 3; $grda['addtime'] = $t; $grda['kt_end_time'] = $t; M("teamgroup")->save($grda); //如果是满团,就要进行推送 $timer = new \app\timer\controller\Index(); $olist = M('order')->where('order_id', 'in', $ud)->where('store_id', $stoid) ->field('store_id,user_id,order_id,pt_prom_id,order_sn') ->select(); if ($olist) { foreach ($olist as $k => $v) { $stoid = $v['store_id']; $title = '拼团满团成功'; $remark = '感谢您的参与~'; $usr = M('users')->where('user_id', $v['user_id'])->field('erpvipid')->find(); $erpid = tpCache('shop_info.ERPId', $stoid); $gd = M('order_goods')->where('order_id', $v['order_id'])->field('goods_name')->find(); $teamlist = M('teamlist')->where('id', $v['pt_prom_id'])->field('ct_num')->find(); $backurl = curHostURL() . "/index.php/Mobile/team/team_success/orderno/" . $v['order_sn'] . "/stoid/" . $stoid; $timer->post_msg($stoid, $erpid, $usr['erpvipid'], $title, $gd['goods_name'], $teamlist['ct_num'], $remark, $backurl); } } } else { $udata['pt_listno'] = $qh; M('order')->where('order_id', 'in', $ud)->where('store_id', $stoid) ->save($udata); //这个地方要用主数据库查询 $o_c = M('order')->where('pt_listno', $qh)->count(); if ($o_c > 0) { $t = time(); $grda['store_id'] = $stoid; $grda['team_id'] = $team_id; $grda['listno'] = $qh; $grda['buystartdate'] = $t; $grda['state'] = 2; $grda['addtime'] = $t; $grda['kt_end_time'] = strtotime('+' . $kt_time . ' hour', $t); M("teamgroup")->save($grda); } break; } } } /*--给明海调用的接口,阶梯团--*/ public function back_ord(){ $store_id=I('store_id'); $teamgroup_id=I('teamgroup_id'); if (empty($store_id) || empty($teamgroup_id)) { return json(['code'=>-1,'msg'=>"参数不全"]); } $tgr=M("teamgroup")->where(array('store_id'=>$store_id,'id'=>$teamgroup_id,'team_type'=>3))->find(); if($tgr){ $this->out_tuan($tgr); return json(['code'=>0,'msg'=>"成功"]); } return json(['code'=>-1,'msg'=>"失败"]); } //判断团的人数是否到了 public function check_the_team($v){ //查找成团人数 $ordarr = M('order')->where('is_zsorder', 4)->where('pt_listno', $v['listno']) ->where('pt_prom_id', $v['team_id'])->where('pt_status', 1)->where('store_id',$v['store_id']) ->where('order_status=0 or order_status=1') ->field('order_id,order_sn,order_amount,user_id,store_id,pt_prom_id,user_money,shipping_price') ->select(); $count=count($ordarr); $jsarr=json_decode($v['jt_json'],true); //如果阶梯团大于这个人数,就是成团了, if($count>=$jsarr[0]['rynum']) { $endtime=strtotime('+24 hour'); M('teamgroup')->where('id',$v['id']) ->save(['state'=>4,'wk_end_time'=>$endtime,'jt_ct_num'=>$count,'buyenddate'=>time()]); //要计算尾款的价钱 $price=0; foreach ($jsarr as $k=>$v){ if($count>=$v['rynum']) $price=$v['price'];} //$oid=get_arr_column($ordarr,'order_id'); foreach ($ordarr as $kp=>$vp){ //更新订单商品表的单价 $dd['goods_price']=$price; $dd['member_goods_price']=$price; M('order_goods')->where('order_id',$vp['order_id'])->save($dd); //更新尾款,要计算物流费用 $num= M('order_goods')->where('order_id',$vp['order_id'])->sum('goods_num'); M('order')->where('order_id',$vp['order_id']) ->save(['pt_status'=>2, 'pt_tail_money'=>$price*$num+$vp['shipping_price']-$vp['order_amount']-$vp['user_money'] ]); } //如果是满团,就要进行推送 //$timer = new \app\timer\controller\Index(); $olist= $ordarr; if($olist){ foreach ($olist as $k=>$v){ $stoid=$v['store_id']; $title='拼团满团,等待支付尾款'; $remark='感谢您的参与~'; $usr=M('users')->where('user_id',$v['user_id'])->field('user_id')->find(); $erpid=tpCache('shop_info.ERPId',$stoid); $gd=M('order_goods')->where('order_id',$v['order_id'])->field('goods_name')->find(); $teamlist=M('teamlist')->where('id',$v['pt_prom_id'])->field('ct_num')->find(); $backurl=curHostURL(). "/Mobile/User/order_detail/id/".$v['order_id']."/stoid/".$stoid; $this->post_msg($stoid,$erpid,$usr['user_id'], $title,$gd['goods_name'],$teamlist['ct_num'],$remark,$backurl); } } return true; } return false; } }