Lessonslot.php 13 KB

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