DataGroup.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace app\admin\controller;
  3. //用户组
  4. use app\admin\BaseController;
  5. use think\Exception;
  6. use think\facade\Db;
  7. use think\facade\Validate;
  8. use app\admin\model\DataGroup as DataGroupModel;
  9. class DataGroup extends BaseController
  10. {
  11. //用户组列表
  12. public function getDataGroupList()
  13. {
  14. $param = $this->request->filter('trim')->only(['page' => 1, 'size' => 10, 'status' => '', 'title' => '', 'nickname' => '', 'type' => '', 'token'], 'post');
  15. $db = new DataGroupModel();
  16. $val = Validate::rule([
  17. 'page|页码' => 'require|number|gt:0',
  18. 'size|每页数量' => 'require|number|gt:0|elt:100',
  19. 'status|状态' => 'number|in:' . $db::$status_启用 . ',' . $db::$status_禁用,
  20. 'title|用户组名称' => 'max:100',
  21. 'nickname|成员名称' => 'max:100',
  22. 'type|共享类型' => 'number|in:' . $db::$type_可编辑 . ',' . $db::$type_只读 . ',' . $db::$type_不共享,
  23. ]);
  24. if (!$val->check($param)) return error_show(1005, $val->getError());
  25. $rs = $db
  26. ->alias('a')
  27. ->leftJoin('data_group_userid b', 'b.data_group_id=a.id AND b.is_del=' . $db::$delete_正常)
  28. ->group('a.id')
  29. ->where('a.is_del', $db::$delete_正常);
  30. if ($param['status'] != '') $rs->where('a.status', $param['status']);
  31. if ($param['title'] != '') $rs->whereLike('a.title', '%' . $param['title'] . '%');
  32. if ($param['nickname'] != '') $rs->whereLike('u.nickname', '%' . $param['nickname'] . '%');
  33. if ($param['type'] != '') $rs->where('a.type', $param['type']);
  34. $count = $rs->count('a.id');
  35. $list = $rs
  36. ->field('a.*,GROUP_CONCAT(b.uid) uid,"" nickname')
  37. ->page($param['page'], $param['size'])
  38. ->order('a.id', 'desc')
  39. ->withAttr('nickname', function ($val, $da) use ($param) {
  40. //从外部获取符合条件的用户信息,并补充到列表中
  41. $temp = GetUserlist($param['token'], ['id' => explode(',', $da['uid'])]);
  42. if ($temp['code'] == 0) return implode(',', array_column($temp['data'], 'nickname'));
  43. else return '';
  44. })
  45. ->select()
  46. ->toArray();
  47. return app_show(0, '请求成功', ['count' => $count, 'list' => $list]);
  48. }
  49. //添加
  50. public function addDataGroup()
  51. {
  52. $param = $this->request->filter('trim')->only(['title', 'uids', 'type', 'remark' => ''], 'post');
  53. $val = Validate::rule([
  54. 'title|用户组名称' => 'require|max:100',
  55. 'uids|组成员' => 'require|array|max:100',
  56. 'type|共享类型' => 'require|number|in:' . DataGroupModel::$type_可编辑 . ',' . DataGroupModel::$type_只读 . ',' . DataGroupModel::$type_不共享,
  57. 'remark|备注' => 'max:100',
  58. ]);
  59. if (!$val->check($param)) return error_show(1005, $val->getError());
  60. Db::startTrans();
  61. try {
  62. //添加用户组
  63. $group_id = Db::name('data_group')->insertGetId([
  64. 'title' => $param['title'],
  65. 'type' => $param['type'],
  66. 'remark' => $param['remark'],
  67. 'is_del' => DataGroupModel::$delete_正常,
  68. 'status' => DataGroupModel::$status_启用,
  69. 'createrid' => $this->uid,
  70. 'creater' => $this->uname,
  71. ]);
  72. //用户组-用户集合
  73. $insert_data = [];
  74. foreach ($param['uids'] as $uids) {
  75. $insert_data[] = [
  76. 'data_group_id' => $group_id,
  77. 'uid' => $uids['uid'],
  78. 'is_del' => DataGroupModel::$delete_正常,
  79. 'status' => DataGroupModel::$status_启用,
  80. ];
  81. }
  82. if ($insert_data) Db::name('data_group_userid')->insertAll($insert_data);
  83. Db::commit();
  84. return app_show(0, '添加用户组成功');
  85. } catch (Exception $exception) {
  86. Db::rollback();
  87. return error_show(1005, '添加用户组失败');
  88. }
  89. }
  90. //获取详情
  91. public function getDataGroupDetail()
  92. {
  93. $param = $this->request->filter('trim')->only(['id', 'token'], 'post');
  94. $val = Validate::rule([
  95. 'id|ID' => 'require|number|gt:0',
  96. 'token' => 'require',
  97. ]);
  98. if (!$val->check($param)) return error_show(1005, $val->getError());
  99. $info = DataGroupModel::field(true)
  100. ->where(['id' => $param['id'], 'is_del' => DataGroupModel::$delete_正常])
  101. ->append(['user_list'])
  102. ->withAttr('user_list', function ($val, $data) {
  103. return Db::name('data_group_userid')
  104. ->alias('a')
  105. ->field('a.id,a.uid,a.is_del,"" nickname')
  106. ->where([
  107. 'a.is_del' => DataGroupModel::$delete_正常,
  108. 'a.status' => DataGroupModel::$status_启用,
  109. 'a.data_group_id' => $data['id']
  110. ])->select()
  111. ->toArray();
  112. })
  113. ->findOrEmpty()
  114. ->toArray();
  115. //从外部获取符合条件的用户信息,并补充到列表中
  116. $uids = array_column($info['user_list'], 'uid');
  117. if ($uids) {
  118. $temp = GetUserlist($param['token'], ['id' => $uids]);
  119. if ($temp['code'] == 0) {
  120. $nickname_uid = array_column($temp['data'], 'nickname', 'id');
  121. foreach ($info['user_list'] as &$value) {
  122. $value['nickname'] = $nickname_uid[$value['uid']] ?? '';
  123. }
  124. }
  125. }
  126. return app_show(0, '获取详情成功', $info);
  127. }
  128. //修改
  129. public function editDataGroup()
  130. {
  131. $param = $this->request->filter('trim')->only(['id', 'title', 'uids', 'type', 'remark'], 'post');
  132. $val = Validate::rule([
  133. 'id' => 'require|number|gt:0',
  134. 'title|用户组名称' => 'require|max:100',
  135. 'uids|组成员' => 'require|array|max:100',
  136. 'type|共享类型' => 'require|number|in:' . DataGroupModel::$type_可编辑 . ',' . DataGroupModel::$type_只读 . ',' . DataGroupModel::$type_不共享,
  137. 'remark|备注' => 'max:100',
  138. ]);
  139. if (!$val->check($param)) return error_show(1005, $val->getError());
  140. Db::startTrans();
  141. try {
  142. $res = DataGroupModel::field('id')
  143. ->where(['id' => $param['id'], 'is_del' => DataGroupModel::$delete_正常])
  144. ->findOrEmpty()
  145. ->isEmpty();
  146. if ($res) throw new Exception('该记录不存在');
  147. DataGroupModel::where(['id' => $param['id'], 'is_del' => DataGroupModel::$delete_正常])->strict(false)->save($param);
  148. //用户组-用户集合
  149. $insert_data = $delete_data = [];
  150. foreach ($param['uids'] as $uid) {
  151. if (isset($uid['id']) && ((int)$uid['id']) != 0) {
  152. if ($uid['is_del'] == DataGroupModel::$delete_删除) $delete_data[] = $uid['id'];
  153. } else $insert_data[] = [
  154. 'data_group_id' => $param['id'],
  155. 'uid' => $uid['uid'],
  156. 'is_del' => DataGroupModel::$delete_正常,
  157. 'status' => DataGroupModel::$status_启用,
  158. ];
  159. }
  160. if ($delete_data) Db::name('data_group_userid')
  161. ->whereIn('id', $delete_data)
  162. ->where(['is_del' => DataGroupModel::$delete_正常, 'data_group_id' => $param['id']])
  163. ->update(['is_del' => DataGroupModel::$delete_删除]);
  164. if ($insert_data) Db::name('data_group_userid')->insertAll($insert_data);
  165. Db::commit();
  166. return app_show(0, '修改用户组成功');
  167. } catch (Exception $exception) {
  168. Db::rollback();
  169. return error_show(1005, '修改用户组失败');
  170. }
  171. }
  172. //删除
  173. public function deleteDataGroup()
  174. {
  175. $param = $this->request->filter('trim')->only(['id'], 'post');
  176. $val = Validate::rule(['id' => 'require|array|max:100']);
  177. if (!$val->check($param)) return error_show(1005, $val->getError());
  178. Db::startTrans();
  179. try {
  180. Db::name('data_group')
  181. ->whereIn('id', $param['id'])
  182. ->where(['is_del' => DataGroupModel::$delete_正常])
  183. ->update(['is_del' => DataGroupModel::$delete_删除]);
  184. Db::name('data_group_userid')
  185. ->whereIn('data_group_id', $param['id'])
  186. ->where('is_del', DataGroupModel::$delete_正常)
  187. ->update(['is_del' => DataGroupModel::$delete_删除]);
  188. Db::commit();
  189. return app_show(0, '删除用户组成功');
  190. } catch (Exception $exception) {
  191. Db::rollback();
  192. return error_show(1005, '删除用户组失败');
  193. }
  194. }
  195. //启/禁用用户组
  196. public function statusDataGroup()
  197. {
  198. $param = $this->request->filter('trim')->only(['id', 'status'], 'post');
  199. $val = Validate::rule([
  200. 'id' => 'require|array|max:100',
  201. 'status|状态' => 'require|number|in:' . DataGroupModel::$status_启用 . ',' . DataGroupModel::$status_禁用,
  202. ]);
  203. if (!$val->check($param)) return error_show(1005, $val->getError());
  204. $status = [0 => '禁用', 1 => '启用'];
  205. Db::startTrans();
  206. try {
  207. Db::name('data_group')
  208. ->where([
  209. ['id', 'in', $param['id']],
  210. ['is_del', '=', DataGroupModel::$delete_正常],
  211. ['status', '<>', $param['status']],
  212. ])->update(['status' => $param['status']]);
  213. Db::name('data_group_userid')
  214. ->where([
  215. ['data_group_id', 'in', $param['id']],
  216. ['is_del', '=', DataGroupModel::$delete_正常],
  217. ['status', '<>', $param['status']],
  218. ])->update(['status' => $param['status']]);
  219. Db::commit();
  220. return app_show(0, $status[$param['status']] . '用户组成功');
  221. } catch (Exception $exception) {
  222. Db::rollback();
  223. return error_show(1005, $status[$param['status']] . '用户组成功');
  224. }
  225. }
  226. }