SpecialityBook.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model;
  13. use app\common\model\SpecialityBook as SpecialityBookModel;
  14. /**
  15. * 学校专业模型类
  16. * Class SpecialityBook
  17. * @package app\api\model
  18. */
  19. class SpecialityBook extends SpecialityBookModel
  20. {
  21. protected $globalScope = [''];
  22. /**
  23. * 隐藏字段
  24. * @var array
  25. */
  26. protected $hidden = [
  27. 'update_time'
  28. ];
  29. /**
  30. * 获取列表
  31. * @param array $param 查询条件
  32. * @param int $listRows 分页数量
  33. * @return mixed|\think\model\Collection|\think\Paginator
  34. * @throws \think\db\exception\DbException
  35. */
  36. public function getList(array $param = [], int $listRows = 12)
  37. {
  38. // 整理查询参数
  39. $params = array_merge($param);
  40. // 获取商品列表
  41. $list = parent::getList($params, $listRows);
  42. if ($list->isEmpty()) {
  43. return $list;
  44. }
  45. // 整理列表数据并返回
  46. return $this->setListDataFromApi($list);
  47. }
  48. /**
  49. * 设置展示的数据 api模块
  50. * @param $info
  51. * @return mixed
  52. */
  53. private function setListDataFromApi($info)
  54. {
  55. return $this->setListData($info, function ($data){
  56. // 整理数据 api模块
  57. $this->setDataFromApi($data);
  58. // 隐藏冗余的字段
  59. $this->hidden(array_merge($this->hidden, ['transaction_id']));
  60. });
  61. }
  62. /**
  63. * 整理数据 api模块
  64. * @param $info
  65. * @return mixed
  66. */
  67. private function setDataFromApi($info)
  68. {
  69. return $this->setData($info, function ($data) {
  70. });
  71. }
  72. /**
  73. * 获取学校的专业
  74. * @param int $school_id 学校ID
  75. * @param string $field 返回字段
  76. * @param int $limit 返回数量
  77. * @return SpecialityBook[]|array|\think\Collection
  78. * @throws \think\db\exception\DataNotFoundException
  79. * @throws \think\db\exception\DbException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. */
  82. public static function getBooks(int $speciality_id)
  83. {
  84. return self::where(['speciality_id'=>$speciality_id])->where('status','>', 2)->count('order_id');
  85. }
  86. /**
  87. * 获取用户已经报名次数
  88. * @param int $userId
  89. * @param int $specialityId
  90. * @return int
  91. */
  92. public static function getUserBooks(int $userId, $specialityId=0)
  93. {
  94. $where = ['book_user_id'=> $userId];
  95. if($specialityId){
  96. $where['speciality_id'] = $specialityId;
  97. }
  98. return self::where($where)->where('status','>', 2)->count('order_id');
  99. }
  100. /**
  101. * 计算咨询老师的报名订单数量
  102. * @param $userId 老师用户ID
  103. * @param int $status 状态:1-包含待支付,2-已支付
  104. * @return int
  105. */
  106. public static function getBooksByTeacher($userId, $status = 2)
  107. {
  108. $where = ['user_id'=> $userId];
  109. $status = $status == 1? 1 : 3;
  110. return (int)self::where($where)->where('status','>=', $status)->count('order_id');
  111. }
  112. /**
  113. * 统计今日新增报名数
  114. * @param int $status 状态:1-包含待支付,2-已支付
  115. * @param $schoolId
  116. * @return int
  117. */
  118. public static function getBooksByToday(int $status = 3, $schoolId=0)
  119. {
  120. $status = $status == 1? 1 : 3;
  121. return (int)self::where('status','>=', $status)
  122. ->where(function($query) use($schoolId){
  123. if($schoolId>0){
  124. $query->where('school_id', $schoolId);
  125. }
  126. })
  127. ->where('create_time','>=', strtotime(date('Y-m-d')))
  128. ->count('order_id');
  129. }
  130. }