package com.engine.salary.wrapper;

import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.entity.salaryBill.bo.SalaryBillItemNameBO;
import com.engine.salary.entity.salaryBill.dto.*;
import com.engine.salary.entity.salaryBill.param.*;
import com.engine.salary.entity.salaryBill.po.SalaryBillItemNamePO;
import com.engine.salary.entity.salaryBill.po.SalaryTemplatePO;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salarysob.po.SalarySobBackItemPO;
import com.engine.salary.entity.salarysob.po.SalarySobItemGroupPO;
import com.engine.salary.entity.salarysob.po.SalarySobPO;
import com.engine.salary.enums.salarybill.SalaryTemplateReplenishRuleEnum;
import com.engine.salary.enums.salarybill.SalaryTemplateVarEnum;
import com.engine.salary.enums.salarybill.SalaryTemplateWhetherEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.mapper.salarysob.SalarySobItemGroupMapper;
import com.engine.salary.service.*;
import com.engine.salary.service.impl.*;
import com.engine.salary.util.JsonUtil;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.db.MapperProxyFactory;
import com.engine.salary.util.page.PageInfo;
import com.mzlion.core.utils.BeanUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import weaver.hrm.User;

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

/**
 * @Description: 工资单模板
 * @Author: wangxiangzhong
 * @Date: 2022/3/16 13:56
 */
public class SalaryTemplateWrapper extends Service {

    private SalaryTemplateService getSalaryTemplateService(User user) {
        return ServiceUtil.getService(SalaryTemplateServiceImpl.class, user);
    }
//    
//    private MailBaseService mailBaseService;

    private SalarySobService getSalarySobService(User user) {
        return ServiceUtil.getService(SalarySobServiceImpl.class, user);
    }

    private SalarySobBackItemService getSalarySobBackItemService(User user) {
        return ServiceUtil.getService(SalarySobBackItemServiceImpl.class, user);
    }

    private SalaryItemService getSalaryItemService(User user) {
        return ServiceUtil.getService(SalaryItemServiceImpl.class, user);
    }

    private SalarySobItemGroupService getSalarySobItemGroupService(User user) {
        return ServiceUtil.getService(SalarySobItemGroupServiceImpl.class, user);
    }

    private TaxAgentService getTaxAgentService(User user) {
        return ServiceUtil.getService(TaxAgentServiceImpl.class, user);
    }

    private SalaryBillBaseSetService getSalaryBillBaseSetService(User user) {
        return ServiceUtil.getService(SalaryBillBaseSetServiceImpl.class, user);
    }

    private SalaryBillItemNameService getSalaryBillItemNameService(User user) {
        return ServiceUtil.getService(SalaryBillItemNameServiceImpl.class, user);
    }

    private SalarySobItemGroupMapper getSalarySobItemGroupMapper() {
        return MapperProxyFactory.getProxy(SalarySobItemGroupMapper.class);
    }

    /**
     * 工资单模板列表
     *
     * @param queryParam
     * @return
     */
    public PageInfo<SalaryTemplateListDTO> list(SalaryTemplateQueryParam queryParam) {
        PageInfo<SalaryTemplateListDTO> listPage = getSalaryTemplateService(user).listPage(queryParam);
        return listPage;
//        SalaryWeaTable<SalaryTemplateListDTO> table = new SalaryWeaTable<SalaryTemplateListDTO>(user, SalaryTemplateListDTO.class);
//
//        String fields = " t.id" +
//                "        , t.name" +
//                "        , t.replenish_name as replenishName" +
//                "        , s.name as salarySob" +
//                "        , t.use_type as useType" +
//                "        , t.description";
//
//        String from = " from hrsa_salary_template t left join hrsa_salary_sob s on t.salary_sob_id = s.id ";
//
//        table.setBackfields(fields);
//        table.setSqlform(from);
//        table.setSqlwhere(makeSqlWhere(queryParam));
//        table.setSqlorderby("t.id DESC");
//        table.setSqlprimarykey("t.id");
//        table.setSqlisdistinct("false");
//
//        WeaResultMsg result = new WeaResultMsg(false);
//        result.putAll(table.makeDataResult());
//        result.success();
//        return result.getResultMap();
    }

//    private String makeSqlWhere(SalaryTemplateQueryParam queryParam) {
//        DBType dbType = DBType.get(new RecordSet().getDBType());
//
//        String sqlWhere = " t.delete_type = 0 ";
//
//        String name = queryParam.getName();
//        if (StringUtils.isNotBlank(name)) {
//            sqlWhere += " AND t.name " + dbType.like(name);
//        }
//
//        Collection<Long> ids = queryParam.getIds();
//
//        if (ids != null && ids.size() > 0) {
//            sqlWhere += " AND t.id in (" + ids.stream().map(Object::toString).collect(Collectors.joining(",")) + ") ";
//        }
//
//        Long salarySobId = queryParam.getSalarySobId();
//
//        if (salarySobId != null) {
//            sqlWhere += " AND t.salary_sob_id = " + salarySobId;
//        }
//
//        long currentEmployeeId = user.getUID();
//        Boolean needAuth = getTaxAgentService(user).isNeedAuth(currentEmployeeId);
//        if (needAuth) {
//            List<SalarySobPO> salarySobPOS = getSalarySobService(user).listByDisable(NumberUtils.INTEGER_ZERO);
//            Set<Long> salarySobIds = SalaryEntityUtil.properties(salarySobPOS, SalarySobPO::getId);
//            if (CollectionUtils.isNotEmpty(salarySobIds)) {
//                sqlWhere += " AND t.salary_sob_id in (" + salarySobIds.stream().map(Object::toString).collect(Collectors.joining(",")) + ") ";
//            } else {
//                sqlWhere += " AND 1=2 ";
//            }
//        }
//
//
//        return sqlWhere;
//    }

    /**
     * 获取工资单模板基础设置表单
     *
     * @param id
     * @return
     */
    public SalaryTemplateBaseFormDTO getBaseForm(Long id) {

        SalaryTemplateBaseSetDTO salaryTemplateBaseSetDTO = new SalaryTemplateBaseSetDTO();

        List<Map<String, String>> replenishRuleSetOptionList = Collections.emptyList();
        if (id != null) {
            SalaryTemplatePO po = getSalaryTemplateService(user).getById(id);
            if (po == null) {
                throw new SalaryRunTimeException("工资单模板不存在");
            }
            BeanUtils.copyProperties(po, salaryTemplateBaseSetDTO);
            salaryTemplateBaseSetDTO.setSendEmail(po.getSendEmailId());
            salaryTemplateBaseSetDTO.setEmailStatus(po.getEmailStatus().equals(SalaryTemplateWhetherEnum.TRUE.getValue()));
            salaryTemplateBaseSetDTO.setMsgStatus(po.getMsgStatus().equals(SalaryTemplateWhetherEnum.TRUE.getValue()));
            salaryTemplateBaseSetDTO.setSalarySob(po.getSalarySobId());
            salaryTemplateBaseSetDTO.setSendEmail(po.getSendEmailId());
            salaryTemplateBaseSetDTO.setAutoSendStatus(po.getAutoSendStatus() != null && po.getAutoSendStatus().equals(SalaryTemplateWhetherEnum.TRUE.getValue()));
            salaryTemplateBaseSetDTO.setAckFeedbackStatus(po.getAckFeedbackStatus() != null && NumberUtils.compare(po.getAckFeedbackStatus(), 1) == 0);
            if (po.getFeedbackStatus() == null) {
                salaryTemplateBaseSetDTO.setFeedbackStatus(salaryTemplateBaseSetDTO.getAckFeedbackStatus());
            } else {
                salaryTemplateBaseSetDTO.setFeedbackStatus(NumberUtils.compare(po.getFeedbackStatus(), 1) == 0);
            }

            salaryTemplateBaseSetDTO.setAutoAckDays(po.getAutoAckDays() == null ? 7 : po.getAutoAckDays());
            // 规则赋值 如果为ALL传“” 如果为byRule传薪资项目ID
            salaryTemplateBaseSetDTO.setReplenishRule(SalaryTemplateReplenishRuleEnum.ALL.getValue().equals(po.getReplenishRule()) ? "" : po.getReplenishRule());
            // 规则设置赋值
//            salaryTemplateBaseSetDTO.setReplenishRuleSet(po.getReplenishRule());
            // 修改时获取补规则设置下拉列表
            replenishRuleSetOptionList = getReplenishRuleSetOptions(po.getSalarySobId());
        }



        // 查询所有启用的薪资账套
        List<SalarySobPO> salarySobs = getSalarySobService(user).listByDisable(NumberUtils.INTEGER_ZERO);
        List<Map<String, Object>> salarySobOptions = salarySobs.stream().map(salarySobPO -> {
            Map<String, Object> item = new HashMap<>();
            item.put("id", salarySobPO.getId());
            item.put("name", salarySobPO.getName());
            return item;
        }).collect(Collectors.toList());

        // 邮箱下拉
//        List<WeaFormOption> sendEmailOptions = Lists.newArrayList();
//        MailPublicAccountListObj mailPublicAccountListObj = mailBaseService.getMailPublicAccountList(employeeId, tenantKey);
//        List<MailPublicAccountBo> mailPublicAccountBos = mailPublicAccountListObj.getMailPublicAccountList();
//        mailPublicAccountBos.forEach(e->sendEmailOptions.add(new WeaFormOption(String.valueOf(e.getId()), e.getMailAddress())));

//        WeaForm salaryTemplateBase = SalaryFormatUtil.<SalaryTemplateBaseSetDTO>getInstance().buildForm(SalaryTemplateBaseSetDTO.class, SalaryTemplateBaseSetDTO.builder().salarySobOptions(salarySobOptions).sendEmailOptions(sendEmailOptions).build());

        if (id == null) {
            // 新建，获取默认的确认反馈信息
            SalaryBillAckFeedbackDTO defaultAckFeedback = getSalaryBillBaseSetService(user).getDefaultAckFeedbackSetting();
            salaryTemplateBaseSetDTO.setAckFeedbackStatus(StringUtils.equals(defaultAckFeedback.getAckStatus(), "1"));
            salaryTemplateBaseSetDTO.setAutoAckDays(defaultAckFeedback.getAutoAckDays());
            salaryTemplateBaseSetDTO.setFeedbackStatus(StringUtils.equals(defaultAckFeedback.getFeedbackStatus(), "1"));
            salaryTemplateBaseSetDTO.setFeedbackUrl(defaultAckFeedback.getFeedBackUrl());
            salaryTemplateBaseSetDTO.setMobileFeedbackUrl(defaultAckFeedback.getMobileFeedbackUrl());
        }

        // 如果设置了pc端反馈流程地址，移动端没有设置,则移动端与pc端一致
        if (StringUtils.isNotBlank(salaryTemplateBaseSetDTO.getFeedbackUrl()) && StringUtils.isBlank(salaryTemplateBaseSetDTO.getMobileFeedbackUrl())) {
            salaryTemplateBaseSetDTO.setMobileFeedbackUrl(salaryTemplateBaseSetDTO.getFeedbackUrl());
        }

        Map<String, Object> salaryTemplateBase = new HashMap<>();
        salaryTemplateBase.put("data", salaryTemplateBaseSetDTO);
        salaryTemplateBase.put("salarySobOptions", salarySobOptions);
        // 编辑时禁止修改薪资账套 前端实现
        // 分组 前端实现

        return SalaryTemplateBaseFormDTO.builder().id(id).salaryTemplateBaseSet(salaryTemplateBase).replenishRuleSetOption(replenishRuleSetOptionList).build();

    }

    /**
     * 获取工资单模板显示设置表单
     *
     * @param id
     * @return
     */
    public SalaryTemplateShowFormDTO getShowForm(Long id) {
        SalaryTemplateShowSetDTO salaryTemplateShowSetDTO = new SalaryTemplateShowSetDTO();

        SalaryTemplatePO po = new SalaryTemplatePO();
        if (id != null) {
            po = getSalaryTemplateService(user).getById(id);
            if (po == null) {
                throw new SalaryRunTimeException("工资单模板不存在");
            }
            BeanUtils.copyProperties(po, salaryTemplateShowSetDTO);
            salaryTemplateShowSetDTO.setTextContentPosition(po.getTextContentPosition().toString());
            salaryTemplateShowSetDTO.setSalaryItemNullStatus(po.getSalaryItemNullStatus().equals(SalaryTemplateWhetherEnum.TRUE.getValue()));
            salaryTemplateShowSetDTO.setSalaryItemZeroStatus(po.getSalaryItemZeroStatus().equals(SalaryTemplateWhetherEnum.TRUE.getValue()));
        }

        Map<String, Object> salaryTemplateShowSet = new HashMap<>();
        salaryTemplateShowSet.put("data", salaryTemplateShowSetDTO);

        // 薪资项目设置
        List<SalaryTemplateSalaryItemSetListDTO> salaryTemplateShowSetData = new ArrayList<>();
        if (StringUtils.isNotEmpty(po.getSalaryItemSetting())) {
            salaryTemplateShowSetData = JsonUtil.parseList(po.getSalaryItemSetting(), SalaryTemplateSalaryItemSetListDTO.class);
        }
        // 补发薪资项目设置，前端保存时需要使用
        List<SalaryTemplateSalaryItemSetListDTO> replenishSalaryTemplateSalaryItemSet = new ArrayList<>();
        if (StringUtils.isNotEmpty(po.getReplenishSalaryItemSetting())) {
            replenishSalaryTemplateSalaryItemSet = JsonUtil.parseList(po.getReplenishSalaryItemSetting(), SalaryTemplateSalaryItemSetListDTO.class);
        }

        //同步项目名称
        List<SalaryItemPO> itemPOS = getSalaryItemService(user).listAll();
        Map<Long, String> itemIdNameMap = SalaryEntityUtil.convert2Map(itemPOS, SalaryItemPO::getId, SalaryItemPO::getName);

//        List<SalarySobItemGroupPO> groupPOS = getSalarySobItemGroupService(user).listBySalarySobId(po.getSalarySobId());
//        Map<Long, String> groupIdNameMap = SalaryEntityUtil.convert2Map(groupPOS, SalarySobItemGroupPO::getId, SalarySobItemGroupPO::getName);

        // 获取工资单薪资项目展示名信息
        Map<Long, String> itemShowNameMap;
        Map<Long, SalaryBillItemNameDTO> salaryBillItemNameDTOMap = Collections.emptyMap();
        if (id != null) {
            List<SalaryBillItemNamePO> billItemNameList = getSalaryBillItemNameService(user).ListByTemplateAndType(SalaryBillItemNamePO.builder().salaryTemplateId(id).salaryBillType(0).build());
            itemShowNameMap = SalaryEntityUtil.convert2Map(billItemNameList, SalaryBillItemNamePO::getSalaryItemId, SalaryBillItemNamePO::getSalaryItemShowName);
            // 获取薪资项目信息
            List<SalaryBillItemNameDTO> salaryBillItemNameDTOS = SalaryBillItemNameBO.convert2ListDTO(billItemNameList);
            salaryBillItemNameDTOMap = SalaryEntityUtil.convert2Map(salaryBillItemNameDTOS, SalaryBillItemNameDTO::getSalaryItemId);
        } else {
            itemShowNameMap = Collections.emptyMap();
        }

        if (CollectionUtils.isNotEmpty(salaryTemplateShowSetData)) {
            salaryTemplateShowSetData.stream()
                    //排除人员消息
                    .filter(data -> !StringUtils.equals(data.getGroupId(), "111111111111111111")).forEach(data -> {
                        data.getItems().forEach(item -> {
                            Long salaryItemId = Long.valueOf(item.getSalaryItemId());
                            item.setOriginName(itemIdNameMap.getOrDefault(salaryItemId, item.getName()));
                            if (itemShowNameMap.containsKey(salaryItemId)) {
                                item.setSalaryItemShowName(itemShowNameMap.get(salaryItemId));
                                item.setName(itemShowNameMap.get(salaryItemId) + "（" + itemIdNameMap.getOrDefault(salaryItemId, item.getName()) + "）");
                            } else {
                                item.setSalaryItemShowName(itemIdNameMap.getOrDefault(salaryItemId, item.getName()));
                                item.setName(itemIdNameMap.getOrDefault(salaryItemId, item.getName()));
                            }
                        });
                    });
        }


        return SalaryTemplateShowFormDTO.builder().id(id).vars(Arrays.stream(SalaryTemplateVarEnum.values()).map(SalaryTemplateVarEnum::getValue).collect(Collectors.toList())).salaryTemplateShowSet(salaryTemplateShowSet).salaryTemplateSalaryItemSet(salaryTemplateShowSetData).replenishSalaryTemplateSalaryItemSet(replenishSalaryTemplateSalaryItemSet).salaryBillItemNameSet(salaryBillItemNameDTOMap).build();
    }

    /**
     * 获取薪资项目设置
     *
     * @param salarySobId
     * @return
     */
    public List<SalaryTemplateSalaryItemSetListDTO> getSalaryItemSet(Long salarySobId) {
        return getSalaryTemplateService(user).getSalaryItemSet(salarySobId, false);
    }


    /**
     * @param salarySobId
     * @return
     */
    public List<SalaryTemplateSalaryItemSetListDTO> getSmsSalaryItemSet(Long salarySobId) {
        List<SalaryTemplateSalaryItemSetListDTO> salaryItemSet = getSalaryTemplateService(user).getSalaryItemSet(salarySobId, false);

        salaryItemSet.add(0, SalaryTemplateSalaryItemSetListDTO.builder().groupId("444444444444444444L").groupName("基本信息").items(Collections.singletonList(SalaryTemplateSalaryItemListDTO.builder().name("薪资所属月").build())).build());
        return salaryItemSet;
    }

    public List<SalaryTemplateSalaryItemSetListDTO> getReplenishSalaryItemSet(Long salarySobId) {
        return getSalaryTemplateService(user).getSalaryItemSet(salarySobId, true);
    }

    /**
     * 工资单模板默认使用
     *
     * @param defaultUseParam
     * @return
     */
    public String defaultUse(SalaryTemplateDefaultUseParam defaultUseParam) {
        return getSalaryTemplateService(user).defaultUse(defaultUseParam);
    }

    /**
     * 新建工资单模板
     *
     * @param saveParam
     * @return
     */
    public String save(SalaryTemplateSaveParam saveParam) {
        return getSalaryTemplateService(user).save(saveParam);
    }

    /**
     * 编辑工资单模板
     *
     * @param saveParam
     * @return
     */
    public String update(SalaryTemplateSaveParam saveParam) {
        return getSalaryTemplateService(user).update(saveParam);
    }

    /**
     * 复制工资单模板
     *
     * @param copyParam
     * @return
     */
    public String copy(SalaryTemplateCopyParam copyParam) {
        return getSalaryTemplateService(user).copy(copyParam);
    }

    /**
     * 删除工资单模板
     *
     * @param ids
     * @return
     */
    public String delete(Collection<Long> ids) {
        return getSalaryTemplateService(user).delete(ids);
    }

    /**
     * 获取薪资账套下拉列表
     *
     * @return
     */
    public List<Map<String, Object>> selectSalarySobList() {
        return getSalaryTemplateService(user).selectSalarySobList();
    }


    /**
     * 获取补发规则设置下拉列表
     *
     * @param salarySobId
     * @return
     */
    public List<Map<String, String>> getReplenishRuleSetOptions(Long salarySobId) {
        if (salarySobId == null) {
            return Collections.emptyList();
        }
        SalarySobPO salarySob = getSalarySobService(user).getById(salarySobId);
        if (salarySob == null) {
            return Collections.emptyList();
        }
        // 查询薪资账套的回算薪资项目
        List<SalarySobBackItemPO> salarySobBackItems = getSalarySobBackItemService(user).listBySalarySobId(salarySobId);
        if (CollectionUtils.isEmpty(salarySobBackItems)) {
            salarySobBackItems = getSalarySobBackItemService(user).listBySalarySobId(salarySob.getId());
        }
        List<SalaryItemPO> salaryItemPOS = getSalaryItemService(user).listByIds(salarySobBackItems.stream().map(SalarySobBackItemPO::getSalaryItemId).collect(Collectors.toList()));

        String ruleTip = "(" + SalaryI18nUtil.getI18nLabel(140142, "不等于0") + ")";

        List<Map<String, String>> result = salaryItemPOS.stream().map(m -> {
            Map<String, String> map = new HashMap<>(2);
            map.put("id", String.valueOf(m.getId()));
            map.put("content", m.getName() + ruleTip);
            return map;
        }).collect(Collectors.toList());

        return result;
//        if(result!= null && result.size()>0){
//            return result.get(0);
//        }
//        return Collections.emptyMap();
    }

    /**
     * 获取工资单模板补发设置表单
     *
     * @param replenishFormQueryParam
     * @return
     */
    public SalaryTemplateReplenishFormDTO getReplenishForm(ReplenishFormQueryParam replenishFormQueryParam) {
        Long id = replenishFormQueryParam.getId();
        SalaryTemplateReplenishFormDTO salaryTemplateReplenishSetDTO = new SalaryTemplateReplenishFormDTO();

        // 补发薪资项目设置
        List<SalaryTemplateSalaryItemSetListDTO> replenishSalaryItemSetting;
        // 补发薪资项目显示名称
        Map<Long, SalaryBillItemNameDTO> salaryBillItemNameDTOMap = new HashMap<>();
        if (id != null) {
            SalaryTemplatePO po = getSalaryTemplateService(user).getById(id);
            if (po == null) {
                throw new SalaryRunTimeException(String.format(SalaryI18nUtil.getI18nLabel(100533, "工资单模板不存在") + "[id:%s]", id));
            }
            BeanUtils.copyProperties(po, salaryTemplateReplenishSetDTO);
            if (StringUtils.isNotEmpty(po.getReplenishSalaryItemSetting())) {
                replenishSalaryItemSetting = JsonUtil.parseList(po.getReplenishSalaryItemSetting(), SalaryTemplateSalaryItemSetListDTO.class);
                // 同步补发薪资项目名称
                List<SalaryItemPO> itemPOS = getSalaryItemService(user).listAll();
                Map<Long, String> itemIdNameMap = SalaryEntityUtil.convert2Map(itemPOS, SalaryItemPO::getId, SalaryItemPO::getName);
                // 获取工资单薪资项目展示名信息
                List<SalaryBillItemNamePO> billItemNameList = getSalaryBillItemNameService(user).ListByTemplateAndType(SalaryBillItemNamePO.builder().salaryTemplateId(id).salaryBillType(1).build());
                Map<Long, String> itemShowNameMap = SalaryEntityUtil.convert2Map(billItemNameList, SalaryBillItemNamePO::getSalaryItemId, SalaryBillItemNamePO::getSalaryItemShowName);
                // 获取薪资项目信息
                List<SalaryBillItemNameDTO> salaryBillItemNameDTOS = SalaryBillItemNameBO.convert2ListDTO(billItemNameList);
                salaryBillItemNameDTOMap = SalaryEntityUtil.convert2Map(salaryBillItemNameDTOS, SalaryBillItemNameDTO::getSalaryItemId);

                if (CollectionUtils.isNotEmpty(replenishSalaryItemSetting)) {
                    replenishSalaryItemSetting.stream()
                            //排除人员消息
                            .filter(data -> !StringUtils.equals(data.getGroupId(), "111111111111111111")).forEach(data -> {
                                data.getItems().forEach(item -> {
                                    Long salaryItemId = Long.valueOf(item.getSalaryItemId());
                                    item.setOriginName(itemIdNameMap.getOrDefault(salaryItemId, item.getName()));
                                    if (itemShowNameMap.containsKey(salaryItemId)) {
                                        item.setSalaryItemShowName(itemShowNameMap.get(salaryItemId));
                                        item.setName(itemShowNameMap.get(salaryItemId) + "(" + itemIdNameMap.getOrDefault(salaryItemId, item.getName()) + ")");
                                    } else {
                                        item.setSalaryItemShowName(itemIdNameMap.getOrDefault(salaryItemId, item.getName()));
                                        item.setName(itemIdNameMap.getOrDefault(salaryItemId, item.getName()));
                                    }
                                });
                            });
                }
            } else {
                replenishSalaryItemSetting = getReplenishSalaryItemSet(replenishFormQueryParam.getSalarySobId());
            }
        } else {
            replenishSalaryItemSetting = getReplenishSalaryItemSet(replenishFormQueryParam.getSalarySobId());
        }

        return SalaryTemplateReplenishFormDTO.builder().id(id).replenishSalaryTemplateSalaryItemSet(replenishSalaryItemSetting).salaryBillItemNameSet(salaryBillItemNameDTOMap).build();
    }

    /**
     * 获取单个分组下，没有被加入到模板的薪资项目
     *
     * @param param 入参
     * @return 单个分组下剩余的薪资项目
     */
    public List<SalaryTemplateSalaryItemListDTO> getSalaryItemSetGrouped(SalaryBillSalaryItemQueryParam param) {
        Boolean isReplenish = Optional.ofNullable(param.getIsReplenish()).orElse(false);
        List<SalaryTemplateSalaryItemSetListDTO> salaryItemSet = getSalaryTemplateService(user).getSalaryItemSetContainHide(param.getSalarySobId(), param.getSalaryTemplateId(), isReplenish);
        Long groupId = param.getGroupId();
        //工资单模板copy可能导致groupId不匹配
        List<SalarySobItemGroupPO> salarySobItemGroupPOS = getSalarySobItemGroupMapper().listSome(SalarySobItemGroupPO.builder().salarySobId(param.getSalarySobId()).name(param.getGroupName()).build());
        if (salarySobItemGroupPOS != null && salarySobItemGroupPOS.size() > 0) {
            groupId = salarySobItemGroupPOS.get(0).getId();
        }
        Long finalGroupId = groupId;
        return salaryItemSet.stream().filter(s -> Objects.equals(s.getGroupId(), finalGroupId + "")).map(SalaryTemplateSalaryItemSetListDTO::getItems).findFirst().orElse(Collections.emptyList()).stream().filter(item -> !Optional.ofNullable(param.getExistSalaryItemIds()).orElse(Collections.emptyList()).contains(item.getId())).collect(Collectors.toList());
    }

    /**
     * 获取账套下没有被加入的分组
     *
     * @param param 入参
     * @return 分组与下属
     */
    public List<SalaryTemplateSalaryItemSetListDTO> getSalaryGroupSet(SalaryBillSalaryGroupQueryParam param) {
        Boolean isReplenish = Optional.ofNullable(param.getIsReplenish()).orElse(false);
        return getSalaryTemplateService(user).getSalaryItemSetContainHide(param.getSalarySobId(), param.getSalaryTemplateId(), isReplenish).stream().filter(group -> !Optional.ofNullable(param.getExistSalaryGroupIds()).orElse(Collections.emptyList()).contains(group.getGroupId())).collect(Collectors.toList());
    }

    public void saveItemShowName(List<SalaryBillItemNameSaveParam> saveList) {
        getSalaryBillItemNameService(user).saveItemShowName(saveList);
    }

}
