package com.engine.salary.wrapper;

import com.api.browser.bean.SearchConditionGroup;
import com.api.browser.bean.SearchConditionItem;
import com.api.browser.util.ConditionFactory;
import com.api.browser.util.ConditionType;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.entity.datacollection.dto.OtherDeductionListDTO;
import com.engine.salary.entity.datacollection.dto.OtherDeductionRecordDTO;
import com.engine.salary.entity.datacollection.param.*;
import com.engine.salary.entity.datacollection.po.OtherDeductionPO;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.service.OtherDeductionService;
import com.engine.salary.service.SalaryEmployeeService;
import com.engine.salary.service.TaxAgentService;
import com.engine.salary.service.impl.OtherDeductionServiceImpl;
import com.engine.salary.service.impl.SalaryEmployeeServiceImpl;
import com.engine.salary.service.impl.TaxAgentServiceImpl;
import com.engine.salary.util.SalaryI18nUtil;
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.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 其他免税扣除
 * @Author: wangxiangzhong
 * @Date: 2022/3/14 16:09
 */
@Slf4j
public class OtherDeductionWrapper extends Service {
    private OtherDeductionService getOtherDeductionService(User user) {
        return ServiceUtil.getService(OtherDeductionServiceImpl.class, user);
    }

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

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

    /**
     * 数据采集-其他免税扣除列表的高级搜索
     *
     * @return
     */
    public Map<String, Object> getSearchCondition() {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        ConditionFactory conditionFactory = new ConditionFactory(user);

        //条件组
        List<SearchConditionGroup> addGroups = new ArrayList<SearchConditionGroup>();

        List<SearchConditionItem> conditionItems = new ArrayList<SearchConditionItem>();

        //文本输入框
        SearchConditionItem username = conditionFactory.createCondition(ConditionType.INPUT, 25034, "username");
        username.setInputType("input");
        username.setColSpan(2);//定义一行显示条件数，默认值为2,当值为1时标识该条件单独占一行
        username.setFieldcol(16);    //条件输入框所占宽度，默认值18
        username.setLabelcol(8);
        username.setViewAttr(2);  //	 编辑权限  1：只读，2：可编辑， 3：必填   默认2
        username.setLabel("姓名"); //设置文本值  这个将覆盖多语言标签的值
        conditionItems.add(username);


        SearchConditionItem departmentName = conditionFactory.createCondition(ConditionType.BROWSER, 502227, "departmentIds", "57");
        departmentName.setInputType("browser");
        departmentName.setColSpan(2);
        departmentName.setFieldcol(16);
        departmentName.setLabelcol(8);
        departmentName.setViewAttr(2);
        departmentName.setIsQuickSearch(false);
        departmentName.setLabel("部门");
        conditionItems.add(departmentName);


        SearchConditionItem jobNum = conditionFactory.createCondition(ConditionType.INPUT, 25034, "jobNum");
        jobNum.setInputType("input");
        jobNum.setColSpan(2);
        jobNum.setFieldcol(16);
        jobNum.setLabelcol(8);
        jobNum.setViewAttr(2);
        jobNum.setLabel("工号");
        conditionItems.add(jobNum);


//        SearchConditionItem idNo = conditionFactory.createCondition(ConditionType.INPUT, 25034, "idNo");
//        idNo.setInputType("input");
//        idNo.setColSpan(2);
//        idNo.setFieldcol(16);
//        idNo.setLabelcol(8);
//        idNo.setViewAttr(2);
//        idNo.setLabel("证件号码");
//        conditionItems.add(idNo);
//
//        //日期范围选项
//        List<SearchConditionOption> dateOptions = new ArrayList<SearchConditionOption>();
//        dateOptions.add(new SearchConditionOption("6", SalaryI18nUtil.getI18nLabel(32530, user.getLanguage()), true));//指定日期范围(必须为6)
//        SearchConditionItem hiredate = conditionFactory.createCondition(ConditionType.RANGEPICKER, 18648, new String[]{"hiredate", "hiredate"});
//        hiredate.setInputType("rangepicker");
//        hiredate.setFormat("yyyy-MM-dd");
//        hiredate.setFieldcol(16);
//        hiredate.setLabelcol(8);
//        hiredate.setViewAttr(2);
//        hiredate.setLabel("入职日期");
//        hiredate.setOptions(dateOptions);
//        conditionItems.add(hiredate);
//
//
//        SearchConditionItem mobile = conditionFactory.createCondition(ConditionType.INPUT, 25034, "mobile");
//        mobile.setInputType("input");
//        mobile.setColSpan(2);
//        mobile.setFieldcol(16);
//        mobile.setLabelcol(8);
//        mobile.setViewAttr(2);
//        mobile.setLabel("手机号");
//        conditionItems.add(mobile);

        addGroups.add(new SearchConditionGroup("常用条件", true, conditionItems));

        apidatas.put("condition", addGroups);
        return apidatas;
    }

    /**
     * 数据采集-其他免税扣除列表(分页)
     *
     * @param queryParam
     * @return
     */
    public PageInfo<OtherDeductionListDTO> list(OtherDeductionQueryParam queryParam) {
        return getOtherDeductionService(user).listPage(queryParam);

    }

    /**
     * 数据采集-其他免税扣除详情列表(分页)
     *
     * @param queryParam
     * @return
     */
    public PageInfo<OtherDeductionRecordDTO> getDetailList(OtherDeductionQueryParam queryParam) {
        Long id = queryParam.getOtherTaxExemptDeductionId();

        OtherDeductionPO po = getOtherDeductionService(user).getById(id);
        if (po == null) {
            throw new SalaryRunTimeException(String.format(SalaryI18nUtil.getI18nLabel(100415, "其他免税扣除不存在") + "[id:%s]", id));
        }
        queryParam.setEmployeeId(po.getEmployeeId());

        return getOtherDeductionService(user).recordListPage(queryParam);
    }

    /**
     * 导出-其他免税扣除列表
     *
     * @param queryParam
     * @return
     */
    public XSSFWorkbook export(OtherDeductionQueryParam queryParam) {
        return getOtherDeductionService(user).export(queryParam);
    }

    /**
     * 导出-其他免税扣除详情列表
     *
     * @param queryParam
     * @return
     */
    public XSSFWorkbook exportDetail(OtherDeductionQueryParam queryParam) {
        Long id = queryParam.getOtherTaxExemptDeductionId();
        OtherDeductionPO po = getOtherDeductionService(user).getById(id);
        if (po == null) {
            throw new SalaryRunTimeException(String.format(SalaryI18nUtil.getI18nLabel(100415, "其他免税扣除不存在") + "[id:%s]", id));
        }
        return getOtherDeductionService(user).exportDetail(queryParam);
    }

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

    /**
     * 预览
     */
    public Map<String, Object> preview(OtherDeductionImportParam importParam) {
        return getOtherDeductionService(user).preview(importParam);
    }

    /**
     * 导入数据
     */
    public Map<String, Object> importData(OtherDeductionImportParam importParam) {
        return getOtherDeductionService(user).importData(importParam);
    }

    /**
     * 编辑数据
     */
    public void editData(OtherDeductionParam otherDeductionParam) {
        getOtherDeductionService(user).editData(otherDeductionParam);
    }


    /**
     * 新增数据
     */
    public void createData(OtherDeductionParam otherDeductionParam) {
        getOtherDeductionService(user).createData(otherDeductionParam);
    }

    /**
     * 删除所选数据
     */
    public void deleteSelectData(AddUpDeductionRecordDeleteParam deleteParam) {
        getOtherDeductionService(user).deleteSelectData(deleteParam);
    }

    /**
     * 一键清空所有数据
     */
    public void deleteAllData(AddUpDeductionRecordDeleteParam deleteParam) {
        getOtherDeductionService(user).deleteAllData(deleteParam);
    }

    /**
     * 获取数据
     */
    public OtherDeductionRecordDTO getOtherDeduction(OtherDeductionParam otherDeductionParam) {
        return getOtherDeductionService(user).getOtherDeduction(otherDeductionParam);
    }

    public String extendToLastMonth(OtherDeductionExtendLastParam param) {
        LocalDateTime localDate = null;
        try {
            localDate = LocalDate.parse(param.getDeclareMonth() + "-01",
                    DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay();
            param.setYearMonthTime(localDate);
        } catch (Exception e) {
            log.error("", e);
        }
        if (null == param.getDeclareMonth() || null == localDate || null == param.getYearMonthTime()) {
            throw new SalaryRunTimeException("当前年月不合法");
        }
        if (Month.JANUARY.equals(localDate.getMonth())) {
            throw new SalaryRunTimeException("操作失败，每年度内首月无法沿用上月");
        }
        param.setYearMonthTime(localDate);
        return getOtherDeductionService(user).extendToLastMonth(param);
    }
}
