where('org_id',$orgid)->select(); if(!$tlist){ $this->error = "未设置开业时间"; return false; } // 检查当前时间是否能点餐 $flag = false; $curTime = time(); foreach ($tlist as $k=>$v){ $start = strtotime(date('Y-m-d').' '.$v['start']); $end = strtotime(date('Y-m-d').' '.$v['end']); if($curTime >= $start && $curTime <= $end){ $flag = true; } } if(!$flag){ $this->error = "未在开业时间范围内,无法下单"; return false; } $arr = json_decode($json,true); $alltotal = 0; foreach ($arr as $k=>$v){ $ginfo = Db::name('shop_goods') ->where('id',$v['id']) ->where('del',0) ->where('enable',1) ->find(); if(!$ginfo){ $this->error = "选择的商品已下架"; return false; } $alltotal += $v['nums'] * $ginfo['price']; $arr[$k]['price'] = $ginfo['price']; } $alltotal = round($alltotal,2); //获取地址信息 $address = Db::name('shop_address') ->where('id',$addressid)->where('user_id',$userid)->find(); if(!$address){ $this->error = "地址不存在"; return false; } $addrs = $address['building'].$address['unit'].$address['content']; Db::startTrans(); try { $data=[ 'order_sn' => get_unique_id(), 'amount' => $alltotal, 'status' => 1, 'address_id' => $addressid, 'org_id' => $orgid, 'user_id' => $userid, 'name' => $address['name'], 'phone' => $address['phone'], 'address' => $addrs, 'create_time' => date('Y-m-d H:i:s'), ]; $orderId = Db::name('shop_orders')->insertGetId($data); if(!$orderId){ throw new Exception('操作失败'); } foreach ($arr as $k=>$v){ $gdata=[ 'goods_id'=>$v['id'], 'nums'=>$v['nums'], 'price'=>$v['price'], 'order_id'=>$orderId, ]; $ret = Db::name('shop_order_goods')->insert($gdata); if(!$ret){ throw new Exception('操作失败'); } } Db::commit(); }catch (Exception $e){ Db::rollback(); $this->error = "操作失败"; return false; } return true; } public function addOrder_old($json,$addressid,$orgid,$userid,$delivery_time,$type){ $tinfo = Db::name('shop_day')->where('id',$type)->find(); if(!$tinfo){ $this->error = "未查询到排餐记录"; return false; } if($tinfo['day'] != $delivery_time){ $this->error = "参数错误"; return false; } $goods = explode(',',$tinfo['goods']); $curDay = date('Y-m-d'); if($tinfo['day'] == $curDay){ //当天需要检查售卖时间 if(!$tinfo['start'] || !$tinfo['end']){ $this->error = "商品未设置售卖时间,无法下单"; return false; }else{ $endTime = strtotime($tinfo['day'].' '.$tinfo['end']); if(time() > $endTime){ $this->error = "已超过售卖时间,无法下单"; return false; } } } $arr = json_decode($json,true); $alltotal = 0; foreach ($arr as $k=>$v){ $ginfo = Db::name('shop_goods') ->where('id',$v['id']) ->where('del',0) ->where('enable',1) ->find(); if(!$ginfo){ $this->error = "选择的商品已下架"; return false; } if(!in_array($v['id'],$goods)){ $this->error = "选择的商品已下架"; return false; } $alltotal += $v['nums'] * $ginfo['price']; $arr[$k]['price'] = $ginfo['price']; } $alltotal = round($alltotal,2); //获取地址信息 $address = Db::name('shop_address')->where('id',$addressid)->where('user_id',$userid)->find(); if(!$address){ $this->error = "地址不存在"; return false; } $addrs = $address['building'].'号楼'.$address['unit'].'单元'.$address['content']; Db::startTrans(); try { $data=[ 'order_sn' => get_unique_id(), 'amount' => $alltotal, 'status' => 1, 'address_id' => $addressid, 'org_id' => $orgid, 'user_id' => $userid, 'name' => $address['name'], 'phone' => $address['phone'], 'address' => $addrs, 'create_time' => date('Y-m-d H:i:s'), 'delivery_time' => $delivery_time, 'cate' => $tinfo['cate'], 'shop_day_id' => $type ]; $orderId = Db::name('shop_orders')->insertGetId($data); if(!$orderId){ throw new Exception('操作失败'); } foreach ($arr as $k=>$v){ $gdata=[ 'goods_id'=>$v['id'], 'nums'=>$v['nums'], 'price'=>$v['price'], 'order_id'=>$orderId, ]; $ret = Db::name('shop_order_goods')->insert($gdata); if(!$ret){ throw new Exception('操作失败'); } } Db::commit(); }catch (Exception $e){ Db::rollback(); $this->error = "操作失败"; return false; } return true; } public function list($page,$size,$userid,$orgid){ $ret = Db::name('shop_orders') ->alias('so') ->join('org o','o.id = so.org_id') ->field('o.name,so.id,so.status,so.create_time,so.cate,so.shop_day_id') ->where([ 'so.org_id'=>$orgid, 'so.del'=>0, 'so.user_id'=>$userid ]) ->order('so.id desc') ->page($page,$size) ->select(); $ret = $ret?$ret:[]; foreach ($ret as $k=>$v){ $orderGoods = Db::name('shop_order_goods')->where('order_id',$v['id'])->field('goods_id')->find(); $goods = Db::name('shop_goods')->where('id',$orderGoods['goods_id'])->field('img')->find(); $ret[$k]['goods_img'] = $goods?$goods['img']:''; } return $ret; } public function details($id){ $ret = Db::name('shop_orders') ->alias('so') ->field('o.name as org_name,so.*') ->join('org o','o.id=so.org_id') ->where('so.id',$id) ->find(); //获取商品信息 $orderGoods = Db::name('shop_order_goods')->where('order_id',$ret['id'])->select(); foreach ($orderGoods as $k=>$v){ $goods = Db::name('shop_goods')->field('title,img')->where('id',$v['goods_id'])->find(); $orderGoods[$k]['goods_name'] = $goods?$goods['title']:''; $orderGoods[$k]['goods_img'] = $goods?$goods['img']:''; } $ret['goods'] = $orderGoods; // 检查订单是否可以取消订单 $ret['cancel'] = 1; if($ret['status'] == 1){ $tinfo = Db::name('shop_day')->where('id',$ret['shop_day_id'])->find(); $endTime = strtotime($tinfo['day'].' '.$tinfo['end']); if(time() > $endTime){ $ret['cancel'] = 0; } } if($ret['status'] == 2){ $ret['cancel'] = 0; } return $ret; } public function cancel($id,$userId){ $info = Db::name('shop_orders') ->where('id',$id) ->where('user_id',$userId) ->find(); if(!$info){ $this->error = "订单不存在"; return false; } if($info['status'] != 1){ $this->error = "无权限取消"; return false; } $tinfo = Db::name('shop_day')->where('id',$info['shop_day_id'])->find(); $endTime = strtotime($tinfo['day'].' '.$tinfo['end']); if(time() > $endTime){ $this->error = "已超过可取消的时间"; return false; } $ret = Db::name('shop_orders')->where('id',$id)->update([ 'status' => 2, 'cancel_time' => date('Y-m-d H:i:s') ]); if(!$ret){ $this->error = "操作失败"; return false; } return true; } }