Appointment.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\AppOrder;
  4. use app\common\model\User;
  5. use think\Db;
  6. use think\Exception;
  7. /**
  8. * @title
  9. * @controller Appointment
  10. * @group base
  11. */
  12. class Appointment extends Base
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. parent::checkLogin();
  18. }
  19. /**
  20. * @title 创建预约||修改预约
  21. * @desc 创建预约||修改预约
  22. * @author qc
  23. * @url /api/Appointment/makeAppointment
  24. * @method POST
  25. * @header name:Authorization require:1 desc:Token
  26. * @param name:id type:int default:-- desc:预约记录id(不传值为创建新预约,传id修改预约记录)
  27. * @param name:goods_id type:int default:-- desc:商品id
  28. * @param name:spec_id type:int default:-- desc:规格id
  29. * @param name:pro_name type:string default:-- desc:预约地址--省
  30. * @param name:city_name type:string default:-- desc:预约地址--市
  31. * @param name:county_name type:string default:-- desc:预约地址--区
  32. * @param name:add_detail type:string default:-- desc:预约地址--详情
  33. * @param name:user_name type:string default:-- desc:预约人
  34. * @param name:phone type:string default:-- desc:人联系方式
  35. * @param name:remark type:string default:1 desc:订单备注
  36. * @return name:order_id type:int default:1 desc:预约订单id
  37. */
  38. public function makeAppointment()
  39. {
  40. $goods_id = input('post.goods_id','');
  41. $spec_id = input('post.spec_id','');
  42. $pro_name = input('post.pro_name','');
  43. $city_name = input('post.city_name','');
  44. $county_name = input('post.county_name','');
  45. $add_detail = input('post.add_detail','');
  46. $user_name = input('post.user_name','');
  47. $phone = input('post.phone','');
  48. $remark = input('post.remark','');
  49. $app_time = input('post.app_time',date("Y-m-d H:i:s"));
  50. $app_id= input('post.id','');
  51. if(!$goods_id || !$spec_id) $this->error('请选择商品');
  52. if(!$pro_name || !$city_name || !$county_name || !$add_detail) $this->error('请完善预约地址');
  53. if(!$user_name || !$phone) $this->error('请完善预约人信息');
  54. Db::startTrans();
  55. try{
  56. $order_insert = [
  57. 'user_id' => $this->user_id,
  58. 'goods_id' => $goods_id,
  59. 'spec_id' => $spec_id,
  60. 'order_no' => get_order_sn(),
  61. 'pro_name' => $pro_name,
  62. 'city_name' =>$city_name,
  63. 'county_name' => $county_name,
  64. 'add_detail' => $add_detail,
  65. 'user_name' => $user_name,
  66. 'phone' => $phone,
  67. 'remark' => $remark,
  68. 'app_time' => $app_time,
  69. 'code' => date('d',strtotime($app_time)).'-'.date('H',strtotime($app_time)).'-'.$this->user_id.rand(00,99)
  70. ];
  71. if($app_id) {
  72. $order_insert = $app_id;
  73. $app_info = AppOrder::where('id',$app_id)->find()->toArray();
  74. if($app_info['status'] == 1) $this->exception('预约已完成无法修改');
  75. if($app_info['status'] == 2) $this->exception('预约已取消无法修改');
  76. AppOrder::where('id',$app_id)->update($order_insert);
  77. }else{
  78. $order_info = AppOrder::create($order_insert);
  79. }
  80. Db::commit();
  81. }catch (\Exception $e){
  82. $this->ret_msg = $e->getMessage();
  83. $this->is_commit = false;
  84. Db::rollback();
  85. }
  86. $this->success('预约成功',['order_id'=>$app_id ? $app_id :$order_info->id ]);
  87. }
  88. /**
  89. * @title 获取预约列表
  90. * @desc 获取预约列表
  91. * @author qc
  92. * @method GET
  93. * @url /api/Appointment/getAppointmentList
  94. * @header name:Authorization require:1 desc:Token
  95. * @param name:page type:int default:1 desc:页数
  96. * @param name:page_num type:int default:20 desc:每页数
  97. * @param name:status type:int default:-1 desc:订单状态(-1全部,0未完成,1已完成,9已取消)
  98. * @return name:id type:int default:-- desc:预约id
  99. * @return name:order_no type:string default:-- desc:订单号
  100. * @return name:status type:int default:-- desc:订单状态(0未完成,1已完成,9已取消)
  101. * @return name:create_at type:string default:-- desc:下单时间
  102. * @return name:code type:string default:-- desc:预约号
  103. * @return name:remark type:string default:-- desc:订单备注
  104. * @return name:goods_name type:string default:-- desc:商品名称
  105. * @return name:cover type:string default:-- desc:商品封面
  106. * @return name:goods_spec type:string default:-- desc:商品规格
  107. * @return name:goods_id type:int default:-- desc:商品id
  108. * @return name:spec_id type:int default:-- desc:规格id
  109. */
  110. public function getAppointmentList()
  111. {
  112. $status = input('get.status',-1);
  113. $where = [];
  114. $where[] = ['a.user_id','=',$this->user_id];
  115. $where[] = ['a.is_deleted','=',0];
  116. if($status > -1) $where[] = ['status','=',$status];
  117. $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')->alias('a')
  118. ->leftJoin('StoreGoods g','a.goods_id = g.id')
  119. ->leftJoin('StoreGoodsItem i','a.spec_id = i.id')
  120. ->where($where)
  121. ->limit($this->off_set,$this->page_num)
  122. ->order('a.id desc')
  123. ->select()->toArray();
  124. $this->success('ok',['list'=>$list]);
  125. }
  126. /**
  127. * @title 获取预约详情
  128. * @desc 获取预约详情
  129. * @author qc
  130. * @method GET
  131. * @url /api/Appointment/getAppointmentInfo
  132. * @header name:Authorization require:1 desc:Token
  133. * @param name:id type:int default:-- desc:预约id
  134. * @return name:id type:int default:-- desc:预约id
  135. * @return name:order_no type:string default:-- desc:订单号
  136. * @return name:status type:int default:-- desc:订单状态(0未完成,1已完成,9已取消)
  137. * @return name:create_at type:string default:-- desc:下单时间
  138. * @return name:remark type:string default:-- desc:订单备注
  139. * @return name:code type:string default:-- desc:预约号
  140. * @return name:pro_name type:string default:-- desc:省名称(预约地址)
  141. * @return name:city_name type:string default:-- desc:市名称(预约地址)
  142. * @return name:county_name type:string default:-- desc:县区名称(预约地址)
  143. * @return name:add_detail type:string default:-- desc:详细地址(预约地址)
  144. * @return name:user_name type:string default:-- desc:预约人
  145. * @return name:phone type:string default:-- desc:人联系
  146. * @return name:app_time type:string default:-- desc:预约时间
  147. * @return name:goods_name type:string default:-- desc:商品名称
  148. * @return name:cover type:string default:-- desc:商品封面
  149. * @return name:goods_spec type:string default:-- desc:商品规格
  150. * @return name:goods_id type:int default:-- desc:商品id
  151. * @return name:spec_id type:int default:-- desc:规格id
  152. */
  153. public function getAppointmentInfo()
  154. {
  155. $order_id = input('get.id');
  156. $detail = AppOrder::field('a.*,g.name goods_name,i.goods_spec,g.cover')->alias('a')
  157. ->leftJoin('StoreGoods g','a.goods_id = g.id')
  158. ->leftJoin('StoreGoodsItem i','a.spec_id = i.id')
  159. ->where('a.id',$order_id)
  160. ->find()->toArray();
  161. $this->success('ok',['detail'=>$detail]);
  162. }
  163. /**
  164. * @title 取消预约
  165. * @desc 取消预约
  166. * @author qc
  167. * @url /api/Appointment/cancelAppointment
  168. * @method POST
  169. * @header name:Authorization require:1 desc:Token
  170. * @param name:id type:int require:1 default:0 desc:预约id
  171. * @param name:cancel_desc type:string default:0 desc:取消原因
  172. */
  173. public function cancelAppointment()
  174. {
  175. $order_id = input('post.id');
  176. $cancel_desc = input('post.cancel_desc');
  177. Db::startTrans();
  178. try {
  179. $detail = AppOrder::where('id',$order_id)->find()->toArray();
  180. if($detail['status'] != 0) $this->exception('预约记录已完成或是已取消');
  181. // if(strtotime($detail['app_time']) < time() + 7200) $this->exception('2小时内无法直接取消预约,请联系客服');
  182. AppOrder::where('id',$order_id)->update(['status'=>9,'cancel_at'=>date("Y-m-d H:i:s"),'cancel_state'=>1,'cancel_desc'=>$cancel_desc]);
  183. Db::commit();
  184. }catch (\Exception $e){
  185. $this->ret_msg = $e->getMessage();
  186. $this->is_commit = false;
  187. Db::rollback();
  188. }
  189. $this->is_commit ? $this->success('取消成功') : $this->error($this->ret_msg);
  190. }
  191. /**
  192. * @title 预约完成
  193. * @desc 预约完成
  194. * @author qc
  195. * @url /api/Appointment/completeAppointment
  196. * @method POST
  197. * @header name:Authorization require:1 desc:Token
  198. * @param name:id type:int require:1 default:0 desc:预约记录id
  199. */
  200. public function completeAppointment()
  201. {
  202. $order_id = input('post.id');
  203. $detail = AppOrder::where('id',$order_id)->find()->toArray();
  204. if($detail['status'] != 0) $this->error('预约记录状态错误');
  205. AppOrder::update(['status'=>1],['id'=>$order_id]);
  206. $this->success('预约已完成');
  207. }
  208. }