DataGroup.php 10 KB

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