DataGroup.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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' => '', 'supplierNo' => '']);
  14. $db = new DataGroupModel();
  15. $rs = $db
  16. ->alias('a')
  17. ->leftJoin('data_group_userid b', 'b.data_group_id=a.id AND b.is_del=' . DataGroupModel::$delete_正常)
  18. ->leftJoin('depart_user u', 'u.uid=b.uid AND u.is_del=0')
  19. ->group('a.id')
  20. ->where('a.is_del', DataGroupModel::$delete_正常);
  21. $val = Validate::rule([
  22. 'page|页码' => 'require|number|gt:0',
  23. 'size|每页数量' => 'require|number|gt:0|elt:100',
  24. 'status|状态' => 'number|in:' . DataGroupModel::$status_启用 . ',' . DataGroupModel::$status_禁用,
  25. 'title|用户组名称' => 'max:100',
  26. 'nickname|成员名称' => 'max:100',
  27. 'type|共享类型' => 'number|in:' . DataGroupModel::$type_可编辑 . ',' . DataGroupModel::$type_只读 . ',' . DataGroupModel::$type_不共享,
  28. ]);
  29. if (!$val->check($param)) return error_show(1005, $val->getError());
  30. if ($param['status'] != '') $rs->where('a.status', $param['status']);
  31. if ($param['title'] != '') $rs->whereLike('a.title', '%' . $param['title'] . '%');
  32. if ($param['nickname'] != '') {
  33. $a = $param['nickname'];
  34. $rs->where("a.id", "IN", function ($query) use ($a) {
  35. $query->name("data_group_userid")->alias("m")->leftJoin('depart_user f', 'f.uid=m.uid AND f.is_del=0')
  36. ->where('f.nickname', "like", "%$a%")->where("m.is_del", "=", 0)->field("m.data_group_id");
  37. });
  38. }
  39. if ($param['type'] != '') $rs->where('a.type', $param['type']);
  40. if ($param['supplierNo'] != '') $rs->where('a.supplierNo', '%' . $param['supplierNo'], '%');
  41. $count = $rs->count('a.id');
  42. $list = $rs
  43. ->field('a.*,GROUP_CONCAT(u.nickname) nickname')
  44. ->page($param['page'], $param['size'])
  45. ->order('a.id', 'desc')
  46. ->select()
  47. ->toArray();
  48. return app_show(0, '请求成功', ['count' => $count, 'list' => $list]);
  49. }
  50. //添加
  51. public function addDataGroup()
  52. {
  53. $param = $this->request->filter('trim')->only(['title', 'uids', 'type', 'remark' => '', 'supplierNo']);
  54. $val = Validate::rule([
  55. 'supplierNo|公司名称' => 'require|length:18',
  56. 'title|用户组名称' => 'require|max:100',
  57. 'uids|组成员' => 'require|array|max:100',
  58. 'type|共享类型' => 'require|number|in:' . DataGroupModel::$type_可编辑 . ',' . DataGroupModel::$type_只读 . ',' . DataGroupModel::$type_不共享,
  59. 'remark|备注' => 'max:100',
  60. ]);
  61. if (!$val->check($param)) return error_show(1005, $val->getError());
  62. Db::startTrans();
  63. try {
  64. //添加用户组
  65. $group_id = Db::name('data_group')->insertGetId([
  66. 'supplierNo' => $param['supplierNo'],
  67. 'title' => $param['title'],
  68. 'type' => $param['type'],
  69. 'remark' => $param['remark'],
  70. 'is_del' => DataGroupModel::$delete_正常,
  71. 'status' => DataGroupModel::$status_启用,
  72. 'createrid' => $this->uid,
  73. 'creater' => $this->uname,
  74. 'addtime' => date('Y-m-d H:i:s'),
  75. 'updatetime' => date('Y-m-d H:i:s'),
  76. ]);
  77. //用户组-用户集合
  78. $insert_data = [];
  79. foreach ($param['uids'] as $uids) {
  80. $insert_data[] = [
  81. 'data_group_id' => $group_id,
  82. 'uid' => $uids['uid'],
  83. 'is_del' => DataGroupModel::$delete_正常,
  84. 'status' => DataGroupModel::$status_启用,
  85. ];
  86. }
  87. if ($insert_data) Db::name('data_group_userid')->insertAll($insert_data);
  88. Db::commit();
  89. return app_show(0, '添加用户组成功');
  90. } catch (Exception $exception) {
  91. Db::rollback();
  92. return error_show(1005, '添加用户组失败');
  93. }
  94. }
  95. //获取详情
  96. public function getDataGroupDetail()
  97. {
  98. $id = $this->request->filter('trim')->post('id/d', 0);
  99. $info = DataGroupModel::field(true)
  100. ->where(['id' => $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,u.nickname,a.is_del')
  106. ->leftJoin('depart_user u', 'u.uid=a.uid AND u.is_del=0')
  107. ->where([
  108. 'a.is_del' => DataGroupModel::$delete_正常,
  109. // 'a.status' => DataGroupModel::$status_启用,
  110. 'a.data_group_id' => $data['id']
  111. ])
  112. ->select()
  113. ->toArray();
  114. })
  115. ->findOrEmpty()
  116. ->toArray();
  117. return app_show(0, '获取详情成功', $info);
  118. }
  119. //修改
  120. public function editDataGroup()
  121. {
  122. $param = $this->request->filter('trim')->only(['id', 'title', 'uids', 'type', 'remark']);
  123. $val = Validate::rule([
  124. 'id' => 'require|number|gt:0',
  125. 'title|用户组名称' => 'require|max:100',
  126. 'uids|组成员' => 'require|array|max:100',
  127. 'type|共享类型' => 'require|number|in:' . DataGroupModel::$type_可编辑 . ',' . DataGroupModel::$type_只读 . ',' . DataGroupModel::$type_不共享,
  128. 'remark|备注' => 'max:100',
  129. ]);
  130. if (!$val->check($param)) return error_show(1005, $val->getError());
  131. Db::startTrans();
  132. try {
  133. $res = DataGroupModel::field('id,status')
  134. ->where(['id' => $param['id'], 'is_del' => DataGroupModel::$delete_正常])
  135. ->findOrEmpty();
  136. if ($res->isEmpty()) throw new Exception('该记录不存在');
  137. $param['updatetime'] = date('Y-m-d H:i:s');
  138. DataGroupModel::where(['id' => $param['id'], 'is_del' => DataGroupModel::$delete_正常])->strict(false)->save($param);
  139. //用户组-用户集合
  140. $insert_data = $delete_data = [];
  141. foreach ($param['uids'] as $uid) {
  142. if (isset($uid['id']) && ((int)$uid['id']) != 0) {
  143. if ($uid['is_del'] == DataGroupModel::$delete_删除) $delete_data[] = $uid['id'];
  144. } else $insert_data[] = [
  145. 'data_group_id' => $param['id'],
  146. 'uid' => $uid['uid'],
  147. 'is_del' => DataGroupModel::$delete_正常,
  148. 'status' => $res->status,
  149. 'addtime' => date('Y-m-d H:i:s'),
  150. 'updatetime' => date('Y-m-d H:i:s'),
  151. ];
  152. }
  153. if ($delete_data) Db::name('data_group_userid')
  154. ->whereIn('id', $delete_data)
  155. ->where(['is_del' => DataGroupModel::$delete_正常, 'data_group_id' => $param['id']])
  156. ->update(['is_del' => DataGroupModel::$delete_删除, 'updatetime' => date('Y-m-d H:i:s')]);
  157. if ($insert_data) Db::name('data_group_userid')->insertAll($insert_data);
  158. Db::commit();
  159. return app_show(0, '修改用户组成功');
  160. } catch (Exception $exception) {
  161. Db::rollback();
  162. return error_show(1005, '修改用户组失败,' . $exception->getMessage());
  163. }
  164. }
  165. //删除
  166. public function deleteDataGroup()
  167. {
  168. $param = $this->request->filter('trim')->only(['id']);
  169. $val = Validate::rule(['id' => 'require|array|max:100']);
  170. if (!$val->check($param)) return error_show(1005, $val->getError());
  171. Db::startTrans();
  172. try {
  173. Db::name('data_group')
  174. ->whereIn('id', $param['id'])
  175. ->where(['is_del' => DataGroupModel::$delete_正常])
  176. ->update(['is_del' => DataGroupModel::$delete_删除, 'updatetime' => date('Y-m-d H:i:s')]);
  177. Db::name('data_group_userid')
  178. ->whereIn('data_group_id', $param['id'])
  179. ->where('is_del', DataGroupModel::$delete_正常)
  180. ->update(['is_del' => DataGroupModel::$delete_删除, 'updatetime' => date('Y-m-d H:i:s')]);
  181. Db::commit();
  182. return app_show(0, '删除用户组成功');
  183. } catch (Exception $exception) {
  184. Db::rollback();
  185. return error_show(1005, '删除用户组失败');
  186. }
  187. }
  188. //启/禁用用户组
  189. public function statusDataGroup()
  190. {
  191. $param = $this->request->filter('trim')->only(['id', 'status']);
  192. $val = Validate::rule([
  193. 'id' => 'require|array|max:100',
  194. 'status|状态' => 'require|number|in:' . DataGroupModel::$status_启用 . ',' . DataGroupModel::$status_禁用,
  195. ]);
  196. if (!$val->check($param)) return error_show(1005, $val->getError());
  197. $status = [0 => '禁用', 1 => '启用'];
  198. Db::startTrans();
  199. try {
  200. Db::name('data_group')
  201. ->where([
  202. ['id', 'in', $param['id']],
  203. ['is_del', '=', DataGroupModel::$delete_正常],
  204. ['status', '<>', $param['status']],
  205. ])->update(['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  206. Db::name('data_group_userid')
  207. ->where([
  208. ['data_group_id', 'in', $param['id']],
  209. ['is_del', '=', DataGroupModel::$delete_正常],
  210. ['status', '<>', $param['status']],
  211. ])->update(['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  212. Db::commit();
  213. return app_show(0, $status[$param['status']] . '用户组成功');
  214. } catch (Exception $exception) {
  215. Db::rollback();
  216. return error_show(1005, $status[$param['status']] . '用户组失败');
  217. }
  218. }
  219. //用户组列表(全部)
  220. public function getDataGroupListAll()
  221. {
  222. // $param = $this->request->filter('trim')->only(['status' => '', 'title' => '', 'type' => '']);
  223. $db = new DataGroupModel();
  224. // $rs = $db
  225. // ->alias('a')
  226. // ->leftJoin('data_group_userid b', 'b.data_group_id=a.id AND b.is_del=' . DataGroupModel::$delete_正常)
  227. // ->leftJoin('depart_user u', 'u.uid=b.uid AND u.is_del=0')
  228. // ->group('a.id')
  229. // ->where('a.is_del', DataGroupModel::$delete_正常);
  230. // $val = Validate::rule([
  231. // 'status|状态' => 'number|in:' . DataGroupModel::$status_启用 . ',' . DataGroupModel::$status_禁用,
  232. // 'title|用户组名称' => 'max:100',
  233. //// 'nickname|成员名称' => 'max:100',
  234. // 'type|共享类型' => 'number|in:' . DataGroupModel::$type_可编辑 . ',' . DataGroupModel::$type_只读 . ',' . DataGroupModel::$type_不共享,
  235. // ]);
  236. // if (!$val->check($param)) return error_show(1005, $val->getError());
  237. // if ($param['status'] != '') $rs->where('a.status', $param['status']);
  238. // if ($param['title'] != '') $rs->whereLike('a.title', '%' . $param['title'] . '%');
  239. // if ($param['nickname'] != '') $rs->whereLike('u.nickname', '%' . $param['nickname'] . '%');
  240. // if ($param['type'] != '') $rs->where('a.type', $param['type']);
  241. $list = $db
  242. ->where('is_del', DataGroupModel::$delete_正常)
  243. // ->field('a.*,GROUP_CONCAT(u.nickname) nickname')
  244. ->field('id,title,status')
  245. ->order('id', 'desc')
  246. ->select()
  247. ->toArray();
  248. return app_show(0, '请求成功', $list);
  249. }
  250. }