Lesson.php 11 KB

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