package com.engine.salary.biz;

import com.engine.salary.entity.salaryarchive.po.SalaryArchivePO;
import com.engine.salary.mapper.archive.SalaryArchiveMapper;
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 SalaryArchiveBiz {

    public SalaryArchivePO getById(Long salaryArchiveId) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryArchiveMapper mapper = sqlSession.getMapper(SalaryArchiveMapper.class);
            return mapper.getById(salaryArchiveId);
        } finally {
            sqlSession.close();
        }
    }


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

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


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