Lesson.php 9.4 KB

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