table='dinner_order_refuse'; } public function index(){ if(request()->isAjax()){ //分页参数 $length = input('rows',10,'intval'); //每页条数 $page = input('page',1,'intval'); //第几页 $start = ($page - 1) * $length; //分页开始位置 //排序 $sortRow = input('sidx','id','trim'); //排序列 $sort = input('sord','desc','trim'); //排序方式 $order = $sortRow.' '.$sort; $title = input('sn','','trim'); if($title){ $map[] = ['b.sn','like','%'.$title.'%']; } $map[] = ['a.org_id','=',$this->orgId]; $map[] = ['b.state','<>',0]; $map= empty($map) ? true: $map; //数据查询 $lists = db($this->table) ->alias('a') ->join('dinner_order b','a.dinner_order_id=b.id') ->field('b.id,b.sn,b.price,b.name,b.mobile,b.address,b.remark,b.pay_time,a.create_time,a.op_time,a.status,a.id as refuse_id') ->where($map)->limit($start,$length)->order( ['a.id'=>'desc'] )->select(); foreach ($lists as $k=>$v){ $all = Db::name('dinner_order_item') ->alias('a') ->join('dinner b','a.dinner_id=b.id') ->join('dinner_type c','a.dinner_type_id=c.id') ->field('a.*,b.name as dinner_name,c.color,c.name as dinner_type_name') ->where('a.order_id',$v['id']) ->select(); $item = ''; foreach ($all as $k2=>$v2){ $ss = $k2+1; if($ss<10){ $ss= '0'.$ss; } $ur = url('dinner/detail').'?id='.$v2['dinner_id']; $item.=''.$ss.','.$v2['dinner_type_name'].''.$v2['dinner_name'].'*'.$v2['num']; if(count($all)>1 && $k2alias('a') ->join('dinner b','a.dinner_id=b.id') ->join('dinner_type c','a.dinner_type_id=c.id') ->field('a.*,b.name as dinner_name,c.color,c.name as dinner_type_name') ->where('a.order_refuse_id',$v['refuse_id']) ->select(); $item_refuse = ''; foreach ($refuse as $k3=>$v3){ $ss1 = $k3+1; if($ss1<10){ $ss1= '0'.$ss1; } $ur = url('dinner/detail').'?id='.$v3['dinner_id']; $item_refuse.=''.$ss1.','.$v3['dinner_type_name'].''.$v3['dinner_name'].'*'.$v3['num']; if(count($refuse)>1 && $k3table) ->alias('a') ->join('dinner_order b','a.dinner_order_id=b.id') ->where($map)->count(); $totalPage = ceil($totalCount/$length); $result['page'] = $page; $result['total'] = $totalPage; $result['records'] = $totalCount; $result['rows'] = $lists; return json($result); }else{ return $this->fetch(); } } public function option($id=0){ if(request()->isPost()){ $res = Db::name($this->table) ->where('org_id',$this->orgId) ->where('id',$id) ->find(); if($res['status']!==4){ $this->error('当前订单已操作'); } $order = Db::name('dinner_order')->where('id',$res['dinner_order_id'])->find(); if(!$order){ $this->error('订单不存在'); } $status = input('status',''); $remark = input('refuse_remark',''); if(!in_array($status,[5,6])){ $this->error('参数错误'); } Db::startTrans(); try{ if($order['cate'] == 0){ $r = Db::name('dinner_order') ->where('id',$res['dinner_order_id']) ->update([ 'state'=>$status, 'update_time'=>getTime() ]); if(!$r){ \exception('操作失败'); } }else{ if($status==5){ $checkAll = Db::name('dinner_order_item') ->where('order_id',$res['dinner_order_id']) ->where('is_refuse',0) ->find(); if(!$checkAll){//已退款 Db::name('dinner_order') ->where('id',$res['dinner_order_id']) ->update([ 'state'=>5, 'update_time'=>getTime() ]); } } } $r = Db::name($this->table) ->where('id',$id) ->update([ 'status'=>$status, 'op_user_id'=>$this->userId, 'op_time'=>getTime(), 'refuse_remark'=>$remark ]); if(!$r){ \exception('操作失败'); } if($status==5&&$res['order_type']==1&&$res['is_replace']==0) { $checkAll = Db::name('dinner_order_item') ->where('order_id',$res['dinner_order_id']) ->where('is_refuse',0) ->find(); if($checkAll){//部分退款 Db::name('dinner_order') ->where('id',$res['dinner_order_id']) ->update([ 'state'=>7, 'update_time'=>getTime() ]); } if($res['refuse_price'] > 0){ $orderInfo = Db::name('dinner_order') ->where('id',$res['dinner_order_id']) ->find(); $config =get_pay_wechat($orderInfo['org_id']); if(empty($config['mch_id']) || empty($config['key'])){ \exception('微信商户信息未配置,请联系管理员'); } $app = \EasyWeChat\Factory::payment($config); $totalFee = intval(round($orderInfo['price']*100)); $refundFee = intval(round($res['refuse_price']*100)); $ret = $app->refund->byOutTradeNumber($orderInfo['sn'], 'T'.$orderInfo['sn'], $totalFee, $refundFee); if($ret['return_code'] != 'SUCCESS' || $ret['result_code'] != 'SUCCESS'){ trace(json_encode($ret),'error'); \exception('退款申请提交失败'); } } } if($status==6){ // 拒绝,返还 $all = Db::name('dinner_order_refuse_item')->where('order_refuse_id',$id)->select(); if($order['cate'] == 0){ foreach ($all as $k=>$v){ Db::name('dinner_order_item') ->where('dinner_id',$v['dinner_id']) ->where('dinner_type_id',$v['dinner_type_id']) ->where('is_refuse',1) ->setField('is_refuse',0); } }else{ foreach ($all as $k=>$v){ Db::name('dinner_order_item') ->where('dinner_id',$v['dinner_id']) ->where('dinner_type_id',$v['dinner_type_id']) ->where('order_id',$res['dinner_order_id']) ->where('day',$v['day']) ->where('is_refuse',1) ->update(['is_refuse'=>0]); } } } Db::commit(); $this->success('操作成功'); }catch (Exception $e){ Db::rollback(); $this->error($e->getMessage()); } }else{ $this->assign('id',$id); return $this->fetch(); } } }