School.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\store\model;
  13. use app\common\model\School as SchoolModel;
  14. /**
  15. * 模型类:学校
  16. * Class School
  17. * @package app\store\model
  18. */
  19. class School extends SchoolModel
  20. {
  21. /**
  22. * 获取列表
  23. * @param array $param 查询条件
  24. * @param int $listRows 分页数量
  25. * @return mixed|\think\model\Collection|\think\Paginator
  26. * @throws \think\db\exception\DbException
  27. */
  28. public function getList(array $param = [], int $listRows = 15)
  29. {
  30. // 整理查询参数
  31. $params = array_merge($param,[]);
  32. // 获取商品列表
  33. $list = parent::getList($params, $listRows);
  34. if ($list->isEmpty()) {
  35. return $list;
  36. }
  37. // 整理列表数据并返回
  38. return $this->setListDataFromApi($list);
  39. }
  40. /**
  41. * 设置展示的数据 api模块
  42. * @param $info
  43. * @return mixed
  44. */
  45. private function setListDataFromApi($info)
  46. {
  47. return $this->setListData($info, function ($data) {
  48. // 整理数据 api模块
  49. $this->setDataFromApi($data);
  50. // 隐藏冗余的字段
  51. $this->hidden(array_merge($this->hidden, ['recruit_phone','post_code','index_url','albums','introduce','recruitment','characteristic','book_fields']));
  52. });
  53. }
  54. /**
  55. * 整理数据 api模块
  56. * @param $info
  57. * @return mixed
  58. */
  59. private function setDataFromApi($info)
  60. {
  61. return $this->setData($info, function ($data) {
  62. // logo封面
  63. $data['logo_preview'] = $data['logo']? getPreview($data['logo']) : '';
  64. $data['labels'] = $data['labels']? explode(',', $data['labels']) : [];
  65. });
  66. }
  67. /**
  68. * 添加
  69. * @param array $data
  70. * @return bool
  71. * @throws \cores\exception\BaseException
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. public function add(array $data): bool
  77. {
  78. // 创建数据
  79. $data = $this->createData($data);
  80. // 事务处理
  81. $this->transaction(function () use ($data) {
  82. // 添加
  83. $this->save($data);
  84. });
  85. return true;
  86. }
  87. /**
  88. * 创建数据
  89. * @param array $data
  90. * @return array
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\DbException
  93. * @throws \think\db\exception\ModelNotFoundException
  94. * @throws \cores\exception\BaseException
  95. */
  96. private function createData(array $data): array
  97. {
  98. // 默认数据
  99. $data = array_merge($data, []);
  100. // 验证数据
  101. $validate = new \app\store\validate\schools\School();
  102. if(!$validate->check($data)){
  103. throwError($validate->getError());
  104. }
  105. $data = [
  106. ];
  107. return $data;
  108. }
  109. /**
  110. * 编辑
  111. * @param array $data
  112. * @return bool
  113. * @throws \cores\exception\BaseException
  114. * @throws \think\db\exception\DataNotFoundException
  115. * @throws \think\db\exception\DbException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. */
  118. public function edit(array $data): bool
  119. {
  120. // 创建数据
  121. $data = $this->createData($data);
  122. // 事务处理
  123. $this->transaction(function () use ($data) {
  124. // 更新
  125. $this->save($data);
  126. });
  127. return true;
  128. }
  129. /**
  130. * 修改状态
  131. * @param array $ids id集
  132. * @param bool $state 为true表示审核通过
  133. * @return bool|false
  134. */
  135. public function setStatus(array $ids, bool $state): bool
  136. {
  137. // 批量更新记录
  138. return static::updateBase(['audit_status' => $state ? 1 : 0], [['id', 'in', $ids]]);
  139. }
  140. /**
  141. * 软删除
  142. * @param array $ids
  143. * @return bool
  144. */
  145. public function setDelete(array $ids): bool
  146. {
  147. // 批量更新记录
  148. return static::updateBase(['audit_status' => 0,'is_delete'=>1], [['id', 'in', $ids]]);
  149. }
  150. }