123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <?php
- namespace app\common\model;
- use think\db;
- use think\Exception;
- class ShopOrders extends Base {
- public function addOrder($json,$addressid,$orgid,$userid){
- $tlist = Db::name('shop_day')->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;
- }
- }
|