123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- namespace app\api\controller;
- use app\common\model\AppOrder;
- use app\common\model\User;
- use think\Db;
- use think\Exception;
- /**
- * @title
- * @controller Appointment
- * @group base
- */
- class Appointment extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::checkLogin();
- }
- /**
- * @title 创建预约||修改预约
- * @desc 创建预约||修改预约
- * @author qc
- * @url /api/Appointment/makeAppointment
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int default:-- desc:预约记录id(不传值为创建新预约,传id修改预约记录)
- * @param name:goods_id type:int default:-- desc:商品id
- * @param name:spec_id type:int default:-- desc:规格id
- * @param name:pro_name type:string default:-- desc:预约地址--省
- * @param name:city_name type:string default:-- desc:预约地址--市
- * @param name:county_name type:string default:-- desc:预约地址--区
- * @param name:add_detail type:string default:-- desc:预约地址--详情
- * @param name:user_name type:string default:-- desc:预约人
- * @param name:phone type:string default:-- desc:人联系方式
- * @param name:remark type:string default:1 desc:订单备注
- * @return name:order_id type:int default:1 desc:预约订单id
- */
- public function makeAppointment()
- {
- $goods_id = input('post.goods_id','');
- $spec_id = input('post.spec_id','');
- $pro_name = input('post.pro_name','');
- $city_name = input('post.city_name','');
- $county_name = input('post.county_name','');
- $add_detail = input('post.add_detail','');
- $user_name = input('post.user_name','');
- $phone = input('post.phone','');
- $remark = input('post.remark','');
- $app_time = input('post.app_time',date("Y-m-d H:i:s"));
- $app_id= input('post.id','');
- if(!$goods_id || !$spec_id) $this->error('请选择商品');
- if(!$pro_name || !$city_name || !$county_name || !$add_detail) $this->error('请完善预约地址');
- if(!$user_name || !$phone) $this->error('请完善预约人信息');
- Db::startTrans();
- try{
- $order_insert = [
- 'user_id' => $this->user_id,
- 'goods_id' => $goods_id,
- 'spec_id' => $spec_id,
- 'order_no' => get_order_sn(),
- 'pro_name' => $pro_name,
- 'city_name' =>$city_name,
- 'county_name' => $county_name,
- 'add_detail' => $add_detail,
- 'user_name' => $user_name,
- 'phone' => $phone,
- 'remark' => $remark,
- 'app_time' => $app_time,
- 'code' => date('d',strtotime($app_time)).'-'.date('H',strtotime($app_time)).'-'.$this->user_id.rand(00,99)
- ];
- if($app_id) {
- $order_insert = $app_id;
- $app_info = AppOrder::where('id',$app_id)->find()->toArray();
- if($app_info['status'] == 1) $this->exception('预约已完成无法修改');
- if($app_info['status'] == 2) $this->exception('预约已取消无法修改');
- AppOrder::where('id',$app_id)->update($order_insert);
- }else{
- $order_info = AppOrder::create($order_insert);
- }
- Db::commit();
- }catch (\Exception $e){
- $this->ret_msg = $e->getMessage();
- $this->is_commit = false;
- Db::rollback();
- }
- $this->success('预约成功',['order_id'=>$app_id ? $app_id :$order_info->id ]);
- }
- /**
- * @title 获取预约列表
- * @desc 获取预约列表
- * @author qc
- * @method GET
- * @url /api/Appointment/getAppointmentList
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int default:1 desc:页数
- * @param name:page_num type:int default:20 desc:每页数
- * @param name:status type:int default:-1 desc:订单状态(-1全部,0未完成,1已完成,9已取消)
- * @return name:id type:int default:-- desc:预约id
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:status type:int default:-- desc:订单状态(0未完成,1已完成,9已取消)
- * @return name:create_at type:string default:-- desc:下单时间
- * @return name:code type:string default:-- desc:预约号
- * @return name:remark type:string default:-- desc:订单备注
- * @return name:goods_name type:string default:-- desc:商品名称
- * @return name:desc type:string default:-- desc:商品副标题
- * @return name:cover type:string default:-- desc:商品封面
- * @return name:goods_spec type:string default:-- desc:商品规格
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:spec_id type:int default:-- desc:规格id
- */
- public function getAppointmentList()
- {
- $status = input('get.status',-1);
- $where = [];
- $where[] = ['a.user_id','=',$this->user_id];
- $where[] = ['a.is_deleted','=',0];
- if($status > -1) $where[] = ['status','=',$status];
- $list = AppOrder::field('a.id,a.goods_id,a.spec_id,a.order_no,a.status,a.code,a.remark,g.name goods_name,g.cover,i.goods_spec,g.desc')->alias('a')
- ->leftJoin('StoreGoods g','a.goods_id = g.id')
- ->leftJoin('StoreGoodsItem i','a.spec_id = i.id')
- ->where($where)
- ->limit($this->off_set,$this->page_num)
- ->order('a.id desc')
- ->select()->toArray();
- $this->success('ok',['list'=>$list]);
- }
- /**
- * @title 获取预约详情
- * @desc 获取预约详情
- * @author qc
- * @method GET
- * @url /api/Appointment/getAppointmentInfo
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int default:-- desc:预约id
- * @return name:id type:int default:-- desc:预约id
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:status type:int default:-- desc:订单状态(0未完成,1已完成,9已取消)
- * @return name:create_at type:string default:-- desc:下单时间
- * @return name:remark type:string default:-- desc:订单备注
- * @return name:code type:string default:-- desc:预约号
- * @return name:pro_name type:string default:-- desc:省名称(预约地址)
- * @return name:city_name type:string default:-- desc:市名称(预约地址)
- * @return name:county_name type:string default:-- desc:县区名称(预约地址)
- * @return name:add_detail type:string default:-- desc:详细地址(预约地址)
- * @return name:user_name type:string default:-- desc:预约人
- * @return name:phone type:string default:-- desc:人联系
- * @return name:app_time type:string default:-- desc:预约时间
- * @return name:goods_name type:string default:-- desc:商品名称
- * @return name:desc type:string default:-- desc:商品副标题
- * @return name:cover type:string default:-- desc:商品封面
- * @return name:goods_spec type:string default:-- desc:商品规格
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:spec_id type:int default:-- desc:规格id
- */
- public function getAppointmentInfo()
- {
- $order_id = input('get.id');
- $detail = AppOrder::field('a.*,g.name goods_name,i.goods_spec,g.cover,g.desc')->alias('a')
- ->leftJoin('StoreGoods g','a.goods_id = g.id')
- ->leftJoin('StoreGoodsItem i','a.spec_id = i.id')
- ->where('a.id',$order_id)
- ->find()->toArray();
- $this->success('ok',['detail'=>$detail]);
- }
- /**
- * @title 取消预约
- * @desc 取消预约
- * @author qc
- * @url /api/Appointment/cancelAppointment
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:0 desc:预约id
- * @param name:cancel_desc type:string default:0 desc:取消原因
- */
- public function cancelAppointment()
- {
- $order_id = input('post.id');
- $cancel_desc = input('post.cancel_desc');
- Db::startTrans();
- try {
- $detail = AppOrder::where('id',$order_id)->find()->toArray();
- if($detail['status'] != 0) $this->exception('预约记录已完成或是已取消');
- // if(strtotime($detail['app_time']) < time() + 7200) $this->exception('2小时内无法直接取消预约,请联系客服');
- AppOrder::where('id',$order_id)->update(['status'=>9,'cancel_at'=>date("Y-m-d H:i:s"),'cancel_state'=>1,'cancel_desc'=>$cancel_desc]);
- Db::commit();
- }catch (\Exception $e){
- $this->ret_msg = $e->getMessage();
- $this->is_commit = false;
- Db::rollback();
- }
- $this->is_commit ? $this->success('取消成功') : $this->error($this->ret_msg);
- }
- /**
- * @title 预约完成
- * @desc 预约完成
- * @author qc
- * @url /api/Appointment/completeAppointment
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:0 desc:预约记录id
- */
- public function completeAppointment()
- {
- $order_id = input('post.id');
- $detail = AppOrder::where('id',$order_id)->find()->toArray();
- if($detail['status'] != 0) $this->error('预约记录状态错误');
- AppOrder::update(['status'=>1],['id'=>$order_id]);
- $this->success('预约已完成');
- }
- }
|