package com.gci.boot.modules.vehicles.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.common.util.ValidatorUtil;
import lombok.Data;
import net.sf.jsqlparser.util.validation.ValidationError;

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

@Data
public class RepairImportVO {
    // 报修信息:  车牌号	报修人  联系电话	报修时间	报修分类	报修内容	是否质保	维修厂
    // 维修信息:  维修分类	维修内容	数量	材料单价	工时	工时单价	内容说明
    @ValidatorUtil.NotEmpty(message = "车牌号不能为空")
    @ExcelProperty({"报修信息", "车牌号"})
    private String plateNumber;
    @ValidatorUtil.NotEmpty(message = "报修人不能为空")
    @ValidatorUtil.StrMaxLength(strMaxLength = 32, message = "报修人长度不能超过32位", fieldNameCN = "报修人")
    @ExcelProperty({"报修信息", "报修人"})
    private String repairPeople;
    @ValidatorUtil.NotEmpty(message = "联系电话不能为空")
    @ValidatorUtil.PhoneNumber(message = "联系电话格式错误")
    @ExcelProperty({"报修信息", "联系电话"})
    private String phone;
    @ValidatorUtil.NotEmpty(message = "报修时间不能为空")
    @ValidatorUtil.DateValue(format = "yyyy-MM-dd", message = "报修时间格式错误:格式为'yyyy-MM-dd'")
    @ExcelProperty({"报修信息", "报修时间"})
    private String repairTime;
    @ValidatorUtil.NotEmpty(message = "报修分类不能为空")
    @ExcelProperty({"报修信息", "报修分类"})
    private String repairClass;
    @ValidatorUtil.NotEmpty(message = "报修内容不能为空")
    @ValidatorUtil.StrMaxLength(strMaxLength = 200, message = "报修内容长度不能超过200位", fieldNameCN = "报修内容")
    @ExcelProperty({"报修信息", "报修内容"})
    private String repairContent;
    @ValidatorUtil.NotEmpty(message = "是否质保不能为空")
    @ValidatorUtil.CollectionStr(collectionStr = "是,否", message = "是否质保只能为'是'或'否'")
    @ExcelProperty({"报修信息", "是否质保"})
    private String isGuarantee;
    @ValidatorUtil.NotEmpty(message = "维修厂不能为空")
    @ExcelProperty({"报修信息", "维修厂"})
    private String repairShop;

    @ValidatorUtil.NotEmpty(message = "维修分类不能为空")
    @ExcelProperty({"维修信息", "维修分类"})
    private String repairClassV2;
    @ValidatorUtil.NotEmpty(message = "维修内容不能为空")
    @ValidatorUtil.StrMaxLength(strMaxLength = 100, message = "维修内容长度不能超过100位", fieldNameCN = "维修内容")
    @ExcelProperty({"维修信息", "维修内容"})
    private String repairContentV2;
    @ValidatorUtil.NotEmpty(message = "数量不能为空")
    @ValidatorUtil.IntValue(minValue= 0, maxValue = 99999999,  message = "数量必须为整数:且范围在[0,99999999]")
    @ExcelProperty({"维修信息", "数量"})
    private String quantity;
    @ValidatorUtil.NumericValue(minValue= 0, maxValue = 99999999,  message = "材料单价必须为数字:且范围在[0,99999999]")
    @ValidatorUtil.NotEmpty(message = "材料单价不能为空")
    @ExcelProperty({"维修信息", "材料单价"})
    private String unitPrice;
    @ValidatorUtil.NotEmpty(message = "工时不能为空")
    @ValidatorUtil.NumericValue(minValue= 0, maxValue = 99999999,  message = "工时必须为数字:且范围在[0,99999999]")
    @ExcelProperty({"维修信息", "工时"})
    private String workHours;
    @ValidatorUtil.NotEmpty(message = "工时单价不能为空")
    @ValidatorUtil.NumericValue(minValue= 0, maxValue = 99999999,  message = "工时单价必须为数字:且范围在[0,99999999]")
    @ExcelProperty({"维修信息", "工时单价"})
    private String workHoursCost;
    @ValidatorUtil.NotEmpty(message = "内容说明不能为空")
    @ValidatorUtil.StrMaxLength(strMaxLength = 100, message = "内容说明长度不能超过100位", fieldNameCN = "内容说明")
    @ExcelProperty({"维修信息", "内容说明"})
    private String content;


    // 用于记录校验异常信息
    @ExcelIgnore
    private List<String> validationErrors;
    @ExcelProperty({"异常信息", "异常信息"})
    private String validationErrorsStr;
    // 用于记录所在行数
    @ExcelIgnore
    private Integer row;
    //字典值
    @ExcelIgnore
    private String repairClassValue;
    @ExcelIgnore
    private String repairClassV2Value;
    //维修厂信息
    @ExcelIgnore
    private String repairShopId;
    @ExcelIgnore
    private String contact;
    @ExcelIgnore
    private String contactPhone;
    //格式异常的
    @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 + "";
        }
    }

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