BasicSchool.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace app\admin\controller\basic;
  3. /**
  4. * @title : 学校列表 管理控制器
  5. * @desc :
  6. * @Author : 系统开发
  7. * @Date : 2024-12-27 09:16:45
  8. * @icon fa fa-leaf
  9. */
  10. use app\admin\controller\Base;
  11. use app\common\model\base\org\OrgType;
  12. use app\common\model\base\OrgTypeClass;
  13. use app\common\model\basic\BasicSchool as BasicSchoolModel;
  14. use daorui\platform\platformAuth;
  15. class BasicSchool extends Base
  16. {
  17. protected $model = null;
  18. protected $noNeedLogin = ['getOptions'];
  19. protected $noNeedAuth = [];
  20. public function initialize()
  21. {
  22. parent::initialize();
  23. $this->model = new BasicSchoolModel;
  24. }
  25. /**
  26. * @title :创建统一查询条件
  27. * @desc : 描述
  28. * @param {char} {school_code} {} {学校类型}
  29. * @param {int} {area_id} {} {所属地区}
  30. * @param {string} {keyword} {} {搜索关键词,可搜索学校名称|学校logo}
  31. * @return {*}
  32. * @author : 系统开发
  33. * @method: POST
  34. * @Date : 2024-12-27 09:16:45
  35. */
  36. private function createWhere()
  37. {
  38. $data = $this->request->param();
  39. $where = [];
  40. $whereOr = [];
  41. if (!empty($data['school_code'])) {
  42. $where[] = ['school_code', '=', $data['school_code']];
  43. }
  44. if (!empty($data['area_id'])) {
  45. $where[] = ['area_id', '=', $data['area_id']];
  46. }
  47. if (!empty($data['keyword'])) {
  48. $keyword = $data['keyword'];
  49. $where[] = ['school_name|school_logo', 'LIKE', "%$keyword%"];
  50. }
  51. return ['where' => $where, 'whereOr' => $whereOr];
  52. }
  53. /**
  54. * @title :查询数据列表
  55. * @desc : 描述
  56. * @param {char} {school_code} {} {学校类型}
  57. * @param {int} {area_id} {} {所属地区}
  58. * @param {string} {keyword} {} {搜索关键词,可搜索学校名称|学校logo}
  59. * @param {int} {pageNo} {0} {页码}
  60. * @param {int} {pageSize} {10} {每页数量}
  61. * @return {*}
  62. * @author : 系统开发
  63. * @method: POST
  64. * @Date : 2024-12-27 09:16:45
  65. */
  66. public function getList(int $pageNo = 0, int $pageSize = 10)
  67. {
  68. $whereAry = $this->createWhere();
  69. if (!empty($pageNo)) {
  70. $res = $this->model
  71. ->where($whereAry['where'])
  72. ->where(function ($query) use ($whereAry) {
  73. $query->whereOr($whereAry['whereOr']);
  74. })
  75. ->with([])
  76. ->order(['sort_no' => 'DESC'])
  77. ->paginate(['page' => $pageNo, 'list_rows' => $pageSize]);
  78. return pageRes(1, '获取成功', $res->total(), $res->items());
  79. } else {
  80. $list = $this->model
  81. ->where($whereAry['where'])
  82. ->where(function ($query) use ($whereAry) {
  83. $query->whereOr($whereAry['whereOr']);
  84. })
  85. ->order(['sort_no' => 'DESC'])
  86. ->with([])
  87. ->select();
  88. return res(1, '获取成功', $list);
  89. }
  90. }
  91. /**
  92. * @title :选择器数据
  93. * @desc : 描述
  94. * @return {*}
  95. * @author : 系统开发
  96. * @method: POST
  97. * @Date : 2024-12-27 09:16:45
  98. */
  99. public function getTree($labelField = 'school_name')
  100. {
  101. $fields = ['id', $labelField];
  102. $where = [];
  103. $list = $this->model->where($where)->field($fields)->select();
  104. return res(1, "获取成功", $list);
  105. }
  106. /**
  107. * @title :查询信息
  108. * @desc : 描述
  109. * @param {int} {id} {} {信息ID}
  110. * @return {*}
  111. * @author : 系统开发
  112. * @method: POST
  113. * @Date : 2024-12-27 09:16:45
  114. */
  115. public function getInfo(int $id = 0)
  116. {
  117. $info = $this->model->with([])->find($id);
  118. return res(1, '获取成功', $info);
  119. }
  120. /**
  121. * @title :新增/编辑信息
  122. * @desc : 描述
  123. * @param {int} {id} {} {主键ID,不传或传0表示新增,大于0表示修改}
  124. * @param {varchar} {school_name} {} {学校名称}
  125. * @param {char} {school_code} {} {学校类型}
  126. * @param {int} {area_id} {} {所属地区}
  127. * @param {varchar} {school_logo} {} {学校logo}
  128. * @return {*}
  129. * @author : 系统开发
  130. * @method: POST
  131. * @Date : 2024-12-27 09:16:45
  132. */
  133. public function doEdit()
  134. {
  135. $data = $this->request->param();
  136. $res = $this->model->replace()->save($data);
  137. return res(1, '编辑成功');
  138. }
  139. /**
  140. * @title :删除信息
  141. * @desc : 描述
  142. * @param {int} id {} {信息ID}
  143. * @return {*}
  144. * @author : 系统开发
  145. * @method: POST
  146. * @Date : 2024-12-27 09:16:45
  147. */
  148. public function doDelete(int $id = 0)
  149. {
  150. $info = $this->model->find($id);
  151. $info->delete();
  152. return res(1, '删除成功');
  153. }
  154. /**
  155. * @title :获取可选项
  156. * @desc : 描述
  157. * @return {*}
  158. * @author : 系统开发
  159. * @method: POST
  160. * @Date : 2024-12-27 09:16:45
  161. */
  162. public function getOptions()
  163. {
  164. $codes = (new platformAuth())->interfaceRequest('getSchoolCode', []);
  165. $codes = array_column($codes['data'], 'code');
  166. $SchoolCodeList = [];
  167. if ($codes) {
  168. $SchoolCodeList = OrgType::where('code', 'IN', $codes)->column('org_type_name', 'code') ?? [];
  169. }
  170. $data = [
  171. 'SchoolCodeList' => $SchoolCodeList,
  172. ];
  173. return res(1, '获取成功', $data);
  174. }
  175. private $fieldList = [
  176. 'index' => '序号',
  177. 'school_name' => '学校名称',
  178. 'school_code' => '学校类型',
  179. 'area_id' => '所属地区',
  180. 'school_logo' => '学校logo',
  181. 'create_at' => '创建时间',
  182. 'update_at' => '更新时间',
  183. ];
  184. /**
  185. * @title :导出数据
  186. * @desc : 描述
  187. * @param {char} {school_code} {} {学校类型}
  188. * @param {int} {area_id} {} {所属地区}
  189. * @param {string} {keyword} {} {搜索关键词,可搜索学校名称|学校logo}
  190. * @return {*}
  191. * @author : 系统开发
  192. * @method: POST
  193. * @Date : 2024-12-27 09:16:45
  194. */
  195. public function doExport()
  196. {
  197. $whereAry = $this->createWhere();
  198. $list = $this->model->where($whereAry['where'])->where(function ($query) use ($whereAry) {
  199. $query->whereOr($whereAry['whereOr']);
  200. })->with([])->select()->toArray();
  201. $head = array_values($this->fieldList);
  202. $body = [];
  203. foreach ($list as $key => $item) {
  204. $index = $key + 1;
  205. foreach ($this->fieldList as $k => $v) {
  206. if ($k == 'index') {
  207. $body[$index][] = $index;
  208. } else {
  209. $body[$index][] = isset($item[$k . '_txt']) ? (is_array($item[$k . '_txt']) ? implode(',', $item[$k . '_txt']) : $item[$k . '_txt']) : $item[$k];
  210. }
  211. }
  212. }
  213. //创建文件夹
  214. $basepath = 'uploads' . DS . 'download' . DS . date('Ymd');
  215. $savepath = public_path() . $basepath;
  216. if (!is_dir($savepath)) {
  217. @mkdir($savepath, 0777, true);
  218. }
  219. //保存文件
  220. $filename = time() . GetRandStr() . '.xls';
  221. $path = $savepath . DS . $filename;
  222. $export_help = new \excel\MultiHeadExcel();
  223. $export_help->export_help($body, $head, $path);
  224. //返回路径
  225. $returnpath = WEBURL . DS . $basepath . DS . $filename;
  226. slog(1, '导出了学校列表');
  227. return res(1, '获取成功', ['url' => $returnpath, 'name' => $filename]);
  228. }
  229. public function __call($name, $arguments)
  230. {
  231. return res(2, "方法{$name}不存在");
  232. }
  233. }