package com.engine.salary.biz;

import com.engine.salary.entity.datacollection.dto.AttendQuoteDataBaseDTO;
import com.engine.salary.entity.datacollection.param.AttendQuoteDataQueryParam;
import com.engine.salary.entity.datacollection.po.AttendQuoteDataPO;
import com.engine.salary.mapper.datacollection.AttendQuoteDataMapper;
import com.engine.salary.util.SalaryI18nUtil;
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 AttendQuoteDataBiz {

    public List<AttendQuoteDataBaseDTO> list(AttendQuoteDataQueryParam param) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AttendQuoteDataMapper mapper = sqlSession.getMapper(AttendQuoteDataMapper.class);
            List<AttendQuoteDataBaseDTO> list = mapper.list(param);
            return SalaryI18nUtil.i18nList(list);
        } finally {
            sqlSession.close();
        }
    }

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



    public void deleteByAttendQuoteIds(List<Long> unAccountingIds) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AttendQuoteDataMapper mapper = sqlSession.getMapper(AttendQuoteDataMapper.class);
            mapper.deleteByAttendQuoteIds(unAccountingIds);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }

    }

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

}
