DepartmentController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace addons\BusinessCard\backend\controllers;
  3. use addons\BusinessCard\common\enums\CardEnums;
  4. use addons\BusinessCard\common\models\BusinessCardPosition;
  5. use addons\BusinessCard\common\models\BusinessCardUser;
  6. use common\enums\StatusEnum;
  7. use common\models\user\User;
  8. use Exception;
  9. use Yii;
  10. class DepartmentController extends BaseController
  11. {
  12. public $enableCsrfValidation = false;
  13. /**
  14. * 部门列表
  15. *
  16. * @author hpei
  17. * @return array|mixed
  18. */
  19. public function actionIndex() {
  20. if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
  21. $params = [
  22. 'where' => [['=', 'mall_id', $this->mall_id], ['=', 'status' , StatusEnum::ENABLED], ['=', 'parent_id', '0']],
  23. 'order' => 'sort',
  24. 'with' => ['departmentPosition'],
  25. 'limit' => 10,
  26. ];
  27. $list = BusinessCardPosition::listPage($params);
  28. return $this->success('操作成功', $list);
  29. }
  30. return $this->render('index');
  31. }
  32. /**
  33. * 编辑职位
  34. *
  35. * @author hpei
  36. * @return string
  37. */
  38. public function actionEditPosition() {
  39. $params = Yii::$app->request->post();
  40. $res = Yii::$app->services->validate->validate($params, [
  41. [['name'], 'required'],
  42. [['id', 'is_default_position'], 'integer'],
  43. [['is_default_position'], 'in', 'range' => [0, 1]],
  44. [['id', 'parent_id'], 'safe']
  45. ]);
  46. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  47. try {
  48. $params['mall_id'] = $this->mall_id;
  49. $res = BusinessCardPosition::setData($params);
  50. if (!$res) return $this->error(BusinessCardPosition::getStaticError());
  51. } catch (\Exception $e) {
  52. return $this->error($e->getMessage());
  53. }
  54. return $this->success('操作成功', ['id' => $res]);
  55. }
  56. /**
  57. * 编辑部门
  58. *
  59. * @author hpei
  60. * @return string
  61. */
  62. public function actionEditDepartment() {
  63. $params = Yii::$app->request->post();
  64. $res = Yii::$app->services->validate->validate($params, [
  65. [['name', 'position'], 'required'],
  66. [['sort'], 'integer'],
  67. [['id'], 'safe']
  68. ]);
  69. if (empty($params['position'])) {
  70. return $this->error("部门职位必须填写一个!");
  71. }
  72. $positions = array_unique(array_column($params['position'],'name'));
  73. if (count($params['position']) !== count($positions)) return $this->error("有相同职位,请重新填写!");
  74. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  75. $params['mall_id'] = $this->mall_id;
  76. $res = BusinessCardPosition::newSaveData($params);
  77. if (!$res) return $this->error(BusinessCardPosition::getStaticError());
  78. return $this->success('操作成功');
  79. }
  80. /**
  81. * 修改成员职位
  82. *
  83. * @author hpei
  84. * @return string
  85. */
  86. public function actionEditDepartmentMember() {
  87. $params = Yii::$app->request->post();
  88. $res = Yii::$app->services->validate->validate($params, [
  89. [['position_id', 'user_ids'], 'required'],
  90. [['position_id'], 'integer'],
  91. [['user_ids', 'department_id', 'role_type'], 'safe']
  92. ]);
  93. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  94. try {
  95. $data = ['mall_id' => $this->mall_id, 'position_id' => $params['position_id']];
  96. if (isset($params['department_id']) && !empty($params['department_id'])) $data['department_id'] = $params['department_id'];
  97. if (isset($params['role_type']) && !empty($params['role_type'])) $data['role_type'] = $params['role_type'];
  98. $res = BusinessCardUser::batchSaves(['user_id' => is_array($params['user_ids']) ? $params['user_ids'] : [$params['user_ids']]], $data);
  99. if (!$res) return $this->error(BusinessCardUser::getStaticError());
  100. } catch (Exception $e) {
  101. return $this->error($e->getMessage());
  102. }
  103. return $this->success('操作成功');
  104. }
  105. /**
  106. * 部门成员
  107. *
  108. * @author hpei
  109. * @return array|mixed
  110. */
  111. public function actionMember() {
  112. if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
  113. $get = Yii::$app->request->get();
  114. $where = [['=', 'mall_id', $this->mall_id], ['<>','status', StatusEnum::DELETE]];
  115. // 根据用户id/昵称/手机号查询
  116. if (isset($get['keyword']) && !empty($get['keyword'])) {
  117. // 获取用户的ids
  118. $uids = User::find()->select('id')->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  119. ->andWhere(['or',['id' => $get['keyword']], ['mobile' => $get['keyword']], ['nickname' => $get['keyword']]])
  120. ->asArray()->column();
  121. !empty($uids) && $where[] = ['in', 'user_id', $uids];
  122. }
  123. // 根据部门id查询
  124. if (isset($get['department_id']) && !empty($get['department_id'])) $where[] = ['=', 'department_id', $get['department_id']];
  125. if (isset($get['role']) && !empty($get['role'])) $where[] = ['=', 'role_type', $get['role']];
  126. $with = ['user' => function ($query) {// 查询用户信息
  127. $query->select('id,mobile,nickname,avatar_url');
  128. }, 'parent' => function ($query) {// 查询推荐人信息
  129. $query->select('id,mobile,nickname,avatar_url');
  130. }];
  131. $params = ['limit' => 10, 'where' => $where, 'order' => 'id desc', 'select' => ['id', 'status', 'user_id', 'parent_id', 'department_id', 'position_id', 'role_type'], 'with' => $with];
  132. $list = BusinessCardUser::listPage($params);
  133. // 获取角色,部门,职位列表
  134. $roleList = CardEnums::getRoleTypeMap();
  135. $departmentList = array_column(BusinessCardPosition::getDepartment($this->mall_id), null, 'id');
  136. $positionList = array_column(BusinessCardPosition::getPosition($this->mall_id, ['name', 'id', 'parent_id']), null, 'id');
  137. $list['roleList'] = $roleList;// 角色类型
  138. $list['departmentList'] = $departmentList;// 部门列表
  139. $list['positionList'] = $positionList;// 职位列表
  140. return $this->success('操作成功', $list);
  141. }
  142. return $this->render('member');
  143. }
  144. /**
  145. * 修改成员权限
  146. *
  147. * @author hpei
  148. * @param int user_id role_type
  149. * @return string
  150. */
  151. public function actionSetRole() {
  152. $params = Yii::$app->request->post();
  153. $res = Yii::$app->services->validate->validate($params, [
  154. [['user_id', 'role_type', 'id'], 'required'],
  155. [['user_id', 'role_type', 'id'], 'integer'],
  156. [['role_type'], 'in', 'range' => array_values(array_keys(CardEnums::getRoleTypeMap()))]
  157. ]);
  158. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  159. try {
  160. $data = ['user_id' => $params['user_id'], 'role_type' => $params['role_type'], 'id' => $params['id']];
  161. $res = BusinessCardUser::setData($data);
  162. if (!$res) return $this->error(BusinessCardUser::getStaticError());
  163. } catch (Exception $e) {
  164. return $this->error($e->getMessage());
  165. }
  166. return $this->success('操作成功');
  167. }
  168. public function actionDel(){
  169. $params = Yii::$app->request->post();
  170. $res = Yii::$app->services->validate->validate($params, [
  171. [['id'], 'required'],
  172. ]);
  173. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  174. try {
  175. BusinessCardPosition::del($this->mall_id,$params['id']);
  176. } catch (Exception $e) {
  177. return $this->error($e->getMessage());
  178. }
  179. return $this->success('操作成功');
  180. }
  181. public function actionChangeIsDefaultPosition()
  182. {
  183. $params = Yii::$app->request->post();
  184. $res = Yii::$app->services->validate->validate($params, [
  185. [['id', 'is_default_position'], 'required'],
  186. [['id', 'is_default_position'], 'integer'],
  187. [['is_default_position'], 'in', 'range' => [0, 1]],
  188. ]);
  189. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  190. try {
  191. $params['mall_id'] = $this->mall_id;
  192. BusinessCardPosition::changeIsDefaultPosition($params);
  193. } catch (\Exception $e) {
  194. return $this->error('操作失败');
  195. }
  196. return $this->success('操作成功');
  197. }
  198. }