InspectionController.php 9.2 KB
<?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\InspectModel;
/**
 * @title 日检
 */
class InspectionController extends RestBaseController
{
    /**
     * @title 巡检列表
     * @description 接口说明
     * @author 开发者
     * @url /api/home/inspection/insList
     * @method GET
     *
     * @header name:token require:1 default: desc:header
     *
     * @param name:project_id type:int require:1 default: other desc:项目id
     *
     * @return id:项目id
     * @return project_name:项目名称
     * @return point: @
     * @point id:巡检点id point_name:巡检名称 status:巡检状态(0:已检查,1:故障,2:未完成)
     * @return is_finish: 未完成数量
     */
    public function insList(){
        if($this->request->isGet()){
            $project_id = $this->request->get('project_id');
            $rule = config('site.project_id');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['project_id'=>$project_id])) {
                $this->error($validate->getError());
            }

            $common = new CommonController();
            $arr = $common->getInsList($project_id);
            $this->success('成功',$arr);
        }else{
            $this->error('请求方式错误!');
        }
    }

    /**
     * @title 添加巡检页面
     * @description 接口说明
     * @author 开发者
     * @url /api/home/inspection/insAdd
     * @method GET
     *
     * @header name:token require:1 default: desc:header
     *
     * @param name:id type:int require:1 default: other desc:巡检点id
     * @return point_id:巡检点id
     * @return point_name:巡检点名称
     * @return project_id:项目id
     * @return project_name:项目名称
     */
    public function insAdd(){
        if($this->request->isGet()){
            $id = $this->request->get('id');
            $rule = config('site.ins_add');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['id'=>$id])) {
                $this->error($validate->getError());
            }
            //查询巡检名称
            $arr = Db::name('point')
                ->alias('po')
                ->join('project p','po.p_id = p.id','LEFT')
                ->where('po.id',$id)
                ->field('po.id point_id,po.point_name,p.id project_id,p.name project_name')
                ->find();
            $this->success('成功',$arr);
        }else{
            $this->error('请求方式错误!');
        }
    }

    /**
     * @title 添加巡检提交
     * @description 接口说明
     * @author 开发者
     * @url /api/home/inspection/insAddPost
     * @method POST
     *
     * @header name:token require:1 default: desc:header
     * @param name:point_id type:int require:1 default: other: desc:巡检点id
     * @param name:project_id type:int require:1 default: other: desc:项目id
     * @param name:status type:int require:1 default: other desc:巡检点状态(0:已检查,1:故障)
     * @param name:create_time type:int require:1 default: other desc:巡检时间
     * @param name:images type:array require:1 default: other desc:巡检图片(二维数组形式)
     *
     */
    public function insAddPost(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $rule = config('site.ins_post');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            $data['uid'] = $this->userId;
            //获取甲方,乙方
            $common = new CommonController();

            //处理图片+时间格式,序列化存入数据库中
            //绝对路径转相对路径
            $data['images'] = $common->relationUrl($data['images']);

            $party = $common->getUserIdentity();

            $where = [
                'project_id'=>$data['project_id'],
                'point_id'=>$data['point_id'],
                'party'=>$party['party']
            ];
            //查询是否已添加巡检
            $result = Db::name('inspect')
                ->where($where)
                ->whereTime('create_time','today')
                ->find();
            if($result){
                $this->error('您已提交过巡检点信息!');
            }

            $data['party'] = $party['party'];
            $inspectModel = new InspectModel();

//            $keys = ['images','file_time'];
//            $images = $common->array_merge_more($keys, $data['images'], $data['file_time']);

            $res = $inspectModel->create($data);
            if($res){
                $this->success('您的巡检点信息添加成功!');
            }else{
                $this->error('失败');
            }
        }else{
            $this->error('请求方式错误!');
        }
    }

    /**
     * @title 编辑巡检页面
     * @description 接口说明
     * @author 开发者
     * @url /api/home/inspection/insEdit
     * @method GET
     *
     * @header name:token require:1 default: desc:header
     *
     * @param name:id type:int require:1 default: other desc:巡检点id
     * @return point_id:巡检点id
     * @return point_name:巡检点名称
     * @return project_id:项目id
     * @return project_name:项目名称
     * @return status:巡检点状态(0:已检查,1:故障)
     * @return images:巡检图片@
     * @images image_url:图片路径 file_time:图片时间
     * @return create_time:巡检时间
     */
    public function insEdit(){
        if($this->request->isGet()){
            $id = $this->request->get('id');
            $rule = config('site.ins_add');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['id'=>$id])) {
                $this->error($validate->getError());
            }
            $common = new CommonController();
            $user = $common->getIdentity();
            //查询巡检名称
            $res = Db::table('xf_point')
                ->alias('po')
                ->join('xf_inspect','po.id = xf_inspect.point_id','LEFT')
                ->join('xf_project p','po.p_id = p.id','LEFT')
                ->whereTime('xf_inspect.create_time','today')
                ->where(['xf_inspect.party'=>$user['party'],'po.id'=>$id])
                ->field('po.id point_id,po.point_name,p.id project_id,p.name project_name,xf_inspect.status,xf_inspect.images,xf_inspect.create_time')
                ->find();
            //相对路径转绝对路径
            if($res['images']){
                $common = new CommonController();
                $res['images'] = $common->absolutionUrl($res['images']);
            }
            $this->success('成功',$res);
        }else{
            $this->error('请求方式错误!');
        }
    }

    /**
     * @title 编辑巡检提交
     * @description 接口说明
     * @author 开发者
     * @url /api/home/inspection/insEditPost
     * @method POST
     *
     * @header name:token require:1 default: desc:header
     * @param name:point_id type:int require:1 default: other: desc:巡检点id
     * @param name:project_id type:int require:1 default: other: desc:项目id
     * @param name:status type:int require:1 default: other desc:巡检点状态(0:已检查,1:故障)
     * @param name:create_time type:int require:1 default: other desc:巡检时间
     * @param name:images type:array require:1 default: other desc:巡检图片(二维数组形式)
     *
     */
    public function insEditPost(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $rule = config('site.ins_post');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            //绝对路径转相对路径
            $common = new CommonController();
            $images = $common->relationUrl($data['images']);
            $arr['images'] = $images;

            $arr['status'] = $data['status'];
            $arr['create_time'] = $data['create_time'];
            $inspectModel = new InspectModel();
            $time = date('Y-m-d',time());//今天时间戳
            $create_time = date('Y-m-d',$data['create_time']);//提交时间戳
            if($create_time != $time){
                $this->error('只能修改当天的巡检信息');
            }
            $user = $common->getIdentity();
            $inspectModel->where(['point_id'=>$data['point_id'],'party'=>$user['party']])->whereTime('create_time','today')->update($arr);
            $this->success('保存成功!');
        }else{
            $this->error('请求方式错误!');
        }
    }

}