package com.ruoyi.ibs.grid.domain.dto.virtual;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.ibs.enumertion.CustTypeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Objects;

/**
 * @Author 吴凯程
 * @Date 2024/9/18
 **/
@Data
public class VirtualGridRelateAddDTO {

    /**
     * 网格名
     */
    @ApiModelProperty(value = "网格名", notes = "")
    @Valid
    @NotNull(message = "网格名不能为空")
    private String gridName;
    /**
     * 网格类型
     */
    @ApiModelProperty(value = "网格类型", notes = "")
    @Valid
    @NotNull(message = "网格类型不能为空")
    private String gridType;
    /**
     * 是否个人
     */
    @ApiModelProperty(value = "是否个人", notes = "")
    @Valid
    @NotNull(message = "是否个人不能为空")
    private Boolean hasRetail;
    /**
     * 是否企业
     */
    @ApiModelProperty(value = "是否企业", notes = "")
    @Valid
    @NotNull(message = "是否企业不能为空")
    private Boolean hasBusiness;
    /**
     * 是否商户
     */
    @ApiModelProperty(value = "是否商户", notes = "")
    @Valid
    @NotNull(message = "是否商户不能为空")
    private Boolean hasRetailBusiness;

    @Valid
    @ApiModelProperty(value = "客户管护列表", notes = "")
    @NotNull(message = "客户管护列表不能为空")
    @Size(min = 1, message = "客户管护列表不能为空")
    private List<VirtualCustUserDTO> virtualCustUserDTOList;


    public void checkValid() {
        virtualCustUserDTOList.forEach(virtualCustUserDTO -> {
            if (Objects.isNull(virtualCustUserDTO.getCustType())
                    || Objects.isNull(virtualCustUserDTO.getCustIdType())
                    || Objects.isNull(virtualCustUserDTO.getCustIdn())
                    || Objects.isNull(virtualCustUserDTO.getCustId())
                    || Objects.isNull(virtualCustUserDTO.getUserName())
            ) {
                throw new ServiceException("创建失败，存在客户管护信息缺失！");
            }
        });

        if (hasBusiness && virtualCustUserDTOList.stream().noneMatch(virtualCustDTO -> virtualCustDTO.getCustType().equals(CustTypeEnum.ENTERPRISE.getName()))) {
            throw new ServiceException("未上传客户类型为企业的客户！");
        }
        if (hasRetail && virtualCustUserDTOList.stream().noneMatch(virtualCustDTO -> virtualCustDTO.getCustType().equals(CustTypeEnum.PERSONAL.getName()))) {
            throw new ServiceException("未上传客户类型为个人的客户！");
        }
        if (hasRetailBusiness && virtualCustUserDTOList.stream().noneMatch(virtualCustDTO -> virtualCustDTO.getCustType().equals(CustTypeEnum.MERCHANT.getName()))) {
            throw new ServiceException("未上传客户类型为商户的客户！");
        }

    }

}
