Lesson.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace app\api\controller\coach;
  3. use app\common\controller\Apic;
  4. use think\Db;
  5. /**
  6. * 售课
  7. */
  8. class Lesson extends Apic
  9. {
  10. // 无需登录的接口,*表示全部
  11. protected $noNeedLogin = [];
  12. // 无需鉴权的接口,*表示全部
  13. protected $noNeedRight = ['*'];
  14. //售课分类列表
  15. public function lesson_cate(){
  16. $list = Db::name('lesson_cate')->order('id asc')->select();
  17. $list = $this->list_lang($list,['name']);
  18. $this->success(1,$list);
  19. }
  20. //课时首页
  21. public function slot_list(){
  22. $date = input('date',date('Y-m-d'),'strtotime');
  23. $where = [
  24. 'slot.starttime' => ['BETWEEN',[$date,$date+86399]],
  25. ];
  26. //课时
  27. $list = Db::name('lesson_slot')->alias('slot')
  28. ->field('slot.*,lesson.name,lesson.name_en,lesson.image,lesson.price')
  29. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  30. ->where($where);
  31. $list->where('find_in_set(:coach_ids,coach_ids)', ['coach_ids' => $this->auth->id]);
  32. $list = $list->order('slot.starttime asc')->select();
  33. if(empty($list)){
  34. $this->success(1,[]);
  35. }
  36. $list = list_domain_image($list,['image']);
  37. $list = $this->list_lang($list,['name']);
  38. foreach($list as $key => &$slot){
  39. //hours转换
  40. $slot['hours'] = floatval($slot['hours']);
  41. //已报名数量,(包含已支付,已点名,没有冲突)
  42. $pay_number = Db::name('lesson_order')->alias('order')
  43. ->field('order.id,user.avatar,order.usernumber')
  44. ->join('user','order.user_id = user.id','LEFT')
  45. ->where('order.slot_id',$slot['id'])
  46. ->where('order.order_status','IN',[10,20])
  47. ->select();
  48. $pay_number = list_domain_image($pay_number,['avatar']);
  49. $slot['usernumber'] = array_sum(array_column($pay_number,'usernumber'));
  50. $slot['useravatar'] = array_column($pay_number,'avatar');
  51. }
  52. $this->success(1,$list);
  53. }
  54. //课时列表首页
  55. //根据年,月获取当月剩余日期,有课程的标记1
  56. public function slot_index(){
  57. $getdate = input('date',date('Y-m'));
  58. if(empty($getdate)){
  59. $getdate = date('Y-m');
  60. }
  61. $year = substr($getdate,0,4);
  62. $month = substr($getdate,-2,2);
  63. //今天是哪日
  64. if($year == date('Y') && $month == date('m')){
  65. //如果是今年今月,默认今天开始
  66. $startday = date('d');
  67. }else{
  68. //从1号开始
  69. $startday = '01';
  70. }
  71. //本月结束日
  72. $endday_arr = [
  73. '01' => 31,
  74. '02' => 28,
  75. '03' => 31,
  76. '04' => 30,
  77. '05' => 31,
  78. '06' => 30,
  79. '07' => 31,
  80. '08' => 31,
  81. '09' => 30,
  82. '10' => 31,
  83. '11' => 30,
  84. '12' => 31,
  85. ];
  86. if($year%4 == 0){
  87. $endday_arr['02'] = 29;
  88. }
  89. $endday = $endday_arr[$month];
  90. //拿数据
  91. $startdate = $getdate.'-'.$startday;
  92. $enddate = $getdate.'-'.$endday;
  93. $where = [
  94. 'starttime' => ['BETWEEN',[strtotime($startdate),strtotime($enddate)+86399]],
  95. 'status' => 0,
  96. ];
  97. //dump($where);exit;
  98. $slots = Db::name('lesson_slot')
  99. ->where($where)
  100. ->where('find_in_set(:coach_ids,coach_ids)', ['coach_ids' => $this->auth->id])
  101. ->field('id,starttime')->select();
  102. $date_arr = [];
  103. for($i=intval($startday);$i<=$endday;$i++){
  104. $j = $i;
  105. if($j < 10){
  106. $j = 0 . $j;
  107. }
  108. $idate = $year.'-'.$month.'-'.$j;
  109. $itime = strtotime($idate);
  110. $week = $this->lang == 'en' ? date('D',$itime) : '周'.date('N',$itime);
  111. $i_data = [
  112. 'yeah' =>$year,
  113. 'month'=>$month,
  114. 'day' => $j.'',
  115. 'date' => $idate,
  116. 'week' => $week,
  117. 'is_today' => 0,
  118. 'slot_num' => 0,
  119. ];
  120. foreach($slots as $slot){
  121. if(date('Y-m-d',$slot['starttime']) == $idate)
  122. {
  123. $i_data['slot_num'] += 1;
  124. }
  125. }
  126. if($idate == date('Y-m-d')){
  127. $i_data['is_today'] = 1;
  128. }
  129. $date_arr[] = $i_data;
  130. }
  131. $this->success(1,$date_arr);
  132. }
  133. //课时详情
  134. public function slot_info(){
  135. $slot_id = input('slot_id',0);
  136. //课时
  137. $info = Db::name('lesson_slot')->alias('slot')
  138. ->field('slot.*,lesson.name,lesson.name_en,lesson.image,lesson.price')
  139. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  140. ->where('slot.id',$slot_id)->find();
  141. $info = info_domain_image($info,['image']);
  142. $info = $this->info_lang($info,['name']);
  143. //hours转换
  144. $info['hours'] = floatval($info['hours']);
  145. //已报名数量,(包含已支付,已点名,没有冲突)
  146. $pay_number = Db::name('lesson_order')->alias('order')
  147. ->field('order.id,user.avatar,order.usernumber,order.usernumber_sign')
  148. ->join('user','order.user_id = user.id','LEFT')
  149. ->where('order.slot_id',$info['id'])
  150. ->where('order.order_status','IN',[10,20])
  151. ->select();
  152. $pay_number = list_domain_image($pay_number,['avatar']);
  153. $info['usernumber'] = array_sum(array_column($pay_number,'usernumber'));
  154. $info['user_list'] = $pay_number;
  155. $this->success(1,$info);
  156. }
  157. //课时申请报名
  158. public function slot_sign(){
  159. $this->apiLimit();
  160. $slot_id = input('slot_id',0,'intval');
  161. $signdata = input('signdata','','htmlspecialchars_decode');
  162. $signdata = json_decode($signdata,true);
  163. //测试
  164. /*$signdata = [
  165. 6 => 1,
  166. 7 => 2,
  167. ];*/
  168. //检查课时状态
  169. $slot_info = Db::name('lesson_slot')->where('id',$slot_id)->find();
  170. if($slot_info['status'] == 20){
  171. $this->error('此课程已经点过名了');
  172. }
  173. if($slot_info['status'] != 0){
  174. $this->error('此课程不能提交点名');
  175. }
  176. //检查报名单数据
  177. //逐个检查报名单,对比数量和状态。并保存
  178. Db::startTrans();
  179. $order_list = Db::name('lesson_order')->where('slot_id',$slot_id)->where('order_status',10)->order('id asc')->lock(true)->select();
  180. if(count($order_list) != count($signdata)){
  181. Db::rollback();
  182. $this->error('点名数据错误');
  183. }
  184. if(empty($order_list)){
  185. Db::rollback();
  186. $this->error('无人报名,请直接取消课程');
  187. }
  188. foreach($order_list as $order){
  189. if(!isset($signdata[$order['id']])){
  190. Db::rollback();
  191. $this->error('点名数据错误');
  192. }
  193. $usernumber_sign = $signdata[$order['id']];
  194. if($usernumber_sign < 0 || $usernumber_sign > $order['usernumber']){
  195. Db::rollback();
  196. $this->error('点名数据错误');
  197. }
  198. $update = [
  199. 'usernumber_sign' => $usernumber_sign,
  200. 'order_status' => 20,
  201. 'finishtime' => time(),
  202. ];
  203. $rs_sign = Db::name('lesson_order')->where('id',$order['id'])->update($update);
  204. if($rs_sign === false){
  205. Db::rollback();
  206. $this->error('点名失败');
  207. }
  208. }
  209. //修改课时状态
  210. $update = [
  211. 'status' => 20,
  212. 'finishtime' => time(),
  213. ];
  214. $slot_rs = Db::name('lesson_slot')->where('id',$slot_id)->update($update);
  215. if($slot_rs === false){
  216. Db::rollback();
  217. $this->error('点名失败');
  218. }
  219. Db::commit();
  220. $this->success('点名完成');
  221. }
  222. //我的课时记录,点过名的
  223. public function my_slot_history(){
  224. $date = strtotime(input('date',date('Y-m-01')));//月初
  225. $date_end = strtotime('+1 month', $date) - 1;//月末
  226. $cate_id = input('cate_id',0);
  227. $where = [
  228. 'slot.starttime' => ['BETWEEN',[$date,$date_end]],
  229. 'slot.status' => 20,
  230. ];
  231. if($cate_id){
  232. $where['lesson.lessoncate_id'] = $cate_id;
  233. }
  234. //课时
  235. $list = Db::name('lesson_slot')->alias('slot')
  236. ->field('slot.*,lesson.name,lesson.name_en')
  237. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  238. ->where($where);
  239. $list->where('find_in_set(:coach_ids,coach_ids)', ['coach_ids' => $this->auth->id]);
  240. $list = $list->order('slot.starttime asc')->select();
  241. if(empty($list)){
  242. $this->success(1,[]);
  243. }
  244. $list = $this->list_lang($list,['name']);
  245. foreach($list as $key => &$slot){
  246. //hours转换
  247. $slot['hours'] = floatval($slot['hours']);
  248. //已报名数量
  249. $pay_number = Db::name('lesson_order')->where('slot_id',$slot['id'])->where('order_status',20)->sum('usernumber');
  250. $slot['usernumber'] = $pay_number;
  251. }
  252. $this->success(1,$list);
  253. }
  254. }