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

import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.salarysob.param.SalarySobItemSaveParam;
import com.engine.salary.entity.salarysob.po.SalarySobEmpFieldPO;
import com.engine.salary.entity.salarysob.po.SalarySobItemGroupPO;
import com.engine.salary.entity.salarysob.po.SalarySobItemPO;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 薪资账套的薪资项目副本保存
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalarySobItemSaveBO {

    /**
     * 处理前端传回的保存参数，转换成po
     *
     * @param saveParam
     * @param employeeId
     * @return
     */
    public static Result handle(SalarySobItemSaveParam saveParam, Long employeeId) {
        Date now = new Date();
        Result result = new Result()
                .setNeedInsertSalarySobEmpFields(Lists.newArrayList())
                .setNeedInsertSalarySobItems(Lists.newArrayList())
                .setNeedInsertSalarySobItemGroups(Lists.newArrayList());
        // 处理需要新增的员工信息字段
        for (SalarySobItemSaveParam.SalarySobEmpFieldParam salarySobEmpFieldParam : saveParam.getEmpFields()) {
            SalarySobEmpFieldPO salarySobEmpFieldPO = SalarySobEmpFieldPO.builder()
                    .salarySobId(saveParam.getSalarySobId())
                    .fieldCode(salarySobEmpFieldParam.getFieldId())
                    .sortedIndex(salarySobEmpFieldParam.getSortedIndex())
                    .canDelete(NumberUtils.INTEGER_ONE)
                    .creator(employeeId)
                    .createTime(now)
                    .updateTime(now)
                    .deleteType(NumberUtils.INTEGER_ZERO)
                    .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                    .build();
            result.getNeedInsertSalarySobEmpFields().add(salarySobEmpFieldPO);
        }
        // 处理需要新增的薪资项目和薪资项目分组
        int sortedIndex = 0;
        for (SalarySobItemSaveParam.SalarySobItemGroupParam itemGroupParam : saveParam.getItemGroups()) {
            SalarySobItemGroupPO salarySobItemGroupPO = SalarySobItemGroupPO.builder()
                    .salarySobId(saveParam.getSalarySobId())
                    .name(itemGroupParam.getName())
                    .sortedIndex(sortedIndex++)
                    .description(StringUtils.EMPTY)
                    .creator(employeeId)
                    .createTime(now)
                    .updateTime(now)
                    .deleteType(NumberUtils.INTEGER_ZERO)
                    .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                    .build();
            result.getNeedInsertSalarySobItemGroups().add(salarySobItemGroupPO);
            // 处理薪资项目分组中的薪资项目
            handleSalarySobItems(result, saveParam.getSalarySobId(), salarySobItemGroupPO.getId(), itemGroupParam.getItems(), employeeId, SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY);
        }
        // 处理未进行分组的薪资项目
        handleSalarySobItems(result, saveParam.getSalarySobId(), NumberUtils.LONG_ZERO, saveParam.getItems(), employeeId, SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY);
        return result;
    }

    /**
     * 将前端传递的参数转换成SalarySobItemPO
     *
     * @param result
     * @param salarySobId
     * @param salarySobItemGroupId
     * @param itemParams
     * @param employeeId
     * @param tenantKey
     */
    private static void handleSalarySobItems(Result result,
                                             Long salarySobId,
                                             Long salarySobItemGroupId,
                                             List<SalarySobItemSaveParam.SalarySobItemParam> itemParams,
                                             Long employeeId, String tenantKey) {
        Date now = new Date();
        for (SalarySobItemSaveParam.SalarySobItemParam itemParam : itemParams) {
            SalarySobItemPO salarySobItemPO = SalarySobItemPO.builder()
                    .salarySobId(salarySobId)
                    .salaryItemId(itemParam.getSalaryItemId())
                    .salarySobItemGroupId(salarySobItemGroupId)
                    .formulaId(Optional.ofNullable(itemParam.getFormulaId()).orElse(NumberUtils.LONG_ZERO))
                    .sortedIndex(itemParam.getSortedIndex())
                    .description(StringUtils.EMPTY)
                    .creator(employeeId)
                    .createTime(now)
                    .updateTime(now)
                    .deleteType(NumberUtils.INTEGER_ZERO)
                    .tenantKey(tenantKey)
                    .build();
            result.getNeedInsertSalarySobItems().add(salarySobItemPO);
        }
    }

    @Data
    @Accessors(chain = true)
    public static class Result {

        /**
         * 薪资账套中需要新增的员工信息字段
         */
        private Collection<SalarySobEmpFieldPO> needInsertSalarySobEmpFields;

        /**
         * 薪资账套中需要新增的薪资项目副本
         */
        private Collection<SalarySobItemPO> needInsertSalarySobItems;

        /**
         * 薪资账套中需要新增的薪资项目分组
         */
        private Collection<SalarySobItemGroupPO> needInsertSalarySobItemGroups;
    }
}
