123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- <?php
- namespace app\api\controller;
- use think\Db;
- use EasyWeChat\Factory;
- /**
- * @title 会员订单管理
- * @controller Order
- * @group base
- */
- class Order extends Base
- {
- public function initialize()
- {
- parent::check_login();
- }
- /**
- * @title 获取订单统计
- * @desc 获取订单统计
- * @author qc
- * @url /api/Order/getOrderStatistics
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @return name:no_hx type:int default:0 desc:未核销数
- * @return name:finish type:int default:0 desc:完成数
- * @return name:no_discuss type:int default:0 desc:未评论数
- */
- public function getOrderStatistics()
- {
- $all_order = Db::table('ticket_order')
- ->field('id,is_hx,is_discuss')
- ->where(['uid'=>$this->uid,'pay_state'=>1,'status'=>[1,2],'is_deleted'=>0])
- ->select();
- $data=[
- 'no_hx'=>0,
- 'finish'=>0,
- 'no_discuss'=>0,
- ];
- array_map(function ($val) use(&$data){
- if($val['is_hx'] == 0 ) $data['no_hx']++;
- if($val['is_hx'] == 1 && $val['is_discuss'] == 1) $data['finish']++;
- if($val['is_discuss'] == 0 && $val['is_hx'] == 1) $data['no_discuss']++;
- },$all_order);
- $this->success('获取成功',$data);
- }
- /**
- * @title 获取订单详情
- * @desc 获取订单详情
- * @author qc
- * @url /api/Order/getOrderDetail
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:-- desc:订单id
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:total_num type:int default:-- desc:订单总票数(包含儿童票)
- * @return name:children_num type:int default:-- desc:儿童票数
- * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
- * @return name:price_total type:float default:-- desc:订单金额
- * @return name:create_at type:string default:-- desc:订单创建时间
- * @return name:tel type:string default:-- desc:订单预留电话
- * @return name:remark type:string default:-- desc:备注
- * @return name:f_id type:string default:-- desc:电影id
- * @return name:film_name type:string default:-- desc:电影名称
- * @return name:cover type:string default:-- desc:电影封面
- * @return name:duration type:string default:-- desc:电影时长(分钟)
- * @return name:day_time type:string default:-- desc:播放时间(天)
- * @return name:point_time type:string default:-- desc:电影播放时间(点)
- * @return name:end_time type:string default:-- desc:播放结束时间(点)
- * @return name:qrcode type:string default:-- desc:核销码
- * @return name:is_hx type:int default:0 desc:是否核销
- * @return name:ticket_price type:float default:0 desc:票价(成人)
- * @return name:is_remind type:int default:0 desc:是否提醒 0 不提醒
- */
- public function getOrderDetail()
- {
- $order_id = input('id',0);
- $detail = Db::table('ticket_order o')
- ->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')
- ->join('film_list f',' o.f_id = f.id ','LEFT')
- ->join('film_info s',' o.s_id = s.id ','LEFT')
- ->where(['o.uid'=>$this->uid,'o.id'=>$order_id])
- ->find();
- if(isset($detail['id']))$detail['end_time'] = date('H:i',strtotime($detail['day_time'].' '.$detail['point_time'].":00") + $detail['duration']*60);
- $this->success('获取成功',$detail);
- }
- /**
- * @title 生成订单
- * @desc 生成订单
- * @author qc
- * @url /api/Order/createOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:s_id type:int require:1 default:-- desc:电影场次id
- * @param name:f_id type:int require:1 default:-- desc:电影id
- * @param name:price_total type:float require:1 default:-- desc:订单金额
- * @param name:total_num type:float require:1 default:-- desc:票总数
- * @param name:children_num type:int require:0 default:0 desc:儿童票数
- * @param name:children_price type:float require:0 default:0 desc:儿童票价
- * @param name:tel type:string require:1 default:0 desc:预留电话
- * @param name:remark type:string require:1 default:0 desc:备注
- */
- public function createOrder()
- {
- $s_id = input('post.s_id');//场次id
- $f_id = input('post.f_id');//电影id
- $price_total = input('post.price_total');//订单总金额
- $total_num = input('post.total_num');//订单总票数
- $ticket_price = input('post.ticket_price');//成人票价
- $children_num = input('post.children_num',0);//儿童票数
- $children_price = input('post.children_price',0);//儿童票价
- $remark = input('post.remark');
- $tel = input('post.tel');//场次id
- if($total_num == 0) $this->error('请选择购票数量');
- $screen = Db::table('film_info')
- ->alias('f')
- ->field('f.*,l.duration')
- ->join('film_list l','f.f_id = l.id')
- ->find($s_id);
- if($screen['surplus_num'] < $total_num) $this->error('余票不足');
- if(strtotime($screen['show_time']) - 600 < time()) $this->error('开场前10分钟已关闭售票');
- $screen['end_time'] = date('H:i',strtotime($screen['day_time'].' '.$screen['point_time'].":00") + $screen['duration']*60);
- $order_no = get_order_sn();
- $order_data=[
- 'order_no'=>$order_no,
- 'uid'=>$this->uid,
- 'create_at'=>date('Y-m-d H:i:s'),
- 'create_tamp'=>time(),
- 's_id'=>$s_id,
- 'f_id'=>$f_id,
- 'price_total'=>$price_total,
- 'total_num'=>$total_num,
- 'children_num'=>$children_num,
- 'ticket_price'=>$ticket_price,
- 'remark'=>$remark,
- 'children_price'=>$children_price,
- 'tel'=>$tel,
- 's_info'=>json_encode($screen),
- 'year'=>date('Y'),
- 'month'=>date('m'),
- 'day'=>date('d'),
- 'hour'=>date('H')
- ];
- Db::startTrans();
- $com = 1;// 是否提交事务
- $msg = '';
- $last_id = 0;
- try{
- Db::table('film_info')->where('id', $s_id)->setDec('surplus_num', $total_num);//扣减余票
- Db::table('ticket_order')->insert($order_data);//
- $last_id = Db::getLastInsID();
- Db::commit();
- }catch(\Exception $e){
- Db::rollback();
- $msg = $e->getMessage();
- $com= 0;
- }
- if($com == 1){
- $this->success('生成订单成功',['id'=>$last_id]);
- } else{
- $this->error($msg);
- }
- }
- /**
- * @title 去支付
- * @desc 去支付
- * @author qc
- * @url /api/Order/payOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:o_id type:int require:1 default:-- desc:订单id
- * @param name:is_remind type:int require:1 default:0 desc:订单是否提醒,0不提醒,1提醒
- */
- public function payOrder()
- {
- $o_id = input('post.o_id');
- $is_remind = input('post.is_remind');
- $user_info = Db::table('store_member')->where('id',$this->uid)->find();
- if(!$user_info['phone'] || !$user_info['openid']) $this->error('请先完善用户信息');
- $order_info = Db::table('ticket_order')->where(['uid'=>$this->uid,'id'=>$o_id,'cancel_state'=>0])->find();
- if(empty($order_info)) $this->error('订单不存在');
- if($order_info['price_total'] <= 0 ) $this->error('订单金额错误');
- if($order_info['status'] != 0 ) $this->error('订单状态错误');
- Db::startTrans();
- $config = false;$msg= '';
- try{
- $notify_url = $this->request->root(true) . '/api/Pay/payNotify';
- $pay_no = get_order_sn();
- Db::table('ticket_order')
- ->where(['uid'=>$this->uid,'id'=>$o_id])
- ->update(['is_remind'=>$is_remind,'pay_no'=>$pay_no]);
- $config = Pay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
- if($config){
- Db::commit();
- }else{
- Db::rollback();
- }
- }catch (\Exception $e){
- Db::rollback();
- $msg = $e->getMessage();
- }
- if($config){
- $this->success('ok',['jump'=>1,'config'=>$config]);
- }else{
- $this->error($msg);
- }
- }
- /**
- * @title 获取订单列表
- * @desc 获取订单列表
- * @author qc
- * @url /api/Order/getMyOrderList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:type type:int : default:1 desc:查询类型,1所有,2待核销,3待评价,4已完成
- * @param name:page type:int : default:1 desc:页数
- * @param name:page_num type:int : default:20 desc:每页数
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:total_num type:int default:-- desc:订单总票数(包含儿童票)
- * @return name:children_num type:int default:-- desc:儿童票数
- * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
- * @return name:price_total type:float default:-- desc:订单金额
- * @return name:create_at type:string default:-- desc:订单创建时间
- * @return name:tel type:string default:-- desc:订单预留电话
- * @return name:remark type:string default:-- desc:备注
- * @return name:f_id type:string default:-- desc:电影id
- * @return name:film_name type:string default:-- desc:电影名称
- * @return name:cover type:string default:-- desc:电影封面
- * @return name:duration type:string default:-- desc:电影时长(分钟)
- * @return name:day_time type:string default:-- desc:播放时间(天)
- * @return name:point_time type:string default:-- desc:电影播放时间(点)
- * @return name:end_time type:string default:-- desc:播放结束时间(点)
- * @return name:qrcode type:string default:-- desc:核销码
- * @return name:is_hx type:int default:0 desc:是否核销
- * @return name:is_discuss type:int default:0 desc:是否评价
- * @return name:ticket_price type:float default:0 desc:票价(成人)
- */
- public function getMyOrderList()
- {
- $type = input('type',1);
- $page = input('page',1);
- $page_num = input('page_num',20);
- $off_set = $page*$page_num - $page_num;
- $where=[];
- $where['o.uid'] = $this->uid;
- $where['o.pay_state'] = 1;
- switch ($type)
- {
- case 1: // 全部
- break;
- case 2: // 待核销
- $where['o.is_hx'] = 0;
- break;
- case 3:// 待评价
- $where['o.is_discuss'] = 0;
- $where['o.is_hx'] = 1;
- break;
- case 4:// 已完成
- $where['o.is_discuss'] = 1;
- $where['o.is_hx'] = 1;
- break;
- }
- $list = Db::table('ticket_order o')
- ->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')
- ->join('film_list f',' o.f_id = f.id ','LEFT')
- ->join('film_info s',' o.s_id = s.id ','LEFT')
- ->where($where)
- ->order('o.id desc')
- ->limit($off_set,$page_num)
- ->select();
- $this->success('获取成功',$list);
- }
- /**
- * @title 取消订单
- * @desc 取消订单
- * @author qc
- * @url /api/Order/cancelOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:-- desc:订单id
- * @param name:desc type:string require:1 default:-- desc:取消描述
- * @param name:is_remind type:int require:1 default:0 desc:订单是否提醒,0不提醒,1提醒
- */
- public function cancelOrder()
- {
- $o_id = input('post.id');
- $desc = input('post.desc');
- $order_detail = Db::table('ticket_order')->where(['id'=>$o_id])->find($o_id);
- if(empty($order_detail)) $this->error('订单不存在');
- if($order_detail['pay_state'] != 0) $this->error('订单已支付');
- if($order_detail['cancel_state'] != 0) $this->error('订单已取消,不能重复取消');
- Db::startTrans();
- try {
- Db::table('ticket_order')->where(['id'=>$o_id])->update(['cancel_state'=>1,'cancel_at'=>date('Y-m-d H:i:s'),'desc'=>$desc]);
- Db::table('film_info')->where('id', $order_detail['s_id'])->setInc('surplus_num', $order_detail['total_num']);//增加场次余票
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- }
- $this->success('取消成功');
- }
- }
|