package com.engine.salary.wrapper;

import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.entity.datacollection.AddUpSituation;
import com.engine.salary.entity.datacollection.dto.AddUpSituationDTO;
import com.engine.salary.entity.datacollection.dto.AddUpSituationRecordDTO;
import com.engine.salary.entity.datacollection.param.AddUpSituationDeleteParam;
import com.engine.salary.entity.datacollection.param.AddUpSituationImportParam;
import com.engine.salary.entity.datacollection.param.AddUpSituationParam;
import com.engine.salary.entity.datacollection.param.AddUpSituationQueryParam;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.service.AddUpSituationService;
import com.engine.salary.service.impl.AddUpSituationServiceImpl;
import com.engine.salary.util.SalaryDateUtil;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.page.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.hrm.User;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 往期累计情况
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class AddUpSituationWrapper extends Service {

    private AddUpSituationService getAddUpSituationService(User user) {
        return ServiceUtil.getService(AddUpSituationServiceImpl.class, user);
    }

    /**
     * 数据采集-累计情况列表的高级搜索
     *
     * @return
     */
    public Map<String, Object> getSearchCondition() {
        return getAddUpSituationService(user).getSearchCondition();
    }

    /**
     * 数据采集-累计情况列表（分页）
     *
     * @param queryParam
     * @return
     */
    public PageInfo<AddUpSituationDTO> list(AddUpSituationQueryParam queryParam) {
        return getAddUpSituationService(user).listPage(queryParam);
    }

    /**
     * 数据采集-往期累计情况详情列表（分页）
     *
     * @param queryParam
     * @return
     */
    public PageInfo<AddUpSituationRecordDTO> getDetailList(AddUpSituationQueryParam queryParam) {
        Long id = queryParam.getAccumulatedSituationId();
        AddUpSituation po = getAddUpSituationService(user).getById(id);
        if (po == null) {
            throw new SalaryRunTimeException(String.format(SalaryI18nUtil.getI18nLabel(100352, "累计情况不存在") + "[id:%s]", id));
        }
        List<String> taxYearMonths = queryParam.getTaxYearMonth();
        if (CollectionUtils.isNotEmpty(taxYearMonths)) {
            queryParam.setTaxYearMonth(taxYearMonths.stream().map(e -> e + "-01 00:00:00").collect(Collectors.toList()));
            queryParam.setTaxYearMonthDate(taxYearMonths.stream().map(e -> e + "-01 00:00:00").map(SalaryDateUtil::dateStrToLocalTime).collect(Collectors.toList()));
        }
        queryParam.setEmployeeId(po.getEmployeeId());
        return getAddUpSituationService(user).recordListPage(queryParam);
    }

    /**
     * 导出-往期累计情况列表
     *
     * @param queryParam
     * @return
     */
    public XSSFWorkbook export(AddUpSituationQueryParam queryParam) {
        return getAddUpSituationService(user).export(queryParam);
    }

    /**
     * 导出详情列表
     *
     * @param queryParam
     * @return
     */
    public XSSFWorkbook exportDetail(AddUpSituationQueryParam queryParam) {
        Long id = queryParam.getAccumulatedSituationId();
        AddUpSituation po = getAddUpSituationService(user).getById(id);
        if (po == null) {
            throw new SalaryRunTimeException(String.format(SalaryI18nUtil.getI18nLabel(100352, "累计情况不存在") + "[id:%s]", id));
        }
        return getAddUpSituationService(user).exportDetail(queryParam);
    }

    /**
     * 下载导入模板
     *
     * @param queryParam
     * @return
     */
    public XSSFWorkbook downloadTemplate(AddUpSituationQueryParam queryParam) {
        return getAddUpSituationService(user).downloadTemplate(queryParam);
    }

    public Map<String, Object> preview(AddUpSituationImportParam importParam){
        return getAddUpSituationService(user).preview(importParam);
    }

    public Map<String, Object> importAddUpSituation(AddUpSituationImportParam importParam){
        return getAddUpSituationService(user).importAddUpSituation(importParam);
    }

    /**
     * @description 编辑数据
     * @return void
     * @author Harryxzy
     * @date 2022/10/27 21:30
     */
    public void editAddUpSituation(AddUpSituationParam addUpSituationParam) {
        getAddUpSituationService(user).editAddUpSituation(addUpSituationParam);
    }

    /**
     * @description 新建数据
     * @return void
     * @author Harryxzy
     * @date 2022/10/27 22:00
     */
    public void createAddUpSituation(AddUpSituationParam addUpSituationParam) {
        getAddUpSituationService(user).createAddUpSituation(addUpSituationParam);
    }

    /**
     * @description 删除所选数据
     * @return void
     * @author Harryxzy
     * @date 2022/10/27 22:34
     */
    public void deleteSelectAddUpSituation(AddUpSituationDeleteParam deleteParam) {
        getAddUpSituationService(user).deleteSelectAddUpSituation(deleteParam);
    }

    /**
     * @description 一键清空数据
     * @return void
     * @author Harryxzy
     * @date 2022/10/27 22:34
     */
    public void deleteAllAddUpSituation(AddUpSituationDeleteParam addUpSituationDeleteParam) {
        getAddUpSituationService(user).deleteAllAddUpSituation(addUpSituationDeleteParam);
    }

    /**
     * @description 获取往期累计情况
     * @return null
     * @author Harryxzy
     * @date 2022/10/31 14:00
     */
    public AddUpSituationRecordDTO getAddUpSituation(AddUpSituationParam addUpSituationParam) {
        return getAddUpSituationService(user).getAddUpSituation(addUpSituationParam);
    }
}
