package com.engine.salary.wrapper;

import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.entity.salaryacct.bo.SalaryAcctEmployeeBO;
import com.engine.salary.entity.salaryacct.dto.SalaryAccEmployeeListDTO;
import com.engine.salary.entity.salaryacct.param.SalaryAcctEmployeeAddParam;
import com.engine.salary.entity.salaryacct.param.SalaryAcctEmployeeDeleteParam;
import com.engine.salary.entity.salaryacct.param.SalaryAcctEmployeeQueryParam;
import com.engine.salary.entity.salaryacct.param.SalaryAcctEmployeeSaveParam;
import com.engine.salary.entity.salaryacct.po.SalaryAcctEmployeePO;
import com.engine.salary.entity.taxagent.po.TaxAgentPO;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.service.SalaryAcctEmployeeService;
import com.engine.salary.service.SalaryEmployeeService;
import com.engine.salary.service.TaxAgentService;
import com.engine.salary.service.impl.SalaryAcctEmployeeServiceImpl;
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 org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import weaver.hrm.User;

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

/**
 * 薪资核算人员
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalaryAcctEmployeeWrapper extends Service {

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

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

    /**
     * 薪资核算人员列表
     *
     * @param queryParam 列表查询参数
     * @return
     */
    public PageInfo<SalaryAccEmployeeListDTO> listPage(SalaryAcctEmployeeQueryParam queryParam) {
        // 查询薪资核算人员（人员）
        PageInfo<SalaryAcctEmployeePO> page = getSalaryAcctEmployeeService(user).listPageByParam(queryParam);
        // 转换成列表dto
        PageInfo<SalaryAccEmployeeListDTO> dtoPage = convert2ListDTO(page);

        return dtoPage;
    }

    /**
     * 同比减少的薪资核算人员列表
     *
     * @param queryParam 列表查询参数
     * @return
     */
    public PageInfo<SalaryAccEmployeeListDTO> listPage4Reduce(SalaryAcctEmployeeQueryParam queryParam) {
        // 查询同比减少的薪资核算人员（分页）
        PageInfo<SalaryAcctEmployeePO> page = getSalaryAcctEmployeeService(user).listPageByParam4Reduce(queryParam);
        // 转换成列表dto
        PageInfo<SalaryAccEmployeeListDTO> dtoPage = convert2ListDTO(page);

        return dtoPage;
    }

    /**
     * 同比增加的薪资核算人员列表
     *
     * @param queryParam 列表查询参数
     * @return
     */
    public PageInfo<SalaryAccEmployeeListDTO> listPage4Add(SalaryAcctEmployeeQueryParam queryParam) {
        // 查询同比减少的薪资核算人员（分页）
        PageInfo<SalaryAcctEmployeePO> page = getSalaryAcctEmployeeService(user).listPageByParam4Add(queryParam);
        // 转换成列表dto
        PageInfo<SalaryAccEmployeeListDTO> dtoPage = convert2ListDTO(page);

        return dtoPage;
    }

    /**
     * 转换成列表dto
     *
     * @param page 薪资核算人员po
     * @return
     */
    private PageInfo<SalaryAccEmployeeListDTO> convert2ListDTO(PageInfo<SalaryAcctEmployeePO> page) {
        List<SalaryAcctEmployeePO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return new PageInfo<>(SalaryAccEmployeeListDTO.class);
        }
        // 查询人员信息
        List<Long> employeeIds = list.stream().map(SalaryAcctEmployeePO::getEmployeeId).collect(Collectors.toList());
        List<DataCollectionEmployee> simpleEmployees = getSalaryEmployeeService(user).getEmployeeByIdsAll(employeeIds);
        // 查询个税扣缴义务人
        List<Long> taxAgentIds = list.stream().map(SalaryAcctEmployeePO::getTaxAgentId).collect(Collectors.toList());
        List<TaxAgentPO> taxAgentPOS = getTaxAgentService(user).listByIds(taxAgentIds);
        // 转换成列表dto
        List<SalaryAccEmployeeListDTO> salaryAccEmployeeListDTOS = SalaryAcctEmployeeBO.convert2EmployeeListDTO(list, taxAgentPOS, simpleEmployees);
        PageInfo<SalaryAccEmployeeListDTO> pageInfo = new PageInfo<SalaryAccEmployeeListDTO>(salaryAccEmployeeListDTOS, SalaryAccEmployeeListDTO.class);
        pageInfo.setTotal(page.getTotal());
        pageInfo.setPageSize(page.getPageSize());
        pageInfo.setPageNum(page.getPageNum());
        return pageInfo;
    }

//    /**
//     * 获取高级搜索表单
//     *
//     * @return
//     */
//    public WeaSearchCondition getCondition(String tenantKey) {
//        SalaryAcctEmpSearchConditionDTO searchConditionDTO = new SalaryAcctEmpSearchConditionDTO();
//        WeaSearchCondition searchCondition = SalaryFormatUtil.<SalaryAcctEmpSearchConditionDTO>getInstance().buildCondition(SalaryAcctEmpSearchConditionDTO.class,
//                searchConditionDTO, "SalaryAcctEmpCondition");
//        // 查询个税扣缴义务人
//        List<TaxAgentPO> taxAgentPOS = getTaxAgentService(user).listAll(tenantKey);
//        List<WeaSearchConditionOption> weaSearchConditionOptions = taxAgentPOS.stream()
//                .map(taxAgentPO -> new WeaSearchConditionOption(String.valueOf(taxAgentPO.getId()), taxAgentPO.getName()))
//                .collect(Collectors.toList());
//        // 给查询条件中的个税扣缴义务人填充下拉项
//        searchCondition.getItems().forEach((k, v) -> {
//            if (StringUtils.equals("taxAgentId", k)) {
//                v.setOptions(weaSearchConditionOptions);
//            }
//        });
//        // 其他条件不要
//        searchCondition.getGroups().remove(1);
//        return searchCondition;
//    }

    /**
     * 添加薪资核算人员
     *
     * @param saveParam 保存参数
     */
    public void save(SalaryAcctEmployeeSaveParam saveParam) {
        getSalaryAcctEmployeeService(user).save(saveParam);
    }

    /**
     * 从环比上月减少添加薪资核算人员
     *
     * @param addParam
     */
    public void addFromReduce(SalaryAcctEmployeeAddParam addParam) {
        getSalaryAcctEmployeeService(user).addFromReduce(addParam);
    }

    /**
     * 删除薪资核算人员
     *
     * @param deleteParam 删除参数
     */
    public void delete(SalaryAcctEmployeeDeleteParam deleteParam) {
        getSalaryAcctEmployeeService(user).deleteByParam(deleteParam);
    }

    /**
     * 检查个税扣缴义务人
     *
     * @param salaryAcctRecordId 薪资核算记录的id
     */
    public void checkTaxAgent(Long salaryAcctRecordId) {
        List<SalaryAcctEmployeePO> salaryAcctEmployeePOS = getSalaryAcctEmployeeService(user)
                .listBySalaryAcctRecordIdAndTaxAgentId(salaryAcctRecordId, NumberUtils.LONG_ZERO);
        if (CollectionUtils.isNotEmpty(salaryAcctEmployeePOS)) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98871, "有{0}个人无个税扣缴义务人，请先去薪资档案里维护员工档案，再点击当前页面的刷新按钮")
                    .replace("{0}", "" + salaryAcctEmployeePOS.size()));
        }
    }

    /**
     * 刷新个税扣缴义务人
     *
     * @param salaryAcctRecordId 薪资核算记录的id
     */
    public void refreshTaxAgent(Long salaryAcctRecordId) {
        getSalaryAcctEmployeeService(user).refresh(salaryAcctRecordId);
    }

}
