Lesson.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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.firstname,user.lastname,user.nickname,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. foreach($pay_number as $key => $val){
  156. $pay_number[$key]['showname'] = $val['firstname'].' '.$val['lastname'];
  157. }
  158. $info['usernumber'] = array_sum(array_column($pay_number,'usernumber'));
  159. $info['user_list'] = $pay_number;
  160. $this->success(1,$info);
  161. }
  162. //课时申请报名
  163. public function slot_sign(){
  164. $this->apiLimit();
  165. $slot_id = input('slot_id',0,'intval');
  166. $signdata = input('signdata','','htmlspecialchars_decode');
  167. $signdata = json_decode($signdata,true);
  168. //测试
  169. /*$signdata = [
  170. 6 => 1,
  171. 7 => 2,
  172. ];*/
  173. //检查课时状态
  174. $slot_info = Db::name('lesson_slot')->where('id',$slot_id)->find();
  175. if($slot_info['status'] == 20){
  176. $this->error('此课程已经点过名了');
  177. }
  178. if($slot_info['status'] != 0){
  179. $this->error('此课程不能提交点名');
  180. }
  181. //检查报名单数据
  182. //逐个检查报名单,对比数量和状态。并保存
  183. Db::startTrans();
  184. $order_list = Db::name('lesson_order')->where('slot_id',$slot_id)->where('order_status',10)->order('id asc')->lock(true)->select();
  185. if(count($order_list) != count($signdata)){
  186. Db::rollback();
  187. $this->error('点名数据错误');
  188. }
  189. if(empty($order_list)){
  190. Db::rollback();
  191. $this->error('无人报名,请直接取消课程');
  192. }
  193. foreach($order_list as $order){
  194. if(!isset($signdata[$order['id']])){
  195. Db::rollback();
  196. $this->error('点名数据错误');
  197. }
  198. $usernumber_sign = $signdata[$order['id']];
  199. if($usernumber_sign < 0 || $usernumber_sign > $order['usernumber']){
  200. Db::rollback();
  201. $this->error('点名数据错误');
  202. }
  203. $update = [
  204. 'usernumber_sign' => $usernumber_sign,
  205. 'order_status' => 20,
  206. 'finishtime' => time(),
  207. ];
  208. $rs_sign = Db::name('lesson_order')->where('id',$order['id'])->update($update);
  209. if($rs_sign === false){
  210. Db::rollback();
  211. $this->error('点名失败');
  212. }
  213. }
  214. //修改课时状态
  215. $update = [
  216. 'status' => 20,
  217. 'finishtime' => time(),
  218. ];
  219. $slot_rs = Db::name('lesson_slot')->where('id',$slot_id)->update($update);
  220. if($slot_rs === false){
  221. Db::rollback();
  222. $this->error('点名失败');
  223. }
  224. Db::commit();
  225. $this->success('点名完成');
  226. }
  227. //我的课时记录,点过名的
  228. public function my_slot_history(){
  229. $date = strtotime(input('date',date('Y-m-01')));//月初
  230. $date_end = strtotime('+1 month', $date) - 1;//月末
  231. $cate_id = input('cate_id',0);
  232. $where = [
  233. 'slot.starttime' => ['BETWEEN',[$date,$date_end]],
  234. 'slot.status' => 20,
  235. ];
  236. if($cate_id){
  237. $where['lesson.lessoncate_id'] = $cate_id;
  238. }
  239. //课时
  240. $list = Db::name('lesson_slot')->alias('slot')
  241. ->field('slot.*,lesson.name,lesson.name_en')
  242. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  243. ->where($where);
  244. $list->where('find_in_set(:coach_ids,coach_ids)', ['coach_ids' => $this->auth->id]);
  245. $list = $list->order('slot.starttime asc')->select();
  246. if(empty($list)){
  247. $this->success(1,[]);
  248. }
  249. $list = $this->list_lang($list,['name']);
  250. foreach($list as $key => &$slot){
  251. //hours转换
  252. $slot['hours'] = floatval($slot['hours']);
  253. //已报名数量
  254. $pay_number = Db::name('lesson_order')->where('slot_id',$slot['id'])->where('order_status',20)->sum('usernumber');
  255. $slot['usernumber'] = $pay_number;
  256. }
  257. $this->success(1,$list);
  258. }
  259. }