Lessonorder.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. $slot = Db::name('lesson_slot')->where('id',$info['slot_id'])->find();
  125. $wait_rs = $this->lesson_order_wait($info,$slot);
  126. //更新已预约人数
  127. $pay_number = Db::name('lesson_order')->where('slot_id',$info['slot_id'])->where('order_status',10)->sum('usernumber');
  128. $rs_slot = Db::name('lesson_slot')->where('id',$info['slot_id'])->update(['bookednum' => $pay_number]);
  129. if($rs_slot === false){
  130. Db::rollback();
  131. $this->error('取消失败');
  132. }
  133. Db::commit();
  134. $slot_info = Db::name('lesson_slot')->where('id',$info['slot_id'])->find();
  135. $lesson_info = Db::name('lesson')->where('id',$slot_info['lesson_id'])->find();
  136. //给用户发通知
  137. $user_info = Db::name('user')->where('id',$info['user_id'])->find();
  138. if(!empty($user_info['email'])){
  139. try {
  140. $obj = new Email();
  141. $result = $obj
  142. ->to($user_info['email'])
  143. ->subject('Class is Cancelled!')
  144. ->message('Hi,'.$user_info['firstname']. ' ' .$user_info['lastname'].',您预约的'.$lesson_info['name'].'已取消')
  145. ->send();
  146. $coach_name = Db::name('coach')->where('id',$slot_info['coach_ids'])->value('nickname');
  147. //发whatsapp
  148. $parameters = [
  149. [
  150. 'type' => 'text',
  151. 'text' => $user_info['firstname'].' '.$user_info['lastname'],
  152. ],
  153. [
  154. 'type' => 'text',
  155. 'text' => $lesson_info['name_en'],
  156. ],
  157. [
  158. 'type' => 'text',
  159. 'text' => $coach_name,
  160. ],
  161. [
  162. 'type' => 'text',
  163. 'text' => date('Y-m-d H:i',$slot_info['starttime']),
  164. ],
  165. ];
  166. $this->whatapp($user_info['whatsapp'],'class_cancelled','en_US',$parameters);
  167. } catch (Exception $e) {
  168. }
  169. }
  170. $this->success('取消完成');
  171. }
  172. $this->view->assign('row',$info);
  173. return $this->view->fetch();
  174. }
  175. //候补上位
  176. private function lesson_order_wait($lesson_order,$slot){
  177. //找到所有的候补单
  178. $houbu_list = Db::name('lesson_order')->where('slot_id',$lesson_order['slot_id'])->where('jointype',2)->order('id asc')->select();
  179. if(!empty($houbu_list)){
  180. $up_usernumber = 0; //此次循环转正的人数
  181. foreach($houbu_list as $key => $order){
  182. if($order['usernumber'] + $up_usernumber <= $lesson_order['usernumber']){
  183. //此订单可以转正
  184. //检查可用的套餐,找一个就可以
  185. $map = [
  186. 'user_id' => $order['user_id'],
  187. 'endtime' => ['gt',time()],
  188. 'remain' => ['gt',$order['usernumber_hours']],
  189. 'order_status' => 1,
  190. 'use_status' => 1, //已激活的
  191. ];
  192. $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();
  193. if(!$package_order){
  194. continue;
  195. }
  196. //扣除一节。
  197. $update = [
  198. 'remain' => bcsub($package_order['remain'],$order['usernumber_hours'],1),
  199. 'updatetime' => time(),
  200. ];
  201. $rs1 = Db::name('package_order')->where('id',$package_order['id'])->update($update);
  202. if($rs1 === false){
  203. Db::rollback();
  204. // $this->error('扣除套餐余额失败');
  205. $this->error('取消失败');
  206. }
  207. //修改预约单数据。延迟到转正的时候处理
  208. $lesson_order['order_amount'] = 0;
  209. $lesson_order['order_status'] = 10;
  210. $lesson_order['paytime'] = time();
  211. $lesson_order['package_order_id'] = $package_order['id'];
  212. $lesson_order['package_remark'] = ($package_order['sessions'] - $package_order['remain']) . '-' . ($package_order['sessions'] - $package_order['remain'] + $order['usernumber_hours']) .'/'. $package_order['sessions'];
  213. $lesson_order['paytype'] = 1; //修改支付方式为配套
  214. $lesson_order['jointype'] = 1; //修改加入方式为预约
  215. $rs2 = Db::name('lesson_order')->where('id',$order['id'])->update($lesson_order);
  216. if($rs2 === false){
  217. Db::rollback();
  218. // $this->error('扣除套餐余额失败');
  219. $this->error('取消失败');
  220. }
  221. //加上本次转正的人数
  222. $up_usernumber += $order['usernumber'];
  223. }else{
  224. break;//跳出
  225. }
  226. }
  227. }
  228. return true;
  229. }
  230. private function whatapp($receive_mobile,$template,$code,$parameters){
  231. if(empty($receive_mobile)){return true;}
  232. $token = config('site.whatsapp_token');
  233. //发送者
  234. $mobile_id = '337736419413019'; //Elin Dance Stuido 2:+65 8015 4154 , WhatsApp Business Account ID: 336509229537586
  235. //发送
  236. $url = 'https://graph.facebook.com/v19.0/'.$mobile_id.'/messages';
  237. $header = [
  238. 'Authorization: Bearer ' . $token,
  239. 'Content-Type: application/json',
  240. ];
  241. $body = [
  242. 'messaging_product' => 'whatsapp',
  243. 'recipient_type' => 'individual',
  244. 'to' => $receive_mobile,
  245. 'type' => 'template',
  246. 'template' => [
  247. 'name' => $template,
  248. 'language' => [
  249. 'code' => $code
  250. ],
  251. 'components' => [
  252. [
  253. 'type' => 'body',
  254. 'parameters' => $parameters
  255. ]
  256. ],
  257. ],
  258. ];
  259. $body = json_encode($body);
  260. $rs = curl_post($url,$body,$header);
  261. return true;
  262. }
  263. }