Packageorder.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use Exception;
  6. use think\exception\DbException;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 用户套餐订单
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Packageorder extends Backend
  15. {
  16. protected $noNeedRight = ['selectpagenew'];
  17. /**
  18. * Packageorder模型对象
  19. * @var \app\admin\model\Packageorder
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\Packageorder;
  26. $this->view->assign("orderStatusList", $this->model->getOrderStatusList());
  27. $this->view->assign("useStatusList", $this->model->getUseStatusList());
  28. $this->view->assign("payTypeList", $this->model->getPayTypeList());
  29. $this->view->assign("isGiftList", $this->model->getIsGiftList());
  30. $this->view->assign("noticeStatusList", $this->model->getNoticeStatusList());
  31. $this->view->assign("buyNoticeStatusList", $this->model->getBuyNoticeStatusList());
  32. }
  33. /**
  34. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  35. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  36. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37. */
  38. /**
  39. * 查看
  40. */
  41. public function index()
  42. {
  43. //当前是否为关联查询
  44. $this->relationSearch = true;
  45. //设置过滤方法
  46. $this->request->filter(['strip_tags', 'trim']);
  47. if ($this->request->isAjax()) {
  48. //如果发送的来源是Selectpage,则转发到Selectpage
  49. if ($this->request->request('keyField')) {
  50. return $this->selectpage();
  51. }
  52. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  53. $list = $this->model
  54. ->with(['user','package'])
  55. ->where($where)
  56. ->where('packageorder.order_status',1)
  57. ->order($sort, $order)
  58. ->paginate($limit);
  59. foreach ($list as $row) {
  60. $row->getRelation('user')->visible(['firstname','lastname']);
  61. $row->getRelation('package')->visible(['name','name_en']);
  62. }
  63. $result = array("total" => $list->total(), "rows" => $list->items());
  64. return json($result);
  65. }
  66. return $this->view->fetch();
  67. }
  68. /**
  69. * 用户配套申请激活
  70. */
  71. public function userapply()
  72. {
  73. //当前是否为关联查询
  74. $this->relationSearch = true;
  75. //设置过滤方法
  76. $this->request->filter(['strip_tags', 'trim']);
  77. if ($this->request->isAjax()) {
  78. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  79. $where2 = [
  80. 'packageorder.order_status' => 1, //已支付
  81. 'packageorder.is_gift' => 0, //不是赠品
  82. 'packageorder.use_status' => 0, //未激活
  83. 'packageorder.use_apply_status' => 1, //已申请激活
  84. ];
  85. $list = $this->model
  86. ->with(['user','package'])
  87. // ->where($where)
  88. ->where($where2)
  89. ->order($sort, $order)
  90. ->paginate($limit);
  91. foreach ($list as $row) {
  92. $row->getRelation('user')->visible(['firstname','lastname']);
  93. $row->getRelation('package')->visible(['name','name_en']);
  94. }
  95. $result = array("total" => $list->total(), "rows" => $list->items());
  96. return json($result);
  97. }
  98. return $this->view->fetch();
  99. }
  100. /**
  101. * 申请激活审核
  102. */
  103. public function userapply_audit(){
  104. $status = input('status',2);
  105. $id = input('id',0);
  106. Db::startTrans();
  107. $package_order = Db::name('package_order')->where('id',$id)->lock(true)->find();
  108. if(empty($package_order)){
  109. Db::rollback();
  110. $this->error(__('请刷新重试'));
  111. }
  112. if($package_order['use_status'] == 1){
  113. Db::rollback();
  114. $this->error(__('已经激活了,请刷新重试'));
  115. }
  116. if($package_order['use_apply_status'] != 1){
  117. Db::rollback();
  118. $this->error(__('已经审核了,请刷新重试'));
  119. }
  120. $time = time();
  121. $update = [
  122. 'updatetime' => $time,
  123. ];
  124. if($status == 2){
  125. $update['use_status'] = 1;
  126. $update['starttime'] = $time;
  127. $update['endtime'] = $time + ($package_order['days'] * 86400);
  128. $update['use_apply_status'] = 2;
  129. $rs = Db::name('package_order')->where('order_no',$package_order['order_no'])->update($update); //这里用order_no,主订单和赠品一起激活
  130. Db::commit();
  131. $this->success(__('已激活'));
  132. }else{
  133. $update['use_apply_status'] = 3;
  134. $rs = Db::name('package_order')->where('id',$package_order['id'])->update($update); //这里用id,主订单驳回就可以
  135. Db::commit();
  136. $this->success(__('已驳回'));
  137. }
  138. }
  139. /**
  140. * 主动激活
  141. */
  142. public function jihuo(){
  143. $id = input('id',0);
  144. Db::startTrans();
  145. $package_order = Db::name('package_order')->where('id',$id)->lock(true)->find();
  146. if(empty($package_order)){
  147. Db::rollback();
  148. $this->error(__('请刷新重试'));
  149. }
  150. if($package_order['use_status'] == 1){
  151. Db::rollback();
  152. $this->error(__('已经激活了,请刷新重试'));
  153. }
  154. $time = time();
  155. $update = [
  156. 'updatetime' => $time,
  157. ];
  158. $update['use_status'] = 1;
  159. $update['starttime'] = $time;
  160. $update['endtime'] = $time + ($package_order['days'] * 86400);
  161. $rs = Db::name('package_order')->where('order_no',$package_order['order_no'])->update($update); //这里用order_no,主订单和赠品一起激活
  162. Db::commit();
  163. $this->success(__('已激活'));
  164. }
  165. /**
  166. * 取消
  167. */
  168. public function cancel(){
  169. $id = input('id',0);
  170. $package_order = Db::name('package_order')->where('id',$id)->find();
  171. if(empty($package_order)){
  172. $this->error(__('请刷新重试'));
  173. }
  174. if($package_order['order_status'] != 1){
  175. $this->error(__('请刷新重试'));
  176. }
  177. $time = time();
  178. $update = [
  179. 'updatetime' => $time,
  180. ];
  181. $update['order_status'] = 10;
  182. $rs = Db::name('package_order')->where('id',$id)->update($update);
  183. $this->success(__('已取消'));
  184. }
  185. /**
  186. * 用户下单
  187. *
  188. * @return string
  189. * @throws \think\Exception
  190. */
  191. public function add()
  192. {
  193. if (false === $this->request->isPost()) {
  194. $package_id = input('package_id',0);
  195. $this->assign('package_id',$package_id);
  196. return $this->view->fetch();
  197. }
  198. //
  199. $package_id = input('post.package_id');
  200. $user_id = input('user_id');
  201. $package_info = Db::name('lesson_package')->where('id',$package_id)->find();
  202. //初次购买,主订单增加赠送小时数
  203. if($package_info['sessions_first'] > 0){
  204. $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();
  205. if(empty($check_first)){
  206. $package_info['sessions'] = $package_info['sessions'] + $package_info['sessions_first'];
  207. }
  208. }
  209. //套餐订单
  210. $data = [
  211. 'order_no' => createUniqueNo('P',$user_id),
  212. 'user_id' => $user_id,
  213. 'type' => $package_info['type'],
  214. 'package_id' => $package_id,
  215. 'lesson_ids' => $package_info['lesson_ids'],
  216. 'sessions' => $package_info['sessions'],
  217. 'starttime' => 0,
  218. 'days' => $package_info['days'],
  219. 'endtime' => 0,
  220. 'price' => $package_info['price'],
  221. 'remain' => $package_info['sessions'],
  222. 'order_status'=> 1, //默认支付了
  223. 'use_status' => 0,//默认不激活
  224. 'paytime' => time(),
  225. 'pay_type' => 2, //线下支付
  226. 'createtime' => time(),
  227. 'updatetime' => time(),
  228. 'is_gift' => 0,
  229. 'remark' => input('remark',''),
  230. 'seller' => input('seller',''),
  231. ];
  232. //如果有赠品,赠品订单
  233. $gift = [];
  234. if(!empty($package_info['gift_lesson_id']) && !empty($package_info['gift_sessions'])){
  235. $gift = $data;
  236. //订单号不换了
  237. $gift['type'] = $package_info['gift_type'];
  238. $gift['lesson_ids'] = $package_info['gift_lesson_id'];
  239. $gift['sessions'] = $package_info['gift_sessions'];
  240. $gift['remain'] = $package_info['gift_sessions'];
  241. $gift['is_gift'] = 1;
  242. }
  243. //入库
  244. Db::startTrans();
  245. $order_id = Db::name('package_order')->insertGetId($data);
  246. if(!$order_id){
  247. Db::rollback();
  248. $this->error('下单失败');
  249. }
  250. //礼物
  251. if(!empty($gift)){
  252. $gift_order_id = Db::name('package_order')->insertGetId($gift);
  253. if(!$gift_order_id){
  254. Db::rollback();
  255. $this->error('下单失败');
  256. }
  257. }
  258. //大于等于5小时的
  259. if($data['remain'] >= 5){
  260. //赠送积分
  261. if($data['price'] > 0){
  262. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'score',$data['price'],4,'线下购买配套','package_order',$order_id,'Buy package offline');
  263. if($wallet_rs['status'] === false){
  264. Db::rollback();
  265. $this->error($wallet_rs['msg']);
  266. }
  267. }
  268. //新会员变成旧会员
  269. $oldstatus = Db::name('user')->where('id',$user_id)->value('oldstatus');
  270. if($oldstatus == 0){
  271. $user_rs = Db::name('user')->where('id',$user_id)->update(['oldstatus'=>1]);
  272. if($user_rs === false){
  273. Db::rollback();
  274. $this->error('更新会员失败');
  275. }
  276. }
  277. }
  278. Db::commit();
  279. $this->success('下单成功');
  280. }
  281. public function selectpagenew()
  282. {
  283. $user_id = input('user_id',0);
  284. $slot_id = input('slot_id',0);
  285. $number = input('number',1,'intval');
  286. $info = Db::name('lesson_slot')->where('id',$slot_id)->find();
  287. //此课程是否还有未使用的套餐(多个包含赠送的)
  288. $number_hours = bcmul($number,$info['hours'],1);//每日课时按小时扣
  289. $map = [
  290. 'o.user_id' =>$user_id,
  291. 'o.endtime' => ['gt',time()],
  292. 'o.remain' => ['egt',$number_hours], //不能只是大于0,还得大于等于报名人数
  293. 'o.order_status' => 1,
  294. 'o.use_status' => 1, //已激活的。未激活的不要展示
  295. ];
  296. $package_list = Db::name('package_order')->alias('o')
  297. ->join('lesson_package p','o.package_id = p.id','LEFT')
  298. ->field('o.id,o.remain,o.sessions,o.starttime,o.endtime,o.is_gift,p.name,p.name_en')
  299. ->where($map)
  300. ->where('find_in_set(:lesson_ids,o.lesson_ids)', ['lesson_ids' => $info['lesson_id']])
  301. ->order('o.endtime asc')
  302. // ->select(false);echo $package_list;exit;
  303. ->select();
  304. if(!empty($package_list)){
  305. foreach($package_list as $key => &$val){
  306. //转换remain
  307. $val['remain'] = floatval($val['remain']);
  308. /*if($this->lang == 'zh-cn'){
  309. $val['time_text'] = date('m月d,Y',$val['starttime']).'-'.date('m月d,Y',$val['endtime']);
  310. }else{*/
  311. $val['time_text'] = date('M d,Y',$val['starttime']).'-'.date('M d,Y',$val['endtime']);
  312. // }
  313. $val['name'] = $val['id'].'['.$val['name'].']--'.$val['time_text'].'--('.$val['remain'].'/'.$val['sessions'].')';
  314. }
  315. }
  316. return json(['list' => $package_list, 'total' => 0]);
  317. }
  318. }