package com.engine.salary.service.impl;

import com.engine.core.impl.Service;
import com.engine.salary.encrypt.EncryptUtil;
import com.engine.salary.entity.datacollection.po.VariableArchiveItemPO;
import com.engine.salary.mapper.datacollection.VariableArchiveItemMapper;
import com.engine.salary.service.VariableArchiveItemService;
import com.engine.salary.util.db.MapperProxyFactory;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * @author Harryxzy
 * @ClassName VariableArchiveItemServiceImpl
 * @date 2024/08/07 9:29
 * @description 浮动薪酬档案明细
 */
public class VariableArchiveItemServiceImpl extends Service implements VariableArchiveItemService {
    private EncryptUtil encryptUtil = new EncryptUtil();

    private VariableArchiveItemMapper getVariableArchiveItemMapper(){
        return MapperProxyFactory.getProxy(VariableArchiveItemMapper.class);
    }

    @Override
    public List<VariableArchiveItemPO> listAll() {
        return getVariableArchiveItemMapper().listAll();
    }

    /**
     * 根据浮动薪资档案id获取
     *
     * @param variableArchiveIds
     * @return
     */
    @Override
    public List<VariableArchiveItemPO> listByVariableArchiveIds(List<Long> variableArchiveIds) {
        if (CollectionUtils.isEmpty(variableArchiveIds)) {
            return Collections.emptyList();
        }
        List<VariableArchiveItemPO> variableArchiveItemPOS = getVariableArchiveItemMapper().listSome(VariableArchiveItemPO.builder().variableArchiveIds(variableArchiveIds).build());
        return encryptUtil.decryptList(variableArchiveItemPOS, VariableArchiveItemPO.class);
    }

    /**
     * 查询浮动薪酬档案中已使用的浮动薪资项目
     *
     * @return
     */
    @Override
    public List<Long> listUsingItems() {
        return getVariableArchiveItemMapper().listUsingItems();
    }

    @Override
    public int batchInsert(List<VariableArchiveItemPO> insertList) {
        if (CollectionUtils.isEmpty(insertList)) {
            return 0;
        }
        encryptUtil.encryptList(insertList, VariableArchiveItemPO.class);
        List<List<VariableArchiveItemPO>> partition = Lists.partition(insertList, 50);
        partition.forEach(part -> getVariableArchiveItemMapper().batchInsert(part));
        return 0;
    }

    @Override
    public void deleteByArchiveIds(List<Long> variableArchiveIds) {
        if (CollectionUtils.isEmpty(variableArchiveIds)) {
            return;
        }
        List<List<Long>> partition = Lists.partition(variableArchiveIds, 500);
        partition.forEach(part -> getVariableArchiveItemMapper().deleteByArchiveIds(part));
    }

    @Override
    public void deleteByIds(List<Long> variableArchiveItemIds) {
        if (CollectionUtils.isEmpty(variableArchiveItemIds)) {
            return;
        }
        List<List<Long>> partition = Lists.partition(variableArchiveItemIds, 500);
        partition.forEach(part -> getVariableArchiveItemMapper().deleteByIds(part));

    }
}
