package com.engine.salary.report.entity.param;

import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.report.enums.SalaryStatisticsDimensionTypeEnum;
import com.engine.salary.util.SalaryI18nUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 薪酬统计维度保存参数
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
//"薪酬统计维度保存参数")
public class SalaryStatisticsDimensionSaveParam {

    //薪酬统计维度主键id")
    private Long id;

    //薪酬统计维度类型")
    private SalaryStatisticsDimensionTypeEnum dimType;

    //薪酬统计维度名称")
    private String dimName;

    //薪酬统计维度编码-》用于组距和单项式的所属字段")
    private String dimCode;

    //备注")
    private String remark;

    //薪酬统计维度定性设置保存参数")
    private Setting4Qualitative setting4Qualitative;

    //薪酬统计维度定量-组距式分组设置保存参数")
    private List<Setting4RationGroupSpacing> setting4RationGroupSpacing;

    //薪酬统计维度定量-单项式分组设置保存参数")
    private List<Setting4RationGroupIndividual> setting4RationGroupIndividual;

    public static void checkParam(SalaryStatisticsDimensionSaveParam saveParam) {
        if (StringUtils.isEmpty(saveParam.getDimName())) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(161289, "统计维度名称必填"));
        }
        saveParam.setDimName(saveParam.getDimName().trim());
        if (Objects.isNull(saveParam.getDimType())) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(161290, "统计维度类型必传"));
        }
        if (SalaryStatisticsDimensionTypeEnum.QUALITATIVE.equals(saveParam.getDimType())) {
            if (Objects.isNull(saveParam.getSetting4Qualitative())) {
                throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(161291, "统计维度必选"));
            }
            if (Objects.isNull(saveParam.getSetting4Qualitative().getId())) {
                throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(161292, "统计维度关联参数错误"));
            }
        } else if (SalaryStatisticsDimensionTypeEnum.RATION_GROUP_SPACING.equals(saveParam.getDimType())) {
            if (CollectionUtils.isEmpty(saveParam.getSetting4RationGroupSpacing())) {
                throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(161293, "分组设置不能为空"));
            }
            saveParam.getSetting4RationGroupSpacing().forEach(s -> {
                if (Objects.isNull(s.getId()) || StringUtils.isEmpty(s.getStartValue()) || StringUtils.isEmpty(s.getEndValue())) {
                    throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(161294, "分组设置参数错误"));
                }
            });
        } else if (SalaryStatisticsDimensionTypeEnum.RATION_GROUP_INDIVIDUAL.equals(saveParam.getDimType())) {
            if (CollectionUtils.isEmpty(saveParam.getSetting4RationGroupIndividual())) {
                throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(161293, "分组设置不能为空"));
            }
            saveParam.getSetting4RationGroupIndividual().forEach(s -> {
                if (Objects.isNull(s.getId()) || StringUtils.isEmpty(s.getValue())) {
                    throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(161294, "分组设置参数错误"));
                }
            });
        }
    }

    @Data
    //"薪酬统计维度定性设置保存参数")
    public static class Setting4Qualitative {

        //关联id")
        private String id;

        //统计维度名称")
        private String name;
    }

    @Data
    //"薪酬统计维度定量-组距式分组设置保存参数")
    public static class Setting4RationGroupSpacing {

        //id")
        private Long id;

        //起始是否包含")
        private boolean includeStart;

        // 起始值")
        private String startValue;

        // 结束是否包含")
        private boolean includeEnd;

        //结束值")
        private String endValue;
    }

    @Data
    //"薪酬统计维度定量-单项式分组设置保存参数")
    public static class Setting4RationGroupIndividual {

        //id")
        private Long id;

        // 值")
        private String value;
    }
}
