package com.xisc.pm.app.service.impl;

import com.xisc.pm.api.dto.export.PmRetainStackDTO;
import com.xisc.pm.infra.mapper.PmRetainStackMapper;
import io.choerodon.core.domain.Page;
import io.choerodon.core.exception.CommonException;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.xisc.pm.app.service.PmRetainStackService;
import org.springframework.stereotype.Service;
import com.xisc.pm.domain.entity.PmRetainStack;
import com.xisc.pm.domain.repository.PmRetainStackRepository;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 仓库交货配置表(PmRetainStack)应用服务
 *
 * @author BKGY
 * @since 2024-12-25 08:45:27
 */
@Service
public class PmRetainStackServiceImpl implements PmRetainStackService {
    @Autowired
    private PmRetainStackRepository pmRetainStackRepository;

    @Autowired
    private PmRetainStackMapper pmRetainStackMapper;

    @Override
    public Page<PmRetainStack> selectList(PageRequest pageRequest, PmRetainStack pmRetainStack) {
        return PageHelper.doPageAndSort(pageRequest, () -> pmRetainStackRepository.selectList(pmRetainStack));
    }

    @Override
    public void saveData(List<PmRetainStack> pmRetainStacks) {
        if(CollectionUtils.isEmpty(pmRetainStacks)){
            return;
        }
        // 提取传入数据的 stackNo 和 mainBacklogCode 组合并去重
        List<String> uniqueIncomingCombinationList = pmRetainStacks.stream()
                .map(stack -> stack.getStackNo() + "_" + stack.getMainBacklogCode())
                .distinct()
                .collect(Collectors.toList());
        // 检查传入数据的唯一性
        if (uniqueIncomingCombinationList.size() < pmRetainStacks.size()) {
            throw new CommonException("pm.retain_stack_001");
        }

        // 提取传入数据中 id 为空的 stackNo 和 mainBacklogCode 组合并去重
        List<String> checkList = pmRetainStacks.stream()
                .filter(stack -> stack.getId() == null) // 过滤 id 为空的数据
                .map(stack -> stack.getStackNo() + "_" + stack.getMainBacklogCode())
                .distinct()
                .collect(Collectors.toList());
        if (!checkList.isEmpty()) {
            List<PmRetainStack> check = pmRetainStackMapper.checkList(checkList);
            if(CollectionUtils.isNotEmpty(check)){
                throw new CommonException("pm.retain_stack_002",check.get(0).getStackNo() + "_" + check.get(0).getMainBacklogCode());
            }
        }


        List<PmRetainStack> insertList = pmRetainStacks.stream().filter(line -> line.getId() == null).collect(Collectors.toList());
        List<PmRetainStack> updateList = pmRetainStacks.stream().filter(line -> line.getId() != null).collect(Collectors.toList());

        pmRetainStackRepository.batchInsertSelective(insertList);
        pmRetainStackRepository.batchUpdateByPrimaryKeySelective(updateList);
    }

    @Override
    public List<PmRetainStackDTO> exportList(Long organizationId, PmRetainStack pmRetainStack) {
        pmRetainStack.setTenantId(organizationId);
        return pmRetainStackMapper.exportList(pmRetainStack);
    }
}

