Lessonorder.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use app\common\library\Email;
  6. /**
  7. * 售课预约订单
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Lessonorder extends Backend
  12. {
  13. /**
  14. * Lessonorder模型对象
  15. * @var \app\admin\model\Lessonorder
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Lessonorder;
  22. $this->view->assign("orderStatusList", $this->model->getOrderStatusList());
  23. $this->view->assign("paytypeList", $this->model->getPaytypeList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //当前是否为关联查询
  36. $this->relationSearch = true;
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $list = $this->model
  46. ->with(['user','slot','lesson','coach'])
  47. ->where($where)
  48. ->where('lessonorder.order_status','NEQ',0)
  49. ->order($sort, $order)
  50. ->paginate($limit);
  51. foreach ($list as $row) {
  52. $row->getRelation('user')->visible(['firstname','lastname']);
  53. $row->getRelation('slot')->visible(['starttime','endtime']);
  54. $row->getRelation('lesson')->visible(['name','name_en']);
  55. $row->getRelation('coach')->visible(['nickname']);
  56. }
  57. $result = array("total" => $list->total(), "rows" => $list->items());
  58. return json($result);
  59. }
  60. $is_back = input('is_back',0);
  61. $this->assign('is_back',$is_back);
  62. return $this->view->fetch();
  63. }
  64. /**
  65. * 取消
  66. */
  67. public function cancel(){
  68. $id = input('id');
  69. $info = Db::name('lesson_order')->where('id',$id)->where('order_status',10)->find();
  70. if(!$info){
  71. $this->error('请刷新重试');
  72. }
  73. if($this->request->isPost()){
  74. $cancel_reason = input('cancel_reason','');
  75. $cancel_time = strtotime(input('cancel_time',''));
  76. Db::startTrans();
  77. //找到所有的已报名订单
  78. $info = Db::name('lesson_order')->where('id',$id)->where('order_status',10)->lock(true)->find();
  79. if(empty($info)){
  80. Db::rollback();
  81. $this->error('请刷新重试');
  82. }
  83. //套餐给加回去
  84. if($info['paytype'] == 1){
  85. $package_order = Db::name('package_order')->where('id',$info['package_order_id'])->lock(true)->find();
  86. $update = [
  87. 'remain' => bcadd($package_order['remain'],$info['usernumber_hours'],1),
  88. 'updatetime' => time(),
  89. ];
  90. $rs_remain = Db::name('package_order')->where('id',$info['package_order_id'])->update($update);
  91. if($rs_remain === false){
  92. Db::rollback();
  93. $this->error('取消失败');
  94. }
  95. }
  96. //试课给改回去
  97. if($info['paytype'] == 4){
  98. $update = [
  99. 'order_status' => 10,
  100. 'updatetime' => time(),
  101. 'lesson_order_id' => 0,
  102. ];
  103. $rs_remain = Db::name('trylesson_order')->where('id',$info['trylesson_order_id'])->update($update);
  104. if($rs_remain === false){
  105. Db::rollback();
  106. $this->error('取消失败');
  107. }
  108. }
  109. //现金支付不给退,线下处理
  110. //取消预约单
  111. $update2 = [
  112. 'order_status' => 30,
  113. 'cancel_time' => $cancel_time,
  114. 'cancel_reason' => $cancel_reason,
  115. ];
  116. if($info['paytype'] == 1 || $info['paytype'] == 4){
  117. $update2['order_status'] = 40;
  118. }
  119. $rs = Db::name('lesson_order')->where('id',$info['id'])->update($update2);
  120. if($rs === false){
  121. Db::rollback();
  122. $this->error('取消失败');
  123. }
  124. //候补上位
  125. $slot = Db::name('lesson_slot')->where('id',$info['slot_id'])->find();
  126. $wait_rs = $this->lesson_order_wait($info,$slot);
  127. //更新已预约人数
  128. $pay_number = Db::name('lesson_order')->where('slot_id',$info['slot_id'])->where('order_status',10)->sum('usernumber');
  129. $rs_slot = Db::name('lesson_slot')->where('id',$info['slot_id'])->update(['bookednum' => $pay_number]);
  130. if($rs_slot === false){
  131. Db::rollback();
  132. $this->error('取消失败');
  133. }
  134. Db::commit();
  135. $slot_info = Db::name('lesson_slot')->where('id',$info['slot_id'])->find();
  136. $lesson_info = Db::name('lesson')->where('id',$slot_info['lesson_id'])->find();
  137. //给用户发通知
  138. $user_info = Db::name('user')->where('id',$info['user_id'])->find();
  139. if(!empty($user_info['email'])){
  140. try {
  141. $obj = new Email();
  142. $result = $obj
  143. ->to($user_info['email'])
  144. ->subject('Class is Cancelled!')
  145. ->message('Hi,'.$user_info['firstname']. ' ' .$user_info['lastname'].',您预约的'.$lesson_info['name'].'已取消')
  146. ->send();
  147. $coach_name = Db::name('coach')->where('id',$slot_info['coach_ids'])->value('nickname');
  148. //发whatsapp
  149. $parameters = [
  150. [
  151. 'type' => 'text',
  152. 'text' => $user_info['firstname'].' '.$user_info['lastname'],
  153. ],
  154. [
  155. 'type' => 'text',
  156. 'text' => $lesson_info['name_en'],
  157. ],
  158. [
  159. 'type' => 'text',
  160. 'text' => $coach_name,
  161. ],
  162. [
  163. 'type' => 'text',
  164. 'text' => date('Y-m-d H:i',$slot_info['starttime']),
  165. ],
  166. ];
  167. $this->whatapp($user_info['whatsapp'],'class_cancelled','en_US',$parameters);
  168. } catch (Exception $e) {
  169. }
  170. }
  171. $this->success('取消完成');
  172. }
  173. $this->view->assign('row',$info);
  174. return $this->view->fetch();
  175. }
  176. //候补上位
  177. private function lesson_order_wait($lesson_order,$slot){
  178. //找到所有的候补单
  179. $houbu_list = Db::name('lesson_order')->where('slot_id',$lesson_order['slot_id'])->where('jointype',2)->order('id asc')->select();
  180. if(!empty($houbu_list)){
  181. $up_usernumber = 0; //此次循环转正的人数
  182. foreach($houbu_list as $key => $order){
  183. if($order['usernumber'] + $up_usernumber <= $lesson_order['usernumber']){
  184. //此订单可以转正
  185. //检查可用的套餐,找一个就可以
  186. $map = [
  187. 'user_id' => $order['user_id'],
  188. 'endtime' => ['gt',time()],
  189. 'remain' => ['gt',$order['usernumber_hours']],
  190. 'order_status' => 1,
  191. 'use_status' => 1, //已激活的
  192. ];
  193. $package_order = Db::name('package_order')->where($map)->where('find_in_set(:lesson_ids,lesson_ids)', ['lesson_ids' => $slot['lesson_id']])->order('endtime asc')->find();
  194. if(!$package_order){
  195. continue;
  196. }
  197. //扣除一节。
  198. $update = [
  199. 'remain' => bcsub($package_order['remain'],$order['usernumber_hours'],1),
  200. 'updatetime' => time(),
  201. ];
  202. $rs1 = Db::name('package_order')->where('id',$package_order['id'])->update($update);
  203. if($rs1 === false){
  204. Db::rollback();
  205. // $this->error('扣除套餐余额失败');
  206. $this->error('取消失败');
  207. }
  208. //修改预约单数据。延迟到转正的时候处理
  209. $lesson_order['order_amount'] = 0;
  210. $lesson_order['order_status'] = 10;
  211. $lesson_order['paytime'] = time();
  212. $lesson_order['package_order_id'] = $package_order['id'];
  213. $lesson_order['package_remark'] = ($package_order['sessions'] - $package_order['remain']) . '-' . ($package_order['sessions'] - $package_order['remain'] + $order['usernumber_hours']) .'/'. $package_order['sessions'];
  214. $lesson_order['paytype'] = 1; //修改支付方式为配套
  215. $lesson_order['jointype'] = 1; //修改加入方式为预约
  216. $rs2 = Db::name('lesson_order')->where('id',$order['id'])->update($lesson_order);
  217. if($rs2 === false){
  218. Db::rollback();
  219. // $this->error('扣除套餐余额失败');
  220. $this->error('取消失败');
  221. }
  222. //加上本次转正的人数
  223. $up_usernumber += $order['usernumber'];
  224. }else{
  225. break;//跳出
  226. }
  227. }
  228. }
  229. return true;
  230. }
  231. private function whatapp($receive_mobile,$template,$code,$parameters){
  232. if(empty($receive_mobile)){return true;}
  233. $token = config('site.whatsapp_token');
  234. //发送者
  235. $mobile_id = '337736419413019'; //Elin Dance Stuido 2:+65 8015 4154 , WhatsApp Business Account ID: 336509229537586
  236. //发送
  237. $url = 'https://graph.facebook.com/v19.0/'.$mobile_id.'/messages';
  238. $header = [
  239. 'Authorization: Bearer ' . $token,
  240. 'Content-Type: application/json',
  241. ];
  242. $body = [
  243. 'messaging_product' => 'whatsapp',
  244. 'recipient_type' => 'individual',
  245. 'to' => $receive_mobile,
  246. 'type' => 'template',
  247. 'template' => [
  248. 'name' => $template,
  249. 'language' => [
  250. 'code' => $code
  251. ],
  252. 'components' => [
  253. [
  254. 'type' => 'body',
  255. 'parameters' => $parameters
  256. ]
  257. ],
  258. ],
  259. ];
  260. $body = json_encode($body);
  261. $rs = curl_post($url,$body,$header);
  262. return true;
  263. }
  264. }