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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.gci.boot.annotation.Query.SelectQuery;
import com.gci.boot.common.dict.annotation.Dict;
import com.gci.boot.common.dict.excel.DictConverter;
import com.gci.boot.common.util.ValidatorUtil;
import com.gci.boot.common.validated.SaveValidated;
import com.gci.boot.common.validated.UpdateValidated;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.Size;
import java.lang.reflect.Field;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class DriverImportVO {
    //所属项目	司机姓名	姓名全拼（小写）	工号	生日	性别	联系电话	身份证号码	入职日期	住址	备注
    @ExcelProperty("所属项目")
    @ValidatorUtil.NotEmpty(message = "所属项目不能为空")
    private String projectNames;
    @ExcelProperty("司机姓名")
    @ValidatorUtil.NotEmpty(message = "司机姓名不能为空")
    @ValidatorUtil.StrMaxLength(strMaxLength = 100, message = "司机姓名长度不能超过100位", fieldNameCN = "司机姓名")
    private String driverName;
    @ExcelProperty("姓名全拼（小写）")
    @ValidatorUtil.NotEmpty(message = "姓名全拼（小写）不能为空")
    @ValidatorUtil.StrMaxLength(strMaxLength = 100, message = "姓名全拼（小写）长度不能超过100位", fieldNameCN = "姓名全拼（小写）")
    private String username;
    @ExcelProperty("工号")
    @ValidatorUtil.StrMaxLength(strMaxLength = 100, message = "工号长度不能超过100位", fieldNameCN = "工号")
    private String workNo;
    @ExcelProperty("生日")
    @ValidatorUtil.DateValue(format = "yyyy-MM-dd", message = "生日格式错误:格式为'yyyy-MM-dd'")
    private String birthday;
    @ExcelProperty("性别")
    @ValidatorUtil.CollectionStr(collectionStr = "男,女", message = "性别只能为'男'或'女'")
    private String sex;
    @ExcelProperty("联系电话")
    @ValidatorUtil.PhoneNumber(message = "联系电话格式错误")
    private String phone;
    @ExcelProperty("身份证号码")
    @ValidatorUtil.IDCard(message = "身份证号码格式错误")
    private String idCard;
    @ExcelProperty("入职日期")
    @ValidatorUtil.NotEmpty(message = "入职日期不能为空")
    @ValidatorUtil.DateValue(format = "yyyy-MM-dd", message = "入职日期格式错误:格式为'yyyy-MM-dd'")
    private String entryTime;
    @ExcelProperty("住址")
    @ValidatorUtil.StrMaxLength(strMaxLength = 64, message = "住址长度不能超过64位", fieldNameCN = "住址")
    private String address;
    @ExcelProperty("备注")
    @ValidatorUtil.StrMaxLength(strMaxLength = 200, message = "备注长度不能超过200位", fieldNameCN = "备注")
    private String remark;

    @ExcelProperty(value = "所属公司")
    @ValidatorUtil.StrMaxLength(strMaxLength = 64, message = "所属公司长度不能超过64位", fieldNameCN = "所属公司")
    private String company;
    @ExcelProperty(value = "员工类别")
    @ValidatorUtil.StrMaxLength(strMaxLength = 64, message = "员工类别长度不能超过64位", fieldNameCN = "员工类别")
    private String employeeType;
    @ExcelProperty(value = "是否定位")
    @ValidatorUtil.CollectionStr(collectionStr = "是,否", message = "是否定位只能为'是'或'否'")
    private String isPosition;
    @ExcelProperty(value = "年龄")
    @ValidatorUtil.IntValue(minValue= 18, maxValue = 200,  message = "年龄必须为整数:且范围在[0,200]")
    private String age;
    @ExcelProperty(value = "婚姻状况")
    @ValidatorUtil.CollectionStr(collectionStr = "未婚,已婚,离婚,丧偶", message = "婚姻状况只能为'未婚'、'已婚'、'离婚'、'丧偶'")
    private String maritalStatus;
    @ExcelProperty(value = "子女数")
    @ValidatorUtil.IntValue(minValue= 0, maxValue = 200,  message = "子女数必须为整数:且范围在[0,200]")
    private String childrens;
    @ExcelProperty(value = "档案号")
    @ValidatorUtil.StrMaxLength(strMaxLength = 64, message = "档案号长度不能超过64位", fieldNameCN = "档案号")
    private String fileNumber;

    // 用于记录校验异常信息
    @ExcelIgnore
    private List<String> validationErrors;
    @ExcelProperty("异常信息")
    private String validationErrorsStr;
    // 用于记录所在行数
    @ExcelIgnore
    private Integer row;
    //客户id
    @ExcelIgnore
    private String customerId;
    //项目id
    @ExcelIgnore
    private List<String> projectIds;
    //格式异常的
    @ExcelIgnore
    private String formatErrors;
    //重复的
    @ExcelIgnore
    private String repeatErrors;

    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 + "";
            this.setFormatErrors("1");
        }
    }

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