package com.engine.salary.biz;

import com.engine.salary.entity.salarysob.po.SalarySobAdjustRulePO;
import com.engine.salary.mapper.salarysob.SalarySobAdjustRuleMapper;
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.Collection;
import java.util.List;

public class SalarySobAdjustRuleBiz {

    public List<SalarySobAdjustRulePO> listBySalarySobId(Long salarySobId) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobAdjustRuleMapper mapper = sqlSession.getMapper(SalarySobAdjustRuleMapper.class);
            return mapper.listSome(SalarySobAdjustRulePO.builder().salarySobId(salarySobId).build());
        } finally {
            sqlSession.close();
        }
    }

    public void deleteBySalarySobIds(Collection<Long> salarySobIds) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobAdjustRuleMapper mapper = sqlSession.getMapper(SalarySobAdjustRuleMapper.class);
            mapper.deleteBySalarySobIds(salarySobIds);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

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