Appointment.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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:desc type:string default:-- desc:商品副标题
  106. * @return name:cover type:string default:-- desc:商品封面
  107. * @return name:goods_spec type:string default:-- desc:商品规格
  108. * @return name:goods_id type:int default:-- desc:商品id
  109. * @return name:spec_id type:int default:-- desc:规格id
  110. */
  111. public function getAppointmentList()
  112. {
  113. $status = input('get.status',-1);
  114. $where = [];
  115. $where[] = ['a.user_id','=',$this->user_id];
  116. $where[] = ['a.is_deleted','=',0];
  117. if($status > -1) $where[] = ['status','=',$status];
  118. $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')
  119. ->leftJoin('StoreGoods g','a.goods_id = g.id')
  120. ->leftJoin('StoreGoodsItem i','a.spec_id = i.id')
  121. ->where($where)
  122. ->limit($this->off_set,$this->page_num)
  123. ->order('a.id desc')
  124. ->select()->toArray();
  125. $this->success('ok',['list'=>$list]);
  126. }
  127. /**
  128. * @title 获取预约详情
  129. * @desc 获取预约详情
  130. * @author qc
  131. * @method GET
  132. * @url /api/Appointment/getAppointmentInfo
  133. * @header name:Authorization require:1 desc:Token
  134. * @param name:id type:int default:-- desc:预约id
  135. * @return name:id type:int default:-- desc:预约id
  136. * @return name:order_no type:string default:-- desc:订单号
  137. * @return name:status type:int default:-- desc:订单状态(0未完成,1已完成,9已取消)
  138. * @return name:create_at type:string default:-- desc:下单时间
  139. * @return name:remark type:string default:-- desc:订单备注
  140. * @return name:code type:string default:-- desc:预约号
  141. * @return name:pro_name type:string default:-- desc:省名称(预约地址)
  142. * @return name:city_name type:string default:-- desc:市名称(预约地址)
  143. * @return name:county_name type:string default:-- desc:县区名称(预约地址)
  144. * @return name:add_detail type:string default:-- desc:详细地址(预约地址)
  145. * @return name:user_name type:string default:-- desc:预约人
  146. * @return name:phone type:string default:-- desc:人联系
  147. * @return name:app_time type:string default:-- desc:预约时间
  148. * @return name:goods_name type:string default:-- desc:商品名称
  149. * @return name:desc type:string default:-- desc:商品副标题
  150. * @return name:cover type:string default:-- desc:商品封面
  151. * @return name:goods_spec type:string default:-- desc:商品规格
  152. * @return name:goods_id type:int default:-- desc:商品id
  153. * @return name:spec_id type:int default:-- desc:规格id
  154. */
  155. public function getAppointmentInfo()
  156. {
  157. $order_id = input('get.id');
  158. $detail = AppOrder::field('a.*,g.name goods_name,i.goods_spec,g.cover,g.desc')->alias('a')
  159. ->leftJoin('StoreGoods g','a.goods_id = g.id')
  160. ->leftJoin('StoreGoodsItem i','a.spec_id = i.id')
  161. ->where('a.id',$order_id)
  162. ->find()->toArray();
  163. $this->success('ok',['detail'=>$detail]);
  164. }
  165. /**
  166. * @title 取消预约
  167. * @desc 取消预约
  168. * @author qc
  169. * @url /api/Appointment/cancelAppointment
  170. * @method POST
  171. * @header name:Authorization require:1 desc:Token
  172. * @param name:id type:int require:1 default:0 desc:预约id
  173. * @param name:cancel_desc type:string default:0 desc:取消原因
  174. */
  175. public function cancelAppointment()
  176. {
  177. $order_id = input('post.id');
  178. $cancel_desc = input('post.cancel_desc');
  179. Db::startTrans();
  180. try {
  181. $detail = AppOrder::where('id',$order_id)->find()->toArray();
  182. if($detail['status'] != 0) $this->exception('预约记录已完成或是已取消');
  183. // if(strtotime($detail['app_time']) < time() + 7200) $this->exception('2小时内无法直接取消预约,请联系客服');
  184. AppOrder::where('id',$order_id)->update(['status'=>9,'cancel_at'=>date("Y-m-d H:i:s"),'cancel_state'=>1,'cancel_desc'=>$cancel_desc]);
  185. Db::commit();
  186. }catch (\Exception $e){
  187. $this->ret_msg = $e->getMessage();
  188. $this->is_commit = false;
  189. Db::rollback();
  190. }
  191. $this->is_commit ? $this->success('取消成功') : $this->error($this->ret_msg);
  192. }
  193. /**
  194. * @title 预约完成
  195. * @desc 预约完成
  196. * @author qc
  197. * @url /api/Appointment/completeAppointment
  198. * @method POST
  199. * @header name:Authorization require:1 desc:Token
  200. * @param name:id type:int require:1 default:0 desc:预约记录id
  201. */
  202. public function completeAppointment()
  203. {
  204. $order_id = input('post.id');
  205. $detail = AppOrder::where('id',$order_id)->find()->toArray();
  206. if($detail['status'] != 0) $this->error('预约记录状态错误');
  207. AppOrder::update(['status'=>1],['id'=>$order_id]);
  208. $this->success('预约已完成');
  209. }
  210. }