package com.xisc.pm.infra.repository.impl;

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

import org.hzero.mybatis.base.impl.BaseRepositoryImpl;

import com.xisc.pm.app.service.autoboard.dto.PmAutoPdCheckDTO;
import com.xisc.pm.app.service.autoboard.dto.PmAutoPdContractSummaryDTO;
import com.xisc.pm.app.service.autoboard.dto.PmAutoPdPublishedDetailDTO;
import com.xisc.pm.app.service.autoboard.dto.PmAutoPdResultRequestDTO;
import com.xisc.pm.app.service.autoboard.dto.PmAutoPdVersionDTO;
import com.xisc.pm.domain.entity.PmAutoPdResult;
import com.xisc.pm.domain.repository.PmAutoPdResultRepository;
import com.xisc.pm.infra.mapper.PmAutoPdResultMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 自动组板结果信息表 资源库实现
 *
 * @author jun.zeng 2024-11-21 11:05:36
 */
@Component
public class PmAutoPdResultRepositoryImpl extends BaseRepositoryImpl<PmAutoPdResult>
        implements PmAutoPdResultRepository {

    @Autowired
    private PmAutoPdResultMapper pmAutoPdResultMapper;

    @Override
    public List<PmAutoPdResult> selectList(PmAutoPdResultRequestDTO pmAutoPdResultRequestDTO, Long organizationId) {
        return pmAutoPdResultMapper.selectList(pmAutoPdResultRequestDTO, organizationId);
    }

    @Override
    public List<PmAutoPdVersionDTO> selectExecVersion(Long requestId, Long tenantId) {
        return pmAutoPdResultMapper.selectExecVersion(requestId, tenantId);
    }

    @Override
    public int deleteByRequestId(List<Long> requestIds) {
        return pmAutoPdResultMapper.deleteByRequestId(requestIds);
    }

    @Override
    public List<PmAutoPdPublishedDetailDTO> selectSummaryDetailList(Long requestId, Long version, Long tenantId) {
        return pmAutoPdResultMapper.selectSummaryDetailList(requestId, version, tenantId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int markPublishedSuccess(Long tenantId, List<Long> resultIdList) {
        return updatePublishedStatus(tenantId, resultIdList, 2);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int markSaveSuccess(Long tenantId, List<Long> resultIdList) {
        return updatePublishedStatus(tenantId, resultIdList, 1);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int markRemovePublishedSave(Long tenantId, List<Long> resultIdList) {
        return updatePublishedStatus(tenantId, resultIdList, 0);
    }

    private int updatePublishedStatus(Long tenantId, List<Long> resultIdList, Integer targetStatus) {
        int allCount = resultIdList.size();
        long limit = (allCount + 1000L - 1) / 1000L;
        Stream.iterate(0, n -> n + 1).limit(limit).forEach(i -> {
            List<Long> subList = resultIdList.stream().skip(i * 1000L).limit(1000L).collect(Collectors.toList());
            this.pmAutoPdResultMapper.updatePublishedStatus(tenantId, subList, targetStatus);
        });
        return 0;
    }

    @Override
    public int initContLock(List<String> virtualSlabNoList, Long requestId, Long version, Long tenantId) {
        return pmAutoPdResultMapper.initContLock(virtualSlabNoList, requestId, version, tenantId);
    }

    @Override
    public List<PmAutoPdCheckDTO> checkStockNum(List<String> virtualSlabNoList, Long requestId, Long version,
            Long tenantId) {
        return pmAutoPdResultMapper.checkStockNum(virtualSlabNoList, requestId, version, tenantId);
    }

    @Override
    public List<PmAutoPdContractSummaryDTO> selectPmAutoPdContractSummary(List<String> virtualSlabNoList,
            Long requestId, Long version, Long tenantId) {
        return pmAutoPdResultMapper.selectPmAutoPdContractSummary(virtualSlabNoList, requestId, version, tenantId);
    }
}
