<?php

namespace catchAdmin\yunying\model;

use catchAdmin\permissions\controller\User;
use catcher\base\CatchModel as Model;
use catchAdmin\permissions\model\DataRangScopeTrait;
use catchAdmin\permissions\model\Department;
use catchAdmin\permissions\model\Users;
use catchAdmin\yunying\model\VehicleBrand;
use catchAdmin\yunying\model\VehicleColor;
use catchAdmin\yunying\model\StolenVehicle;

class Vehicle extends Model
{
    use DataRangScopeTrait;
    // 表名
    public $name = 'vehicles';
    // 数据库字段映射
    public $field = array(
        'id',
        // 车牌号码
        'license_plate',
        // 所属部门
        'department_id',
        // 用户id
        'user_id',
        // 上牌时间
        'license_time',
        // 车架号
        'frame_number',
        // 电机号
        'motor_number',
        // 车辆品牌
        'brand_id',
        // 车辆颜色
        'color_id',
        'car_type',
        'rfid_sn',
        'installer',
        // 备注
        'remark',
        // 购车日期
        'buydate',
        // 上牌员工
        'open_user_id',
        // 创建人ID
        'creator_id',
        // 创建时间
        'created_at',
        // 更新时间
        'updated_at',
        // 软删除
        'deleted_at',
    );
    /**
     * 获取车辆列表
     */
    public function getVehicleList($field, $order)
    {
        $res =  $this->dataRange()
            ->catchSearch()
            ->append(['depart_name', 'vehicle_state', 'owner', 'brand_name', 'color_name', 'open_user_name'])
            ->order($this->aliasField($field), $order)
            ->paginate();
        return $res;
    }
    /**
     * 获取车辆导出列表
     */
    public function getVehiclesExportList()
    {
        $res =  $this->dataRange()
            ->catchSearch()
            ->append(['depart_name', 'vehicle_state', 'owner', 'brand_name', 'color_name', 'open_user_name'])
            ->order($this->aliasField('id'), 'desc')
            ->select();
        return $res;
    }
    //根据部门搜索
    public function searchDepartmentIdAttr($query, $value, $data)
    {
        if (count($value)) {
            $value = $value[count($value) - 1];
            return $query->where('department_id', $value);
        } else {
            return $query;
        }
    }
    //根据车牌搜索
    public function searchLicensePlateAttr($query, $value, $data)
    {
        return $query->where('license_plate', 'like', '%' . $value . '%');
    }
    //根据车架号搜索
    public function searchFrameNumberAttr($query, $value, $data)
    {
        return $query->where('frame_number', 'like', '%' . $value . '%');
    }
    //根据电机号搜索
    public function searchMotorNumberAttr($query, $value, $data)
    {
        return $query->where('motor_number', 'like', '%' . $value . '%');
    }
    //根据上牌时间搜索
    public function searchLicenseTimeAttr($query, $value, $data)
    {
        if (empty($value)) {
            return $query;
        }
        return $query->where('license_time', 'between', $value);
    }
    //获取部门名称
    public function getDepartNameAttr($value)
    {
        $depart_id = $this->getData('department_id');
        return (new Department)->where('id', $depart_id)->value('department_name');
    }
    //获取车主名称
    public function getOwnerAttr($value)
    {
        $uid = $this->getData('user_id');
        return (new Users)->where('id', $uid)->value('realname');
    }
    //获取开户员工账号
    public function getOpenUserNameAttr($value)
    {
        $uid = $this->getData('open_user_id');
        return (new Users)->where('id', $uid)->value('username');
    }
    //获取车辆品牌名称
    public function getBrandNameAttr($value)
    {
        $bid = $this->getData('brand_id');
        return (new VehicleBrand())->where('id', $bid)->value('name');
    }
    //获取车辆颜色名称
    public function getColorNameAttr($value)
    {
        $cid = $this->getData('color_id');
        return (new VehicleColor())->where('id', $cid)->value('name');
    }
    //获取车辆状态名称
    public function getVehicleStateAttr($value)
    {
        $id = $this->getData('id');
        $stolen_info = (new StolenVehicle())->where('vehicle_id', $id)->find();
        if (!$stolen_info || in_array($stolen_info['result'], [2, 4])) { //0-待处理  1-已受理 2-已找回 3-已理赔 4-误报
            $value = 0;
        } else {
            $value = 1;
        }
        return $value;
    }
}