Lesson.php 9.1 KB

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