package com.engine.salary.biz;

import com.engine.salary.entity.salarysob.param.SalarySobCheckRuleQueryParam;
import com.engine.salary.entity.salarysob.po.SalarySobCheckRulePO;
import com.engine.salary.mapper.salarysob.SalarySobCheckRuleMapper;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.page.SalaryPageUtil;
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 SalarySobCheckRuleBiz {


    public SalarySobCheckRulePO getById(Long id) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobCheckRuleMapper mapper = sqlSession.getMapper(SalarySobCheckRuleMapper.class);
            return mapper.getById(id);
        } finally {
            sqlSession.close();
        }
    }

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

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

    public void updateById(SalarySobCheckRulePO newSalarySobCheckRulePO) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobCheckRuleMapper mapper = sqlSession.getMapper(SalarySobCheckRuleMapper.class);
            mapper.updateIgnoreNull(newSalarySobCheckRulePO);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

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

    public void insert(SalarySobCheckRulePO salarySobCheckRulePO) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobCheckRuleMapper mapper = sqlSession.getMapper(SalarySobCheckRuleMapper.class);
            mapper.insertIgnoreNull(salarySobCheckRulePO);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

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

    public PageInfo<SalarySobCheckRulePO> listPageByParam(SalarySobCheckRuleQueryParam param) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySobCheckRuleMapper mapper = sqlSession.getMapper(SalarySobCheckRuleMapper.class);
            List<SalarySobCheckRulePO> list = mapper.listByParam(param);
            return SalaryPageUtil.buildPage(param.getCurrent(), param.getPageSize(),
                    list, SalarySobCheckRulePO.class);
        } finally {
            sqlSession.close();
        }
    }
}
