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

import cn.hutool.json.JSONUtil;
import com.engine.salary.constant.SalaryTemplateSalaryItemSetGroupConstant;
import com.engine.salary.entity.salaryBill.dto.SalaryTemplateSalaryItemListDTO;
import com.engine.salary.entity.salaryBill.dto.SalaryTemplateSalaryItemSetListDTO;
import com.engine.salary.entity.salaryBill.param.SalaryTemplateSaveParam;
import com.engine.salary.entity.salaryBill.po.SalaryTemplatePO;
import com.engine.salary.entity.salarysob.dto.SalarySobItemAggregateDTO;
import com.engine.salary.util.JsonUtil;
import com.engine.salary.util.SalaryI18nUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Description: 工资单
 */
public class SalaryTemplateBO {

    @Override
    public String toString() {
        return "SalaryTemplateBO{}";
    }


    /**
     * 获取人员信息项目分类
     *
     * @param salaryItemSetting
     * @return
     */
    public static SalaryTemplateSalaryItemSetListDTO getEmployeeInformation(List<SalaryTemplateSalaryItemSetListDTO> salaryItemSetting) {
        Optional<SalaryTemplateSalaryItemSetListDTO> optionalEmployeeInformation = salaryItemSetting.stream().filter(e -> SalaryTemplateSalaryItemSetGroupConstant.EMPLOYEE_INFO_GROUP_ID.equals(e.getGroupId())).findFirst();
        SalaryTemplateSalaryItemSetListDTO employeeInformation = optionalEmployeeInformation.orElse(null);
        // 排除名称为空的
        if (employeeInformation != null && org.apache.commons.collections.CollectionUtils.isNotEmpty(employeeInformation.getItems())) {
            employeeInformation.setItems(employeeInformation.getItems().stream().filter(e -> StringUtils.isNotEmpty(e.getName())).collect(Collectors.toList()));
        }
        return employeeInformation;
    }

    /**
     * 保存参数转表数据
     * @param saveParam
     * @param employeeId
     * @return
     */
    public static SalaryTemplatePO convertToPO(SalaryTemplateSaveParam saveParam, Long employeeId) {
        if (saveParam == null) {
            return null;
        }

        return SalaryTemplatePO.builder()
                .id(saveParam.getId())
                .name(saveParam.getName())
                .salarySobId(saveParam.getSalarySobId())
                .description(saveParam.getDescription())
                .emailStatus(saveParam.getEmailStatus()?1:0)
//                .sendEmailId(saveParam.getEmailStatus()?saveParam.getSendEmail():0L)
                .msgStatus(saveParam.getMsgStatus()?1:0)
                .theme(saveParam.getTheme())
                .background(saveParam.getBackground())
                .textContent(saveParam.getTextContent())
                .textContentPosition(saveParam.getTextContentPosition())
                .salaryItemNullStatus(saveParam.getSalaryItemNullStatus()?1:0)
                .salaryItemZeroStatus(saveParam.getSalaryItemZeroStatus()?1:0)
                .salaryItemSetting(saveParam.getSalaryItemSetting() != null ? JSONUtil.toJsonStr(saveParam.getSalaryItemSetting()) : "")
                .replenishName(saveParam.getReplenishName())
                .replenishRule(saveParam.getReplenishRule())
                .replenishSalaryItemSetting((saveParam.getSalaryItemSetting() != null ? JsonUtil.toJsonString(saveParam.getReplenishSalaryItemSetting()) : ""))
                .autoSendStatus(saveParam.getAutoSendStatus()?1:0)
                .autoSendDayOfMonth(saveParam.getAutoSendDayOfMonth())
                .autoSendTimeOfDay(saveParam.getAutoSendTimeOfDay())
                .ackFeedbackStatus(saveParam.getAckFeedbackStatus()?1:0)
                .feedbackStatus(saveParam.getFeedbackStatus()?1:0)
                .autoAckDays(saveParam.getAutoAckDays())
                .feedbackUrl(saveParam.getFeedbackUrl())
                .mobileFeedbackUrl(saveParam.getMobileFeedbackUrl())
                .createTime(new Date())
                .updateTime(new Date())
                .creator(employeeId)
                .build();
    }

    public static List<SalaryTemplateSalaryItemSetListDTO> convertSalarySobItemAggregateToSalaryItemSet(SalarySobItemAggregateDTO salarySobItemAggregateDTO, Long currentEmployeeId, boolean isReplenish) {
        List<SalaryTemplateSalaryItemSetListDTO> groups = new LinkedList<>();
        if (salarySobItemAggregateDTO!=null) {
            // 1.员工信息
            if (CollectionUtils.isNotEmpty(salarySobItemAggregateDTO.getEmpFields())) {
                List<SalaryTemplateSalaryItemListDTO> items = new LinkedList<>();
                salarySobItemAggregateDTO.getEmpFields().stream().forEach(e->{
                    items.add(
                            SalaryTemplateSalaryItemListDTO.builder()
                                    .id(String.valueOf(e.getId()))
                                    .salaryItemId(String.valueOf(e.getId()))
                                    .name(e.getFieldName())
                                    .sortedIndex(e.getSortedIndex())
                                    .build()
                    );
                });
                groups.add(
                        SalaryTemplateSalaryItemSetListDTO.builder()
                                .groupId(SalaryTemplateSalaryItemSetGroupConstant.EMPLOYEE_INFO_GROUP_ID)
                                .groupName("员工信息")
                                .sortedIndex(-1)
                                .items(items)
                                .build());
            }
            // 2.普通分组
            if (CollectionUtils.isNotEmpty(salarySobItemAggregateDTO.getItemGroups())) {
                salarySobItemAggregateDTO.getItemGroups().stream().forEach(e->{
                    List<SalaryTemplateSalaryItemListDTO> items = new LinkedList<>();
                    e.getItems().stream().forEach(i->{
                        items.add(
                                SalaryTemplateSalaryItemListDTO.builder()
                                        .id(String.valueOf(i.getSalaryItemId()))
                                        .salaryItemId(String.valueOf(i.getSalaryItemId()))
                                        .name(i.getName())
                                        .salaryItemShowName(i.getName())
                                        .originName(i.getName())
                                        .sortedIndex(i.getSortedIndex())
                                        .build());
                    });
                    groups.add(
                            SalaryTemplateSalaryItemSetListDTO.builder()
                                    .groupId(String.valueOf(e.getId()))
                                    .groupName(e.getName())
                                    .sortedIndex(e.getSortedIndex())
                                    .items(items)
                                    .build());
                });
            }
            // 3.无分类
            if (CollectionUtils.isNotEmpty(salarySobItemAggregateDTO.getItems())) {
                List<SalaryTemplateSalaryItemListDTO> items = new LinkedList<>();
                salarySobItemAggregateDTO.getItems().stream().forEach(e->{
                    items.add(
                            SalaryTemplateSalaryItemListDTO.builder()
                                    .id(String.valueOf(e.getSalaryItemId()))
                                    .salaryItemId(String.valueOf(e.getSalaryItemId()))
                                    .name(e.getName())
                                    .salaryItemShowName(e.getName())
                                    .originName(e.getName())
                                    .sortedIndex(e.getSortedIndex())
                                    .build());
                });
                groups.add(
                        SalaryTemplateSalaryItemSetListDTO.builder()
                                .groupId(SalaryTemplateSalaryItemSetGroupConstant.NO_TYPE_GROUP_ID)
                                .groupName("无分类")
                                .sortedIndex(salarySobItemAggregateDTO.getItemGroups().size())
                                .items(items)
                                .build());
            }

            if (isReplenish) {
                // 4.已发补发
                if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(salarySobItemAggregateDTO.getBackCalcItems())) {
                    List<SalaryTemplateSalaryItemListDTO> items = new LinkedList<>();
                    salarySobItemAggregateDTO.getBackCalcItems().forEach(e -> {
                        items.add(
                                SalaryTemplateSalaryItemListDTO.builder()
                                        .id(String.valueOf(e.getSalaryItemId()))
                                        .salaryItemId(String.valueOf(e.getSalaryItemId()))
                                        .name(e.getName())
                                        .salaryItemShowName(e.getName())
                                        .originName(e.getName())
                                        .sortedIndex(e.getSortedIndex())
                                        .build()
                        );
                    });

                    groups.add(
                            SalaryTemplateSalaryItemSetListDTO.builder()
                                    .groupId(SalaryTemplateSalaryItemSetGroupConstant.REPLENISH_GROUP_ID)
                                    .groupName(SalaryI18nUtil.getI18nLabel(139698, "已发补发"))
                                    .sortedIndex(groups.size() + 1)
                                    .items(items)
                                    .build());
                }
            }

        }
        return groups;
    }
}
