<?php
class renjiazhuangClassModel extends Model
{
    
    public function baseCarList() {
        $sqlStr = 'select * from xinhu_rjz_car_list';
		$rows 	= $this->db->getall($sqlStr);
        return $rows;
	}

    // 车辆管理
    public function getCarList()
    {
        $data = $this->baseCarList();
        $arr = [];
        foreach ($data as $k => $v) {
            
            $arr[] = array('name' => $v['car_num'].'【'.$v['car_type'].'】', 'value'=>$v['car_num']);
        }
        return $arr;
    }

    public function getCarListByType($carType)
    {
        $sqlStr = "select id, car_num, car_type, car_state from xinhu_rjz_car_list where car_type in (".$carType.") and car_state = '正常'";
//        return $sqlStr;
        $rows 	= $this->db->getall($sqlStr);
        return $rows;
    }

    public function getCarCon() {
        $sqlStr = 'SELECT car_type, count(1) all_con, count(case when car_state = "正常" then 1 end) free_con, ';
        $sqlStr .= ' count(case when car_state = "装车" then 1 end) loading_con,';
        $sqlStr .= ' count(case when car_state = "卸车" then 1 end) unload_con,';
        $sqlStr .= ' count(case when car_state = "维修中" then 1 end) repair_con,';
        $sqlStr .= ' count(case when car_state = "停用" then 1 end) stop_con';
        $sqlStr .= ' FROM xinhu_rjz_car_list GROUP BY car_type';
        $rows = $this->db->getall($sqlStr);
        $data = [];
        for($i = 0; $i < sizeof($rows); $i++) {
            $j=0;
            $data[$i]['car_type'] = $rows[$i]["car_type"];
            $data[$i]['con'][$j]['title'] = "全部";
            $data[$i]['con'][$j]['con'] = $rows[$i]["all_con"];
            $data[$i]['con'][$j]['is_show'] = 1;
            $data[$i]['con'][$j++]['order'] = 0;

            $data[$i]['con'][$j]['title'] = "空闲";
            $data[$i]['con'][$j]['con'] = $rows[$i]["free_con"];
            $data[$i]['con'][$j]['is_show'] = 1;
            $data[$i]['con'][$j++]['order'] = 1;

            $data[$i]['con'][$j]['title'] = "装车";
            $data[$i]['con'][$j]['con'] = $rows[$i]["loading_con"];
            $data[$i]['con'][$j]['is_show'] = 1;
            $data[$i]['con'][$j++]['order'] = 2;

            $data[$i]['con'][$j]['title'] = "卸车";
            $data[$i]['con'][$j]['con'] = $rows[$i]["unload_con"];
            $data[$i]['con'][$j]['is_show'] = 1;
            $data[$i]['con'][$j++]['order'] = 3;

            $data[$i]['con'][$j]['title'] = "维修";
            $data[$i]['con'][$j]['con'] = $rows[$i]["repair_con"];
            $data[$i]['con'][$j]['is_show'] = 1;
            $data[$i]['con'][$j++]['order'] = 4;

            $data[$i]['con'][$j]['title'] = "停用";
            $data[$i]['con'][$j]['con'] = $rows[$i]["stop_con"];
            $data[$i]['con'][$j]['is_show'] = 1;
            $data[$i]['con'][$j++]['order'] = 5;
        }
        return $data;
    }

    public function getOneCarCon($cartype='') {

        if(strlen($cartype) > 0) {
            $where = " and car_type in (" . $cartype . ")";
        }
        // option表选项id
        $optionTablePid = 1146;
        $table = '[Q]option a left join [Q]rjz_car_list b on a.`name` = b.car_type';
        $where = "pid = $optionTablePid $where";
        $fields = "a.`name` as car_type, count(b.id) car_con, count(case when car_state = '正常' then 1 end) car_free_con";
        $rows = $this->db->getone($table, $where, $fields);
        return $rows;
    }

    public function getCarIdleCon($cartypes='') {

        if(strlen($cartypes) > 0) {
            $where = " and car_type in (" . $cartypes . ")";
        }
        $sqlStr = 'select a.name as car_type, count(b.id) car_con, count(case when car_state = "正常" then 1 end) car_free_con';
        $sqlStr .= ' from xinhu_option a left join xinhu_rjz_car_list b on a.name = b.car_type';
        $sqlStr .= ' where pid = 1146 '.$where;
        $sqlStr .= ' group by a.name';
        $rows = $this->db->getall($sqlStr);
        return $rows;
    }

    public function getCarIdleConByApplyStartTime($car_types='', $apply_start_time = '') {
        if(strlen($car_types) > 0) {
            $where = " and car_type in (" . $car_types . ")";
        }
        # 申请使用时间大于前车空闲时间
        if (strlen($apply_start_time) > 0) {
            $free_where = " or (end_time <= '".$apply_start_time."' or end_time is null)";
        }
        $sqlStr = 'select a.name as car_type, count(b.id) car_con,';
        $sqlStr .= ' count(case when car_state = "正常" '.$free_where.' then 1 end) car_free_con';
        $sqlStr .= ' from xinhu_option a left join xinhu_rjz_car_list b on a.name = b.car_type';
        $sqlStr .= ' where pid = 1146 '.$where;
        $sqlStr .= ' group by a.name';
        $rows = $this->db->getall($sqlStr);

        return $rows;
    }

    public function updateCarState($carname, $carstate) {
        if ($carname) {
            $res = $this->db->update('[Q]rjz_car_list','`car_state`="'.$carstate.'"','`car_num` in ('.$carname.')');
            return returnsuccess($res);
        }
        return returnerror('车牌号为空');
    }
}