Lesson.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. $week_arr = [
  74. 1 => '周一',
  75. 2 => '周二',
  76. 3 => '周三',
  77. 4 => '周四',
  78. 5 => '周五',
  79. 6 => '周六',
  80. 7 => '周日',
  81. ];
  82. //本月结束日
  83. $endday_arr = [
  84. '01' => 31,
  85. '02' => 28,
  86. '03' => 31,
  87. '04' => 30,
  88. '05' => 31,
  89. '06' => 30,
  90. '07' => 31,
  91. '08' => 31,
  92. '09' => 30,
  93. '10' => 31,
  94. '11' => 30,
  95. '12' => 31,
  96. ];
  97. if($year%4 == 0){
  98. $endday_arr['02'] = 29;
  99. }
  100. $endday = $endday_arr[$month];
  101. //拿数据
  102. $startdate = $getdate.'-'.$startday;
  103. $enddate = $getdate.'-'.$endday;
  104. $where = [
  105. 'starttime' => ['BETWEEN',[strtotime($startdate),strtotime($enddate)+86399]],
  106. 'status' => 0,
  107. 'is_show' => 1,
  108. ];
  109. //dump($where);exit;
  110. $slots = Db::name('lesson_slot')
  111. ->where($where)
  112. ->where('find_in_set(:coach_ids,coach_ids)', ['coach_ids' => $this->auth->id])
  113. ->field('id,starttime')->select();
  114. $date_arr = [];
  115. for($i=intval($startday);$i<=$endday;$i++){
  116. $j = $i;
  117. if($j < 10){
  118. $j = 0 . $j;
  119. }
  120. $idate = $year.'-'.$month.'-'.$j;
  121. $itime = strtotime($idate);
  122. $week = $this->lang == 'en' ? date('D',$itime) : $week_arr[date('N',$itime)];
  123. $i_data = [
  124. 'yeah' =>$year,
  125. 'month'=>$month,
  126. 'day' => $j.'',
  127. 'date' => $idate,
  128. 'week' => $week,
  129. 'is_today' => 0,
  130. 'slot_num' => 0,
  131. ];
  132. foreach($slots as $slot){
  133. if(date('Y-m-d',$slot['starttime']) == $idate)
  134. {
  135. $i_data['slot_num'] += 1;
  136. }
  137. }
  138. if($idate == date('Y-m-d')){
  139. $i_data['is_today'] = 1;
  140. }
  141. $date_arr[] = $i_data;
  142. }
  143. $this->success(1,$date_arr);
  144. }
  145. //课时详情
  146. public function slot_info(){
  147. $slot_id = input('slot_id',0);
  148. //课时
  149. $info = Db::name('lesson_slot')->alias('slot')
  150. ->field('slot.*,lesson.name,lesson.name_en,lesson.image,lesson.price')
  151. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  152. ->where('slot.id',$slot_id)->find();
  153. $info = info_domain_image($info,['image']);
  154. $info = $this->info_lang($info,['name']);
  155. //hours转换
  156. $info['hours'] = floatval($info['hours']);
  157. //已报名数量,(包含已支付,已点名,没有冲突)
  158. $pay_number = Db::name('lesson_order')->alias('order')
  159. ->field('order.id,user.firstname,user.lastname,user.nickname,user.avatar,order.usernumber,order.usernumber_sign')
  160. ->join('user','order.user_id = user.id','LEFT')
  161. ->where('order.slot_id',$info['id'])
  162. ->where('order.order_status','IN',[10,20])
  163. ->select();
  164. $pay_number = list_domain_image($pay_number,['avatar']);
  165. foreach($pay_number as $key => $val){
  166. $pay_number[$key]['showname'] = $val['firstname'].' '.$val['lastname'];
  167. }
  168. $info['usernumber'] = array_sum(array_column($pay_number,'usernumber'));
  169. $info['user_list'] = $pay_number;
  170. $this->success(1,$info);
  171. }
  172. //课时申请报名
  173. public function slot_sign(){
  174. $this->apiLimit();
  175. $slot_id = input('slot_id',0,'intval');
  176. $signdata = input('signdata','','htmlspecialchars_decode');
  177. $signdata = json_decode($signdata,true);
  178. //测试
  179. /*$signdata = [
  180. 6 => 1,
  181. 7 => 2,
  182. ];*/
  183. //检查课时状态
  184. $slot_info = Db::name('lesson_slot')->where('id',$slot_id)->find();
  185. if($slot_info['status'] == 20){
  186. $this->error('此课程已经点过名了');
  187. }
  188. if($slot_info['status'] != 0){
  189. $this->error('此课程不能提交点名');
  190. }
  191. //检查报名单数据
  192. //逐个检查报名单,对比数量和状态。并保存
  193. Db::startTrans();
  194. $order_list = Db::name('lesson_order')->where('slot_id',$slot_id)->where('order_status',10)->order('id asc')->lock(true)->select();
  195. if(count($order_list) != count($signdata)){
  196. Db::rollback();
  197. $this->error('点名数据错误');
  198. }
  199. if(empty($order_list)){
  200. Db::rollback();
  201. $this->error('无人报名,请直接取消课程');
  202. }
  203. foreach($order_list as $order){
  204. if(!isset($signdata[$order['id']])){
  205. Db::rollback();
  206. $this->error('点名数据错误');
  207. }
  208. $usernumber_sign = $signdata[$order['id']];
  209. if($usernumber_sign < 0 || $usernumber_sign > $order['usernumber']){
  210. Db::rollback();
  211. $this->error('点名数据错误');
  212. }
  213. $update = [
  214. 'usernumber_sign' => $usernumber_sign,
  215. 'order_status' => 20,
  216. 'finishtime' => time(),
  217. ];
  218. $rs_sign = Db::name('lesson_order')->where('id',$order['id'])->update($update);
  219. if($rs_sign === false){
  220. Db::rollback();
  221. $this->error('点名失败');
  222. }
  223. }
  224. //修改课时状态
  225. $update = [
  226. 'status' => 20,
  227. 'finishtime' => time(),
  228. ];
  229. $slot_rs = Db::name('lesson_slot')->where('id',$slot_id)->update($update);
  230. if($slot_rs === false){
  231. Db::rollback();
  232. $this->error('点名失败');
  233. }
  234. Db::commit();
  235. $this->success('点名完成');
  236. }
  237. //我的课时记录,点过名的
  238. public function my_slot_history(){
  239. $date = strtotime(input('date',date('Y-m-01')));//月初
  240. $date_end = strtotime('+1 month', $date) - 1;//月末
  241. $cate_id = input('cate_id',0);
  242. $lesson_type = input('lesson_type',0);//私教课,普通课
  243. $where = [
  244. 'slot.starttime' => ['BETWEEN',[$date,$date_end]],
  245. 'slot.status' => 20,
  246. ];
  247. if($cate_id){
  248. $where['lesson.lessoncate_id'] = $cate_id;
  249. }
  250. if($lesson_type){
  251. $where['lesson.type'] = $lesson_type;
  252. }
  253. //课时
  254. $list = Db::name('lesson_slot')->alias('slot')
  255. ->field('slot.*,lesson.name,lesson.name_en')
  256. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  257. ->where($where);
  258. $list->where('find_in_set(:coach_ids,coach_ids)', ['coach_ids' => $this->auth->id]);
  259. $list = $list->order('slot.starttime asc')->select();
  260. $result = [
  261. 'list' => [],
  262. 'total' => 0,
  263. 'total_hours' => 0,
  264. ];
  265. if(empty($list)){
  266. $this->success(1,$result);
  267. }
  268. $list = $this->list_lang($list,['name']);
  269. $total_hours = 0;
  270. foreach($list as $key => &$slot){
  271. //hours转换
  272. $slot['hours'] = floatval($slot['hours']);
  273. //已报名数量
  274. $pay_number = Db::name('lesson_order')->where('slot_id',$slot['id'])->where('order_status',20)->sum('usernumber');
  275. $slot['usernumber'] = $pay_number;
  276. $total_hours = bcadd($total_hours,$slot['hours'],1);
  277. }
  278. $buttom = __('共计N节课,M小时',['total'=>count($list),'total_hours'=>$total_hours]);
  279. $result = [
  280. 'list' => $list,
  281. 'buttom' => $buttom,
  282. ];
  283. $this->success(1,$result);
  284. }
  285. }