package com.engine.salary.biz;

import com.engine.salary.entity.salarysob.po.SalarySobRangePO;
import com.engine.salary.mapper.salarysob.SalarySobRangeMapper;
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 SalarySobRangeBiz {


    public List<SalarySobRangePO> listSome(SalarySobRangePO build) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobRangeMapper mapper = sqlSession.getMapper(SalarySobRangeMapper.class);
            return mapper.listSome(build);
        } finally {
            sqlSession.close();
        }
    }

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

    public void updateById(SalarySobRangePO po) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobRangeMapper mapper = sqlSession.getMapper(SalarySobRangeMapper.class);
            mapper.updateIgnoreNull(po);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

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

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

    public void updateEmployeeStatuses(SalarySobRangePO item) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobRangeMapper mapper = sqlSession.getMapper(SalarySobRangeMapper.class);
            mapper.updateEmployeeStatuses(item);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }
}
