SpecialityBook.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\common\model;
  13. use cores\BaseModel;
  14. use think\model\Collection;
  15. use think\model\relation\HasOne;
  16. use think\Paginator;
  17. /**
  18. * 学校专业模型类
  19. * Class SpecialityBook
  20. * @package app\common\model
  21. */
  22. class SpecialityBook extends BaseModel
  23. {
  24. // 定义表名
  25. protected $name = 'speciality_book';
  26. // 定义主键
  27. protected $pk = 'order_id';
  28. /**
  29. * 获取列表
  30. * @param array $param 查询条件
  31. * @param int $listRows 分页数量
  32. * @return mixed
  33. * @throws \think\db\exception\DbException
  34. */
  35. public function getList(array $param = [], int $listRows = 15)
  36. {
  37. // 筛选条件
  38. $query = $this->getQueryFilter($param);
  39. // 排序条件
  40. $sort = $this->setQuerySort($param);
  41. // 执行查询
  42. $list = $query->alias($this->name)
  43. ->order($sort)
  44. ->paginate($listRows);
  45. // 整理列表数据并返回
  46. return $list;
  47. }
  48. /**
  49. * 设置商品展示的数据
  50. * @param Collection|Paginator $list 商品列表
  51. * @param callable|null $callback 回调函数
  52. * @return mixed
  53. */
  54. protected function setListData($list, callable $callback = null)
  55. {
  56. if ($list->isEmpty()) return $list;
  57. // 遍历商品列表整理数据
  58. foreach ($list as &$item) {
  59. $data = $this->setData($item, $callback);
  60. }
  61. return $list;
  62. }
  63. /**
  64. * 整理数据
  65. * @param Collection|static $info
  66. * @param callable|null $callback
  67. * @return mixed
  68. */
  69. protected function setData($info, callable $callback = null)
  70. {
  71. // 回调函数
  72. is_callable($callback) && call_user_func($callback, $info);
  73. return $info->hidden(array_merge($this->hidden, ['transaction_id']));
  74. }
  75. /**
  76. * 检索查询条件
  77. * @param array $params
  78. * @return \think\db\BaseQuery
  79. */
  80. private function getQueryFilter(array $params)
  81. {
  82. $filter = [];
  83. // 实例化新查询对象
  84. $query = $this->getNewQuery();
  85. // 报名用户
  86. !empty($params['user_id']) && $filter[] = ['user_id', '=', "{$params['user_id']}"];
  87. // 专业学校
  88. !empty($params['school_id']) && $filter[] = ['school_id', '=', "{$params['school_id']}"];
  89. // 专业
  90. !empty($params['speciality_id']) && $filter[] = ['speciality_id', '=', "{$params['speciality_id']}"];
  91. // 状态
  92. !empty($params['status']) && $filter[] = ['status', '=', "{$params['status']}"];
  93. // 实例化新查询对象
  94. return $query->where($filter)->where(function($query) use ($params){
  95. // 关键词
  96. if(!empty($params['keyword'])){
  97. $query->where('order_no','like', "%{$params['keyword']}%");
  98. }
  99. });
  100. }
  101. /**
  102. * 检索排序条件
  103. * @param array $param
  104. * @return array|string[]
  105. */
  106. private function setQuerySort(array $param = [])
  107. {
  108. $params = $this->setQueryDefaultValue($param, [
  109. 'sortType' => 'all', // 排序类型
  110. 'create_time' => false, // 排序 (true高到低 false低到高)
  111. ]);
  112. // 排序规则
  113. $sort = [];
  114. if ($params['sortType'] === 'all') {
  115. $sort = ['create_time' => 'desc','pay_time'=>'desc'];
  116. } elseif ($params['sortType'] === 'pay') {
  117. $sort = ['pay_time' => 'desc'];
  118. }
  119. return array_merge($sort, [$this->getPk() => 'desc']);
  120. }
  121. /**
  122. * @return HasOne
  123. */
  124. public function speciality(): HasOne
  125. {
  126. return $this->HasOne('SchoolSpeciality','speciality_id','speciality_id')->bind(['speciality_name','speciality_logo']);
  127. }
  128. /**
  129. * @return HasOne
  130. */
  131. public function school(): HasOne
  132. {
  133. return $this->HasOne('School','id','school_id')->bind(['school_name','logo as school_logo']);
  134. }
  135. /**
  136. * @return HasOne
  137. */
  138. public function user(): HasOne
  139. {
  140. return $this->HasOne('User','user_id','user_id')
  141. ->with('avatar')
  142. ->field('user_id,gender,nick_name,preview_url as avatar_url')
  143. ->bind(['nick_name','avatar_url']);
  144. }
  145. /**
  146. * 批量更新订单
  147. * @param $orderIds
  148. * @param $data
  149. * @return bool|false
  150. */
  151. public function onBatchUpdate($orderIds, $data): bool
  152. {
  153. return static::updateBase($data, [['order_id', 'in', $orderIds]]);
  154. }
  155. }