Lesson.php 11 KB

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