123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- namespace app\api\controller;
- use app\common\model\AppOrder;
- use app\common\model\User;
- use think\Db;
- use think\Exception;
- class Appointment extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::checkLogin();
- }
-
- 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 ]);
- }
-
- 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]);
- }
-
- 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]);
- }
-
- 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('预约记录已完成或是已取消');
-
- 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);
- }
-
- 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('预约已完成');
- }
- }
|