Lessonslot.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 Lessonslot extends Backend
  12. {
  13. protected $noNeedLogin = ['vue_index','vue_staff','slot_add','slot_info','slot_edit'];
  14. /**
  15. * Lessonslot模型对象
  16. * @var \app\admin\model\Lessonslot
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\Lessonslot;
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. $this->view->assign("noticeStatusList", $this->model->getNoticeStatusList());
  25. $this->view->assign("isShowList", $this->model->getIsShowList());
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //当前是否为关联查询
  38. $this->relationSearch = true;
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. $list = $this->model
  48. ->with(['coach','lesson','danceroom'])
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. foreach ($list as $row) {
  53. $row->getRelation('coach')->visible(['nickname']);
  54. $row->getRelation('lesson')->visible(['name','name_en']);
  55. $row->getRelation('danceroom')->visible(['name','name_en']);
  56. }
  57. $result = array("total" => $list->total(), "rows" => $list->items());
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 取消
  64. */
  65. public function cancel(){
  66. $id = input('id');
  67. $info = Db::name('lesson_slot')->where('id',$id)->where('status',0)->find();
  68. if(!$info){
  69. $this->error('请刷新重试');
  70. }
  71. if($this->request->isPost()){
  72. $remark = input('remark','');
  73. $cancel_reason = input('cancel_reason','');
  74. $cancel_time = strtotime(input('cancel_time',''));
  75. Db::startTrans();
  76. $update = [
  77. 'status' => 30,
  78. 'remark' => $remark,
  79. 'cancel_reason' => $cancel_reason,
  80. 'cancel_time' => $cancel_time
  81. ];
  82. $rs1 = Db::name('lesson_slot')->where('id',$id)->where('status',0)->update($update);
  83. if($rs1 === false){
  84. Db::rollback();
  85. $this->error('取消失败,请刷新重试');
  86. }
  87. $lesson_info = Db::name('lesson')->where('id',$info['lesson_id'])->find();
  88. //找到所有的已报名订单
  89. $lesson_order_list = Db::name('lesson_order')->where('slot_id',$id)->where('order_status',10)->lock(true)->select();
  90. if(!empty($lesson_order_list)){
  91. foreach($lesson_order_list as $lesson_order){
  92. //套餐给加回去
  93. if($lesson_order['paytype'] == 1){
  94. $package_order = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->lock(true)->find();
  95. $update = [
  96. 'remain' => bcadd($package_order['remain'],$lesson_order['usernumber_hours'],1),
  97. 'updatetime' => time(),
  98. ];
  99. $rs_remain = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->update($update);
  100. if($rs_remain === false){
  101. Db::rollback();
  102. $this->error('取消失败');
  103. }
  104. }
  105. //试课给改回去
  106. if($lesson_order['paytype'] == 4){
  107. $update = [
  108. 'order_status' => 10,
  109. 'updatetime' => time(),
  110. 'lesson_order_id' => 0,
  111. ];
  112. $rs_remain = Db::name('trylesson_order')->where('id',$lesson_order['trylesson_order_id'])->update($update);
  113. if($rs_remain === false){
  114. Db::rollback();
  115. $this->error('取消失败');
  116. }
  117. }
  118. //现金支付不给退,线下处理
  119. //取消预约单
  120. $update = [
  121. 'order_status' => 30,
  122. 'cancel_time' => $cancel_time,
  123. 'cancel_reason' => $cancel_reason,
  124. ];
  125. if($lesson_order['paytype'] == 1 || $lesson_order['paytype'] == 4){
  126. $update['order_status'] = 40;
  127. }
  128. $rs = Db::name('lesson_order')->where('id',$lesson_order['id'])->update($update);
  129. if($rs === false){
  130. Db::rollback();
  131. $this->error('取消失败');
  132. }
  133. }
  134. }
  135. Db::commit();
  136. $this->success('取消完成');
  137. }
  138. $this->view->assign('row',$info);
  139. return $this->view->fetch();
  140. }
  141. /**
  142. * 复制本周的课程表
  143. */
  144. public function copyweek(){
  145. exit;
  146. $starttime = strtotime('this week Monday'); // 获取本周一的时间戳
  147. $endtime = $starttime + 86400*7;
  148. $list = Db::name('lesson_slot')->where('is_show',1)->where('starttime','BETWEEN',[$starttime,$endtime])->order('starttime asc')->select();
  149. if(empty($list)){
  150. $this->error('本周还没有课程表');
  151. }
  152. foreach($list as $key => &$val){
  153. unset($val['id']);
  154. $val['starttime'] = $val['starttime'] + 86400*7;
  155. $val['endtime'] = $val['endtime'] + 86400*7;
  156. $val['status'] = 0;
  157. $val['notice_status'] = 0;
  158. $val['finishtime'] = 0;
  159. $val['cancel_reason'] = '';
  160. $val['cancel_time'] = 0;
  161. }
  162. Db::name('lesson_slot')->insertAll($list);
  163. $this->success('已复制到下周');
  164. }
  165. /**
  166. * 查看
  167. */
  168. public function vue_index()
  169. {
  170. $where = [];
  171. $coach_id = input('coach_id','');
  172. $lesson_id = input('lesson_id','');
  173. $danceroom_id = input('danceroom_id',0);
  174. $bookstatus = input('bookstatus',0);
  175. $starttime = input('starttime',0);//默认看今天
  176. $endtime = input('endtime',0);
  177. if(empty($starttime) || empty($endtime)){
  178. $starttime = strtotime(date('Y-m-d'));
  179. $endtime = $starttime + 86399;
  180. }
  181. if(!empty($coach_id)){
  182. $where['coach_ids'] = ['IN',$coach_id];
  183. }
  184. if(!empty($lesson_id)){
  185. $where['lesson_id'] = ['IN',$lesson_id];
  186. }
  187. if(!empty($danceroom_id)){
  188. $where['danceroom_id'] = $danceroom_id;
  189. }
  190. $where['starttime'] = ['BETWEEN',[$starttime,$endtime]];
  191. $wherenew = '';
  192. if(!empty($bookstatus)){
  193. if($bookstatus == 1){
  194. $wherenew = 'bookednum = 0';
  195. }
  196. if($bookstatus == 2){
  197. $wherenew = 'bookednum != 0 and bookednum < num_max';
  198. }
  199. if($bookstatus == 3){
  200. $wherenew = 'bookednum = num_max';
  201. }
  202. }
  203. $field = [
  204. 'slot.id','slot.starttime','slot.endtime','slot.num_max','slot.bookednum','coach_ids',
  205. 'coach.nickname as Staff','coach.bgcolor',
  206. 'lesson.name_en as title',
  207. 'danceroom.name_en as Location',
  208. ];
  209. $list = Db::name('lesson_slot')->alias('slot')
  210. ->join('coach','slot.coach_ids = coach.id','LEFT')
  211. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  212. ->join('danceroom','slot.danceroom_id = danceroom.id','LEFT')
  213. ->field($field)
  214. ->where($where)
  215. ->where($wherenew)
  216. ->order('id desc')
  217. ->select();
  218. if(!empty($list)){
  219. foreach($list as $key => $val){
  220. $val['resourceId'] = $val['coach_ids'];
  221. $val['start'] = date('Y-m-d H:i',$val['starttime']);
  222. $val['end'] = date('Y-m-d H:i',$val['endtime']);
  223. //无人约
  224. $val['backgroundColor'] = '#ffffff';
  225. $val['borderColor'] = $val['bgcolor'];
  226. $val['textColor'] = '#333333';
  227. //有人约
  228. if($val['bookednum'] > 0){
  229. $val['backgroundColor'] = $val['bgcolor'];
  230. $val['borderColor'] = $val['bgcolor'];
  231. $val['textColor'] = '#ffffff';
  232. }
  233. //详情
  234. $val['extendedProps'] = [
  235. 'Session' => $val['title'],
  236. 'time' => date('D, M d Y, H:i',$val['starttime']).' - '.date('H:i',$val['endtime']),
  237. 'Staff' => $val['Staff'],
  238. 'Location' => $val['Location'],
  239. 'Participar' => $val['bookednum'].'/'.$val['num_max'].' participants',
  240. 'id' => $val['id'],
  241. ];
  242. $list[$key] = $val;
  243. }
  244. }
  245. $this->result($list,1,'success','json');
  246. }
  247. //教练表头
  248. public function vue_staff()
  249. {
  250. $coach_id = input('coach_id','');
  251. //教练列表
  252. $coach_map = [];
  253. if(!empty($coach_id)){
  254. $coach_map['coach_ids'] = ['IN',$coach_id];
  255. }
  256. $coach_list = Db::name('coach')->where($coach_map)->order('id desc')->select();
  257. $result = [];
  258. if(!empty($coach_list)){
  259. foreach($coach_list as $ckey => $cval){
  260. $result[] = [
  261. 'id' => $cval['id'],
  262. 'title' => $cval['nickname'],
  263. 'extendedProps' => [
  264. 'name' => $cval['nickname'],
  265. 'avatar' => localpath_to_netpath($cval['avatar']),
  266. ],
  267. ];
  268. }
  269. }
  270. $this->result($result,1,'success','json');
  271. }
  272. //课程新增
  273. public function slot_add(){
  274. $field = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','address','remark','is_show'];
  275. $require = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','is_show'];
  276. $data = request_post_hub($field,$require);
  277. $data['endtime'] = $data['starttime'] + ($data['hours'] * 3600);
  278. $id = Db::name('lesson_slot')->insertGetId($data);
  279. $this->result($id,1,'添加完成','json');
  280. }
  281. //课程详情
  282. public function slot_info(){
  283. $id = input('id',0);
  284. $info = Db::name('lesson_slot')->where('id',$id)->find();
  285. $this->result($info,1,'success','json');
  286. }
  287. //课程编辑
  288. public function slot_edit(){
  289. $id = input('id',0);
  290. $field = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','address','remark','is_show'];
  291. $require = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','is_show'];
  292. $data = request_post_hub($field,$require);
  293. $data['endtime'] = $data['starttime'] + ($data['hours'] * 3600);
  294. Db::name('lesson_slot')->where('id',$id)->update($data);
  295. $this->result('',1,'修改完成','json');
  296. }
  297. }