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.taxagent.bo.TaxAgentBO;
import com.engine.salary.entity.taxagent.dto.*;
import com.engine.salary.entity.taxagent.param.TaxAgentSubAdminBaseFormParam;
import com.engine.salary.entity.taxagent.param.TaxAgentSubAdminBaseSaveParam;
import com.engine.salary.entity.taxagent.param.TaxAgentSubAdminQueryParam;
import com.engine.salary.entity.taxagent.param.TaxAgentSubAdminRangeQueryParam;
import com.engine.salary.entity.taxagent.po.TaxAgentSubAdminPO;
import com.engine.salary.enums.UserStatusEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.service.SalaryEmployeeService;
import com.engine.salary.service.TaxAgentManageRangeService;
import com.engine.salary.service.TaxAgentSubAdminService;
import com.engine.salary.service.impl.SalaryEmployeeServiceImpl;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.page.SalaryPageUtil;
import com.engine.salary.util.valid.ValidUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.Collection;
import java.util.Collections;
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 TaxAgentSubAdminWrapper extends Service {

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

    private TaxAgentSubAdminService taxAgentSubAdminService;
    private TaxAgentManageRangeService taxAgentManageRangeService;
//    private HrmCommonEmployeeService hrmCommonEmployeeService;

    /**
     * 获取分管理员列表
     *
     * @param queryParam
     * @return
     */
    public PageInfo<TaxAgentSubAdminListDTO> list(TaxAgentSubAdminQueryParam queryParam) {
        ValidUtil.doValidator(queryParam);

        List<TaxAgentSubAdminPO> list = taxAgentSubAdminService.listByTaxAgentIds(Collections.singletonList(queryParam.getTaxAgentId()));
        String range = SalaryI18nUtil.getI18nLabel(106290, "详情");
        List<DataCollectionEmployee> subAdminList = getSalaryEmployeeService().getEmployeeByIds(list.stream().map(TaxAgentSubAdminPO::getEmployeeId).distinct().collect(Collectors.toList()));

        List<TaxAgentSubAdminListDTO> records = TaxAgentBO.convertToSubAdminListDTO(list, range, subAdminList);
        PageInfo<TaxAgentSubAdminListDTO> listPage = SalaryPageUtil.buildPage(queryParam.getCurrent(), queryParam.getPageSize(), TaxAgentSubAdminListDTO.class);
        // 填充总数和当页数据
        listPage.setTotal(records.size());
        listPage.setList(SalaryPageUtil.subList(listPage.getPageNum(), listPage.getPageSize(), records));

        return listPage;
    }

    /**
     * 删除分管理员
     *
     * @param ids
     * @return
     */
    public String delete(Collection<Long> ids) {
        return taxAgentSubAdminService.deleteByIds(ids);
    }

    /**
     * 获取基础设置表单
     *
     * @param baseFormParam
     * @return
     */
    public TaxAgentSubAdminBaseFormDTO getBaseFrom(TaxAgentSubAdminBaseFormParam baseFormParam) {
        ValidUtil.doValidator(baseFormParam);

        TaxAgentSubAdminBaseFormDTO taxAgentSubAdminBaseFormDTO = new TaxAgentSubAdminBaseFormDTO();
        Long id = baseFormParam.getId();
        if (id != null) {
            TaxAgentSubAdminPO taxAgentSubAdmin = taxAgentSubAdminService.getById(id);
            if (taxAgentSubAdmin == null) {
                throw new SalaryRunTimeException(String.format(SalaryI18nUtil.getI18nLabel(106466, "分管理员不存在") + "[id:%s]", id));
            }
            if (!baseFormParam.getTaxAgentId().equals(taxAgentSubAdmin.getTaxAgentId())) {
                throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(106685, "分管理员与所属个税扣缴义务人不匹配"));
            }
            taxAgentSubAdminBaseFormDTO.setId(taxAgentSubAdmin.getId());
            taxAgentSubAdminBaseFormDTO.setDescription(taxAgentSubAdmin.getRemark());
            TaxAgentEmployeeOptionDTO taxAgentEmployee = new TaxAgentEmployeeOptionDTO();
            taxAgentEmployee.setId(taxAgentSubAdmin.getEmployeeId());
            DataCollectionEmployee employee = getSalaryEmployeeService().getEmployeeById(taxAgentEmployee.getId());
            taxAgentEmployee.setContent(employee == null ? "" : employee.getUsername());

            taxAgentSubAdminBaseFormDTO.setSubAdminUser(Collections.singletonList(taxAgentEmployee));
        } else {
            taxAgentSubAdminBaseFormDTO.setSubAdminUser(Lists.newArrayList());
        }

        return taxAgentSubAdminBaseFormDTO;
    }

    /**
     * 保存分管理员基础设置
     *
     * @param saveParam
     * @return
     */
    public String saveBase(TaxAgentSubAdminBaseSaveParam saveParam) {
        return taxAgentSubAdminService.saveBase(saveParam);
    }

    /**
     * 范围列表
     *
     * @param queryParam
     * @return
     */
    public PageInfo<TaxAgentManageRangeListDTO> listRangeInclude(TaxAgentSubAdminRangeQueryParam queryParam) {
        PageInfo<TaxAgentManageRangeListDTO> listPage = taxAgentManageRangeService.listPageByParamAndIncludeType(queryParam, NumberUtils.INTEGER_ONE);
        return listPage;
    }

    /**
     * 范围排除列表
     *
     * @param queryParam
     * @return
     */
    public PageInfo<TaxAgentManageRangeListDTO> listRangeExclude(TaxAgentSubAdminRangeQueryParam queryParam) {
        PageInfo<TaxAgentManageRangeListDTO> listPage = taxAgentManageRangeService.listPageByParamAndIncludeType(queryParam, NumberUtils.INTEGER_ZERO);
        return listPage;
    }

    /**
     * 获取管理范围表单
     *
     * @return
     */
    public TaxAgentManageRangeFormDTO getRangeFrom() {
        return TaxAgentManageRangeFormDTO.builder().employeeStatus(UserStatusEnum.getHrmStatusList()).build();
    }

    /**
     * 保存管理范围
     *
     * @param saveParam
     * @return
     */
//    public String saveRange(TaxAgentSubAdminRangeSaveParam saveParam) {
//        taxAgentManageRangeService.save4SubAdmin(saveParam);
//        return StringUtils.EMPTY;
//    }

    /**
     * 删除管理范围
     *
     * @param ids
     * @return
     */
    public String deleteRange(Collection<Long> ids) {
        taxAgentManageRangeService.deleteByIds(ids);
        return StringUtils.EMPTY;
    }
}
