Order.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use EasyWeChat\Factory;
  5. /**
  6. * @title 会员订单管理
  7. * @controller Order
  8. * @group base
  9. */
  10. class Order extends Base
  11. {
  12. public function initialize()
  13. {
  14. parent::check_login();
  15. }
  16. /**
  17. * @title 获取订单统计
  18. * @desc 获取订单统计
  19. * @author qc
  20. * @url /api/Order/getOrderStatistics
  21. * @method GET
  22. * @header name:Authorization require:1 desc:Token
  23. * @return name:no_hx type:int default:0 desc:未核销数
  24. * @return name:finish type:int default:0 desc:完成数
  25. * @return name:no_discuss type:int default:0 desc:未评论数
  26. */
  27. public function getOrderStatistics()
  28. {
  29. $all_order = Db::table('ticket_order')
  30. ->field('id,is_hx,is_discuss')
  31. ->where(['uid'=>$this->uid,'pay_state'=>1,'status'=>[1,2],'is_deleted'=>0])
  32. ->select();
  33. $data=[
  34. 'no_hx'=>0,
  35. 'finish'=>0,
  36. 'no_discuss'=>0,
  37. ];
  38. array_map(function ($val) use(&$data){
  39. if($val['is_hx'] == 0 ) $data['no_hx']++;
  40. if($val['is_hx'] == 1 && $val['is_discuss'] == 1) $data['finish']++;
  41. if($val['is_discuss'] == 0 && $val['is_hx'] == 1) $data['no_discuss']++;
  42. },$all_order);
  43. $this->success('获取成功',$data);
  44. }
  45. /**
  46. * @title 获取订单详情
  47. * @desc 获取订单详情
  48. * @author qc
  49. * @url /api/Order/getOrderDetail
  50. * @method GET
  51. * @header name:Authorization require:1 desc:Token
  52. * @param name:id type:int require:1 default:-- desc:订单id
  53. * @return name:order_no type:string default:-- desc:订单号
  54. * @return name:total_num type:int default:-- desc:订单总票数(包含儿童票)
  55. * @return name:children_num type:int default:-- desc:儿童票数
  56. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  57. * @return name:price_total type:float default:-- desc:订单金额
  58. * @return name:create_at type:string default:-- desc:订单创建时间
  59. * @return name:tel type:string default:-- desc:订单预留电话
  60. * @return name:remark type:string default:-- desc:备注
  61. * @return name:f_id type:string default:-- desc:电影id
  62. * @return name:film_name type:string default:-- desc:电影名称
  63. * @return name:cover type:string default:-- desc:电影封面
  64. * @return name:duration type:string default:-- desc:电影时长(分钟)
  65. * @return name:day_time type:string default:-- desc:播放时间(天)
  66. * @return name:point_time type:string default:-- desc:电影播放时间(点)
  67. * @return name:end_time type:string default:-- desc:播放结束时间(点)
  68. * @return name:qrcode type:string default:-- desc:核销码
  69. * @return name:is_hx type:int default:0 desc:是否核销
  70. * @return name:ticket_price type:float default:0 desc:票价(成人)
  71. * @return name:is_remind type:int default:0 desc:是否提醒 0 不提醒
  72. */
  73. public function getOrderDetail()
  74. {
  75. $order_id = input('id',0);
  76. $detail = Db::table('ticket_order o')
  77. ->field('o.id,o.total_num,order_no,o.pay_state,o.create_at,o.is_remind,children_num,pay_at,o.price_total,o.tel,o.remark,o.is_hx,o.ticket_price,f.name as film_name,o.qrcode ,o.f_id,f.duration,f.desc as film_desc,f.cover,s.day_time,s.point_time')
  78. ->join('film_list f',' o.f_id = f.id ','LEFT')
  79. ->join('film_info s',' o.s_id = s.id ','LEFT')
  80. ->where(['o.uid'=>$this->uid,'o.id'=>$order_id])
  81. ->find();
  82. if(isset($detail['id']))$detail['end_time'] = date('H:i',strtotime($detail['day_time'].' '.$detail['point_time'].":00") + $detail['duration']*60);
  83. $this->success('获取成功',$detail);
  84. }
  85. /**
  86. * @title 生成订单
  87. * @desc 生成订单
  88. * @author qc
  89. * @url /api/Order/createOrder
  90. * @method POST
  91. * @header name:Authorization require:1 desc:Token
  92. * @param name:s_id type:int require:1 default:-- desc:电影场次id
  93. * @param name:f_id type:int require:1 default:-- desc:电影id
  94. * @param name:price_total type:float require:1 default:-- desc:订单金额
  95. * @param name:total_num type:float require:1 default:-- desc:票总数
  96. * @param name:children_num type:int require:0 default:0 desc:儿童票数
  97. * @param name:children_price type:float require:0 default:0 desc:儿童票价
  98. * @param name:tel type:string require:1 default:0 desc:预留电话
  99. * @param name:remark type:string require:1 default:0 desc:备注
  100. */
  101. public function createOrder()
  102. {
  103. $s_id = input('post.s_id');//场次id
  104. $f_id = input('post.f_id');//电影id
  105. $price_total = input('post.price_total');//订单总金额
  106. $total_num = input('post.total_num');//订单总票数
  107. $ticket_price = input('post.ticket_price');//成人票价
  108. $children_num = input('post.children_num',0);//儿童票数
  109. $children_price = input('post.children_price',0);//儿童票价
  110. $remark = input('post.remark');
  111. $tel = input('post.tel');//场次id
  112. if($total_num == 0) $this->error('请选择购票数量');
  113. $screen = Db::table('film_info')
  114. ->alias('f')
  115. ->field('f.*,l.duration')
  116. ->join('film_list l','f.f_id = l.id')
  117. ->find($s_id);
  118. if($screen['surplus_num'] < $total_num) $this->error('余票不足');
  119. if(strtotime($screen['show_time']) - 600 < time()) $this->error('开场前10分钟已关闭售票');
  120. $screen['end_time'] = date('H:i',strtotime($screen['day_time'].' '.$screen['point_time'].":00") + $screen['duration']*60);
  121. $order_no = get_order_sn();
  122. $order_data=[
  123. 'order_no'=>$order_no,
  124. 'uid'=>$this->uid,
  125. 'create_at'=>date('Y-m-d H:i:s'),
  126. 'create_tamp'=>time(),
  127. 's_id'=>$s_id,
  128. 'f_id'=>$f_id,
  129. 'price_total'=>$price_total,
  130. 'total_num'=>$total_num,
  131. 'children_num'=>$children_num,
  132. 'ticket_price'=>$ticket_price,
  133. 'remark'=>$remark,
  134. 'children_price'=>$children_price,
  135. 'tel'=>$tel,
  136. 's_info'=>json_encode($screen),
  137. 'year'=>date('Y'),
  138. 'month'=>date('m'),
  139. 'day'=>date('d'),
  140. 'hour'=>date('H')
  141. ];
  142. Db::startTrans();
  143. $com = 1;// 是否提交事务
  144. $msg = '';
  145. $last_id = 0;
  146. try{
  147. Db::table('film_info')->where('id', $s_id)->setDec('surplus_num', $total_num);//扣减余票
  148. Db::table('ticket_order')->insert($order_data);//
  149. $last_id = Db::getLastInsID();
  150. Db::commit();
  151. }catch(\Exception $e){
  152. Db::rollback();
  153. $msg = $e->getMessage();
  154. $com= 0;
  155. }
  156. if($com == 1){
  157. $this->success('生成订单成功',['id'=>$last_id]);
  158. } else{
  159. $this->error($msg);
  160. }
  161. }
  162. /**
  163. * @title 去支付
  164. * @desc 去支付
  165. * @author qc
  166. * @url /api/Order/payOrder
  167. * @method POST
  168. * @header name:Authorization require:1 desc:Token
  169. * @param name:o_id type:int require:1 default:-- desc:订单id
  170. * @param name:is_remind type:int require:1 default:0 desc:订单是否提醒,0不提醒,1提醒
  171. */
  172. public function payOrder()
  173. {
  174. $o_id = input('post.o_id');
  175. $is_remind = input('post.is_remind');
  176. $user_info = Db::table('store_member')->where('id',$this->uid)->find();
  177. if(!$user_info['phone'] || !$user_info['openid']) $this->error('请先完善用户信息');
  178. $order_info = Db::table('ticket_order')->where(['uid'=>$this->uid,'id'=>$o_id,'cancel_state'=>0])->find();
  179. if(empty($order_info)) $this->error('订单不存在');
  180. if($order_info['price_total'] <= 0 ) $this->error('订单金额错误');
  181. if($order_info['status'] != 0 ) $this->error('订单状态错误');
  182. Db::startTrans();
  183. $config = false;$msg= '';
  184. try{
  185. $notify_url = $this->request->root(true) . '/api/Pay/payNotify';
  186. $pay_no = get_order_sn();
  187. Db::table('ticket_order')
  188. ->where(['uid'=>$this->uid,'id'=>$o_id])
  189. ->update(['is_remind'=>$is_remind,'pay_no'=>$pay_no]);
  190. $config = Pay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
  191. if($config){
  192. Db::commit();
  193. }else{
  194. Db::rollback();
  195. }
  196. }catch (\Exception $e){
  197. Db::rollback();
  198. $msg = $e->getMessage();
  199. }
  200. if($config){
  201. $this->success('ok',['jump'=>1,'config'=>$config]);
  202. }else{
  203. $this->error($msg);
  204. }
  205. }
  206. /**
  207. * @title 获取订单列表
  208. * @desc 获取订单列表
  209. * @author qc
  210. * @url /api/Order/getMyOrderList
  211. * @method GET
  212. * @header name:Authorization require:1 desc:Token
  213. * @param name:type type:int : default:1 desc:查询类型,1所有,2待核销,3待评价,4已完成
  214. * @param name:page type:int : default:1 desc:页数
  215. * @param name:page_num type:int : default:20 desc:每页数
  216. * @return name:order_no type:string default:-- desc:订单号
  217. * @return name:total_num type:int default:-- desc:订单总票数(包含儿童票)
  218. * @return name:children_num type:int default:-- desc:儿童票数
  219. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  220. * @return name:price_total type:float default:-- desc:订单金额
  221. * @return name:create_at type:string default:-- desc:订单创建时间
  222. * @return name:tel type:string default:-- desc:订单预留电话
  223. * @return name:remark type:string default:-- desc:备注
  224. * @return name:f_id type:string default:-- desc:电影id
  225. * @return name:film_name type:string default:-- desc:电影名称
  226. * @return name:cover type:string default:-- desc:电影封面
  227. * @return name:duration type:string default:-- desc:电影时长(分钟)
  228. * @return name:day_time type:string default:-- desc:播放时间(天)
  229. * @return name:point_time type:string default:-- desc:电影播放时间(点)
  230. * @return name:end_time type:string default:-- desc:播放结束时间(点)
  231. * @return name:qrcode type:string default:-- desc:核销码
  232. * @return name:is_hx type:int default:0 desc:是否核销
  233. * @return name:is_discuss type:int default:0 desc:是否评价
  234. * @return name:ticket_price type:float default:0 desc:票价(成人)
  235. */
  236. public function getMyOrderList()
  237. {
  238. $type = input('type',1);
  239. $page = input('page',1);
  240. $page_num = input('page_num',20);
  241. $off_set = $page*$page_num - $page_num;
  242. $where=[];
  243. $where['o.uid'] = $this->uid;
  244. $where['o.pay_state'] = 1;
  245. switch ($type)
  246. {
  247. case 1: // 全部
  248. break;
  249. case 2: // 待核销
  250. $where['o.is_hx'] = 0;
  251. break;
  252. case 3:// 待评价
  253. $where['o.is_discuss'] = 0;
  254. $where['o.is_hx'] = 1;
  255. break;
  256. case 4:// 已完成
  257. $where['o.is_discuss'] = 1;
  258. $where['o.is_hx'] = 1;
  259. break;
  260. }
  261. $list = Db::table('ticket_order o')
  262. ->field('o.id,o.total_num,order_no,o.pay_state,o.create_at,children_num,pay_at,o.price_total,o.tel,o.remark,o.is_hx,o.ticket_price,o.is_discuss,f.name as film_name,o.qrcode ,o.f_id,f.duration,f.desc as film_desc,f.cover,s.day_time,s.point_time')
  263. ->join('film_list f',' o.f_id = f.id ','LEFT')
  264. ->join('film_info s',' o.s_id = s.id ','LEFT')
  265. ->where($where)
  266. ->order('o.id desc')
  267. ->limit($off_set,$page_num)
  268. ->select();
  269. $this->success('获取成功',$list);
  270. }
  271. /**
  272. * @title 取消订单
  273. * @desc 取消订单
  274. * @author qc
  275. * @url /api/Order/cancelOrder
  276. * @method POST
  277. * @header name:Authorization require:1 desc:Token
  278. * @param name:id type:int require:1 default:-- desc:订单id
  279. * @param name:desc type:string require:1 default:-- desc:取消描述
  280. * @param name:is_remind type:int require:1 default:0 desc:订单是否提醒,0不提醒,1提醒
  281. */
  282. public function cancelOrder()
  283. {
  284. $o_id = input('post.id');
  285. $desc = input('post.desc');
  286. $order_detail = Db::table('ticket_order')->where(['id'=>$o_id])->find($o_id);
  287. if(empty($order_detail)) $this->error('订单不存在');
  288. if($order_detail['pay_state'] != 0) $this->error('订单已支付');
  289. if($order_detail['cancel_state'] != 0) $this->error('订单已取消,不能重复取消');
  290. Db::startTrans();
  291. try {
  292. Db::table('ticket_order')->where(['id'=>$o_id])->update(['cancel_state'=>1,'cancel_at'=>date('Y-m-d H:i:s'),'desc'=>$desc]);
  293. Db::table('film_info')->where('id', $order_detail['s_id'])->setInc('surplus_num', $order_detail['total_num']);//增加场次余票
  294. Db::commit();
  295. }catch (\Exception $e){
  296. Db::rollback();
  297. }
  298. $this->success('取消成功');
  299. }
  300. }