package com.engine.salary.wrapper;

import com.cloudstore.eccom.pc.table.WeaTableColumn;
import com.cloudstore.eccom.result.WeaResultMsg;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.component.SalaryWeaTable;
import com.engine.salary.entity.datacollection.bo.VariableArchiveBO;
import com.engine.salary.entity.datacollection.dto.VariableArchiveListDTO;
import com.engine.salary.entity.datacollection.dto.VariableItemListDTO;
import com.engine.salary.entity.datacollection.param.VariableArchiveImportHandleParam;
import com.engine.salary.entity.datacollection.param.VariableArchiveQueryParam;
import com.engine.salary.entity.datacollection.param.VariableArchiveSaveParam;
import com.engine.salary.entity.datacollection.po.VariableItemPO;
import com.engine.salary.service.VariableArchiveService;
import com.engine.salary.service.VariableItemService;
import com.engine.salary.service.impl.VariableArchiveServiceImpl;
import com.engine.salary.service.impl.VariableItemServiceImpl;
import com.engine.salary.util.excel.ExcelPreviewDTO;
import com.engine.salary.util.page.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.hrm.User;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 浮动薪酬
 * @Author: xzy
 * @Date: 2024/8/6 17:39
 */
@Slf4j
public class VariableArchiveWrapper extends Service {
    private VariableArchiveService getVariableArchiveService(User user) {
        return ServiceUtil.getService(VariableArchiveServiceImpl.class, user);
    }

    private VariableItemService getVariableItemService(User user) {
        return ServiceUtil.getService(VariableItemServiceImpl.class, user);
    }

    /**
     * 数据采集-浮动薪酬列表(分页)
     *
     * @param queryParam
     * @return
     */
    public Map<String, Object> list(VariableArchiveQueryParam queryParam) {

        //薪资档案列表
        PageInfo<VariableArchiveListDTO> pageInfo = getVariableArchiveService(user).listPage(queryParam);
        List<VariableArchiveListDTO> salaryArchives = pageInfo.getList();

        // 获取所有浮动薪酬项目
        List<VariableItemPO> variableItems = getVariableItemService(user).listAll();

        //整合所有的显示列（固定列+薪资项目动态列）
        List<Map<String, Object>> listMaps = getVariableArchiveService(user).buildVariableArchiveData(salaryArchives);

        PageInfo<Map<String, Object>> pageInfos = new PageInfo<Map<String, Object>>(listMaps);
        pageInfos.setTotal(pageInfo.getTotal());
        pageInfos.setPageNum(pageInfo.getPageNum());
        pageInfos.setPageSize(pageInfo.getPageSize());

        //动态列组装
        List<WeaTableColumn> columns = VariableArchiveBO.buildVariableArchiveTable(variableItems);

        SalaryWeaTable<VariableArchiveListDTO> table = new SalaryWeaTable<VariableArchiveListDTO>(user, VariableArchiveListDTO.class);
        table.setColumns(columns);
        WeaResultMsg result = new WeaResultMsg(false);
        result.putAll(table.makeDataResult());
        result.success();

        Map<String, Object> datas = new HashMap<>();
        datas.put("pageInfo", pageInfos);
        datas.put("dataKey", result.getResultMap());

        return datas;
    }

    /**
     * 创建浮动薪酬档案
     *
     * @param saveParam
     */
    public void createData(VariableArchiveSaveParam saveParam) {
        getVariableArchiveService(user).createData(saveParam);
    }

    /**
     * 浮动薪酬档案明细
     *
     * @param queryParam
     * @return
     */
    public Map<String, Object> getDetail(VariableArchiveQueryParam queryParam) {
        return getVariableArchiveService(user).getDetail(queryParam);
    }

    public List<VariableItemListDTO> getCreateForm() {
        return getVariableArchiveService(user).getCreateForm();
    }

    public XSSFWorkbook downloadTemplate(VariableArchiveQueryParam param) {
        return getVariableArchiveService(user).downloadTemplate(param);
    }

    public ExcelPreviewDTO preview(VariableArchiveImportHandleParam importParam) {
        return getVariableArchiveService(user).preview(importParam);
    }

    public Map<String, Object> importData(VariableArchiveImportHandleParam importParam) {
        return getVariableArchiveService(user).importData(importParam);
    }

    public XSSFWorkbook export(VariableArchiveQueryParam param) {
        return getVariableArchiveService(user).export(param);
    }

    public void deleteSelectVariableArchive(Collection<Long> deleteIds) {
        getVariableArchiveService(user).deleteSelectVariableArchive(deleteIds);
    }

    public void updateData(VariableArchiveSaveParam updateParam) {
        getVariableArchiveService(user).updateData(updateParam);
    }
}
