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

import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salarysob.dto.SalaryApprovalDTO;
import com.engine.salary.entity.salarysob.dto.SalarySobItemAggregateDTO;
import com.engine.salary.entity.salarysob.dto.SalarySobItemDTO;
import com.engine.salary.entity.salarysob.dto.SalarySobItemGroupDTO;
import com.engine.salary.entity.salarysob.po.SalaryApprovalRulePO;
import com.engine.salary.util.JsonUtil;
import com.engine.salary.util.SalaryEntityUtil;
import org.apache.commons.lang.StringUtils;
import weaver.hrm.User;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Harryxzy
 * @ClassName SalaryApprovalBO
 * @date 2024/04/23 17:47
 * @description 薪资账套的薪资审批
 */
public class SalaryApprovalBO {

    public static SalaryApprovalDTO convert2DTO(SalaryApprovalRulePO salaryApprovalRulePO, List<SalaryItemPO> salaryItemList) {
        if (salaryApprovalRulePO == null) {
            return  new SalaryApprovalDTO();
        }
        Map<Long, String> salaryItemNameMap = SalaryEntityUtil.convert2Map(salaryItemList, SalaryItemPO::getId, SalaryItemPO::getName);
        List<SalaryApprovalDTO.approvalItemGroup> approvalItemGroups = JsonUtil.parseList(salaryApprovalRulePO.getApprovalGroupSetting(), SalaryApprovalDTO.approvalItemGroup.class);
        for (SalaryApprovalDTO.approvalItemGroup group : approvalItemGroups) {
            for(SalaryApprovalDTO.approvalItem item : group.getApprovalItems()) {
                item.setSalaryItemName(salaryItemNameMap.getOrDefault(item.getSalaryItemId(), ""));
            }
        }

        return SalaryApprovalDTO.builder()
                .id(salaryApprovalRulePO.getId())
                .salarySobId(salaryApprovalRulePO.getSalarySobId())
                .isOpenApproval(salaryApprovalRulePO.getOpenApproval().equals(1))
                .approvalWorkflowUrl(StringUtils.isBlank(salaryApprovalRulePO.getWorkflowUrl()) ? "" : salaryApprovalRulePO.getWorkflowUrl())
                .approvalItemGroup(approvalItemGroups)
                .build();

    }

    /**
     * 将账套薪资项目设置转换成审批中的项目设置信息
     * @param aggregateBySalarySobId
     */
    public static String sobItemAggregate2approvalGroupSetting(SalarySobItemAggregateDTO aggregateBySalarySobId) {
        List<SalaryApprovalDTO.approvalItemGroup> groupList = new ArrayList<>();
        // 薪资项目分组
        List<SalarySobItemGroupDTO> itemGroups = aggregateBySalarySobId.getItemGroups();
        for (SalarySobItemGroupDTO sobItemGroupDTO: itemGroups) {
            SalaryApprovalDTO.approvalItemGroup approvalGroup = new SalaryApprovalDTO.approvalItemGroup();
            approvalGroup.setGroupName(sobItemGroupDTO.getName());
            approvalGroup.setSorted(sobItemGroupDTO.getSortedIndex());
            List<SalaryApprovalDTO.approvalItem> approvalItemList = new ArrayList<>();
            for (SalarySobItemDTO groupItem : sobItemGroupDTO.getItems()) {
                SalaryApprovalDTO.approvalItem approvalItem = new SalaryApprovalDTO.approvalItem();
                approvalItem.setSalaryItemId(groupItem.getSalaryItemId());
                approvalItem.setSalaryItemName(groupItem.getName());
                approvalItem.setSorted(groupItem.getSortedIndex());
                approvalItemList.add(approvalItem);
            }
            approvalGroup.setApprovalItems(approvalItemList);
            groupList.add(approvalGroup);
        }
        // 未分类
        List<SalarySobItemDTO> items = aggregateBySalarySobId.getItems();
        List<SalaryApprovalDTO.approvalItem> approvalItemList2 = new ArrayList<>();
        for (SalarySobItemDTO groupItem : items) {
            SalaryApprovalDTO.approvalItem approvalItem = new SalaryApprovalDTO.approvalItem();
            approvalItem.setSalaryItemId(groupItem.getSalaryItemId());
            approvalItem.setSalaryItemName(groupItem.getName());
            approvalItem.setSorted(groupItem.getSortedIndex());
            approvalItemList2.add(approvalItem);
        }
        SalaryApprovalDTO.approvalItemGroup approvalGroup = new SalaryApprovalDTO.approvalItemGroup();
        approvalGroup.setGroupName("未分类");
        approvalGroup.setSorted(groupList.size());
        approvalGroup.setApprovalItems(approvalItemList2);
        groupList.add(approvalGroup);
        return JsonUtil.toJsonString(groupList);
    }


}
