package com.engine.salary.biz;

import com.engine.salary.entity.datacollection.po.AttendQuoteDataValuePO;
import com.engine.salary.mapper.datacollection.AttendQuoteDataValueMapper;
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.ArrayList;
import java.util.List;

public class AttendQuoteDataValueBiz {

    /**
     * 根据考勤引用字段表的主键id删除
     *
     * @param attendQuoteDataIds
     */
    public void deleteByAttendQuoteDataIds(List<Long> attendQuoteDataIds) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AttendQuoteDataValueMapper mapper = sqlSession.getMapper(AttendQuoteDataValueMapper.class);
            List<List<Long>> partition = Lists.partition(attendQuoteDataIds, 500);
            partition.forEach(mapper::deleteByAttendQuoteDataIds);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }


    public List<AttendQuoteDataValuePO> listSome(AttendQuoteDataValuePO param) {
        List<AttendQuoteDataValuePO> list = new ArrayList<>();
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AttendQuoteDataValueMapper mapper = sqlSession.getMapper(AttendQuoteDataValueMapper.class);

            List<Long> employeeIds = param.getEmployeeIds();
            if (CollectionUtils.isNotEmpty(employeeIds)) {
                List<List<Long>> partition = Lists.partition(employeeIds, 100);
                partition.forEach(empIds -> {
                    param.setEmployeeIds(empIds);
                    list.addAll(mapper.listSome(param));
                });
            } else {
                list.addAll(mapper.listSome(param));
            }
        } finally {
            sqlSession.close();
        }
        return list;
    }

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

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

    public void updateDataValue(AttendQuoteDataValuePO po) {

        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AttendQuoteDataValueMapper mapper = sqlSession.getMapper(AttendQuoteDataValueMapper.class);
            mapper.updateDataValueByFiledIdAndEmployeeId(po);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }

    }
}
