package com.engine.salary.biz;

import com.engine.salary.encrypt.EncryptUtil;
import com.engine.salary.entity.datacollection.AddUpDeduction;
import com.engine.salary.entity.datacollection.dto.AddUpDeductionDTO;
import com.engine.salary.entity.datacollection.dto.AddUpDeductionRecordDTO;
import com.engine.salary.entity.datacollection.param.AddUpDeductionQueryParam;
import com.engine.salary.mapper.datacollection.AddUpDeductionMapper;
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 weaver.general.BaseBean;

import java.util.ArrayList;
import java.util.List;

public class AddUpDeductionBiz extends BaseBean {

    private final EncryptUtil encryptUtil = new EncryptUtil();


    /**
     * 关联查询查询列表
     *
     * @param param
     * @return
     */
    public List<AddUpDeductionDTO> list(AddUpDeductionQueryParam param) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AddUpDeductionMapper mapper = sqlSession.getMapper(AddUpDeductionMapper.class);
            List<AddUpDeductionDTO> list = mapper.list(param);
            encryptUtil.decryptList(list, AddUpDeductionDTO.class);
            SalaryI18nUtil.i18nList(list);
            return list;
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 条件查询
     *
     * @param param
     * @return
     */
    public List<AddUpDeduction> listSome(AddUpDeduction param) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AddUpDeductionMapper mapper = sqlSession.getMapper(AddUpDeductionMapper.class);
            if (CollectionUtils.isNotEmpty(param.getEmployeeIds())) {
                List<AddUpDeduction> addUpDeductions = new ArrayList<>();
                List<List<Long>> partition = Lists.partition(param.getEmployeeIds(), 500);
                partition.forEach(l -> {
                    param.setEmployeeIds(l);
                    addUpDeductions.addAll(mapper.listSome(param));
                });
                return encryptUtil.decryptList(addUpDeductions, AddUpDeduction.class);
            } else {
                List<AddUpDeduction> addUpDeductions = mapper.listSome(param);
                return encryptUtil.decryptList(addUpDeductions, AddUpDeduction.class);
            }
        } finally {
            sqlSession.close();
        }
    }


    /**
     * 根据id获取
     *
     * @param id
     * @return
     */
    public AddUpDeduction getById(Long id) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AddUpDeductionMapper mapper = sqlSession.getMapper(AddUpDeductionMapper.class);
            AddUpDeduction byId = mapper.getById(id);
            return encryptUtil.decrypt(byId, AddUpDeduction.class);
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 详情列表
     *
     * @param param
     * @return
     */
    public List<AddUpDeductionRecordDTO> recordList(AddUpDeductionQueryParam param) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AddUpDeductionMapper mapper = sqlSession.getMapper(AddUpDeductionMapper.class);
            List<AddUpDeductionRecordDTO> addUpDeductionRecordStrDTOS = mapper.recordList(param);
            return encryptUtil.decryptList(addUpDeductionRecordStrDTOS, AddUpDeductionRecordDTO.class);
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 批量插入
     *
     * @param param
     * @return
     */
    public void batchSave(List<AddUpDeduction> param) {
        if (CollectionUtils.isEmpty(param)) {
            return;
        }
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AddUpDeductionMapper mapper = sqlSession.getMapper(AddUpDeductionMapper.class);
            encryptUtil.encryptList(param, AddUpDeduction.class);
            List<List<AddUpDeduction>> partition = Lists.partition(param, 100);
            partition.forEach(mapper::insertData);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 批量插入
     *
     * @param param
     * @return
     */
    public void batchUpdate(List<AddUpDeduction> param) {
        if (CollectionUtils.isEmpty(param)) {
            return;
        }
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AddUpDeductionMapper mapper = sqlSession.getMapper(AddUpDeductionMapper.class);
            encryptUtil.encryptList(param, AddUpDeduction.class);
            List<List<AddUpDeduction>> partition = Lists.partition(param, 100);
            partition.forEach(mapper::updateData);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }





    public void batchDeleteByIDS(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            AddUpDeductionMapper mapper = sqlSession.getMapper(AddUpDeductionMapper.class);
            List<List<Long>> partition = Lists.partition(ids, 100);
            partition.forEach(mapper::deleteData);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }


}
