NoticeCommon.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class NoticeCommon extends Base
  5. {
  6. public function updates(){
  7. $data = request()->post();
  8. $data['org_id'] = cur_org_id();
  9. $data['create_user_id'] = is_login();
  10. $data['create_time'] = date('Y-m-d H:i:s');
  11. $data['create_yyyymmdd'] = date('Ymd');
  12. $result = validate('NoticeCommon')->check($data,[],'');
  13. if(true !== $result){
  14. $this->error = validate('NoticeCommon')->getError();
  15. return false;
  16. }
  17. $user = $data['users'];
  18. $roles = $data['roles'];
  19. unset($data['users'], $data['roles']);
  20. switch ($data['group']) {
  21. case 1:
  22. $data['ids'] = implode(',', array_column($this->gerUserAll($data['org_id']), 'id'));
  23. $data['userIds']= $data['ids'];
  24. break;
  25. case 2:
  26. if (empty($user)) {
  27. $this->error = '请选择指定某人';
  28. return false;
  29. }
  30. $data['ids'] = $user;
  31. $data['userIds']= $data['ids'];
  32. break;
  33. case 3:
  34. if (empty($roles)) {
  35. $this->error = '请选择指定角色';
  36. return false;
  37. }
  38. $data['ids'] = $roles;
  39. $data['userIds']= implode(',', $this->getRolesUser($roles,$data['org_id']));
  40. break;
  41. }
  42. Db::startTrans();
  43. try {
  44. $userIds = $data['userIds'];
  45. unset( $data['userIds']);
  46. $id = $this->insertGetId($data);
  47. if (!$id) {
  48. exception('通知保存失败');
  49. }
  50. $ids = explode(',', $userIds);
  51. foreach ($ids as $k => $v) {
  52. $noticeData = [
  53. 'cate_id' => $data['cate_id'],
  54. 'create_user_id' => $data['create_user_id'],
  55. 'title' => $data['title'],
  56. 'content' => $data['content'],
  57. 'org_id' => $data['org_id'],
  58. 'user_id' => $v,
  59. 'create_time' => $data['create_time'],
  60. 'create_yyyymmdd' => $data['create_yyyymmdd'],
  61. 'common_id' => $id,
  62. ];
  63. $res = Db::name('notice')->insertGetId($noticeData);
  64. if (!$res) {
  65. exception('user_id:' . $v . ' 保存失败');
  66. }
  67. // $this->pushNotice($res);
  68. }
  69. Db::commit();
  70. return true;
  71. } catch (Exception $e) {
  72. $this->error = $e->getMessage();
  73. Db::rollback();
  74. return false;
  75. }
  76. }
  77. public function del($id){
  78. Db::startTrans();
  79. try {
  80. $data = [
  81. 'del'=>1,
  82. 'del_user_id'=>is_login(),
  83. 'del_time'=>getTime(),
  84. ];
  85. $res = db('notice_common')->where('id',$id)->update($data);
  86. if (!$res) {
  87. exception('删除失败');
  88. }
  89. $res = Db::name('notice')->where('common_id',$id)
  90. ->update([
  91. 'del'=>1,
  92. 'del_user_id'=>is_login(),
  93. 'del_time'=>date('Y-m-d H:i:s'),
  94. ]);
  95. if (!$res) {
  96. exception('用户通知删除失败');
  97. }
  98. Db::commit();
  99. return true;
  100. } catch (Exception $e) {
  101. $this->error = $e->getMessage();
  102. Db::rollback();
  103. return false;
  104. }
  105. }
  106. /*
  107. * 获取某组织下全部人员
  108. * @param $orgId
  109. */
  110. public function gerUserAll($orgId = 0){
  111. $user = Db::name('user')
  112. ->alias('u')
  113. ->field('u.id,u.real_name as title')
  114. ->join('user_org uo','u.id=uo.user_id')
  115. ->where('uo.org_id',$orgId)
  116. ->where('enable',1)
  117. ->where('del',0)
  118. ->select();
  119. return $user;
  120. }
  121. /**
  122. * 获取角色下所有用户
  123. *
  124. * @author wst
  125. * @date 2021/4/9 10:21
  126. * @param string|id $roles
  127. * @return array
  128. */
  129. public function getRolesUser($roles,$orgId=0) {
  130. if (strpos($roles, ',') !== false) {
  131. $where[] = ['rs.id', 'in', explode(',', $roles)];
  132. }
  133. else {
  134. $where[] = ['rs.id', '=', $roles];
  135. }
  136. $where[] = ['a.del', '=', 0];
  137. $where[] = ['a.enable', '=', 1];
  138. $where[] = ['org.org_id', '=', $orgId >0 ?$orgId:cur_org_id()];
  139. $userIds = Db::name('user')
  140. ->alias('a')
  141. ->join('user_roles r', 'a.id=r.user_id', 'left')
  142. ->join('roles rs', 'r.roles_id=rs.id', 'left')
  143. ->join('user_org org', 'org.user_id=a.id', 'left')
  144. ->where($where)
  145. ->column('a.id');
  146. return $userIds;
  147. }
  148. }