SystemController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace frontend\modules\api\controllers;
  3. use common\models\BaseRole;
  4. use common\models\Department;
  5. use common\models\Job;
  6. use frontend\modules\api\components\BaseAdminController;
  7. use yii\web\Response;
  8. class SystemController extends BaseAdminController
  9. {
  10. /**
  11. * 部门列表
  12. * @param int $current
  13. * @param int $page
  14. * @param bool $call
  15. * @return Response
  16. */
  17. public function actionDepartmentList(int $current = 1, int $page = 10, bool $call = false)
  18. {
  19. //如果是调用
  20. if ($call) {
  21. $query = Department::find()
  22. ->select("id,department_name")
  23. ->where(['department_status' => 1])
  24. ->orderBy("id desc");
  25. $data['data'] = $query->asArray()->all();
  26. return $this->asJson($data);
  27. }
  28. //公共部分
  29. $query = Department::find()
  30. ->limit($page)
  31. ->offset(($current - 1) * $page);
  32. //排序
  33. $this->initSequence($query, "id desc", "sequenceDepartment");
  34. //主要筛选条件
  35. $query = $this->addConditionToQuery("department_name", $query, true);
  36. $query = $this->addConditionToQuery("department_status", $query);
  37. $data['data'] = $query->asArray()->all();
  38. $data['total'] = $query->count();
  39. return $this->asJson($data);
  40. }
  41. /**
  42. * 部门添加
  43. * @param null $department_name
  44. * @param null $department_status
  45. * @return Response
  46. */
  47. public function actionDepartmentAdd($department_name = null, $department_status = null)
  48. {
  49. // 基础校验
  50. $info = Department::findOne(['department_name' => $department_name]);
  51. if ($info) {
  52. return $this->asJson([], 1, "该部门:{$department_name} 已存在!"); //通知前端
  53. }
  54. $time = strtotime('now');
  55. //插入
  56. $params = [
  57. 'department_name' => $department_name,
  58. 'department_status' => $department_status,
  59. 'create_time' => date("Y-m-d H:i:s", $time),
  60. ];
  61. $insertInfo = new Department($params);
  62. $insertInfo->save();
  63. //返回
  64. return $this->asJson();
  65. }
  66. /**
  67. * 部门修改
  68. * @param null $department_name
  69. * @param null $department_status
  70. * @return Response
  71. */
  72. public function actionDepartmentUpdate($id, $department_name = null, $department_status = null)
  73. {
  74. // 基础校验
  75. $info = Department::findOne(['id' => $id]);
  76. if (!$info) {
  77. return $this->asJson([], 1, "没有这条记录!"); //通知前端
  78. }
  79. $ifUpdateName = false;
  80. if ($info->department_name !== $department_name) {
  81. //意味着没有变更名称
  82. $ifUpdateName = true;
  83. }
  84. $infoName = Department::find()
  85. ->where(['department_name' => $department_name])
  86. ->andWhere("id <> $id")
  87. ->one();
  88. if ($infoName) {
  89. return $this->asJson([], 1, "该部门:{$department_name} 已存在!"); //通知前端
  90. }
  91. //修改
  92. if ($ifUpdateName) {
  93. $info->department_name = $department_name;
  94. }
  95. $info->department_status = $department_status;
  96. $info->save();
  97. //返回
  98. return $this->asJson();
  99. }
  100. /**
  101. * 岗位列表
  102. * @param int $current
  103. * @param int $page
  104. * @param bool $call
  105. * @return Response
  106. */
  107. public function actionJobList(int $current = 1, int $page = 10, bool $call = false)
  108. {
  109. //如果是调用
  110. if ($call) {
  111. $query = Job::find()
  112. ->select("id,job_name")
  113. ->where(['job_status' => 1])
  114. ->orderBy("id desc");
  115. $data['data'] = $query->asArray()->all();
  116. return $this->asJson($data);
  117. }
  118. //公共部分
  119. $query = Job::find()
  120. ->limit($page)
  121. ->offset(($current - 1) * $page);
  122. //排序
  123. $this->initSequence($query, "id desc", "sequenceJob");
  124. //主要筛选条件
  125. $query = $this->addConditionToQuery("job_name", $query, true);
  126. $query = $this->addConditionToQuery("job_status", $query);
  127. $data['data'] = $query->asArray()->all();
  128. $data['total'] = $query->count();
  129. return $this->asJson($data);
  130. }
  131. /**
  132. * 岗位添加
  133. * @param null $job_name
  134. * @param null $job_status
  135. * @return Response
  136. */
  137. public function actionJobAdd($job_name = null, $job_status = null)
  138. {
  139. // 基础校验
  140. $info = Job::findOne(['job_name' => $job_name]);
  141. if ($info) {
  142. return $this->asJson([], 1, "该岗位:{$job_name} 已存在!"); //通知前端
  143. }
  144. $time = strtotime('now');
  145. //插入
  146. $params = [
  147. 'job_name' => $job_name,
  148. 'job_status' => $job_status,
  149. 'create_time' => date("Y-m-d H:i:s", $time),
  150. ];
  151. $insertInfo = new Job($params);
  152. $insertInfo->save();
  153. //返回
  154. return $this->asJson();
  155. }
  156. /**
  157. * 岗位修改
  158. * @param null $job_name
  159. * @param null $job_status
  160. * @return Response
  161. */
  162. public function actionJobUpdate($id, $job_name = null, $job_status = null)
  163. {
  164. // 基础校验
  165. $info = Job::findOne(['id' => $id]);
  166. if (!$info) {
  167. return $this->asJson([], 1, "没有这条记录!"); //通知前端
  168. }
  169. $ifUpdateName = false;
  170. if ($info->job_name !== $job_name) {
  171. //意味着没有变更名称
  172. $ifUpdateName = true;
  173. }
  174. $infoName = Job::find()
  175. ->where(['job_name' => $job_name])
  176. ->andWhere("id <> $id")
  177. ->one();
  178. if ($infoName) {
  179. return $this->asJson([], 1, "该部门:{$job_name} 已存在!"); //通知前端
  180. }
  181. //修改
  182. if ($ifUpdateName) {
  183. $info->job_name = $job_name;
  184. }
  185. $info->job_status = $job_status;
  186. $info->save();
  187. //返回
  188. return $this->asJson();
  189. }
  190. /**
  191. * 类型列表
  192. * @return Response
  193. */
  194. public function actionRolesList()
  195. {
  196. $query = BaseRole::find()
  197. ->select("id,name as role_name")
  198. ->where("id > 1")
  199. ->orderBy("id desc");
  200. $data['data'] = $query->asArray()->all();
  201. return $this->asJson($data);
  202. }
  203. }