package com.engine.salary.biz;

import com.engine.salary.entity.salaryarchive.param.SalaryArchiveTaxAgentQueryParam;
import com.engine.salary.entity.salaryarchive.po.SalaryArchiveTaxAgentPO;
import com.engine.salary.mapper.archive.SalaryArchiveTaxAgentMapper;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.SqlSession;
import weaver.conn.mybatis.MyBatisFactory;

import java.util.List;

public class SalaryArchiveTaxAgentBiz {

    public List<SalaryArchiveTaxAgentPO> listByParam(SalaryArchiveTaxAgentQueryParam build) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryArchiveTaxAgentMapper mapper = sqlSession.getMapper(SalaryArchiveTaxAgentMapper.class);
            return mapper.listByParam(build);
        } finally {
            sqlSession.close();
        }
    }

    public void deleteBySalaryArchiveId(List<Long> salaryArchiveIds) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryArchiveTaxAgentMapper mapper = sqlSession.getMapper(SalaryArchiveTaxAgentMapper.class);
            mapper.deleteBySalaryArchiveId(salaryArchiveIds);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

    public List<SalaryArchiveTaxAgentPO> getEffectiveTaxAgentList(SalaryArchiveTaxAgentQueryParam build) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryArchiveTaxAgentMapper mapper = sqlSession.getMapper(SalaryArchiveTaxAgentMapper.class);
            return mapper.getEffectiveTaxAgentList(build);
        } finally {
            sqlSession.close();
        }
    }

    public List<SalaryArchiveTaxAgentPO> getIneffectiveTaxAgentList(SalaryArchiveTaxAgentQueryParam build) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryArchiveTaxAgentMapper mapper = sqlSession.getMapper(SalaryArchiveTaxAgentMapper.class);
            return mapper.getIneffectiveTaxAgentList(build);
        } finally {
            sqlSession.close();
        }
    }

    public void deleteBatchIds(List<Long> ids) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryArchiveTaxAgentMapper mapper = sqlSession.getMapper(SalaryArchiveTaxAgentMapper.class);
            mapper.deleteBatchIds(ids);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

    public void batchInsert(List<SalaryArchiveTaxAgentPO> salaryArchiveTaxAgentSaves) {
        if (CollectionUtils.isEmpty(salaryArchiveTaxAgentSaves)) {
            return;
        }
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryArchiveTaxAgentMapper mapper = sqlSession.getMapper(SalaryArchiveTaxAgentMapper.class);
            List<List<SalaryArchiveTaxAgentPO>> partition = Lists.partition(salaryArchiveTaxAgentSaves, 100);
            partition.forEach(mapper::batchInsert);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }

    }

    public void batchDeleteHistoryData(SalaryArchiveTaxAgentPO build) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryArchiveTaxAgentMapper mapper = sqlSession.getMapper(SalaryArchiveTaxAgentMapper.class);
            mapper.batchDeleteHistoryData(build);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }
}
