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

import com.xisc.pm.api.dto.PmPdContPanelMaterialDTO;
import com.xisc.pm.app.service.PmPdContSumService;
import com.xisc.pm.domain.entity.PmContMscTech;
import com.xisc.pm.domain.entity.PmPdContSum;
import com.xisc.pm.domain.repository.PmContMscTechRepository;
import com.xisc.pm.domain.repository.PmPdContSumRepository;
import io.choerodon.core.domain.Page;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

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

/**
 * 组板合同总量表(PmPdContSum)应用服务
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-07-30 10:29:33
 */
@Service
public class PmPdContSumServiceImpl implements PmPdContSumService {

    private PmPdContSumRepository pmPdContSumRepository;

    private PmContMscTechRepository pmContMscTechRepository;

    public PmPdContSumServiceImpl(PmPdContSumRepository pmPdContSumRepository, PmContMscTechRepository pmContMscTechRepository) {
        this.pmPdContSumRepository = pmPdContSumRepository;
        this.pmContMscTechRepository = pmContMscTechRepository;
    }

    @Override
    public Page<PmPdContSum> selectList(PageRequest pageRequest, PmPdContSum pmPdContSum) {
        return PageHelper.doPageAndSort(pageRequest, () -> pmPdContSumRepository.selectList(pmPdContSum));
    }

    @Override
    public void saveData(List<PmPdContSum> pmPdContSums) {
        List<PmPdContSum> insertList = pmPdContSums.stream().filter(line -> line.getPpcsId() == null).collect(Collectors.toList());
        List<PmPdContSum> updateList = pmPdContSums.stream().filter(line -> line.getPpcsId() != null).collect(Collectors.toList());
        pmPdContSumRepository.batchInsertSelective(insertList);
        pmPdContSumRepository.batchUpdateByPrimaryKeySelective(updateList);
    }

    @Override
    public Page<PmPdContPanelMaterialDTO> selectConPanelMaterialsList(PageRequest pageRequest, String orderNoFrom, String orderNoTo, Long tenantId) {
        Page<PmPdContPanelMaterialDTO> page =  PageHelper.doPageAndSort(pageRequest, () -> pmPdContSumRepository.selectConPanelMaterialsList(orderNoFrom, orderNoTo, tenantId));
        if (CollectionUtils.isEmpty(page.getContent())) {
            return page;
        }
        // 获取全程工序码
        List<String> orderNoList = page.getContent().stream().map(PmPdContPanelMaterialDTO::getOrderNo).distinct().collect(Collectors.toList());
        List<PmContMscTech> pmContMscTechList = pmContMscTechRepository.selectByOrderNoList(orderNoList, tenantId);
        Map<String, String> pmContMscTechMap = pmContMscTechList.stream().collect(Collectors.toMap(PmContMscTech::getOrderNo, PmContMscTech::getWholeBacklog, (k, v) -> k));
        page.getContent().forEach(item->{
            item.setWholeBacklog(pmContMscTechMap.get(item.getOrderNo()));
        });
        return page;
    }
}

