123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use think\Db;
- use Exception;
- use think\exception\DbException;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- /**
- * 用户套餐订单
- *
- * @icon fa fa-circle-o
- */
- class Packageorder extends Backend
- {
- protected $noNeedRight = ['selectpagenew'];
- /**
- * Packageorder模型对象
- * @var \app\admin\model\Packageorder
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\Packageorder;
- $this->view->assign("orderStatusList", $this->model->getOrderStatusList());
- $this->view->assign("useStatusList", $this->model->getUseStatusList());
- $this->view->assign("payTypeList", $this->model->getPayTypeList());
- $this->view->assign("isGiftList", $this->model->getIsGiftList());
- $this->view->assign("noticeStatusList", $this->model->getNoticeStatusList());
- $this->view->assign("buyNoticeStatusList", $this->model->getBuyNoticeStatusList());
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- //当前是否为关联查询
- $this->relationSearch = true;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $list = $this->model
- ->with(['user','package'])
- ->where($where)
- ->where('packageorder.order_status',1)
- ->order($sort, $order)
- ->paginate($limit);
- foreach ($list as $row) {
-
- $row->getRelation('user')->visible(['firstname','lastname']);
- $row->getRelation('package')->visible(['name','name_en']);
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 用户配套申请激活
- */
- public function userapply()
- {
- //当前是否为关联查询
- $this->relationSearch = true;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $where2 = [
- 'packageorder.order_status' => 1, //已支付
- 'packageorder.is_gift' => 0, //不是赠品
- 'packageorder.use_status' => 0, //未激活
- 'packageorder.use_apply_status' => 1, //已申请激活
- ];
- $list = $this->model
- ->with(['user','package'])
- // ->where($where)
- ->where($where2)
- ->order($sort, $order)
- ->paginate($limit);
- foreach ($list as $row) {
- $row->getRelation('user')->visible(['firstname','lastname']);
- $row->getRelation('package')->visible(['name','name_en']);
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 申请激活审核
- */
- public function userapply_audit(){
- $status = input('status',2);
- $id = input('id',0);
- Db::startTrans();
- $package_order = Db::name('package_order')->where('id',$id)->lock(true)->find();
- if(empty($package_order)){
- Db::rollback();
- $this->error(__('请刷新重试'));
- }
- if($package_order['use_status'] == 1){
- Db::rollback();
- $this->error(__('已经激活了,请刷新重试'));
- }
- if($package_order['use_apply_status'] != 1){
- Db::rollback();
- $this->error(__('已经审核了,请刷新重试'));
- }
- $time = time();
- $update = [
- 'updatetime' => $time,
- ];
- if($status == 2){
- $update['use_status'] = 1;
- $update['starttime'] = $time;
- $update['endtime'] = $time + ($package_order['days'] * 86400);
- $update['use_apply_status'] = 2;
- $rs = Db::name('package_order')->where('order_no',$package_order['order_no'])->update($update); //这里用order_no,主订单和赠品一起激活
- Db::commit();
- $this->success(__('已激活'));
- }else{
- $update['use_apply_status'] = 3;
- $rs = Db::name('package_order')->where('id',$package_order['id'])->update($update); //这里用id,主订单驳回就可以
- Db::commit();
- $this->success(__('已驳回'));
- }
- }
- /**
- * 主动激活
- */
- public function jihuo(){
- $id = input('id',0);
- Db::startTrans();
- $package_order = Db::name('package_order')->where('id',$id)->lock(true)->find();
- if(empty($package_order)){
- Db::rollback();
- $this->error(__('请刷新重试'));
- }
- if($package_order['use_status'] == 1){
- Db::rollback();
- $this->error(__('已经激活了,请刷新重试'));
- }
- $time = time();
- $update = [
- 'updatetime' => $time,
- ];
- $update['use_status'] = 1;
- $update['starttime'] = $time;
- $update['endtime'] = $time + ($package_order['days'] * 86400);
- $rs = Db::name('package_order')->where('order_no',$package_order['order_no'])->update($update); //这里用order_no,主订单和赠品一起激活
- Db::commit();
- $this->success(__('已激活'));
- }
- /**
- * 取消
- */
- public function cancel(){
- $id = input('id',0);
- $package_order = Db::name('package_order')->where('id',$id)->find();
- if(empty($package_order)){
- $this->error(__('请刷新重试'));
- }
- if($package_order['order_status'] != 1){
- $this->error(__('请刷新重试'));
- }
- $time = time();
- $update = [
- 'updatetime' => $time,
- ];
- $update['order_status'] = 10;
- $rs = Db::name('package_order')->where('id',$id)->update($update);
- $this->success(__('已取消'));
- }
- /**
- * 用户下单
- *
- * @return string
- * @throws \think\Exception
- */
- public function add()
- {
- if (false === $this->request->isPost()) {
- $package_id = input('package_id',0);
- $this->assign('package_id',$package_id);
- return $this->view->fetch();
- }
- //
- $package_id = input('post.package_id');
- $user_id = input('user_id');
- $package_info = Db::name('lesson_package')->where('id',$package_id)->find();
- //初次购买,主订单增加赠送小时数
- if($package_info['sessions_first'] > 0){
- $check_first = Db::name('package_order')->where('user_id',$user_id)->where('package_id',$package_id)->where('order_status',1)->where('is_gift',0)->find();
- if(empty($check_first)){
- $package_info['sessions'] = $package_info['sessions'] + $package_info['sessions_first'];
- }
- }
- //套餐订单
- $data = [
- 'order_no' => createUniqueNo('P',$user_id),
- 'user_id' => $user_id,
- 'type' => $package_info['type'],
- 'package_id' => $package_id,
- 'lesson_ids' => $package_info['lesson_ids'],
- 'sessions' => $package_info['sessions'],
- 'starttime' => 0,
- 'days' => $package_info['days'],
- 'endtime' => 0,
- 'price' => $package_info['price'],
- 'remain' => $package_info['sessions'],
- 'order_status'=> 1, //默认支付了
- 'use_status' => 0,//默认不激活
- 'paytime' => time(),
- 'pay_type' => 2, //线下支付
- 'createtime' => time(),
- 'updatetime' => time(),
- 'is_gift' => 0,
- 'remark' => input('remark',''),
- 'seller' => input('seller',''),
- ];
- //如果有赠品,赠品订单
- $gift = [];
- if(!empty($package_info['gift_lesson_id']) && !empty($package_info['gift_sessions'])){
- $gift = $data;
- //订单号不换了
- $gift['type'] = $package_info['gift_type'];
- $gift['lesson_ids'] = $package_info['gift_lesson_id'];
- $gift['sessions'] = $package_info['gift_sessions'];
- $gift['remain'] = $package_info['gift_sessions'];
- $gift['is_gift'] = 1;
- }
- //入库
- Db::startTrans();
- $order_id = Db::name('package_order')->insertGetId($data);
- if(!$order_id){
- Db::rollback();
- $this->error('下单失败');
- }
- //礼物
- if(!empty($gift)){
- $gift_order_id = Db::name('package_order')->insertGetId($gift);
- if(!$gift_order_id){
- Db::rollback();
- $this->error('下单失败');
- }
- }
- //大于等于5小时的
- if($data['remain'] >= 5){
- //赠送积分
- if($data['price'] > 0){
- $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'score',$data['price'],4,'线下购买配套','package_order',$order_id,'Buy package offline');
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- }
- //新会员变成旧会员
- $oldstatus = Db::name('user')->where('id',$user_id)->value('oldstatus');
- if($oldstatus == 0){
- $user_rs = Db::name('user')->where('id',$user_id)->update(['oldstatus'=>1]);
- if($user_rs === false){
- Db::rollback();
- $this->error('更新会员失败');
- }
- }
- }
- Db::commit();
- $this->success('下单成功');
- }
- public function selectpagenew()
- {
- $user_id = input('user_id',0);
- $slot_id = input('slot_id',0);
- $number = input('number',1,'intval');
- $info = Db::name('lesson_slot')->where('id',$slot_id)->find();
- //此课程是否还有未使用的套餐(多个包含赠送的)
- $number_hours = bcmul($number,$info['hours'],1);//每日课时按小时扣
- $map = [
- 'o.user_id' =>$user_id,
- 'o.endtime' => ['gt',time()],
- 'o.remain' => ['egt',$number_hours], //不能只是大于0,还得大于等于报名人数
- 'o.order_status' => 1,
- 'o.use_status' => 1, //已激活的。未激活的不要展示
- ];
- $package_list = Db::name('package_order')->alias('o')
- ->join('lesson_package p','o.package_id = p.id','LEFT')
- ->field('o.id,o.remain,o.sessions,o.starttime,o.endtime,o.is_gift,p.name,p.name_en')
- ->where($map)
- ->where('find_in_set(:lesson_ids,o.lesson_ids)', ['lesson_ids' => $info['lesson_id']])
- ->order('o.endtime asc')
- // ->select(false);echo $package_list;exit;
- ->select();
- if(!empty($package_list)){
- foreach($package_list as $key => &$val){
- //转换remain
- $val['remain'] = floatval($val['remain']);
- /*if($this->lang == 'zh-cn'){
- $val['time_text'] = date('m月d,Y',$val['starttime']).'-'.date('m月d,Y',$val['endtime']);
- }else{*/
- $val['time_text'] = date('M d,Y',$val['starttime']).'-'.date('M d,Y',$val['endtime']);
- // }
- $val['name'] = $val['id'].'['.$val['name'].']--'.$val['time_text'].'--('.$val['remain'].'/'.$val['sessions'].')';
- }
- }
- return json(['list' => $package_list, 'total' => 0]);
- }
- }
|