package com.engine.salary.service.impl;

import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.encrypt.AESEncryptUtil;
import com.engine.salary.encrypt.EncryptUtil;
import com.engine.salary.entity.report.po.SalaryAcctResultReportPO;
import com.engine.salary.mapper.report.SalaryAcctResultReportMapper;
import com.engine.salary.service.SalaryAcctReportService;
import com.engine.salary.sys.entity.po.SalarySysConfPO;
import com.engine.salary.sys.enums.OpenEnum;
import com.engine.salary.sys.service.SalarySysConfService;
import com.engine.salary.sys.service.impl.SalarySysConfServiceImpl;
import com.engine.salary.util.db.MapperProxyFactory;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import weaver.hrm.User;

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

import static com.engine.salary.sys.constant.SalarySysConstant.DISPLAY_EMP_INFO_REPORT;

/**
 * 薪资报表
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalaryAcctReportServiceImpl extends Service implements SalaryAcctReportService {
    private EncryptUtil encryptUtil = new EncryptUtil();
    private SalaryAcctResultReportMapper getSalaryAcctResultReportMapper() {
        return MapperProxyFactory.getProxy(SalaryAcctResultReportMapper.class);
    }


    private SalarySysConfService getSalarySysConfService(User user) {
        return ServiceUtil.getService(SalarySysConfServiceImpl.class, user);
    }

    /**
     * 保存核算报表数据
     *
     * @param pos
     */
    @Override
    public void batchSave(Collection<SalaryAcctResultReportPO> pos) {
        if (CollectionUtils.isNotEmpty(pos)) {
            SalarySysConfPO disPlay = getSalarySysConfService(user).getOneByCode(DISPLAY_EMP_INFO_REPORT);
            //默认不显示，关闭状态
            if (disPlay == null || OpenEnum.OFF.getValue().equals(disPlay.getConfValue())) {
                pos = encryptUtil.encryptList(new ArrayList<>(pos), SalaryAcctResultReportPO.class);
            }
//            List<List<SalaryAcctResultReportPO>> partition = Lists.partition((List) pos, 100);
//            partition.forEach(getSalaryAcctResultReportMapper()::batchInsert);

            pos.forEach(getSalaryAcctResultReportMapper()::insertIgnoreNull);
        }
    }

    @Override
    public void deleteBySalaryAcctRecordIds(Collection<Long> salaryAcctRecordIds) {
        if (CollectionUtils.isNotEmpty(salaryAcctRecordIds)) {
            getSalaryAcctResultReportMapper().deleteBySalaryAcctRecordIds(salaryAcctRecordIds);
        }
    }

    @Override
    public void deleteBySalaryAcctRecordId(Long salaryAcctRecordId) {
        getSalaryAcctResultReportMapper().deleteBySalaryAcctRecordId(salaryAcctRecordId);
    }

    @Override
    public void deleteBySalaryAcctEmpIds(Collection<Long> salaryAcctEmpIds) {
        if (CollectionUtils.isNotEmpty(salaryAcctEmpIds)) {
            List<String> collect = salaryAcctEmpIds.stream().map(e -> AESEncryptUtil.encrypt(e.toString())).collect(Collectors.toList());
            getSalaryAcctResultReportMapper().deleteBySalaryAcctEmpIds(collect);
        }
    }

    @Override
    public void deleteByAcctEmployeeIdsAndSalaryItemIds(List<Long> salaryAcctEmployeeIds, Collection<Long> salaryItemIds) {
        if (CollectionUtils.isNotEmpty(salaryAcctEmployeeIds) && CollectionUtils.isNotEmpty(salaryItemIds)) {
            List<String> salaryAcctEmployeeIdsStr = salaryAcctEmployeeIds.stream().map(e -> AESEncryptUtil.encrypt(e.toString())).collect(Collectors.toList());
            List<List<String>> partition = Lists.partition(salaryAcctEmployeeIdsStr, 300);
            partition.forEach(part -> getSalaryAcctResultReportMapper().deleteByAcctEmpIdsAndSalaryItemIds(part, salaryItemIds));
        }
    }
}
