123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- namespace app\sys\controller;
- use app\BaseController;
- use app\model\Employee;
- use app\model\Grant;
- use app\model\OperateLog;
- use think\facade\View;
- use app\model\EmployeeMsg;
- use app\model\EmployeeMsgSend;
- use think\facade\Request;
- use app\model\Org;
- class Msg extends BaseController
- {
- private $rootId;
- protected function initialize()
- {
- $this->rootId = $this->request->employee->root_id;
- }
- /**
- * 消息列表
- */
- public function index()
- {
- if (!$this->request->isAjax()) {
- return View::fetch();
- }
- $page = input('get.page',1);
- $limit = input('get.limit',10);
- $keyword = input('get.keyword');
- $where[] = ['type','=','system_msg_'.$this->rootId];
- if(!empty($keyword)){
- $where[] = ['msg', 'like', '%' . $keyword . '%'];
- }
- $data = EmployeeMsg::where($where)->group('index,msg,addtime')->order('addtime desc')->field('msg,addtime,index')->page($page,$limit)->select();
- $count = EmployeeMsg::field('index')->where($where)->group('index')->count();
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- /**
- * 添加消息
- */
- public function add_msg()
- {
- return View::fetch();
- }
- /**
- * 添加消息
- */
- public function add()
- {
- $day_start = date("Y-m-d 23:59:59", strtotime("-1 day"));
- $day_end = date("Y-m-d 00:00:00", strtotime("+1 day"));
- $times = EmployeeMsgSend::where([['root_id', '=', $this->rootId], ['addtime', 'between', [$day_start, $day_end]]])->count();
- if ($times > 2) {
- return json(['code' => 1, 'msg' => '每天最多发送三条通知']);
- }
- $msg = input('msg','');
- $where[] = ['root_id','=',$this->rootId];
- $where[] = ['state','=','在职'];
- $where[] = ['uid', '<>', 0];
- $eid = input('select','');
- $where[] = ['id','in',explode(',',$eid)];
- $eids = Employee::where($where)->column('id');
- $time = date('Y-m-d H:i:s',time());
- $group = (string)microtime(true).(string)mt_rand(100000,9999999);
- $saves = [];
- foreach ($eids as $v) {
- $save = [];
- $save['employee_id'] = $v;
- $save['msg'] = $msg;
- $save['type'] = 'system_msg_'.$this->rootId;
- $save['addtime'] = $time;
- $save['saw'] = 0;
- $save['index'] = $group;
- $saves[] = $save;
- }
- if ($saves) (new EmployeeMsg())->saveAll($saves);
- $msg_data['root_id'] = $this->rootId;
- $msg_data['content'] = $msg;
- EmployeeMsgSend::create($msg_data);
- return json(['code' => 0, 'msg' => '添加成功']);
- }
- /**
- * 消息阅读视图
- */
- public function read_list_view()
- {
- $id = input('id');
- View::assign('id', $id);
- return View::fetch();
- }
- /**
- * 消息阅读列表
- */
- public function read_list()
- {
- $page = input('get.page',1);
- $limit = input('get.limit',10);
- $index = input('index','');
- $where[] = ['type','=','system_msg_'.$this->rootId];
- $where[] = ['index','=',$index];
- $data = EmployeeMsg::with(['employee' => function($query) {
- $query->field('name,id');
- }])->where($where)->order('saw desc')->page($page,$limit)->select();
- $count = EmployeeMsg::where($where)->count();
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- /*
- * 发送对象
- */
- public function get_person()
- {
- $ids = input('id','');
- $root_id = request()->employee->root_id;
- $type = input('type',0);
- //树形
- if ($type==1) {
- //所有员工
- $w1[] = ['root_id','=',$root_id];
- $w1[] = ['state','=','在职'];
- $w1[] = ['org_id','>',0];
- $employee = Employee::where($w1)->field('id value,name,org_id')->select()->toArray();
- if ($ids) {
- $ids = explode(',',$ids);
- foreach ($employee as $k => &$v) {
- $v['selected'] = in_array($v['value'],$ids);
- }
- }else{
- foreach ($employee as $k => &$v) {
- $v['selected'] = false;
- }
- }
- $persons = [];
- foreach ($employee as $k => $v1) {
- $persons[$v1['org_id']][] = $v1;
- }
- $where = [
- ['path', 'like', $root_id . '-%'],
- ['status', '=', 1]
- ];
- $allnodes = Org::where($where)->field('id value,id,pid,name')->order('level asc, id asc')->select()->toArray();
- $tree = $this->tree($allnodes,0, $persons);
- return json($tree);
- }
-
-
- $w[] = ['root_id','=',$root_id];
- $w[] = ['state','=','在职'];
- $w[] = ['role','=','领导'];
- $employee = Employee::where($w)->field('id value,name')->select()->toArray();
- if ($ids) {
- $ids = explode(',',$ids);
- foreach ($employee as $k => &$v) {
- $v['selected'] = in_array($v['value'],$ids);
- }
- }else{
- foreach ($employee as $k => &$v) {
- $v['selected'] = false;
- }
- }
- return json($employee);
- }
- public function tree($data,$pid = 0, $persons)
- {
- $new_arr = [];
- foreach($data as $k => $v){
- if($v['pid'] == $pid) {
- $persions = isset($persons[$v['id']])?$persons[$v['id']]:[];
- $children = $this->tree($data, $v['id'], $persons);
- $v['children'] = array_merge_recursive($children,$persions);
- if (empty($v['children'])) $v['disabled']=true;
- $new_arr[] =$v;
- }
- }
- return $new_arr;
- }
- }
|