CommonController.php 17.0 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\home\controller;

use think\Db;
use think\Validate;
use cmf\controller\RestBaseController;
use app\portal\model\StepModel;
/**
 * @title 巡检管理
 */
class CommonController extends RestBaseController
{
    //获取首页用户身份信息
    public function getIndexData(){
        //查询用户所属公司
        $identity = $this->user['identity'];

        if($identity == 0){
            //员工
            //找到公司名称,甲方或者乙方企业
            $result = [];

            $where = ['u_s_id'=>['like','%,'.$this->userId.',%']];
            $field = 'id c_id,company_name,pid,logo';
            $res = $this->getCompany($where,$field);

            $result['is_work'] = $this->user['is_work'];
            if($res){
//                $result['uid'] = $this->userId;
                $result['company_name'] = $res['company_name'];
                $result['logo'] = $res['logo'];
                //查找企业拥有的服务
                $rule = $this->getRule($res);
                $result['service'] = $rule;

                if($res['pid'] == 0){
                    //甲方企业
                    $result['identity'] = config('site.a_staff');
                    $where = ['a_sid'=>['like','%,'.$this->userId.',%'],'a_cid'=>$res['c_id']];
                    $field = 'id,name,create_time';
                    $res1 = $this->getProject($where,$field);
                    $result['run_time'] = 0;
                    $result['is_project'] = 0;
                    if($res1){
                        $result['run_time'] = $this->getRunTime($res1['create_time']);
                        $result['is_project'] = 1;
                    }

//                $res['p_id'] = isset($res1['p_id']) && !empty($res1['p_id'])?$res1['p_id']:'';
                    //项目名称
                    $result['project_name'] = isset($res1['name']) && !empty($res1['name'])?$res1['name']:'';
//                $res['point'] = isset($res1['point']) && !empty($res1['point'])?$res1['point']:'';
                }else{
                    //乙方企业
                    $result['identity'] = config('site.b_staff');
                    $where = ['b_sid'=>['like','%,'.$this->userId.',%'],'b_cid'=>$res['c_id']];
                    $field = 'id,name,create_time';
                    $res1 = $this->getProject($where,$field);
                    //当前时间
                    $result['run_time'] = 0;

                    $result['is_project'] = 0;
                    if($res1){
                        $result['run_time'] = $this->getRunTime($res1['create_time']);
                        $result['is_project'] = 1;
                    }
//                $res['p_id'] = isset($res1['p_id']) && !empty($res1['p_id'])?$res1['p_id']:'';
                    $result['project_name'] = isset($res1['name']) && !empty($res1['name'])?$res1['name']:'';
//                $res['point'] = isset($res1['point']) && !empty($res1['point'])?$res1['point']:'';
                }
            }
        }else if($identity == 1){
            //领导
            $where = ['u_l_id' => $this->userId];
            $field = 'id c_id,company_name,pid,logo';
            $res = $this->getCompany($where,$field);
            $result['company_name'] = $res['company_name'];
            $result['logo'] = $res['logo'];
//            $res['uid'] = $this->userId;
            //甲方
            //查找企业拥有的服务
            $rule = $this->getRule($res);
            $result['service'] = $rule;
            foreach($result['service'] as &$value){
                $value['name'] = $value['name'].'报表';
            }
            if($res['pid'] == 0){
                $result['identity'] = config('site.a_leader');
                $where = ['a_cid'=>$res['c_id']];
                $field = 'id,name,create_time';
                $res1 = $this->getProject($where,$field);
            }else{
                //乙方
                $result['identity'] = config('site.b_leader');
                $where = ['b_cid'=>$res['c_id']];
                $field = 'id,name,create_time';
                $res1 = $this->getProject($where,$field);
            }

            $result['run_time'] = 0;

            //是否有项目
            $result['is_project'] = 0;
            if($res1){
                $result['run_time'] = $this->getRunTime($res1['create_time']);
                $result['is_project'] = 1;
            }
        }else{
            //总领导(待完善)
            $result['identity'] = config('site.a_leaders');
        }
        return $result;
    }

    //查找企业拥有的服务权限
    public function getRule($res){
        $rule = Db::name('service_rule')
            ->alias('r')
            ->join('service s','r.s_id = s.id','LEFT')
            ->where('r.c_id','like','%,'.$res['c_id'].',%')
            ->field('s.id,s.name,s.english_name,s.icon')
            ->select()
            ->toArray();
        return $rule;
    }

    //获取项目相关信息
    public function getProject($where,$field){
        $res = Db::name('project')
            ->where($where)
            ->field($field)
            ->order('id desc')
            ->find();
        return $res;
    }

    //根据乙方企业id获取项目所关联的月检相关信息
    public function getCheckByCompanyBId($b_company_id,$page){
        $limit = config('site.limit');
        $res = Db::name('project')
            ->alias('p')
            ->join('check c','p.id = c.project_id')
            ->where(['p.b_cid'=>$b_company_id,'c.status'=>0])
            ->field('c.id,p.id project_id,p.name project_name,c.ins_m_time')
            ->order('c.id desc')
            ->page($page,$limit)
            ->select()
            ->toArray();
        return $res;
    }

    //根据乙方企业id获取项目所关联的培训相关信息
    public function getTrainByCompanyBId($b_company_id,$page){
        $limit = config('site.limit');
        $res = Db::name('project')
            ->alias('p')
            ->join('train t','p.id = t.project_id')
            ->where(['p.b_cid'=>$b_company_id,'t.status'=>2])
            ->field('t.id,p.id project_id,p.name project_name,t.train_time')
            ->order('t.id desc')
            ->page($page,$limit)
            ->select()
            ->toArray();
        return $res;
    }

    //获取企业相关信息
    public function getCompany($where,$field){
        $res = Db::name('company')
            ->where($where)
            ->field($field)
            ->order('id desc')
            ->find();
        return $res;
    }

    //获取企业相关信息(总领导)
    public function getTotalCompany($where,$field){
        $res = Db::name('company')
            ->where($where)
            ->field($field)
            ->select()
            ->toArray();
        return $res;
    }

    //获取项目运行时间
    public function getRunTime($time){
        $currentTime = time();
        $cnt=$currentTime-strtotime(date('Y-m-d',$time));
        $days = floor($cnt/(3600*24))+1;//算出天数
        return $days;
    }

    //查询巡检点列表名称
    public function getPoint($where,$field){
        $res = Db::name('point')
            ->where($where)
            ->field($field)
            ->select()
            ->toArray();
        return $res;
    }

    //获取巡检列表
    public function getInsList(){
        $identity = $this->user['identity'];
        $result = [];
        if($identity == 0){
            //找到公司名称,甲方或者乙方企业
            $where = ['u_s_id'=>['like','%,'.$this->userId.',%']];
            $field = 'id c_id,company_name,pid';
            $res = $this->getCompany($where,$field);
            if($res){
                if($res['pid'] == 0){
                    //甲方企业
                    $where = ['a_sid'=>['like','%,'.$this->userId.',%'],'a_cid'=>$res['c_id']];
                    $field = 'id,name project_name,point';
                    $result = $this->getProject($where,$field);
                    //获取该项目的巡检点
                    $where = ['p_id'=>$result['id']];
                    $field = 'id,point_name';
                    $arr = $this->getPoint($where,$field);
                    $party = 0;
                }else{
                    //乙方企业
                    $where = ['b_sid'=>['like','%,'.$this->userId.',%'],'b_cid'=>$res['c_id']];
                    $field = 'id,name project_name,point';
                    $result = $this->getProject($where,$field);
                    $where = ['p_id'=>$result['id']];
                    $field = 'id,point_name';
                    $arr = $this->getPoint($where,$field);
                    $party = 1;
                }
                //获取今天的巡检信息(共同合作完成)
                $where = ['project_id'=>$result['id'],'party'=>$party];
                $field = 'status,point_id';
                $todayIns = $this->getTodayIns($where,$field);
                $is_finish = 0;
                foreach ($arr as &$value){
                    $value['status'] = 2;
                    foreach ($todayIns as $ins_value){
                        if($value['id'] == $ins_value['point_id']){
                            $value['status'] = $ins_value['status'];
                        }
                    }
                    if($value['status'] == 2){
                        $is_finish ++;
                    }
                }
                $result['point'] = $arr;
                $result['is_finish'] = $is_finish;
            }
        }
        return $result;
    }

    //获取今天的巡检信息
    public function getTodayIns($where,$field){
        $res = Db::name('inspect')
            ->where($where)
            ->whereTime('create_time','today')
            ->field($field)
            ->select()
            ->toArray();
        return $res;
    }

    //根据uid查询用户
    public function getUser($where){
        $res = Db::name('user')
            ->where($where)
            ->field('id,user_login,identity')
            ->find();
        $res['other'] = '';
        if(isset($res['identity']) && !empty($res['identity'])){
            //获取用户身份,所属企业,项目
            $other = $this->getUserIdentity($res['identity']);
            $res['other'] = $other;
        }
        return $res;
    }

    //查看用户是甲方乙方(party 0:甲方,1:乙方)用户信息,企业信息,项目信息
    public function getUserIdentity($user_identity=''){
        $identity = empty($user_identity)?$this->user['identity']:$user_identity;
        $result = [];
        $result['project_id'] = '';
        $result['company_id'] = '';
        if($identity == 0){
            //找到公司名称,甲方或者乙方企业
            $where = ['u_s_id'=>['like','%,'.$this->userId.',%']];
            $field = 'id c_id,company_name,pid,u_l_id';
            $res = $this->getCompany($where,$field);
            if($res){
                if($res['pid'] == 0){
                    //甲方企业
                    $result['identity'] = config('site.a_staff');
                    $where = ['a_sid'=>['like','%,'.$this->userId.',%'],'a_cid'=>$res['c_id']];
                    $field = 'id,name project_name';
                    $res1 = $this->getProject($where,$field);
                    $result['party'] = 0;
                }else{
                    //乙方企业
                    $result['identity'] = config('site.b_staff');
                    $where = ['b_sid'=>['like','%,'.$this->userId.',%'],'b_cid'=>$res['c_id']];
                    $field = 'id,name project_name';
                    $res1 = $this->getProject($where,$field);
                    $result['party'] = 1;
                }
                //企业
                $result['company_id'] = $res['c_id'];
                $result['company_name'] = $res['company_name'];

                //查找所属领导id,姓名
                $leader = $this->getLeader(['id'=>$res['u_l_id']]);
                $result['leader'] = $leader['user_login'];

                //查询所属项目
                if($res1){
                    $result['project_id'] = $res1['id'];
                    $result['project_name'] = $res1['project_name'];
                }else{
                    $result['project_id'] = 0;
                    $result['project_name'] = '';
                }
            }
        }else if($identity == 1){
            //领导
            $where = ['u_l_id' => $this->userId];
            $field = 'id c_id,company_name,pid';
            $res = $this->getCompany($where,$field);
            //甲方
            if($res['pid'] == 0){
                $result['identity'] = config('site.a_leader');
                $result['party'] = 0;
                //获取甲方领导管理的项目
//                $res1 = $this->getProjectById(['a_cid'=>$res['c_id']],'id,name project_name');
            }else{
                //乙方
                $result['identity'] = config('site.b_leader');
                $result['party'] = 1;
                //获取乙方领导管理的项目
//                $res1 = $this->getProjectById(['b_cid'=>$res['c_id']],'id,name project_name');
            }
            //企业
            $result['company_id'] = $res['c_id'];
            $result['company_name'] = $res['company_name'];

            //查询所属项目
//            if($res1){
//                $result['project'] = $res1;
//            }else{
//                $result['project'] = $res1;
//            }
        }else{
            $result['identity'] = config('site.a_leaders');
            $result['party'] = 0;
        }
        return $result;
    }

    //获取领导姓名
    public function getLeader($where){
        $leader = Db::name('user')
            ->where($where)
            ->field('id,user_login,identity')
            ->find();
        return $leader;
    }

    //报修步骤
    public function repairStep($r_id,$step_name){
        $stepModel = new StepModel();
        $data['r_id'] = $r_id;
        $data['step_name'] = $step_name;
        $data['create_time'] = time();
        $stepModel->create($data);
    }

    //根据项目id获取乙方项目组人员
    public function getUserByProjectId($project_id,$field){
        $users = Db::name('project')
            ->where('id',$project_id)
            ->field($field)
            ->find();
        $b_sid = explode(',',trim($users['b_sid'],','));
        $user_login = Db::name('user')
            ->whereIn('id',$b_sid)
            ->where('user_status',1)
            ->field('id,user_login')
            ->select()
            ->toArray();
        $users = trim(implode(array_column($user_login,'user_login'),'/'),'/');
        return $users;
    }

    //根据项目id获取甲方项目组人员
    public function getUserByProject($project_id,$field){
        $users = Db::name('project')
            ->where('id',$project_id)
            ->field($field)
            ->find();
        $a_sid = explode(',',trim($users['a_sid'],','));
        $user_login = Db::name('user')
            ->whereIn('id',$a_sid)
            ->where('user_status',1)
            ->field('id,user_login')
            ->select()
            ->toArray();
        return $user_login;
    }

    //查询是否已有乙方提交维修单流程
    public function getStep($id){
        $res = Db::name('step')
            ->where('r_id',$id)
            ->field('id,step_name')
            ->order('id desc')
            ->find();
        $step_name = config('site.a_step')[3];
        $stepModel = new StepModel();
        if($res['step_name'] == $step_name){
            //如果有,更新流程日期
            $stepModel->where('id',$res['id'])->update(['create_time'=>time()]);
        }else{
            //如果没有,创建流程
            $this->repairStep($id,$step_name);
        }
    }

    //获取月检列表
    public function getCheckList($page){
        //获取登录人所在的项目
        $limit = config('site.limit');
        $user = $this->getUserIdentity();
        $res = Db::name('check')
            ->alias('c')
            ->join('user u','c.uid = u.id','LEFT')
            ->where(['c.project_id'=>$user['project_id'],'u.user_status'=>1])
            ->field('c.id,u.user_login,c.status,c.ins_m_time')
            ->page($page,$limit)
            ->select()
            ->toArray();
        foreach($res as &$value){
            $value['leader'] = $user['leader'];
            $value['project_name'] = $user['project_name'];
        }
        return $res;
    }

    //根据id查询月检信息
    public function getCheckById($where,$field){
        $res = Db::name('check')
            ->where($where)
            ->field($field)
            ->find();
        return $res;
    }

}