package com.engine.salary.report.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.engine.core.impl.Service;
import com.engine.salary.mapper.report.salaryStatisticsPushDetailMapper;
import com.engine.salary.report.entity.po.SalaryStatisticsPushDetailPO;
import com.engine.salary.report.service.SalaryStatisticsPushDetailService;
import com.engine.salary.util.db.MapperProxyFactory;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Harryxzy
 * @ClassName SalaryStatisticsPushDetailServiceImpl
 * @date 2023/09/13 15:13
 * @description
 */
public class SalaryStatisticsPushDetailServiceImpl extends Service implements SalaryStatisticsPushDetailService {

    private salaryStatisticsPushDetailMapper getsalaryStatisticsPushDetailMapper() {
        return MapperProxyFactory.getProxy(salaryStatisticsPushDetailMapper.class);
    }

    @Override
    public List<SalaryStatisticsPushDetailPO> listAll() {
        return getsalaryStatisticsPushDetailMapper().listAll();
    }

    @Override
    public List<SalaryStatisticsPushDetailPO> listSome(SalaryStatisticsPushDetailPO po) {
        return getsalaryStatisticsPushDetailMapper().listSome(po);
    }

    @Override
    public SalaryStatisticsPushDetailPO getById(Long id) {
        return getsalaryStatisticsPushDetailMapper().getById(id);
    }

    @Override
    public int insertIgnoreNull(SalaryStatisticsPushDetailPO po) {
        return getsalaryStatisticsPushDetailMapper().insertIgnoreNull(po);
    }

    @Override
    public int update(SalaryStatisticsPushDetailPO po) {
        return getsalaryStatisticsPushDetailMapper().update(po);
    }

    @Override
    public int updateIgnoreNull(SalaryStatisticsPushDetailPO po) {
        return getsalaryStatisticsPushDetailMapper().updateIgnoreNull(po);
    }

    @Override
    public int delete(SalaryStatisticsPushDetailPO po) {
        return getsalaryStatisticsPushDetailMapper().delete(po);
    }

    /**
     * 根据批次id查询
     * @param batchIds
     */
    public List<SalaryStatisticsPushDetailPO> queryPushDetailPOByBatchIds(List<Long> batchIds) {
        if (CollectionUtils.isEmpty(batchIds)) {
            return Collections.emptyList();
        }
        List<List<Long>> partition = Lists.partition(batchIds, 999);
        List<SalaryStatisticsPushDetailPO> list = new ArrayList<>();
        partition.forEach(l -> {
            list.addAll(getsalaryStatisticsPushDetailMapper().queryPushDetailPOByBatchId(l));
        });
        return list;
    }

    /**
     * 根据批次id、员工id查询
     * @param
     */
    public List<SalaryStatisticsPushDetailPO> queryPushDetailPOByBatchIdAndEmpIds(Long batchId, List<Long> sharedBy) {
        if (CollectionUtils.isEmpty(sharedBy) || ObjectUtil.isEmpty(batchId)) {
            return Collections.emptyList();
        }
        List<List<Long>> partition = Lists.partition(sharedBy, 999);
        List<SalaryStatisticsPushDetailPO> list = new ArrayList<>();
        partition.forEach(l -> {
            list.addAll(getsalaryStatisticsPushDetailMapper().queryPushDetailPOByBatchIdAndEmpIds(batchId, l));
        });
        return list;
    }

    public List<SalaryStatisticsPushDetailPO> getSuccessPushDetailListByReceiver(Long uid) {
        List<SalaryStatisticsPushDetailPO> detailList = listSome(SalaryStatisticsPushDetailPO.builder()
                .employeeId(uid)
                .pushStatus("true")
                .deleteType(NumberUtils.INTEGER_ZERO)
                .build());
        // 过滤已经撤回的
        return detailList.stream().filter(po -> !(Objects.equals(po.getRebackStatus(), "true"))).collect(Collectors.toList());
    }
}
