package com.engine.salary.entity.salarysob.bo;

import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.salarysob.dto.SalarySobBasicFormDTO;
import com.engine.salary.entity.salarysob.dto.SalarySobListDTO;
import com.engine.salary.entity.salarysob.param.SalarySobBasicSaveParam;
import com.engine.salary.entity.salarysob.po.SalarySobPO;
import com.engine.salary.enums.SalaryCycleTypeEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.util.SalaryI18nUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 薪资账套
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/

public class SalarySobBO {

    /**
     * 薪资账套基础设置保存参数转换成薪资账套基础设置po
     *
     * @param saveParam  薪资账套基础设置保存参数
     * @param employeeId 人员id
     * @return
     */
    public static SalarySobPO convert2PO(SalarySobBasicSaveParam saveParam, Long employeeId) {
        Date now = new Date();
        if (saveParam == null) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(84026, "参数错误"));
        }
        return SalarySobPO.builder()
                .name(saveParam.getName())
                .incomeCategory(saveParam.getTaxableItems())
                .salaryCycleType(saveParam.getSalaryCycleType())
                .salaryCycleFromDay(saveParam.getSalaryCycleFromDay())
                .taxCycleType(saveParam.getTaxCycleType())
                .attendCycleType(saveParam.getAttendCycleType())
                .attendCycleFromDay(saveParam.getAttendCycleFromDay())
                .socialSecurityCycleType(saveParam.getSocialSecurityCycleType())
                .disable(NumberUtils.INTEGER_ZERO)
                .creator(employeeId)
                .createTime(now)
                .updateTime(now)
                .deleteType(NumberUtils.INTEGER_ZERO)
                .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                .taxAgentId(saveParam.getTaxAgentId())
                .build();
    }

    /**
     * 薪资账套po转换成列表dto
     *
     * @param salarySobs 薪资账套po
     * @return
     */
    public static List<SalarySobListDTO> convert2ListDTO(Collection<SalarySobPO> salarySobs,Map<Long, String> taxAgentIdTONameMap) {
        if (CollectionUtils.isEmpty(salarySobs)) {
            return Collections.emptyList();
        }
        return salarySobs.stream()
                .map(salarySobPO -> SalarySobListDTO.builder()
                        .id(salarySobPO.getId())
                        .name(salarySobPO.getName())
                        .taxAgentName(taxAgentIdTONameMap.get(salarySobPO.getTaxAgentId()))
                        .taxAgentId(salarySobPO.getTaxAgentId())
                        .salaryCycle(buildSalaryCycle(salarySobPO))
                        .disable(salarySobPO.getDisable())
                        .description(salarySobPO.getDescription())
                        .build())
                .collect(Collectors.toList());
    }

    /**
     * 解析薪资账套列表的薪资周期
     *
     * @param salarySobPO
     * @return
     */
    private static String buildSalaryCycle(SalarySobPO salarySobPO) {
        String salaryCycleStr;
        SalaryCycleTypeEnum salaryCycleTypeEnum = SalaryCycleTypeEnum.parseByValue(salarySobPO.getSalaryCycleType());
        if (salaryCycleTypeEnum == null) {
            return StringUtils.EMPTY;
        }
        Integer salaryCycleFromDay = salarySobPO.getSalaryCycleFromDay();
        switch (salaryCycleTypeEnum) {
            case BEFORE_LAST_MONTH:
                salaryCycleStr = Objects.equals(salaryCycleFromDay, 1) ? SalaryI18nUtil.getI18nLabel(98395, "上上月1号——上上月最后一天")
                        : SalaryI18nUtil.getI18nLabel(98399, "上上月{0}号——上月{1}号")
                        .replace("{0}", "" + salaryCycleFromDay)
                        .replace("{1}", "" + (salaryCycleFromDay - 1));
                break;
            case LAST_MONTH:
                salaryCycleStr = Objects.equals(salaryCycleFromDay, 1) ? SalaryI18nUtil.getI18nLabel(98396, "上月1号——上月最后一天")
                        : SalaryI18nUtil.getI18nLabel(98400, "上月{0}号——本月{1}号")
                        .replace("{0}", "" + salaryCycleFromDay)
                        .replace("{1}", "" + (salaryCycleFromDay - 1));
                break;
            case THIS_MONTH:
                salaryCycleStr = Objects.equals(salaryCycleFromDay, 1) ? SalaryI18nUtil.getI18nLabel(98397, "本月1号——本月最后一天")
                        : SalaryI18nUtil.getI18nLabel(98401, "本月{0}号——下月{1}号")
                        .replace("{0}", "" + salaryCycleFromDay)
                        .replace("{1}", "" + (salaryCycleFromDay - 1));
                break;
            case NEXT_MONTH:
                salaryCycleStr = Objects.equals(salaryCycleFromDay, 1) ? SalaryI18nUtil.getI18nLabel(98398, "下月1号——下月最后一天")
                        : SalaryI18nUtil.getI18nLabel(98402, "下月{0}号——下下月{1}号")
                        .replace("{0}", "" + salaryCycleFromDay)
                        .replace("{1}", "" + (salaryCycleFromDay - 1));
                break;
            default:
                salaryCycleStr = "";
        }
        return salaryCycleStr;
    }

    /**
     * 薪资账套po转换成薪资账套详情dto
     *
     * @param salarySobPO 薪资账套po
     * @return
     */
    public static SalarySobBasicFormDTO convert2FormDTO(SalarySobBasicFormDTO basicForm,SalarySobPO salarySobPO) {
        return basicForm
                .setId(salarySobPO.getId())
                .setName(salarySobPO.getName())
                .setName(salarySobPO.getName())
                .setTaxableItems(salarySobPO.getIncomeCategory())
                .setSalaryCycleType(salarySobPO.getSalaryCycleType())
                .setSalaryCycleFromDay(salarySobPO.getSalaryCycleFromDay())
                .setTaxCycleType(salarySobPO.getTaxCycleType())
                .setAttendCycleType(salarySobPO.getAttendCycleType())
                .setAttendCycleFromDay(salarySobPO.getAttendCycleFromDay())
                .setSocialSecurityCycleType(salarySobPO.getSocialSecurityCycleType())
                .setTaxAgentId(salarySobPO.getTaxAgentId())
                .setDescription(salarySobPO.getDescription());

    }
}
