Lessonslot.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. 'bookednum' => 0,
  82. ];
  83. $rs1 = Db::name('lesson_slot')->where('id',$id)->where('status',0)->update($update);
  84. if($rs1 === false){
  85. Db::rollback();
  86. $this->error('取消失败,请刷新重试');
  87. }
  88. $lesson_info = Db::name('lesson')->where('id',$info['lesson_id'])->find();
  89. //找到所有的已报名订单
  90. $lesson_order_list = Db::name('lesson_order')->where('slot_id',$id)->where('order_status',10)->lock(true)->select();
  91. if(!empty($lesson_order_list)){
  92. foreach($lesson_order_list as $lesson_order){
  93. //套餐给加回去
  94. if($lesson_order['paytype'] == 1){
  95. $package_order = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->lock(true)->find();
  96. $update = [
  97. 'remain' => bcadd($package_order['remain'],$lesson_order['usernumber_hours'],1),
  98. 'updatetime' => time(),
  99. ];
  100. $rs_remain = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->update($update);
  101. if($rs_remain === false){
  102. Db::rollback();
  103. $this->error('取消失败');
  104. }
  105. }
  106. //试课给改回去
  107. if($lesson_order['paytype'] == 4){
  108. $update = [
  109. 'order_status' => 10,
  110. 'updatetime' => time(),
  111. 'lesson_order_id' => 0,
  112. ];
  113. $rs_remain = Db::name('trylesson_order')->where('id',$lesson_order['trylesson_order_id'])->update($update);
  114. if($rs_remain === false){
  115. Db::rollback();
  116. $this->error('取消失败');
  117. }
  118. }
  119. //现金支付不给退,线下处理
  120. //取消预约单
  121. $update = [
  122. 'order_status' => 30,
  123. 'cancel_time' => $cancel_time,
  124. 'cancel_reason' => $cancel_reason,
  125. ];
  126. if($lesson_order['paytype'] == 1 || $lesson_order['paytype'] == 4){
  127. $update['order_status'] = 40;
  128. }
  129. $rs = Db::name('lesson_order')->where('id',$lesson_order['id'])->update($update);
  130. if($rs === false){
  131. Db::rollback();
  132. $this->error('取消失败');
  133. }
  134. }
  135. }
  136. Db::commit();
  137. $this->success('取消完成');
  138. }
  139. $this->view->assign('row',$info);
  140. return $this->view->fetch();
  141. }
  142. /**
  143. * 复制本周的课程表
  144. */
  145. public function copyweek(){
  146. exit;
  147. $starttime = strtotime('this week Monday'); // 获取本周一的时间戳
  148. $endtime = $starttime + 86400*7;
  149. $list = Db::name('lesson_slot')->where('is_show',1)->where('starttime','BETWEEN',[$starttime,$endtime])->order('starttime asc')->select();
  150. if(empty($list)){
  151. $this->error('本周还没有课程表');
  152. }
  153. foreach($list as $key => &$val){
  154. unset($val['id']);
  155. $val['starttime'] = $val['starttime'] + 86400*7;
  156. $val['endtime'] = $val['endtime'] + 86400*7;
  157. $val['status'] = 0;
  158. $val['notice_status'] = 0;
  159. $val['finishtime'] = 0;
  160. $val['cancel_reason'] = '';
  161. $val['cancel_time'] = 0;
  162. }
  163. Db::name('lesson_slot')->insertAll($list);
  164. $this->success('已复制到下周');
  165. }
  166. /**
  167. * 查看
  168. */
  169. public function vue_index()
  170. {
  171. $where = [];
  172. $coach_id = input('coach_id','');
  173. $lesson_id = input('lesson_id','');
  174. $danceroom_id = input('danceroom_id',0);
  175. $bookstatus = input('bookstatus',0);
  176. $starttime = input('starttime',0);//默认看今天
  177. $endtime = input('endtime',0);
  178. if(empty($starttime) || empty($endtime)){
  179. $starttime = strtotime(date('Y-m-d'));
  180. $endtime = $starttime + 86399;
  181. }
  182. if(!empty($coach_id)){
  183. $where['coach_ids'] = ['IN',$coach_id];
  184. }
  185. if(!empty($lesson_id)){
  186. $where['lesson_id'] = ['IN',$lesson_id];
  187. }
  188. if(!empty($danceroom_id)){
  189. $where['danceroom_id'] = $danceroom_id;
  190. }
  191. $where['starttime'] = ['BETWEEN',[$starttime,$endtime]];
  192. $wherenew = '';
  193. if(!empty($bookstatus)){
  194. if($bookstatus == 1){
  195. $wherenew = 'bookednum = 0';
  196. }
  197. if($bookstatus == 2){
  198. $wherenew = 'bookednum != 0 and bookednum < num_max';
  199. }
  200. if($bookstatus == 3){
  201. $wherenew = 'bookednum = num_max';
  202. }
  203. }
  204. $field = [
  205. 'slot.id','slot.starttime','slot.endtime','slot.num_max','slot.bookednum','coach_ids',
  206. 'coach.nickname as Staff','coach.bgcolor',
  207. 'lesson.name_en as title',
  208. 'danceroom.name_en as Location',
  209. ];
  210. $list = Db::name('lesson_slot')->alias('slot')
  211. ->join('coach','slot.coach_ids = coach.id','LEFT')
  212. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  213. ->join('danceroom','slot.danceroom_id = danceroom.id','LEFT')
  214. ->field($field)
  215. ->where($where)
  216. ->where($wherenew)
  217. ->order('id desc')
  218. ->select();
  219. if(!empty($list)){
  220. foreach($list as $key => $val){
  221. $val['resourceId'] = $val['coach_ids'];
  222. $val['start'] = date('Y-m-d H:i',$val['starttime']);
  223. $val['end'] = date('Y-m-d H:i',$val['endtime']);
  224. //无人约
  225. $val['backgroundColor'] = '#ffffff';
  226. $val['borderColor'] = $val['bgcolor'];
  227. $val['textColor'] = '#333333';
  228. //有人约
  229. if($val['bookednum'] > 0){
  230. $val['backgroundColor'] = $val['bgcolor'];
  231. $val['borderColor'] = $val['bgcolor'];
  232. $val['textColor'] = '#ffffff';
  233. }
  234. //详情
  235. $val['extendedProps'] = [
  236. 'Session' => $val['title'],
  237. 'time' => date('D, M d Y, H:i',$val['starttime']).' - '.date('H:i',$val['endtime']),
  238. 'Staff' => $val['Staff'],
  239. 'Location' => $val['Location'],
  240. 'Participar' => $val['bookednum'].'/'.$val['num_max'].' participants',
  241. 'id' => $val['id'],
  242. ];
  243. $list[$key] = $val;
  244. }
  245. }
  246. $this->result($list,1,'success','json');
  247. }
  248. //教练表头
  249. public function vue_staff()
  250. {
  251. $coach_id = input('coach_id','');
  252. //教练列表
  253. $coach_map = [];
  254. if(!empty($coach_id)){
  255. $coach_map['coach_ids'] = ['IN',$coach_id];
  256. }
  257. $coach_list = Db::name('coach')->where($coach_map)->order('id desc')->select();
  258. $result = [];
  259. if(!empty($coach_list)){
  260. foreach($coach_list as $ckey => $cval){
  261. $result[] = [
  262. 'id' => $cval['id'],
  263. 'title' => $cval['nickname'],
  264. 'extendedProps' => [
  265. 'name' => $cval['nickname'],
  266. 'avatar' => localpath_to_netpath($cval['avatar']),
  267. ],
  268. ];
  269. }
  270. }
  271. $this->result($result,1,'success','json');
  272. }
  273. //课程新增
  274. public function slot_add(){
  275. $field = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','address','remark','is_show'];
  276. $require = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','is_show'];
  277. $data = request_post_hub($field,$require);
  278. $data['endtime'] = $data['starttime'] + ($data['hours'] * 3600);
  279. $id = Db::name('lesson_slot')->insertGetId($data);
  280. $this->result($id,1,'添加完成','json');
  281. }
  282. //课程详情
  283. public function slot_info(){
  284. $id = input('id',0);
  285. $info = Db::name('lesson_slot')->where('id',$id)->find();
  286. $this->result($info,1,'success','json');
  287. }
  288. //课程编辑
  289. public function slot_edit(){
  290. $id = input('id',0);
  291. $field = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','address','remark','is_show'];
  292. $require = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','is_show'];
  293. $data = request_post_hub($field,$require);
  294. $data['endtime'] = $data['starttime'] + ($data['hours'] * 3600);
  295. Db::name('lesson_slot')->where('id',$id)->update($data);
  296. $this->result('',1,'修改完成','json');
  297. }
  298. }