School.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, ['sortType'=>'time']);
  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. return $data;
  101. }
  102. /**
  103. * 编辑
  104. * @param array $data
  105. * @return bool
  106. * @throws \cores\exception\BaseException
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. */
  111. public function edit(array $data): bool
  112. {
  113. // 创建数据
  114. $data = $this->createData($data);
  115. // 事务处理
  116. $this->transaction(function () use ($data) {
  117. // 更新
  118. $this->save($data);
  119. });
  120. return true;
  121. }
  122. /**
  123. * 修改状态
  124. * @param array $ids id集
  125. * @param bool $state 为true表示审核通过
  126. * @return bool|false
  127. */
  128. public function setStatus(array $ids, bool $state): bool
  129. {
  130. // 批量更新记录
  131. return static::updateBase(['audit_status' => $state ? 1 : 0], [['id', 'in', $ids]]);
  132. }
  133. /**
  134. * 软删除
  135. * @param array $ids
  136. * @return bool
  137. */
  138. public function setDelete(array $ids): bool
  139. {
  140. // 批量更新记录
  141. return static::updateBase(['audit_status' => 0,'is_delete'=>1], [['id', 'in', $ids]]);
  142. }
  143. }