package com.engine.salary.biz;

import com.engine.salary.entity.salaryBill.dto.SalaryMySalaryBillListDTO;
import com.engine.salary.entity.salaryBill.dto.SalarySendDetailListDTO;
import com.engine.salary.entity.salaryBill.dto.SalarySendInfoListDTO;
import com.engine.salary.entity.salaryBill.param.SalaryBillQueryParam;
import com.engine.salary.entity.salaryBill.param.SalarySendDetailQueryParam;
import com.engine.salary.entity.salaryBill.param.SalarySendInfoQueryParam;
import com.engine.salary.entity.salaryBill.po.SalarySendInfoPO;
import com.engine.salary.mapper.salarybill.SalarySendInfoMapper;
import com.engine.salary.util.SalaryI18nUtil;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import weaver.conn.mybatis.MyBatisFactory;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class SalarySendInfoBiz {
    /**
     * 查询工资单发放信息列表
     */
    public List<SalarySendInfoListDTO> list(SalarySendInfoQueryParam param) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySendInfoMapper mapper = sqlSession.getMapper(SalarySendInfoMapper.class);
            List<SalarySendInfoListDTO> list = mapper.list(param);
            return SalaryI18nUtil.i18nList(list);
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 工资单发放
     *
     * @param po
     * @param salarySendId
     * @param statusList
     * @param ids
     */
    public void updateGrantWithdraw(SalarySendInfoPO po, Long salarySendId, List<Integer> statusList, Collection<Long> ids) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySendInfoMapper mapper = sqlSession.getMapper(SalarySendInfoMapper.class);
            mapper.updateGrantWithdraw(po, salarySendId, statusList, ids);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }


    /**
     * 根据现有字段查询
     *
     * @param params
     * @return
     */
    public List<SalarySendInfoPO> listSome(@Param("param") SalarySendInfoPO params) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySendInfoMapper mapper = sqlSession.getMapper(SalarySendInfoMapper.class);
            return mapper.listSome(params);
        } finally {
            sqlSession.close();
        }
    }

    public void updateById(SalarySendInfoPO po) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySendInfoMapper mapper = sqlSession.getMapper(SalarySendInfoMapper.class);
            mapper.updateIgnoreNull(po);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 工资单发放详情列表
     *
     * @param param
     * @param otherSalaryAccRecordIds
     * @return
     */
    public List<SalarySendDetailListDTO> detailList(@Param("param") SalarySendDetailQueryParam param,
                                                    @Param("otherSalaryAccRecordIds") Set<Long> otherSalaryAccRecordIds) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySendInfoMapper mapper = sqlSession.getMapper(SalarySendInfoMapper.class);
            List<SalarySendDetailListDTO> detailList = mapper.detailList(param, otherSalaryAccRecordIds);
            SalaryI18nUtil.i18nList(detailList);
            return detailList;
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 我的工资单列表详情
     *
     * @param queryParam
     * @return
     */
    public List<SalaryMySalaryBillListDTO> mySalaryBillList(SalaryBillQueryParam queryParam) {
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySendInfoMapper mapper = sqlSession.getMapper(SalarySendInfoMapper.class);
            return mapper.mySalaryBillList(queryParam);
        } finally {
            sqlSession.close();
        }
    }

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

    public List<Long> listSalaryAccRecordIds(SalarySendDetailQueryParam queryParam) {
        if (null == queryParam) {
            return Collections.emptyList();
        }
        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
        try {
            SalarySendInfoMapper mapper = sqlSession.getMapper(SalarySendInfoMapper.class);
            return mapper.listSalaryAccRecordIds(queryParam);
        } finally {
            sqlSession.close();
        }
    }
}
