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

import com.engine.salary.enums.salaryarchive.SalaryArchiveItemAdjustReasonEnum;
import com.engine.salary.exception.SalaryRunTimeException;
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.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 薪资项目调整保存参数
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
//薪资项目调整保存参数")
public class SalaryArchiveItemSaveParam {

    // 主键id
    private Long salaryArchiveItemId;

    //薪资档案id
    private Long salaryArchiveId;

    //生效时间
    private Date effectiveTime;

    // 是否允许编辑
    private Boolean canOperator;

    /**
     * 调整原因
     *
     * @see SalaryArchiveItemAdjustReasonEnum
     */
    private String adjustReason;

    //备注
    private String description;

    //薪资项目
    private List<SalaryArchiveItemDetailSaveParam> salaryArchiveItems;



    public static void checkParam(SalaryArchiveItemSaveParam saveParam) {
        if (saveParam.getSalaryArchiveId() == null) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(100305, "薪资档案id必传"));
        }
        if (saveParam.getAdjustReason() == null) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(100306, "请选择调整原因"));
        }
//        if (SalaryArchiveItemAdjustReasonEnum.parseByValue(saveParam.getAdjustReason()) == null) {
//            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(100306, "调整原因不在范围内"));
//        }
        if (saveParam.getEffectiveTime() == null) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(100307, "请选择生效日期"));
        }
        if (CollectionUtils.isEmpty(saveParam.getSalaryArchiveItems())) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(100308, "请输入薪资项目明细"));
        }
        Set<Long> salaryItemIds = new HashSet<>();
        for (SalaryArchiveItemDetailSaveParam e : saveParam.getSalaryArchiveItems()) {
            if (e.getSalaryItemId() == null || StringUtils.isEmpty((e.getAdjustValue() + "").trim())) {
                throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(100309, "明细中的薪资项目不能为空并且调整后必须输入值"));
            }
            salaryItemIds.add(e.getSalaryItemId());
        }
        if (saveParam.getSalaryArchiveItems().size() != salaryItemIds.size()) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(100310, "明细中的薪资项目不能重复"));
        }
    }
}
