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

import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

import javax.validation.constraints.NotBlank;

import cn.hutool.core.date.DateUtil;
import com.gci.boot.modules.vehicles.vo.InsuranceImportVO;
import org.hibernate.validator.constraints.Length;

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.FieldStrategy;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
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.annotation.repeat.FieldRepeat;
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.validated.SaveValidated;
import com.gci.boot.common.validated.UpdateValidated;

import cn.afterturn.easypoi.excel.annotation.Excel;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

/**
 * <p>
 * 保险信息
 * </p>
 *
 * @author xushuhan
 * @since 2023-09-21
 */
@Getter
@Setter
@TableName("tb_insurance")
@ApiModel(value = "TbInsurance对象", description = "保险信息")
@ColumnWidth(20)
public class TbInsurance implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.ASSIGN_ID)
    @ExcelIgnore
    private String id;

    @ApiModelProperty("所属车辆")
    @ExcelIgnore
    @SelectQuery(Type = SelectQuery.SelectType.EQ)
    private String vehicleId;

    @ExcelProperty(value = "车牌号", index = 1)
    @SelectQuery
    private String plateNumber;

    @ApiModelProperty("所属项目")
    //@ExcelProperty(value = "所属项目", converter = DictConverter.class, index = 0)
    @ExcelIgnore
    @Dict(dictTable = "tb_projects", dicText = "project_name", dicCode = "id")
    @SelectQuery(Type = SelectQuery.SelectType.EQ)
    private String projectId;

    @ApiModelProperty("是否购买交强险（0,1）")
    @ExcelIgnore
    private String hasCompulsoryInsurance;

    @ApiModelProperty("交强险保险单号")
    @SelectQuery
    @ExcelProperty(value = "交强险保险单号", index = 8)
    private String compulsoryInsuranceNo;

    @ApiModelProperty("交强险办理日期")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat("yyyy-MM-dd")
    @ExcelIgnore
    private Date compulsoryCreateDate;

    @ApiModelProperty("交强险保险公司")
    @ExcelProperty(value = "保险公司", converter = DictConverter.class, index = 5)
    @Dict(dictTable = "tb_insurance_company_archives", dicText = "name", dicCode = "id")
    @SelectQuery(Type = SelectQuery.SelectType.EQ)
    private String compulsoryCompanyId;

    @ApiModelProperty("交强险保险金额")
    @ExcelProperty(value = "交强险保险金额", index = 9)
    private Double compulsoryPrice;

    @ApiModelProperty("车船税")
    @ExcelProperty(value = "车船税", index = 3)
    private Double vehicleVesselTax;

    @ApiModelProperty("交强险起保日期")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat("yyyy-MM-dd")
    @ExcelProperty(value = "起保日期", index = 6)
    private Date compulsoryStartTime;

    @ApiModelProperty("交强险有效日期")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat("yyyy-MM-dd")
    @ExcelProperty(value = "有效日期", index = 7)
    private Date compulsoryValidTime;

    @ApiModelProperty("交强险附件")
    @ExcelIgnore
    @JsonDeserialize(using = FileUrlHandle.class)
    @FileUrl
    private String compulsoryUrl;

    @ApiModelProperty("是否购买商业保险（0,1）")
    @ExcelIgnore
    private String hasCommercialInsurance;

    @ApiModelProperty("商业保险单号")
    @SelectQuery
    @ExcelProperty(value = "商业保险单号", index = 10)
    private String commercialInsuranceNo;

    @ApiModelProperty("商业保险办理日期")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat("yyyy-MM-dd")
    @ExcelIgnore
    private Date commercialCreateDate;

    @ApiModelProperty("商业保险公司")
    @ExcelIgnore
    @Dict(dictTable = "tb_insurance_company_archives", dicText = "name", dicCode = "id")
    @SelectQuery(Type = SelectQuery.SelectType.EQ)
    private String commercialCompanyId;

    @ApiModelProperty("商业保险费合计")
    @ExcelProperty(value = "商业保险费合计", index = 11)
    private Double commercialPrice;

    @ApiModelProperty("商业保险起保日期")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat("yyyy-MM-dd")
    @ExcelIgnore
    private Date commercialStartTime;

    @ApiModelProperty("商业保险有效日期")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @DateTimeFormat("yyyy-MM-dd")
    @ExcelIgnore
    private Date commercialValidTime;

    @ApiModelProperty("商业保险附件")
    @ExcelIgnore
    @JsonDeserialize(using = FileUrlHandle.class)
    @FileUrl
    private String commercialUrl;

    @ApiModelProperty("备注")
    @ExcelIgnore
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String remark;

    @ApiModelProperty("所属机构")
    @ExcelIgnore
    private String sysOrgCode;

    @ApiModelProperty("创建时间")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @ExcelIgnore
    private Date createTime;

    @ApiModelProperty("更新时间")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @ExcelIgnore
    private Date updateTime;

    @ApiModelProperty("创建人")
    @ExcelIgnore
    private String createBy;

    @ApiModelProperty("更新人")
    @ExcelIgnore
    private String updateBy;

    @ExcelIgnore
    @ApiModelProperty("删除标志")
    private Integer delFlag;


    @ApiModelProperty("车辆型号")
    @SelectQuery
    @ExcelIgnore
    private String vehicleModel;

    @ApiModelProperty("车辆种类")
    @Dict(dicCode = "vehicle_type")
    @SelectQuery(Type = SelectQuery.SelectType.EQ)
    @ExcelProperty(value = "车辆种类", converter = DictConverter.class, index = 2)
    private String vehicleType;

    @ApiModelProperty("车架号")
    @SelectQuery
    @ExcelProperty(value = "车架号", index = 4)
    private String frameNumber;

    @ApiModelProperty("所属合同")
    @ExcelProperty(value = "所属合同", converter = DictConverter.class, index = 0)
    @SelectQuery(Type = SelectQuery.SelectType.EQ)
    @Dict(dictTable = "tb_contract", dicText = "name", dicCode = "id")
    private String contractId;


    @ApiModelProperty("交强险有效日期开始")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd")
    @TableField(exist = false)
    @ExcelIgnore
    private Date compulsoryValidTimeStart;

    @ApiModelProperty("交强险有效日期结束")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd")
    @TableField(exist = false)
    @ExcelIgnore
    private Date compulsoryValidTimeEnd;

    @ApiModelProperty("商业保险有效日期开始")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd")
    @TableField(exist = false)
    @ExcelIgnore
    private Date commercialValidTimeStart;

    @ApiModelProperty("商业保险有效日期结束")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd")
    @TableField(exist = false)
    @ExcelIgnore
    private Date commercialValidTimeEnd;
    //1将逾期，2已逾期
    @TableField(exist = false)
    @ExcelIgnore
    private Integer isOverTime;

    public Date getMaxMonth() {
    	if(DateUtils.compareTo(this.getCompulsoryValidTime(), this.getCommercialValidTime())>0) {
    		return this.getCompulsoryValidTime();
    	}
    	return this.getCommercialValidTime();
    }

	public void init() {
		/*if("0".equals(this.hasCompulsoryInsurance)) {
			this.compulsoryInsuranceNo = null;
			this.compulsoryCreateDate = null;
			this.compulsoryCompanyId = null;
			this.compulsoryPrice = null;
			this.vehicleVesselTax = null;
			this.compulsoryStartTime = null;
			this.compulsoryValidTime = null;
			this.compulsoryUrl = null;
		}
		if("0".equals(this.hasCommercialInsurance)) {
			this.commercialInsuranceNo = null;
			this.commercialCreateDate = null;
			this.commercialCompanyId = null;
			this.commercialPrice = null;
			this.commercialStartTime = null;
			this.commercialValidTime = null;
			this.commercialUrl = null;
		}*/
	}

    public static TbInsurance buildByImportVo(InsuranceImportVO vo){
        TbInsurance insurance = new TbInsurance();
        insurance.setVehicleId(vo.getVehicleId());
        insurance.setProjectId(vo.getProjectId());


        //保险公司跟有效日期 默认给交强险
        insurance.setCompulsoryCompanyId(vo.getCompulsoryCompanyId());
        insurance.setCompulsoryStartTime(DateUtil.parseDate(vo.getCompulsoryStartTime()));
        insurance.setCompulsoryValidTime(DateUtil.parseDate(vo.getCompulsoryValidTime()));

        if(Objects.equals("是",vo.getHasCompulsoryInsurance())){
            insurance.setHasCompulsoryInsurance("1");
            insurance.setCompulsoryInsuranceNo(vo.getCompulsoryInsuranceNo());
            insurance.setCompulsoryCreateDate(DateUtil.parseDate(vo.getCompulsoryStartTime()));
            insurance.setCompulsoryPrice(vo.getCompulsoryPrice());
            insurance.setVehicleVesselTax(vo.getVehicleVesselTax());
        }
        if(Objects.equals("是",vo.getHasCommercialInsurance())){
            insurance.setHasCommercialInsurance("1");
            insurance.setCommercialInsuranceNo(vo.getCommercialInsuranceNo());
            insurance.setCommercialCreateDate(DateUtil.parseDate(vo.getCompulsoryStartTime()));
            insurance.setCommercialPrice(vo.getCommercialPrice());
            //如果商业险录入了。额外把交强险内容赋值给商业险。
            insurance.setCommercialCompanyId(vo.getCompulsoryCompanyId());
            insurance.setCommercialStartTime(DateUtil.parseDate(vo.getCompulsoryStartTime()));
            insurance.setCommercialValidTime(DateUtil.parseDate(vo.getCompulsoryValidTime()));
        }
        insurance.setSysOrgCode(vo.getSysOrgCode());
        insurance.setCreateBy(vo.getCreateBy());
        insurance.setCreateTime(new Date());
        insurance.setDelFlag(0);
        insurance.setVehicleModel(vo.getVehicleModel());
        insurance.setVehicleType(vo.getVehicleType());
        insurance.setRemark(vo.getRemark());
        insurance.setContractId(vo.getContractId());
        insurance.setFrameNumber(vo.getFrameNumber());
        insurance.setPlateNumber(vo.getPlateNumber());
        return insurance;
    }


}
