package com.engine.salary.biz;

import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.entity.hrm.DeptInfo;
import com.engine.salary.entity.hrm.PositionInfo;
import com.engine.salary.entity.hrm.SubCompanyInfo;
import com.engine.salary.entity.salarysob.param.SalarySobRangeEmpQueryParam;
import com.engine.salary.mapper.datacollection.EmployMapper;
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.Collections;
import java.util.List;

public class EmployBiz extends BaseBean {

    public List<DataCollectionEmployee> listAll(){
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            EmployMapper mapper = sqlSession.getMapper(EmployMapper.class);
            return mapper.listAll();
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 查询人员列表
     *
     * @return
     */
    public List<DataCollectionEmployee> listEmployee() {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            EmployMapper mapper = sqlSession.getMapper(EmployMapper.class);
            return mapper.listEmployee();
        } finally {
            sqlSession.close();
        }
    }

    public List<DataCollectionEmployee> getEmployeeByIds(List<Long> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            List<DataCollectionEmployee> dataList = new ArrayList<>();
            EmployMapper mapper = sqlSession.getMapper(EmployMapper.class);
            List<List<Long>> partition = Lists.partition(list, 1000);
            for (List<Long> longs : partition) {
                dataList.addAll(mapper.getEmployeeByIds(longs));
            }
            return dataList;
        } finally {
            sqlSession.close();
        }
    }


    public List<DataCollectionEmployee> getEmployeeByIdsAll(List<Long> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            List<DataCollectionEmployee> employeeList = new ArrayList<>();
            EmployMapper mapper = sqlSession.getMapper(EmployMapper.class);
            List<List<Long>> partition = Lists.partition(list, 1000);
            partition.forEach(e->{
                List<DataCollectionEmployee> employeeByIdsAll = mapper.getEmployeeByIdsAll(e);
                employeeList.addAll(employeeByIdsAll);
            });
            return employeeList;
        } finally {
            sqlSession.close();
        }
    }


    /**
     * 岗位信息
     *
     * @param list
     * @return
     */
    public List<PositionInfo> listPositionInfo(List<Long> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            EmployMapper mapper = sqlSession.getMapper(EmployMapper.class);
            return mapper.listPositionInfo(list);
        } finally {
            sqlSession.close();
        }
    }

    public List<DataCollectionEmployee> listByParams(List<SalarySobRangeEmpQueryParam> includeQueryParams) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            EmployMapper mapper = sqlSession.getMapper(EmployMapper.class);
            List<DataCollectionEmployee> emps = new ArrayList<>();
            List<List<SalarySobRangeEmpQueryParam>> partition = Lists.partition(includeQueryParams, 100);
            partition.forEach(list->{
                emps.addAll(mapper.listByParams(list));
            });
            return emps;
        } finally {
            sqlSession.close();
        }
    }

    public List<DataCollectionEmployee> listByVirtualParams(List<SalarySobRangeEmpQueryParam> virtualQueryParams) {
        if (CollectionUtils.isEmpty(virtualQueryParams)) {
            return Collections.emptyList();
        }
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            EmployMapper mapper = sqlSession.getMapper(EmployMapper.class);
            List<DataCollectionEmployee> emps = new ArrayList<>();
            List<List<SalarySobRangeEmpQueryParam>> partition = Lists.partition(virtualQueryParams, 100);
            partition.forEach(list->{
                emps.addAll(mapper.listByVirtualParams(list));
            });
            return emps;
        } finally {
            sqlSession.close();
        }
    }

    public DataCollectionEmployee getEmployeeById(Long employeeId) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            EmployMapper mapper = sqlSession.getMapper(EmployMapper.class);
            return mapper.getEmployeeById(employeeId);
        } finally {
            sqlSession.close();
        }
    }

    public List<DeptInfo> getDeptInfoList(List<Long> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            EmployMapper mapper = sqlSession.getMapper(EmployMapper.class);
            return mapper.getDeptInfoList(list);
        } finally {
            sqlSession.close();
        }
    }

    public List<SubCompanyInfo> getSubCompanyInfoList(List<Long> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            EmployMapper mapper = sqlSession.getMapper(EmployMapper.class);
            return mapper.getSubCompanyInfoList(list);
        } finally {
            sqlSession.close();
        }
    }

    public List<DataCollectionEmployee> listAllForReport() {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            EmployMapper mapper = sqlSession.getMapper(EmployMapper.class);
            return mapper.listAllForReport();
        } finally {
            sqlSession.close();
        }
    }

    public List<DataCollectionEmployee> listByDismissDate(String dismissDate) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            EmployMapper mapper = sqlSession.getMapper(EmployMapper.class);
            return mapper.listByDismissDate(dismissDate);
        } finally {
            sqlSession.close();
        }
    }
}
