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

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.ibs.enumertion.CustIdTypeEnum;
import com.ruoyi.ibs.enumertion.CustTypeEnum;
import com.ruoyi.ibs.grid.domain.dto.GridUserRelateDTO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @Author 吴凯程
 * @Date 2024/9/18
 **/
@Data
@ApiModel(description = "网格虚拟对象DTO")
public class VirtualGridDTO {

    @ApiModelProperty(value = "网格创建类型", notes = "1导入客户 2导入管护")
    @NotNull(message = "创建方式不能为空")
    private String createMode;

    /**
     * 网格名
     */
    @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;
    /**
     * 分配方式;1指定 2分配引擎
     */
    @ApiModelProperty(value = "分配方式", notes = "1指定 2分配引擎")
    private String gridAssignType;
    /**
     * 网格责任类型;1责任 2竞争
     */
    @ApiModelProperty(value = "网格责任类型", notes = "1责任 2竞争")
    private String gridDutyType;

    @ApiModelProperty(value = "归属部室 0零售 1公司 2运管")
    @Valid
    @NotNull(message = "归属部室不能为空")
    private String opsDept;

    @ApiModelProperty(value = "机构号")
    private List<Long> userDeptList;

    @ApiModelProperty(value = "关联机构用户",notes = "")
    private List<GridUserRelateDTO> gridUserRelateDTOList;

    @ApiModelProperty(value = "网格关联客户列表", notes = "")
    private List<VirtualCustDTO> custDTOList;

    @ApiModelProperty(value = "客户管护列表", notes = "")
    private List<VirtualCustUserDTO> virtualCustUserDTOList;

    @ApiModelProperty(value = "网格分配规则列表", notes = "")
    private List<VirtualGridRuleDTO> virtualGridRuleDTOList;

    public void checkValid() {
        if (createMode.equals("1")) {
            custDTOList.forEach(custDTO -> {
                if (Objects.isNull(custDTO.getCustType())
                        || Objects.isNull(custDTO.getCustIdType())
                        || Objects.isNull(custDTO.getCustIdn())
                        || Objects.isNull(custDTO.getCustId())) {
                    throw new ServiceException("创建失败，存在客户信息缺失！");
                }
                if (Objects.isNull(CustTypeEnum.getByName(custDTO.getCustType()))) {
                    throw new ServiceException("客户类型错误！");
                }
                if (Objects.isNull(CustIdTypeEnum.getByName(custDTO.getCustIdType()))) {
                    throw new ServiceException("客户证件类型错误！");
                }
            });

            List<String> custIds = custDTOList.stream().map(VirtualCustDTO::getCustId).distinct().collect(Collectors.toList());
            if (custIds.size() != custDTOList.size()) {
                throw new ServiceException("客户列表存在重复客户！");
            }
            if (hasBusiness && custDTOList.stream().noneMatch(virtualCustDTO -> virtualCustDTO.getCustType().equals(CustTypeEnum.ENTERPRISE.getName()))) {
                throw new ServiceException("未上传客户类型为企业的客户！");
            }
            if (hasRetail && custDTOList.stream().noneMatch(virtualCustDTO -> virtualCustDTO.getCustType().equals(CustTypeEnum.PERSONAL.getName()))) {
                throw new ServiceException("未上传客户类型为个人的客户！");
            }
            if (hasRetailBusiness && custDTOList.stream().noneMatch(virtualCustDTO -> virtualCustDTO.getCustType().equals(CustTypeEnum.MERCHANT.getName()))) {
                throw new ServiceException("未上传客户类型为商户的客户！");
            }

            if (gridAssignType.equals("1")) {
                if (Objects.isNull(gridUserRelateDTOList) || gridUserRelateDTOList.isEmpty()) {
                    throw new ServiceException("分配方式为指定时必须绑定客户经理");
                }
                if (gridDutyType.equals("1") && gridUserRelateDTOList.size() != 1) {
                    throw new ServiceException("责任网格只能选择一个客户经理");
                }
                if (gridDutyType.equals("2") && gridUserRelateDTOList.size() < 2) {
                    throw new ServiceException("竞争网格需要选择多个客户经理");
                }
            } else if (gridAssignType.equals("2")) {
                if (Objects.isNull(virtualGridRuleDTOList) || virtualGridRuleDTOList.isEmpty()) {
                    throw new ServiceException("分配方式为自定义时必须添加分配规则！");
                }
            }
        } else {
            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("未上传客户类型为商户的客户！");
            }

        }



    }

}
