DataShare.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\Db;
  4. use think\facade\Validate;
  5. use app\admin\model\DataShare as DataShareModel;
  6. use app\admin\model\DataGroup as DataGroupModel;
  7. //数据共享规则
  8. class DataShare extends Base
  9. {
  10. //列表
  11. public function getDataShareList()
  12. {
  13. $param = $this->request->filter('trim')->only(['page' => 1, 'size' => 10, 'source_nickname' => '', 'source_group_title' => '', 'nickname' => '', 'group_title' => '', 'type' => '']);
  14. $db = new DataShareModel();
  15. $rs = $db
  16. ->alias('a')
  17. ->leftJoin('depart_user b', 'b.uid=a.source_uid AND b.is_del=0')
  18. ->leftJoin('data_group c', 'c.id=a.source_data_group_id AND c.is_del=' . DataGroupModel::$delete_正常)
  19. ->leftJoin('depart_user d', 'd.uid=a.uid AND d.is_del=0')
  20. ->leftJoin('data_group e', 'e.id=a.data_group_id AND e.is_del=' . DataGroupModel::$delete_正常)
  21. ->where('a.is_del', $db::$delete_正常);
  22. $val = Validate::rule([
  23. 'page|页码' => 'require|number|gt:0',
  24. 'size|每页数量' => 'require|number|gt:0|elt:100',
  25. 'status|状态' => 'number|in:' . $db::$status_启用 . ',' . $db::$status_禁用,
  26. 'source_nickname|数据源用户' => 'max:100',
  27. 'source_group_title|数据源用户组' => 'max:100',
  28. 'nickname|共享用户' => 'max:100',
  29. 'type|共享权限' => 'number|in:' . $db::$type_只读 . ',' . $db::$type_读写,
  30. ]);
  31. if (!$val->check($param)) return error_show(1005, $val->getError());
  32. if ($param['source_nickname'] != '') $rs->whereLike('b.nickname', '%' . $param['source_nickname'] . '%');
  33. if ($param['source_group_title'] != '') $rs->whereLike('c.title', '%' . $param['source_group_title'] . '%');
  34. if ($param['nickname'] != '') $rs->whereLike('d.nickname', '%' . $param['nickname'] . '%');
  35. if ($param['group_title'] != '') $rs->whereLike('e.title', '%' . $param['group_title'] . '%');
  36. if ($param['type'] != '') $rs->whereLike('a.type', $param['type']);
  37. $count = $rs->count('a.id');
  38. $list = $rs
  39. ->field('a.*,b.nickname source_nickname,c.title source_group_title,d.nickname,e.title group_title')
  40. ->append(['menu_list'])
  41. ->withAttr('menu_list', function ($val, $data) {
  42. return Db::name('admin_menu')
  43. ->field('id,menu_name')
  44. ->whereIn('id', $data['menu_ids'])
  45. ->where('is_del', 0)
  46. ->select()
  47. ->toArray();
  48. })
  49. ->page($param['page'], $param['size'])
  50. ->order('a.id', 'desc')
  51. ->select()
  52. ->toArray();
  53. return app_show(0, '请求成功', ['count' => $count, 'list' => $list]);
  54. }
  55. //新建
  56. public function addDataShare()
  57. {
  58. $param = $this->request->filter('trim')->only(['title' => '', 'menu_ids', 'source_uid', 'source_data_group_id', 'uid', 'data_group_id', 'type', 'remark' => '']);
  59. $val = Validate::rule([
  60. 'title|共享规则名称' => 'max:100',
  61. 'menu_ids|菜单数据' => 'require|array|max:100',
  62. 'source_uid|数据源用户' => 'requireWithout:source_data_group_id|number|gt:0',
  63. 'source_data_group_id|数据源用户组' => 'requireWithout:source_uid|number|gt:0',
  64. 'uid|数据共享用户' => 'requireWithout:data_group_id|number|gt:0',
  65. 'data_group_id|数据共享用户组' => 'requireWithout:uid|number|gt:0',
  66. 'type|数据权限' => 'require|number|in:' . DataShareModel::$type_只读 . ',' . DataShareModel::$type_读写,
  67. 'remark|备注' => 'max:100',
  68. ]);
  69. if (!$val->check($param)) return error_show(1005, $val->getError());
  70. $rs = DataShareModel::create([
  71. 'title' => $param['title'],
  72. 'menu_ids' => implode(',', $param['menu_ids']),
  73. 'source_uid' => $param['source_uid'],
  74. 'source_data_group_id' => $param['source_data_group_id'],
  75. 'uid' => $param['uid'],
  76. 'data_group_id' => $param['data_group_id'],
  77. 'type' => $param['type'],
  78. 'remark' => $param['remark'],
  79. 'is_del' => DataShareModel::$delete_正常,
  80. 'status' => DataShareModel::$status_启用,
  81. 'createrid' => $this->uid,
  82. 'creater' => $this->uname,
  83. 'addtime' => date('Y-m-d H:i:s'),
  84. 'updatetime' => date('Y-m-d H:i:s'),
  85. ])->save();
  86. return $rs ? app_show(0, '新建共享规则成功') : error_show(1004, '新建共享规则失败');
  87. }
  88. //获取详情
  89. public function getDataShareDetail()
  90. {
  91. $id = $this->request->filter('trim')->post('id/d', 0);
  92. $info = DataShareModel::field('a.*,b.nickname source_nickname,c.title source_group_title,d.nickname,e.title group_title')
  93. ->alias('a')
  94. ->leftJoin('depart_user b', 'b.uid=a.source_uid AND b.is_del=0')
  95. ->leftJoin('data_group c', 'c.id=a.source_data_group_id AND c.is_del=' . DataGroupModel::$delete_正常)
  96. ->leftJoin('depart_user d', 'd.uid=a.uid AND d.is_del=0')
  97. ->leftJoin('data_group e', 'e.id=a.data_group_id AND e.is_del=' . DataGroupModel::$delete_正常)
  98. ->where(['a.id' => $id, 'a.is_del' => DataShareModel::$delete_正常])
  99. ->append(['menu_list'])
  100. ->withAttr('menu_list', function ($val, $data) {
  101. return Db::name('admin_menu')
  102. ->field('id,menu_name')
  103. ->whereIn('id', $data['menu_ids'])
  104. ->where('is_del', 0)
  105. ->select()
  106. ->toArray();
  107. })
  108. ->findOrEmpty()
  109. ->toArray();
  110. return app_show(0, '获取详情成功', $info);
  111. }
  112. //修改
  113. public function editDataShare()
  114. {
  115. $param = $this->request->filter('trim')->only(['id', 'menu_ids', 'source_uid', 'source_data_group_id', 'uid', 'data_group_id', 'type', 'remark']);
  116. $val = Validate::rule([
  117. 'id' => 'require|number|gt:0',
  118. 'title|共享规则名称' => 'max:100',
  119. 'menu_ids|菜单数据' => 'require|array|max:100',
  120. 'source_uid|数据源用户' => 'requireWithout:source_data_group_id|number|gt:0',
  121. 'source_data_group_id|数据源用户组' => 'requireWithout:source_uid|number|gt:0',
  122. 'uid|数据共享用户' => 'requireWithout:data_group_id|number|gt:0',
  123. 'data_group_id|数据共享用户组' => 'requireWithout:uid|number|gt:0',
  124. 'type|数据权限' => 'require|number|in:' . DataShareModel::$type_只读 . ',' . DataShareModel::$type_读写,
  125. 'remark|备注' => 'max:100',
  126. ]);
  127. if (!$val->check($param)) return error_show(1005, $val->getError());
  128. $res = DataShareModel::field('id')
  129. ->where(['id' => $param['id'], 'is_del' => DataShareModel::$delete_正常])
  130. ->findOrEmpty()
  131. ->isEmpty();
  132. if ($res) return error_show(1005, '该记录不存在');
  133. if (isset($param['menu_ids']) && is_array($param['menu_ids'])) $param['menu_ids'] = implode(',', $param['menu_ids']);
  134. $param['updatetime'] = date('Y-m-d H:i:s');
  135. $rs = DataShareModel::where(['id' => $param['id'], 'is_del' => DataShareModel::$delete_正常])->strict(false)->save($param);
  136. return $rs ? app_show(0, '修改共享规则成功') : error_show(1005, '修改共享规则失败');
  137. }
  138. //删除
  139. public function deleteDataShare()
  140. {
  141. $param = $this->request->filter('trim')->only(['id']);
  142. $val = Validate::rule(['id' => 'require|array|max:100']);
  143. if (!$val->check($param)) return error_show(1005, $val->getError());
  144. $rs = DataShareModel::whereIn('id', $param['id'])
  145. ->where(['is_del' => DataShareModel::$delete_正常])
  146. ->update(['is_del' => DataShareModel::$delete_删除, 'updatetime' => date('Y-m-d H:i:s')]);
  147. return $rs ? app_show(0, '删除共享规则成功') : error_show(1005, '删除共享规则失败');
  148. }
  149. //启/禁用
  150. public function statusDataShare()
  151. {
  152. $param = $this->request->filter('trim')->only(['id', 'status']);
  153. $val = Validate::rule([
  154. 'id' => 'require|array|max:100',
  155. 'status|状态' => 'require|number|in:' . DataShareModel::$status_启用 . ',' . DataShareModel::$status_禁用,
  156. ]);
  157. if (!$val->check($param)) return error_show(1005, $val->getError());
  158. $status = [0 => '禁用', 1 => '启用'];
  159. $rs = DataShareModel::where([
  160. ['id', 'in', $param['id']],
  161. ['is_del', '=', DataShareModel::$delete_正常],
  162. ['status', '<>', $param['status']],
  163. ])->update(['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')]);
  164. return $rs ? app_show(0, $status[$param['status']] . '共享规则成功') : error_show(1005, $status[$param['status']] . '共享规则失败');
  165. }
  166. }