package com.engine.salary.biz;

import com.engine.salary.entity.salaryitem.param.SalaryItemSearchParam;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.enums.SalaryOnOffEnum;
import com.engine.salary.mapper.salaryitem.SalaryItemMapper;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.page.SalaryPageUtil;
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 SalaryItemBiz {

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

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

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

    public void batchSave(Collection<SalaryItemPO> salaryItems) {
        if (CollectionUtils.isEmpty(salaryItems)) {
            return;
        }
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryItemMapper mapper = sqlSession.getMapper(SalaryItemMapper.class);
            List<List<SalaryItemPO>> partition = Lists.partition((List) salaryItems, 100);
            partition.forEach(mapper::batchInsert);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

    public void insert(SalaryItemPO salaryItemPO) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryItemMapper mapper = sqlSession.getMapper(SalaryItemMapper.class);
            mapper.insertIgnoreNull(salaryItemPO);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

    public void updateById(SalaryItemPO newSalaryItemPO) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryItemMapper mapper = sqlSession.getMapper(SalaryItemMapper.class);
            mapper.updateIgnoreNull(newSalaryItemPO);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

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

    public PageInfo<SalaryItemPO> listPageByParam(SalaryItemSearchParam searchParam) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryItemMapper mapper = sqlSession.getMapper(SalaryItemMapper.class);
            List<SalaryItemPO> salaryItemPOList = mapper.listByParam(searchParam);
            return SalaryPageUtil.buildPage(searchParam.getCurrent(), searchParam.getPageSize(),
                    salaryItemPOList, SalaryItemPO.class);
        } finally {
            sqlSession.close();
        }

    }

    public List<SalaryItemPO> listByParam(SalaryItemSearchParam searchParam) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryItemMapper mapper = sqlSession.getMapper(SalaryItemMapper.class);
            return mapper.listByParam(searchParam);
        } finally {
            sqlSession.close();
        }
    }

    public List<SalaryItemPO> getCanAdjustSalaryItems() {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryItemMapper mapper = sqlSession.getMapper(SalaryItemMapper.class);
            return mapper.listSome(SalaryItemPO.builder().useInEmployeeSalary(SalaryOnOffEnum.ON.getValue()).build());
        } finally {
            sqlSession.close();
        }

    }


    public void batchUpdateSortedIndex(List<SalaryItemPO> list) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryItemMapper mapper = sqlSession.getMapper(SalaryItemMapper.class);
            List<List<SalaryItemPO>> partition = (List<List<SalaryItemPO>>)Lists.partition(list, 100);
            partition.forEach(mapper::batchUpdateSortedIndex);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }


    public List<SalaryItemPO> listByParamOrderById(SalaryItemSearchParam searchParam) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalaryItemMapper mapper = sqlSession.getMapper(SalaryItemMapper.class);
            return mapper.listByParamOrderById(searchParam);
        } finally {
            sqlSession.close();
        }
    }



}
