package com.engine.salary.wrapper;

import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.config.SalaryElogConfig;
import com.engine.hrmelog.entity.dto.LoggerContext;
import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.entity.salaryarchive.dto.SalaryArchiveItemFormDTO;
import com.engine.salary.entity.salaryarchive.dto.SalaryItemAdjustRecordListDTO;
import com.engine.salary.entity.salaryarchive.dto.SingleSalaryItemAdjustRecordListDTO;
import com.engine.salary.entity.salaryarchive.param.SalaryArchiveItemSaveParam;
import com.engine.salary.entity.salaryarchive.param.SalaryItemAdjustBeforeParam;
import com.engine.salary.entity.salaryarchive.param.SalaryItemAdjustRecordQueryParam;
import com.engine.salary.entity.salaryarchive.param.SingleSalaryItemAdjustRecordQueryParam;
import com.engine.salary.entity.salaryarchive.po.SalaryArchiveItemPO;
import com.engine.salary.entity.salaryarchive.po.SalaryArchivePO;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.taxagent.po.TaxAgentPO;
import com.engine.salary.enums.OperateTypeEnum;
import com.engine.salary.enums.UserStatusEnum;
import com.engine.salary.enums.datacollection.UseEmployeeTypeEnum;
import com.engine.salary.enums.salaryarchive.SalaryArchiveItemAdjustReasonEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.service.*;
import com.engine.salary.service.impl.*;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.page.SalaryPageUtil;
import com.engine.salary.wrapper.proxy.SalaryArchiveItemWrapperProxy;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import weaver.hrm.User;

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

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

public class SalaryArchiveItemWrapper extends Service implements SalaryArchiveItemWrapperProxy {

    private SalaryArchiveItemService getSalaryArchiveItemService(User user) {
        return (SalaryArchiveItemService) ServiceUtil.getService(SalaryArchiveItemServiceImpl.class, user);
    }

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

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

    private SalaryArchiveService getSalaryArchiveService(User user) {
        return ServiceUtil.getService(SalaryArchiveServiceImpl.class, user);
    }

    private SalaryEmployeeService getSalaryEmployeeService(User user) {
        return ServiceUtil.getService(SalaryEmployeeServiceImpl.class, user);
    }


    /**
     * 构建薪资项目基础信息表单
     *
     * @param effectiveTime
     * @param adjustReason
     * @param description
     * @return
     */
    private Map<String, Object> buildSalaryArchiveItemForm(Date effectiveTime, String adjustReason, String description) {

        // 控件数据
        Map<String, Object> data = new HashMap<>();
        data.put("effectiveTime", effectiveTime == null ? null : effectiveTime + "");
        data.put("adjustReason", adjustReason);
        data.put("description", description);
        data.put("adjustReasonList", SalaryArchiveItemAdjustReasonEnum.getList());
        return data;
    }

    /**
     * 构建薪资档案调整明细
     *
     * @return
     */
    private Map<String, Object> buildSalaryArchiveItemAdjustDetailTable(List<Map<String, Object>> tableData) {
        Map<String, Object> map = new HashMap<>();
        // 表头
        List<String> columns = new ArrayList<>();
        columns.add(SalaryI18nUtil.getI18nLabel(84960, "薪资项目"));
        columns.add(SalaryI18nUtil.getI18nLabel(85433, "调整前"));
        columns.add(SalaryI18nUtil.getI18nLabel(85434, "调整后"));
        // 获取所有可被引用的薪资项目
        List<SalaryItemPO> salaryItemList = getSalaryArchiveItemService(user).getCanAdjustSalaryItems();
        Set<Long> taxAgentIds = getTaxAgentService(user).listAllTaxAgents((long) user.getUID())
                .stream().map(TaxAgentPO::getId)
                .collect(Collectors.toSet());
        List<Map<String, Object>> salaryItems = salaryItemList.stream()
                .filter(po -> getSalaryItemService(user).filterInRange(taxAgentIds, po))
                .map(m -> {
                    Map<String, Object> salaryItemMap = new LinkedHashMap<>();
                    salaryItemMap.put("id", String.valueOf(m.getId()));
                    salaryItemMap.put("content", m.getName());
                    salaryItemMap.put("dataType", m.getDataType());
                    salaryItemMap.put("pattern", m.getPattern());
                    return salaryItemMap;
                }).collect(Collectors.toList());

        map.put("columns", columns);
        map.put("list", tableData);
        map.put("salaryItemList", salaryItems);

        return map;
    }

    /**
     * 获取薪资项目调整前的值
     *
     * @param adjustBeforeParam
     * @return
     */
    public String getSalaryItemAdjustBeforeValue(SalaryItemAdjustBeforeParam adjustBeforeParam) {
        return getSalaryArchiveItemService(user).getSalaryItemAdjustBeforeValue(adjustBeforeParam);
    }

    public SalaryArchiveItemFormDTO getSalaryItemForm(Long salaryArchiveId) {

        // 调整明细数据
        List<Map<String, Object>> tableData = Lists.newArrayList();

        return SalaryArchiveItemFormDTO.builder()
                .salaryArchiveId(salaryArchiveId)
                .salaryArchiveItemForm(buildSalaryArchiveItemForm(null, null, null))
                .salaryArchiveItemDetail(buildSalaryArchiveItemAdjustDetailTable(tableData))
                .build();
    }


    /**
     * 通过薪资档案的薪资项目id获取薪资项目调整表单
     *
     * @param salaryArchiveItemId
     * @return
     */
    public SalaryArchiveItemFormDTO getSalaryItemFormBySalaryArchiveItemId(Long salaryArchiveItemId) {
        if (salaryArchiveItemId == null) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(100427, "薪资档案的薪资项目id不能为空"));
        }
        SalaryArchiveItemPO salaryArchiveItem = getSalaryArchiveItemService(user).getById(salaryArchiveItemId);
        if (salaryArchiveItem == null) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(100428, "该薪资档案的薪资项目的调整记录不存在"));
        }
        Long salaryArchiveId = salaryArchiveItem.getSalaryArchiveId();
        Date effectiveTime;
        String adjustReason;
        String description;
        // 调整明细数据
        List<Map<String, Object>> tableData = Lists.newArrayList();
        // 获取调整前
        String archiveItemBeforeValue = getSalaryArchiveItemService(user).getSalaryItemAdjustBeforeValue(SalaryItemAdjustBeforeParam.builder()
                .salaryArchiveId(salaryArchiveItem.getSalaryArchiveId())
                .salaryArchiveItemId(salaryArchiveItem.getId())
                .salaryItemId(salaryArchiveItem.getSalaryItemId())
                .build());

        effectiveTime = salaryArchiveItem.getEffectiveTime();
        adjustReason = salaryArchiveItem.getAdjustReason();
        description = salaryArchiveItem.getDescription();
        Map<String, Object> map = new HashMap<>(3);
        map.put("salaryItem", String.valueOf(salaryArchiveItem.getSalaryItemId()));
        map.put("salaryBefore", archiveItemBeforeValue);
        map.put("adjustAfter", salaryArchiveItem.getItemValue());
        tableData.add(map);

        return SalaryArchiveItemFormDTO.builder()
                .salaryArchiveId(salaryArchiveId)
                .salaryArchiveItemForm(buildSalaryArchiveItemForm(effectiveTime, adjustReason, description))
                .salaryArchiveItemDetail(buildSalaryArchiveItemAdjustDetailTable(tableData))
                .build();
    }

    /**
     * 调整前校验
     *
     * @param saveParam
     * @return
     */
    public String checkSaveSalaryItem(SalaryArchiveItemSaveParam saveParam) {
        return getSalaryArchiveItemService(user).checkSaveSalaryItem(saveParam);
    }

    /**
     * 保存薪资项目调整
     *
     * @param saveParam
     * @return
     */
    public String saveSalaryItem(SalaryArchiveItemSaveParam saveParam) {
        return getSalaryArchiveItemService(user).saveSalaryItem(saveParam);
    }

    /**
     * 删除薪资项目调整
     *
     * @param salaryArchiveItemId
     * @return
     */
    public String deleteSalaryItem(Long salaryArchiveItemId) {
        return getSalaryArchiveItemService(user).deleteSalaryItem(salaryArchiveItemId);
    }


    /**
     * 薪资项目调整记录列表
     *
     * @param queryParam
     * @return
     */
    public PageInfo<SalaryItemAdjustRecordListDTO> adjustRecordList(SalaryItemAdjustRecordQueryParam queryParam) {
        // 获取所有可被引用的薪资项目
        List<SalaryItemPO> salaryItemList = getSalaryArchiveItemService(user).getCanAdjustSalaryItems();
        List<Long> salaryItemIds = salaryItemList.stream().map(SalaryItemPO::getId).collect(Collectors.toList());

        List<DataCollectionEmployee> employeeList = getSalaryEmployeeService(user).listAll(UseEmployeeTypeEnum.ALL);
        Map<Long, DataCollectionEmployee> employeeMap = SalaryEntityUtil.convert2Map(employeeList, DataCollectionEmployee::getEmployeeId);

        List<SalaryItemAdjustRecordListDTO> listAll = getSalaryArchiveItemService(user).salaryItemAdjustRecordList(SalaryItemAdjustRecordQueryParam.builder().build(), salaryItemIds);
        long currentEmployeeId = user.getUID();
        //是否开启分权
        Boolean needAuth = getTaxAgentService(user).isNeedAuth(currentEmployeeId);
        if (needAuth) {
            Collection<TaxAgentPO> taxAgentPOS = getTaxAgentService(user).listAllTaxAgentsAsAdmin(currentEmployeeId);
            List<Long> taxAgentIds = SalaryEntityUtil.properties(taxAgentPOS, TaxAgentPO::getId, Collectors.toList());

            if (CollectionUtils.isEmpty(taxAgentIds)) {
                return new PageInfo<SalaryItemAdjustRecordListDTO>();
            }

            List<SalaryArchivePO> salaryArchives = getSalaryArchiveService(user).listSome(SalaryArchivePO.builder().taxAgentIds(taxAgentIds).build());
            List<Long> salaryArchiveIds = SalaryEntityUtil.properties(salaryArchives, SalaryArchivePO::getId, Collectors.toList());
            queryParam.setSalaryArchiveIds(salaryArchiveIds);
        }

        List<SalaryItemAdjustRecordListDTO> listResult = getSalaryArchiveItemService(user).salaryItemAdjustRecordList(queryParam, salaryItemIds);
        listResult = listResult.stream()
                .filter(r -> {
                    DataCollectionEmployee employee = employeeMap.getOrDefault(r.getEmployeeId(), new DataCollectionEmployee());
                    String username = queryParam.getUsername();
                    List<Long> departmentIds = queryParam.getDepartmentIds();
                    List<Long> positionIds = queryParam.getPositionIds();
                    String userstatus = queryParam.getUserstatus();
                    r.setUsername(employee.getUsername());
                    r.setDepartmentName(employee.getDepartmentName());
                    r.setEmployeeStatus(StringUtils.isNotBlank(employee.getStatus())&& NumberUtils.isCreatable(employee.getStatus())?
                            UserStatusEnum.getDefaultLabelByValue(Integer.parseInt(employee.getStatus())):"");
                    r.setAdjustReason(SalaryArchiveItemAdjustReasonEnum.getDefaultLabelByValue(r.getAdjustReason()));
                    return (StringUtils.isBlank(username) || employee.getUsername().contains(username))
                            && (CollectionUtils.isEmpty(departmentIds) || departmentIds.contains(employee.getDepartmentId()))
                            && (CollectionUtils.isEmpty(positionIds) || positionIds.contains(employee.getJobtitleId()))
                            && (StringUtils.isBlank(userstatus) || Objects.equals(employee.getStatus(), userstatus))
                            && (CollectionUtils.isEmpty(positionIds) || positionIds.contains(employee.getJobtitleId()))
                            ;
                })
                .collect(Collectors.toList());

        PageInfo<SalaryItemAdjustRecordListDTO> pageInfo = SalaryPageUtil.buildPage(queryParam.getCurrent(), queryParam.getPageSize(), listResult, SalaryItemAdjustRecordListDTO.class);

        listResult = pageInfo.getList();
        //获取上次更改的记录
        listResult.forEach(m -> {
            if (!CollectionUtils.isEmpty(listAll)) {
                listAll.removeIf(a -> a.getId().equals(m.getId()));
            }
            Optional<SalaryItemAdjustRecordListDTO> optional = listAll.stream().filter(f -> f.getSalaryArchiveId().equals(m.getSalaryArchiveId()) && f.getSalaryItemId().equals(m.getSalaryItemId())).findFirst();
            m.setAdjustBefore(optional.isPresent() ? optional.get().getAdjustAfter() : "");
        });
        return pageInfo;
    }


    /**
     * 我的调薪记录
     *
     * @param queryParam
     * @return
     */
    public PageInfo<SalaryItemAdjustRecordListDTO> myAdjustRecordList(SalaryItemAdjustRecordQueryParam queryParam) {
        // 获取所有可被引用的薪资项目
        List<SalaryItemPO> salaryItemList = getSalaryArchiveItemService(user).getCanAdjustSalaryItems();
        List<Long> salaryItemIds = salaryItemList.stream().map(SalaryItemPO::getId).collect(Collectors.toList());

        DataCollectionEmployee employee = getSalaryEmployeeService(user).getEmployeeById(queryParam.getEmployeeId());

        List<SalaryItemAdjustRecordListDTO> listAll = getSalaryArchiveItemService(user).salaryItemAdjustRecordList(SalaryItemAdjustRecordQueryParam.builder().build(), salaryItemIds);
        PageInfo<SalaryItemAdjustRecordListDTO> list = getSalaryArchiveItemService(user).salaryItemAdjustRecordListPage(queryParam, salaryItemIds);
        List<SalaryItemAdjustRecordListDTO> listResult = list.getList();
        //获取上次更改的记录
        listResult.forEach(m -> {
            if (!CollectionUtils.isEmpty(listAll)) {
                listAll.removeIf(a -> a.getId().equals(m.getId()));
            }
            Optional<SalaryItemAdjustRecordListDTO> optional = listAll.stream().filter(f -> f.getSalaryArchiveId().equals(m.getSalaryArchiveId()) && f.getSalaryItemId().equals(m.getSalaryItemId())).findFirst();
            m.setAdjustBefore(optional.isPresent() ? optional.get().getAdjustAfter() : "");

            m.setUsername(employee.getUsername());
            m.setDepartmentName(employee.getDepartmentName());
            m.setEmployeeStatus(StringUtils.isNotBlank(employee.getStatus())&& NumberUtils.isCreatable(employee.getStatus())?
                    UserStatusEnum.getDefaultLabelByValue(Integer.parseInt(employee.getStatus())):"");
            m.setAdjustReason(SalaryArchiveItemAdjustReasonEnum.getDefaultLabelByValue(m.getAdjustReason()));
        });

        // 记录查看日志
        LoggerContext<SalaryItemPO> loggerContext = new LoggerContext<>();
        loggerContext.setUser(user);
        // TODO 我的调薪记录setTargetId
        // loggerContext.setTargetId();
        loggerContext.setTargetName("我的调薪记录");
        loggerContext.setOperateType(OperateTypeEnum.READ.getValue());
        loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(0, "查看调薪记录"));
        loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(0, "查看调薪记录"));
        SalaryElogConfig.myAdjustRecordLoggerTemplate.write(loggerContext);
        return list;
    }


    /**
     * 单个档案的薪资项目调整记录列表
     *
     * @param queryParam
     * @return
     */
    public PageInfo<SingleSalaryItemAdjustRecordListDTO> singleSalaryItemAdjustRecordList(SingleSalaryItemAdjustRecordQueryParam queryParam) {
        Long salaryArchiveId = queryParam.getSalaryArchiveId();

        if (salaryArchiveId == null) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(100425, "薪资档案id不能为空"));
        }
        // 获取所有可被引用的薪资项目
        List<SalaryItemPO> salaryItemList = getSalaryArchiveItemService(user).getCanAdjustSalaryItems();
        List<Long> salaryItemIds = salaryItemList.stream().map(SalaryItemPO::getId).collect(Collectors.toList());

        SalaryItemAdjustRecordQueryParam adjustRecordQueryParam = new SalaryItemAdjustRecordQueryParam();
        BeanUtils.copyProperties(queryParam, adjustRecordQueryParam);

        //所有调整记录
        List<SalaryItemAdjustRecordListDTO> listAll = getSalaryArchiveItemService(user).salaryItemAdjustRecordList(SalaryItemAdjustRecordQueryParam.builder().build(), salaryItemIds);

        //操作记录
        //根据条件分页查询调整记录
        PageInfo<SalaryItemAdjustRecordListDTO> adjustlistByParamPage = getSalaryArchiveItemService(user).salaryItemAdjustRecordListPage(adjustRecordQueryParam, salaryItemIds);
        List<SalaryItemAdjustRecordListDTO> adjustPageList = adjustlistByParamPage.getList();
        List<SingleSalaryItemAdjustRecordListDTO> resultList = Lists.newArrayList();
        adjustPageList.forEach(m -> {
            if (CollectionUtils.isNotEmpty(listAll)) {
                listAll.removeIf(a -> a.getId().equals(m.getId()));
            }
            Optional<SalaryItemAdjustRecordListDTO> optional = listAll.stream()
                    .filter(f -> f.getSalaryArchiveId().equals(m.getSalaryArchiveId()))
                    .filter(f -> f.getSalaryItemId().equals(m.getSalaryItemId()))
                    .filter(f -> f.getEffectiveTime().before(m.getEffectiveTime()))
                    .findFirst();
            m.setAdjustBefore(optional.isPresent() ? optional.get().getAdjustAfter() : "");

            m.setAdjustReason(SalaryArchiveItemAdjustReasonEnum.getDefaultLabelByValue(m.getAdjustReason()));

            SingleSalaryItemAdjustRecordListDTO singleSalaryItemAdjustRecordList = new SingleSalaryItemAdjustRecordListDTO();
            BeanUtils.copyProperties(m, singleSalaryItemAdjustRecordList);
            resultList.add(singleSalaryItemAdjustRecordList);
        });

        List<Long> salaryItemPageIds = resultList.stream().map(SingleSalaryItemAdjustRecordListDTO::getSalaryItemId).collect(Collectors.toList());
        List<SalaryArchiveItemPO> effectiveSalaryItems = getSalaryArchiveItemService(user).getEffectiveSalaryItems(salaryArchiveId, salaryItemPageIds);
        // 行记录按钮权限控制
        for (int i = 0; i < resultList.size(); i++) {
            SingleSalaryItemAdjustRecordListDTO singleSalaryItemAdjustRecord = resultList.get(i);
            Optional<SalaryArchiveItemPO> optional = effectiveSalaryItems.stream().filter(f -> f.getSalaryItemId().equals(singleSalaryItemAdjustRecord.getSalaryItemId())).findFirst();
            Date effectiveDate = optional.map(SalaryArchiveItemPO::getEffectiveTime).orElse(null);

            if (effectiveDate != null && singleSalaryItemAdjustRecord.getEffectiveTime().before(effectiveDate)) {
                singleSalaryItemAdjustRecord.setCanOperator(Boolean.FALSE);
            } else {
                singleSalaryItemAdjustRecord.setCanOperator(Boolean.TRUE);
            }
        }


        PageInfo<SingleSalaryItemAdjustRecordListDTO> listPage = new PageInfo<SingleSalaryItemAdjustRecordListDTO>(resultList, SingleSalaryItemAdjustRecordListDTO.class);
        listPage.setTotal(adjustlistByParamPage.getTotal());
        listPage.setPageNum(adjustlistByParamPage.getPageNum());
        listPage.setPageSize(adjustlistByParamPage.getPageSize());


        return listPage;
    }

    /**
     * @return void
     * @description 单个档案的薪资项目调整的编辑
     * @author Harryxzy
     * @date 2022/11/14 11:46
     */
    @Override
    public String editSingleSalaryItem(SalaryArchiveItemSaveParam salaryArchiveItemSaveParam) {
        return getSalaryArchiveItemService(user).editSingleSalaryItem(salaryArchiveItemSaveParam);
    }

    /**
     * 导出薪资项目调整记录列表
     *
     * @param queryParam
     * @return
     */
    public XSSFWorkbook exportAdjustRecordList(SalaryItemAdjustRecordQueryParam queryParam) {
        return getSalaryArchiveItemService(user).exportAdjustRecordList(queryParam);
    }


    /**
     * @return void
     * @description 获取单个档案的单个薪资项目调整信息
     * @author Harryxzy
     * @date 2022/11/11 14:39
     */
    public SalaryArchiveItemFormDTO getSingleSalaryItemInfo(SingleSalaryItemAdjustRecordListDTO queryParam) {
        SalaryArchiveItemPO byId = getSalaryArchiveItemService(user).getById(queryParam.getId());
        if (Objects.isNull(byId)) {
            throw new SalaryRunTimeException("该薪资项目调整信息不存在");
        }
        // 获取所有可被引用的薪资项目
        List<SalaryItemPO> salaryItemList = getSalaryArchiveItemService(user).getCanAdjustSalaryItems();
        List<Long> salaryItemIds = salaryItemList.stream().map(SalaryItemPO::getId).collect(Collectors.toList());
        //所有调整记录
        List<SalaryItemAdjustRecordListDTO> listAll = getSalaryArchiveItemService(user).salaryItemAdjustRecordList(SalaryItemAdjustRecordQueryParam.builder().build(), salaryItemIds);

        //根据条件查询调整记录
        List<SalaryItemAdjustRecordListDTO> adjustPageList = getSalaryArchiveItemService(user).salaryItemAdjustRecordList(SalaryItemAdjustRecordQueryParam.builder().salaryArchiveId(byId.getSalaryArchiveId()).build(), salaryItemIds);

        List<SingleSalaryItemAdjustRecordListDTO> resultList = Lists.newArrayList();
        adjustPageList.forEach(m -> {
            if (CollectionUtils.isNotEmpty(listAll)) {
                listAll.removeIf(a -> a.getId().equals(m.getId()));
            }
            Optional<SalaryItemAdjustRecordListDTO> optional = listAll.stream().filter(f -> f.getSalaryArchiveId().equals(m.getSalaryArchiveId()) && f.getSalaryItemId().equals(m.getSalaryItemId())).findFirst();
            m.setAdjustBefore(optional.isPresent() ? optional.get().getAdjustAfter() : "");

            m.setAdjustReason(SalaryArchiveItemAdjustReasonEnum.getDefaultLabelByValue(m.getAdjustReason()));

            SingleSalaryItemAdjustRecordListDTO singleSalaryItemAdjustRecordList = new SingleSalaryItemAdjustRecordListDTO();
            BeanUtils.copyProperties(m, singleSalaryItemAdjustRecordList);
            resultList.add(singleSalaryItemAdjustRecordList);
        });

        List<Long> salaryItemPageIds = resultList.stream().map(SingleSalaryItemAdjustRecordListDTO::getSalaryItemId).collect(Collectors.toList());
        List<SalaryArchiveItemPO> effectiveSalaryItems = getSalaryArchiveItemService(user).getEffectiveSalaryItems(byId.getSalaryArchiveId(), salaryItemPageIds);
        // 行记录按钮权限控制
        for (int i = 0; i < resultList.size(); i++) {
            SingleSalaryItemAdjustRecordListDTO singleSalaryItemAdjustRecord = resultList.get(i);
            if (singleSalaryItemAdjustRecord.getId().equals(queryParam.getId())) {
//                Optional<SalaryArchiveItemPO> optional = effectiveSalaryItems.stream().filter(f -> f.getSalaryItemId().equals(singleSalaryItemAdjustRecord.getSalaryItemId())).findFirst();
//                Date effectiveDate = optional.map(SalaryArchiveItemPO::getEffectiveTime).orElse(null);
//                if (effectiveDate != null && singleSalaryItemAdjustRecord.getEffectiveTime().before(effectiveDate)) {
//                    singleSalaryItemAdjustRecord.setCanOperator(Boolean.FALSE);
//                } else {
//                    singleSalaryItemAdjustRecord.setCanOperator(Boolean.TRUE);
//                }
                singleSalaryItemAdjustRecord.setCanOperator(Boolean.TRUE);
                Map<String, Object> map = new HashMap<>(3);
                map.put("salaryItem", String.valueOf(singleSalaryItemAdjustRecord.getSalaryItemId()));
                map.put("salaryBefore", singleSalaryItemAdjustRecord.getAdjustBefore());
                map.put("adjustAfter", singleSalaryItemAdjustRecord.getAdjustAfter());

                // 调整明细数据
                List<Map<String, Object>> tableData = Lists.newArrayList();
                tableData.add(map);

                return SalaryArchiveItemFormDTO.builder()
                        .canOperator(singleSalaryItemAdjustRecord.getCanOperator())
                        .salaryArchiveId(byId.getSalaryArchiveId())
                        .salaryArchiveItemForm(buildSalaryArchiveItemForm(byId.getEffectiveTime(), byId.getAdjustReason(), byId.getDescription()))
                        .salaryArchiveItemDetail(buildSalaryArchiveItemAdjustDetailTable(tableData))
                        .build();

            }
        }
        return null;
    }
}
