Route.php 4.7 KB
<?php

namespace app\api\controller\inspection;

use app\admin\model\inspection\Plan;
use app\common\controller\Api;
use think\Db;

/**
 * 巡检任务接口
 */
class Route extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    public $staffInfo;

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $staffModel = new \app\admin\model\inspection\Staff();
        $staffInfo = $staffModel->where(['user_id' => $this->auth->id])->find();
        if (empty($staffInfo)) {
            $this->error('当前员工账号异常');
        }

        $this->staffInfo = $staffInfo;
    }

    // public function routeList($ids=null)
    // {
    //     $list = Db::name('inspection_area_site')
    //         ->alias('as')
    //         ->join('')
    // }
    /**
     * 路线列表
     */
    public function index()
    {
        $page = $this->request->param("page");
        $limit = $this->request->param("limit");

        $planModel = new Plan();
        $total = $planModel
            ->alias("plan")
            ->join("inspection_route route", "plan.route_id=route.id")
            ->where(['plan.staff_id' => $this->staffInfo['id'], 'plan.begintime' => ['elt', time()], 'plan.endtime' => [['gt', time()], ['EXP', Db::raw('is null')], 'or']])
            ->count();

        $planModel = new Plan();
        $data = collection($planModel
            ->alias("plan")
            ->field("plan.id,plan.num,plan.circle_type,plan.circle_value,route.route_name,plan.begintime,plan.endtime")
            ->join("inspection_route route", "plan.route_id=route.id")
            ->where(['plan.staff_id' => $this->staffInfo['id'], 'plan.begintime' => ['elt', time()], 'plan.endtime' => [['gt', time()], ['EXP', Db::raw('is null')], 'or']])
            ->order('plan.endtime')
            ->page($page, $limit)
            ->select())->toArray();

        $week = ["1" => "一", "2" => "二", "3" => "三", "4" => "四", "5" => "五", "6" => "六", "7" => "日"];
        foreach ($data as &$v) {
            $w=[];
            $w["plan_id"] = ["=", $v['id']];
            $w["begintime"] = ["<", time()];
            $w["endtime"] = [">", time()];
            $v['today_need_xj'] = Db::name("inspection_project")->where($w)->count();//今日需要完成巡检总数

            $w["state"] = ["neq", 0];
            $v['today_has_xj'] = Db::name("inspection_project")->where($w)->count();//今日已经巡检总数

            $v['begintime_text'] = date("Y-m-d", $v['begintime']);
            $v['endtime_text'] = $v['endtime'] ? date("Y-m-d", $v['endtime']) : "结束待通知";
            //进行中
            $state = Db::name('inspection_project')->where('plan_id', $v['id'])->where('state', 0)->find();
            if ($state) {
                $v['state'] = 0; //未巡检完毕
            } else {
                $v['state'] = 1; //巡检完毕
            }
            switch ($v['circle_type']) {
                case "1":
                    $v['circle_type_text'] = "每日执行";
                    $v['sign'] = "每日" . $v['num'] . "次";
                    break;
                case "2":
//                    dump($v['circle_value']);die;
                    if ($v['circle_value']) {
                        $circle_value = explode(",", $v['circle_value']);
                        $week_arr = [];
                        foreach ($circle_value as &$v1) {
                            $week_arr[] = $week[$v1];
                        }
                    }
                    $week_value = $week_arr ? implode("、", $week_arr) : "";
                    $v['circle_type_text'] = "每周" . $week_value . "执行";
                    $v['sign'] = "每周" . $v['num'] . "次";
                    break;
                case "3":
                    $v['circle_type_text'] = "每月" . $v['circle_value'] . "号执行";
                    $v['sign'] = "每月" . $v['num'] . "次";
                    break;
            }

        }

        $this->success('请求成功', ['total' => $total, 'data' => $data]);
    }


    /**
     * 当日巡查进度(已巡查几次)
     */
    public function index2()
    {

        $staff_id = Db::name("inspection_staff")->where("user_id", $this->auth->id)->value("id");
        $count = 0;
        if (!empty($staff_id)) {
            $where["staff_id"] = ["=", $staff_id];
            $where["begintime"] = ["<", time()];
            $where["endtime"] = [">", time()];
            $count = Db::name('inspection_project')->where($where)->count();
        }

        $this->success('请求成功', ["data" => $count]);
    }
}