TrainController.php 17.8 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
<?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 app\portal\model\ConfirmImageModel;
use think\Db;
use think\Validate;
use cmf\controller\RestBaseController;
use app\portal\model\TrainModel;
/**
 * @title 培训
 */
class TrainController extends RestBaseController
{

    /**
     * @title 甲方申请培训列表
     * @description 接口说明
     * @author 开发者
     * @url /api/home/train/trainList
     * @method GET
     *
     * @header name:token require:1 default: desc:header
     *
     * @param name:page type:inter require:1 default: other desc:分页页码
     * @param name:project_id type:inter require:1 default: other desc:项目id
     * @param name:status type:inter require:1 default: other desc:切换状态(-1:全部,0:待确认,1:进行中,2:已完成)
     * @return data:培训列表@
     * @data id:培训id uid:甲方提交人id status:培训状态(0:待乙方确认,1:进行中,2:已完成 ) user_login:甲方发起人 b_staff:乙方确认员工 project_name:项目名称 train_time:培训时间 address:培训地点 confirm:是否有确认按钮(0:无,1:有) finish:是否有确认完成按钮(0:无,1:有)
     * @return page:当前页数
     * @return total_page:总页数
     */
    public function trainList(){
        if($this->request->isGet()){
            $page = $this->request->get('page');
            $status = $this->request->get('status');//切换状态(-1:全部,0:待确认,1:进行中,2:已完成)
            $project_id = $this->request->get('project_id');

            $limit = config('site.limit');
            $rule = config('site.pages_list');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['page'=>$page,'status'=>$status,'project_id'=>$project_id])) {
                $this->error($validate->getError());
            }

            $common = new CommonController();
            if($status == -1){
                $where = ['project_id' => $project_id];
            }else{
                $where = ['project_id' => $project_id, 'status' => $status];
            }

            $res = Db::name('train')
                ->where($where)
                ->page($page,$limit)
                ->field('id,uid,status,h_uid,train_time,address,confirm_uid')
                ->order('id desc')
                ->select()
                ->toArray();
            //项目名称
            $project_name = $common->getProjectName($project_id);

            $total_page = $common->getCountPage('train',$where);
            foreach($res as &$value){
                //查看甲方提交人用户信息
                $user = $common->getUserById(['id'=>$value['uid']]);
                $value['user_login'] = isset($user['user_login'])&&!empty($user['user_login'])?$user['user_login']:'';
                //是否有确认按钮
                $value['confirm'] = 0;
                if($value['status'] == 0 && $value['h_uid'] == $this->userId){
                    $value['confirm'] = 1;
                }
                //是否有确认完成按钮
                $value['finish'] = 0;
                if($value['status'] == 1){
                    if($value['h_uid'] == $this->userId && empty($value['confirm_uid'])){
                        $value['finish'] = 1;
                    }else{
                        $u_ids = $common->getUnFinishUid(['type'=>0,'c_id'=>$value['id'],'is_image'=>1]);
                        $u_ids = array_values(array_column($u_ids,'uid'));
                        if(in_array($this->userId,$u_ids)){
                            $value['finish'] = 1;
                        }else{
                            $value['finish'] = 0;
                        }
                    }
                }
                //查看乙方确认员工
                $staff = $common->getUserById(['id'=>$value['h_uid']]);
                $value['b_staff'] = isset($staff['user_login'])&&!empty($staff['user_login'])?$staff['user_login']:'';
                $value['project_name'] = $project_name['project_name'];
            }
            $this->success('成功',['data'=>$res,'page'=>intval($page),'total_page'=>$total_page]);
        }else{
            $this->error('请求方式错误!');
        }
    }

    /**
     * @title 甲方提交培训申请
     * @description 接口说明
     * @author 开发者
     * @url /api/home/train/applyTrain
     * @method POST
     *
     * @header name:token require:1 default: desc:header
     *
     * @param name:title type:string require:1 default: other desc:表头信息
     * @param name:train_time type:int require:1 default: other desc:培训时间
     * @param name:project_id type:int require:1 default: other desc:项目id
     * @param name:address type:string require:1 default: other desc:培训地点
     * @param name:remark type:string require:1 default: other desc:培训备注
     * @param name:u_id type:inter require:1 default: other desc:指派乙方员工id
     */
    public function applyTrain(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $common = new CommonController();
            $user = $common->getUserIdentity();
            //如果是乙方,则没有权限操作
            if($user['party'] == 1){
                $this->error('无权操作');
            }
            $rule = config('site.train_post');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $data['uid'] = $this->userId;
            $data['create_time'] = time();
            $data['number'] = $common->genOrderSn();

            //指派乙方人员uid
            $data['h_uid'] = $data['u_id'];
            unset($data['u_id']);

            $trainModel = new TrainModel();
            $res = $trainModel->create($data);
            if($res){
                //极光推送
                //甲方发起,提醒指派乙方员工
                $user_ids = [$data['h_uid']];
                $content = config('site.train_content_add_a');

                $registration_id = $common->getPushUsers($user_ids);
                if($registration_id){
                    $common = new CommonController();
                    $title = config('site.title');
                    $common->pushMessage($registration_id,$content,$title);
                }

                //记录参与人员id
                $join_uid['project_id'] = $data['project_id'];
                $join_uid['create_time'] = $data['create_time'];
                $join_uid['service_id'] = $res->id;
                $common->createInsUid($join_uid,5);

                $this->success('培训信息提交成功');
            }else{
                $this->error('失败');
            }
        }else{
            $this->error('请求方式错误!');
        }
    }

    /**
     * @title 乙方报修确认
     * @description 接口说明
     * @author 开发者
     * @url /api/home/train/confirmB
     * @method GET
     *
     * @header name:token require:1 default: desc:header
     *
     * @param name:id type:inter require:1 default: other desc:培训id
     */
    public function confirmB(){
        if($this->request->isGet()){
            $id = $this->request->get('id');//培训列表id

            $rule = config('site.data');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['id'=>$id])) {
                $this->error($validate->getError());
            }
            $common = new CommonController();
            $user = $common->getIdentity();
            //如果是甲方,则没有权限操作
            if($user['party'] == 0){
                $this->error('无权操作');
            }
            $trainModel = new TrainModel();
            $res = $trainModel->where(['id'=>$id,'status'=>0])->update(['status'=>1,'b_confirm_time'=>time()]);
            if($res){
                //极光推送,推送发起甲方员工
                $user_ids = $common->getLaunchUid('train',$id);
                $user_ids = explode(',',trim($user_ids['uid'],','));
                $content = config('site.train_content_confirm_a');

                $registration_id = $common->getPushUsers($user_ids);
                if($registration_id){
                    $common = new CommonController();
                    $title = config('site.title');
                    $common->pushMessage($registration_id,$content,$title);
                }

                $this->success('成功!');
            }else{
                $this->error('失败');
            }
        }else{
            $this->error('请求方式错误!');
        }
    }

    /**
     * @title 甲乙方员工完善培训单详情
     * @description 接口说明
     * @author 开发者
     * @url /api/home/train/improveTrainDetail
     * @method GET
     *
     * @header name:token require:1 default: desc:header
     * @param name:id type:inter require:1 default: other desc:列表id
     *
     * @return id:培训列表id
     * @return title:培训表头信息
     * @return train_time:培训时间
     * @return address:培训地点
     * @return images:完善图片@
     * @images image_url:图片路径 file_time:图片时间
     * @return remark:培训备注
     * @return number:培训单号
     */
    public function improveTrainDetail(){
        if($this->request->isGet()){
            $id = $this->request->get('id');
            $rule = config('site.data');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['id'=>$id])) {
                $this->error($validate->getError());
            }
            //如果不是甲乙方员工,则没有权限操作
            $common = new CommonController();
            $user = $common->getUserIdentity();
            if($user['identity'] != config('site.a_staff') && $user['identity'] != config('site.b_staff')){
                $this->error('无权操作');
            }
            $field = 'id,train_time,title,address,images,number,project_id';
            $res = $common->getTrainById(['id'=>$id,'status'=>1],$field);
            if($res){
                //查询培训演习项目
                $project = $common->getProjectName($res['project_id']);
                $res['project_name'] = $project['project_name'];

                if($res['images']){
                    $res['images'] = $common->absolutionUrl($res['images']);
                }else{
                    $res['images'] = [];
                }
            }
            $this->success('成功',$res);
        }else{
            $this->error('请求方式错误!');
        }
    }

    /**
     * @title 甲乙方员工完善培训单
     * @description 接口说明
     * @author 开发者
     * @url /api/home/train/improveTrainPost
     * @method POST
     *
     * @header name:token require:1 default: desc:header
     * @param name:id type:inter require:1 default: other desc:列表id
     * @param name:images type:array require:1 default: other desc:完善图片(二维数组形式)
     */
    public function improveTrainPost(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $rule = config('site.improve_check_post');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $common = new CommonController();
            $user = $common->getIdentity();
            //如果不是甲乙方员工,则无权操作
            if($user['identity'] != config('site.a_staff') && $user['identity'] != config('site.b_staff')){
                $this->error('无权操作');
            }
            $trainModel = new TrainModel();
            $images = $common->relationUrl($data['images']);
            $res = $trainModel->where(['id'=>$data['id'],'status'=>1])->update(['images'=>$images]);
            if($res){
                //记录参与人员id
                $project = $common->getLaunchUid('train',$data['id']);
                $join_uid['project_id'] = $project['project_id'];
                $join_uid['create_time'] = time();
                $join_uid['service_id'] = $data['id'];
                $common->createInsUid($join_uid,5);

                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }else{
            $this->error('请求方式错误!');
        }
    }

    /**
     * @title 乙方员工完成培训单
     * @description 接口说明
     * @author 开发者
     * @url /api/home/train/finishTrainB
     * @method POST
     *
     * @header name:token require:1 default: desc:header
     * @param name:id type:inter require:1 default: other desc:列表id
     * @param name:confirm_images type:file require:1 default: other desc:验收照片(二维数组形式)
     * @param name:u_ids type:string require:1 default: other desc:甲方被培训人员字符串逗号拼接("2,4,5,8")
     */
    public function finishTrainB(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $rule = config('site.check_train');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            $common = new CommonController();
            $user = $common->getIdentity();
            //如果不是乙方员工,则无权操作
            if($user['identity'] != config('site.b_staff')){
                $this->error('无权操作');
            }
            $trainModel = new TrainModel();
            $confirm_images = $common->relationUrl($data['confirm_images']);
            $arr = [
                'confirm_images'=>$confirm_images,
                'confirm_uid' => $this->userId
            ];
            $res = $trainModel->where(['id'=>$data['id'],'status'=>1])->update($arr);
            if($res){
                //记录参与人员id
                $project = $common->getLaunchUid('train',$data['id']);
                $join_uid['project_id'] = $project['project_id'];
                $join_uid['create_time'] = time();
                $join_uid['service_id'] = $data['id'];
                $common->createInsUid($join_uid,5);

                //指派甲方人员
                $u_ids = explode(',',$data['u_ids']);
                $user1['c_id'] = $data['id'];
                $user1['create_time'] = time();
                $confirmModel = new ConfirmImageModel();
                foreach ($u_ids as $value) {
                    $user1['uid'] = $value;
                    $confirmModel->create($user1);
                }
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }else{
            $this->error('请求方式错误!');
        }
    }

    /**
     * @title 甲方员工完成培训单
     * @description 接口说明
     * @author 开发者
     * @url /api/home/train/finishTrainA
     * @method POST
     *
     * @header name:token require:1 default: desc:header
     * @param name:id type:inter require:1 default: other desc:列表id
     * @param name:confirm_images type:file require:1 default: other desc:验收照片(二维数组形式)
     */
    public function finishTrainA(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $rule = config('site.check_train_a');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            $common = new CommonController();
            $user = $common->getIdentity();
            //如果不是甲方员工,则无权操作
            if($user['identity'] != config('site.a_staff')){
                $this->error('无权操作');
            }
            $confirm_images = $common->relationUrl($data['confirm_images']);
            $arr = [
                'confirm_images'=>$confirm_images,
                'is_image' => 0,
            ];
            $confirmModel = new ConfirmImageModel();
            $res = $confirmModel->where(['c_id'=>$data['id'],'type'=>0,'uid'=>$this->userId])->update($arr);
            if($res){
                //记录参与人员id
                $project = $common->getLaunchUid('train',$data['id']);
                $join_uid['project_id'] = $project['project_id'];
                $join_uid['create_time'] = time();
                $join_uid['service_id'] = $data['id'];
                $common->createInsUid($join_uid,5);

                //查询培训单 甲方员工是否全部完成
                //已完成
                $finish_count = $common->getFinishCount(['type'=>0,'c_id'=>$data['id'],'is_image'=>0]);
                //所有
                $count = $common->getFinishCount(['type'=>0,'c_id'=>$data['id']]);
                if($finish_count == $count){
                    //全部甲方员已完成,更新状态已完成
                    $trainModel = new TrainModel();
                    $trainModel->where(['id'=>$data['id'],'status'=>1])->update(['status'=>2,'finish_time'=>time()]);

                    //推送系统指定人员

                }

                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }else{
            $this->error('请求方式错误!');
        }
    }

}