package com.gci.boot.modules.vehicles.vo;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.gci.boot.annotation.Query.SelectQuery;
import com.gci.boot.annotation.fileUrl.FileUrl;
import com.gci.boot.annotation.fileUrl.handle.FileUrlHandle;
import com.gci.boot.common.dict.annotation.Dict;
import com.gci.boot.common.dict.excel.DictConverter;
import com.gci.boot.common.util.DateUtils;
import com.gci.boot.common.util.ValidatorUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 保险信息
 * </p>
 *
 * @author xushuhan
 * @since 2023-09-21
 */
@Data
public class InsuranceImportVO implements Serializable {

    @ExcelProperty("车牌号")
    @ValidatorUtil.NotEmpty(message = "车牌号不能为空")
    private String plateNumber;

    @ExcelProperty(value = "所属保险合同编号")
    @ValidatorUtil.NotEmpty(message = "所属保险合同编号不能为空")
    private String contractName;

    @ExcelProperty(value = "保险公司")
    @ValidatorUtil.NotEmpty(message = "保险公司不能为空")
    private String compulsoryCompany;

    @ExcelProperty(value = "起保日期")
    @ValidatorUtil.NotEmpty(message = "起保日期不能为空")
    @ValidatorUtil.DateValue(format = "yyyy-MM-dd", message = "日期格式错误:格式为'yyyy-MM-dd'")
    private String compulsoryStartTime;

    @ExcelProperty(value = "有效日期")
    @ValidatorUtil.NotEmpty(message = "有效日期不能为空")
    @ValidatorUtil.DateValue(format = "yyyy-MM-dd", message = "日期格式错误:格式为'yyyy-MM-dd'")
    private String compulsoryValidTime;

    @ExcelProperty(value = "交强险")
    @ValidatorUtil.CollectionStr(collectionStr="是,否",message = "取值范围为是或否")
    private String hasCompulsoryInsurance;

    @ExcelProperty(value = "交强险保险单号")
    @ValidatorUtil.NotEmptyByField(field = "hasCompulsoryInsurance",message = "交强险为'是',交强险保险单号不能为空")
    private String compulsoryInsuranceNo;

    @ExcelProperty(value = "保险金额(元)")
    @ValidatorUtil.NumericValue(minValue = 0,maxValue = 99999999,message = "超过取值范围,金额范围为[0,99999999]")
    @ValidatorUtil.NotEmptyByField(field = "hasCompulsoryInsurance",message = "交强险为'是',保险金额不能为空")
    private Double compulsoryPrice;

    @ExcelProperty(value = "车船税(元)")
    @ValidatorUtil.NumericValue(minValue = 0,maxValue = 99999999,message = "超过取值范围,金额范围为[0,99999999]")
    @ValidatorUtil.NotEmptyByField(field = "hasCompulsoryInsurance",message = "交强险为'是',车船税不能为空")
    private Double vehicleVesselTax;

    @ExcelProperty(value = "商业险")
    @ValidatorUtil.CollectionStr(collectionStr="是,否",message = "取值范围为是或否")
    private String hasCommercialInsurance;

    @ExcelProperty(value = "商业险保险单号")
    @ValidatorUtil.NotEmptyByField(field = "hasCommercialInsurance",message = "商业险为'是',商业险保单号不能为空")
    private String commercialInsuranceNo;

    @ExcelProperty(value = "保险费(元)")
    @ValidatorUtil.NumericValue(minValue = 0,maxValue = 99999999,message = "超过取值范围,金额范围为[0,99999999]")
    @ValidatorUtil.NotEmptyByField(field = "hasCommercialInsurance",message = "商业险为'是',保险费不能为空")
    private Double commercialPrice;

    @ExcelProperty("备注")
    private String remark;

    /**
     * 用于记录校验异常信息
     */
    @ExcelIgnore
    private List<String> validationErrors;
    @ExcelProperty("异常信息")
    private String validationErrorsStr;

    /**
     * 合同id
     */
    @ExcelIgnore
    private String contractId;


    /**
     * 保险公司id
     */
    @ExcelIgnore
    private String compulsoryCompanyId;


    /**
     * 所属机构
     */
    @ExcelIgnore
    private String sysOrgCode;


    /**
     * 创建人
     */
    @ExcelIgnore
    private String createBy;

    /**
     * 车辆id
     */
    @ExcelIgnore
    private String vehicleId;
    /**
     * 项目id
     */
    @ExcelIgnore
    private String projectId;

    /**
     * 车辆型号
     */
    @ExcelIgnore
    private String vehicleModel;
    /**
     * 车辆种类
     */
    @ExcelIgnore
    private String vehicleType;
    /**
     * 车架号
     */
    @ExcelIgnore
    private String frameNumber;


    public void validate() {
        ValidatorUtil.trimStringFields(this);
        List<ValidatorUtil.ValidationError> validationErrors = ValidatorUtil.validate(this);
        if(CollectionUtil.isNotEmpty(validationErrors)){
            this.validationErrors  = validationErrors.stream().map(ValidatorUtil.ValidationError::getMessage).collect(Collectors.toList());
            this.validationErrorsStr =  this.validationErrors + "";
        }
    }

    public void putErrorMsg(String msg){
        if(CollectionUtil.isNotEmpty(this.validationErrors)){
            this.validationErrors.add(msg);
        }else{
            this.validationErrors = CollectionUtil.newArrayList(msg);
        }
        this.validationErrorsStr =  this.validationErrors + "";
    }
}
