123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace app\common\model;
- use think\Db;
- class NoticeCommon extends Base
- {
- public function updates(){
- $data = request()->post();
- $data['org_id'] = cur_org_id();
- $data['create_user_id'] = is_login();
- $data['create_time'] = date('Y-m-d H:i:s');
- $data['create_yyyymmdd'] = date('Ymd');
- $result = validate('NoticeCommon')->check($data,[],'');
- if(true !== $result){
- $this->error = validate('NoticeCommon')->getError();
- return false;
- }
- $user = $data['users'];
- $roles = $data['roles'];
- unset($data['users'], $data['roles']);
- switch ($data['group']) {
- case 1:
- $data['ids'] = implode(',', array_column($this->gerUserAll($data['org_id']), 'id'));
- $data['userIds']= $data['ids'];
- break;
- case 2:
- if (empty($user)) {
- $this->error = '请选择指定某人';
- return false;
- }
- $data['ids'] = $user;
- $data['userIds']= $data['ids'];
- break;
- case 3:
- if (empty($roles)) {
- $this->error = '请选择指定角色';
- return false;
- }
- $data['ids'] = $roles;
- $data['userIds']= implode(',', $this->getRolesUser($roles,$data['org_id']));
- break;
- }
- Db::startTrans();
- try {
- $userIds = $data['userIds'];
- unset( $data['userIds']);
- $id = $this->insertGetId($data);
- if (!$id) {
- exception('通知保存失败');
- }
- $ids = explode(',', $userIds);
- foreach ($ids as $k => $v) {
- $noticeData = [
- 'cate_id' => $data['cate_id'],
- 'create_user_id' => $data['create_user_id'],
- 'title' => $data['title'],
- 'content' => $data['content'],
- 'org_id' => $data['org_id'],
- 'user_id' => $v,
- 'create_time' => $data['create_time'],
- 'create_yyyymmdd' => $data['create_yyyymmdd'],
- 'common_id' => $id,
- ];
- $res = Db::name('notice')->insertGetId($noticeData);
- if (!$res) {
- exception('user_id:' . $v . ' 保存失败');
- }
- // $this->pushNotice($res);
- }
- Db::commit();
- return true;
- } catch (Exception $e) {
- $this->error = $e->getMessage();
- Db::rollback();
- return false;
- }
- }
- public function del($id){
- Db::startTrans();
- try {
- $data = [
- 'del'=>1,
- 'del_user_id'=>is_login(),
- 'del_time'=>getTime(),
- ];
- $res = db('notice_common')->where('id',$id)->update($data);
- if (!$res) {
- exception('删除失败');
- }
- $res = Db::name('notice')->where('common_id',$id)
- ->update([
- 'del'=>1,
- 'del_user_id'=>is_login(),
- 'del_time'=>date('Y-m-d H:i:s'),
- ]);
- if (!$res) {
- exception('用户通知删除失败');
- }
- Db::commit();
- return true;
- } catch (Exception $e) {
- $this->error = $e->getMessage();
- Db::rollback();
- return false;
- }
- }
- /*
- * 获取某组织下全部人员
- * @param $orgId
- */
- public function gerUserAll($orgId = 0){
- $user = Db::name('user')
- ->alias('u')
- ->field('u.id,u.real_name as title')
- ->join('user_org uo','u.id=uo.user_id')
- ->where('uo.org_id',$orgId)
- ->where('enable',1)
- ->where('del',0)
- ->select();
- return $user;
- }
- /**
- * 获取角色下所有用户
- *
- * @author wst
- * @date 2021/4/9 10:21
- * @param string|id $roles
- * @return array
- */
- public function getRolesUser($roles,$orgId=0) {
- if (strpos($roles, ',') !== false) {
- $where[] = ['rs.id', 'in', explode(',', $roles)];
- }
- else {
- $where[] = ['rs.id', '=', $roles];
- }
- $where[] = ['a.del', '=', 0];
- $where[] = ['a.enable', '=', 1];
- $where[] = ['org.org_id', '=', $orgId >0 ?$orgId:cur_org_id()];
- $userIds = Db::name('user')
- ->alias('a')
- ->join('user_roles r', 'a.id=r.user_id', 'left')
- ->join('roles rs', 'r.roles_id=rs.id', 'left')
- ->join('user_org org', 'org.user_id=a.id', 'left')
- ->where($where)
- ->column('a.id');
- return $userIds;
- }
- }
|