package com.engine.salary.biz;

import com.engine.salary.entity.datacollection.dto.AttendQuoteFieldListDTO;
import com.engine.salary.entity.datacollection.param.AttendQuoteFieldQueryParam;
import com.engine.salary.entity.datacollection.po.AttendQuoteFieldPO;
import com.engine.salary.mapper.datacollection.AttendQuoteFieldMapper;
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 AttendQuoteFieldBiz {

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

    public void save(AttendQuoteFieldPO po) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AttendQuoteFieldMapper mapper = sqlSession.getMapper(AttendQuoteFieldMapper.class);
            mapper.insertIgnoreNull(po);

            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

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

    public void update(AttendQuoteFieldPO po) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AttendQuoteFieldMapper mapper = sqlSession.getMapper(AttendQuoteFieldMapper.class);
            mapper.updateIgnoreNull(po);

            sqlSession.commit();
        } finally {
            sqlSession.close();
        }

    }

    public List<AttendQuoteFieldListDTO> list(AttendQuoteFieldQueryParam param) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AttendQuoteFieldMapper mapper = sqlSession.getMapper(AttendQuoteFieldMapper.class);
            return mapper.list(param);
        } finally {
            sqlSession.close();
        }
    }

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

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

    public void deleteAttendByCode(AttendQuoteFieldQueryParam param) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AttendQuoteFieldMapper mapper = sqlSession.getMapper(AttendQuoteFieldMapper.class);
            mapper.deleteAttendByCode(param);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }
}
