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

import com.xisc.pm.api.dto.PmIrsubableSlabRequestDTO;
import com.xisc.pm.api.dto.export.PmIrsubableSlabDTO;
import com.xisc.pm.domain.entity.PmIrsubableCont;
import com.xisc.pm.domain.entity.PmProContProdTtr;
import com.xisc.pm.infra.client.MmEventClient;
import com.xisc.pm.infra.feign.MmApiFeign;
import com.xisc.pm.infra.mapper.PmIrsubableContMapper;
import com.xisc.pm.infra.mapper.PmIrsubableSlabMapper;
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.hzero.mybatis.domian.Condition;
import org.hzero.mybatis.util.Sqls;
import org.springframework.beans.factory.annotation.Autowired;
import com.xisc.pm.app.service.PmIrsubableSlabService;
import org.springframework.stereotype.Service;
import com.xisc.pm.domain.entity.PmIrsubableSlab;
import com.xisc.pm.domain.repository.PmIrsubableSlabRepository;

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

/**
 * 不可替代板坯表(PmIrsubableSlab)应用服务
 *
 * @author BKGY
 * @since 2024-12-20 15:57:39
 */
@Service
public class PmIrsubableSlabServiceImpl implements PmIrsubableSlabService {
    @Autowired
    private PmIrsubableSlabRepository pmIrsubableSlabRepository;

    @Autowired
    private PmIrsubableSlabMapper pmIrsubableSlabMapper;

    @Autowired
    private MmEventClient mmEventClient;

    @Override
    public Page<PmIrsubableSlab> selectList(PageRequest pageRequest, PmIrsubableSlabRequestDTO pmIrsubableSlab) {
        return PageHelper.doPageAndSort(pageRequest, () -> pmIrsubableSlabRepository.selectList(pmIrsubableSlab));
    }

    @Override
    public void saveData(Long tenantId , List<PmIrsubableSlab> pmIrsubableSlabs) {
        if(CollectionUtils.isEmpty(pmIrsubableSlabs)){
            return;
        }
        // 校验唯一性
        List<String> slabNoList = pmIrsubableSlabs.stream().map(PmIrsubableSlab::getSlabNo).distinct().collect(Collectors.toList());
        if (slabNoList.size() < pmIrsubableSlabs.size()) {
            throw new CommonException("pm.irsubable_slab_001");
        }
        List<PmIrsubableSlab> existIrsubableSlabList = pmIrsubableSlabRepository.selectByCondition(Condition.builder(PmIrsubableSlab.class)
                .andWhere(Sqls.custom().andIn(PmIrsubableSlab.FIELD_SLAB_NO, slabNoList))
                .andWhere(Sqls.custom().andEqualTo(PmIrsubableSlab.FIELD_TENANT_ID, tenantId)).build());
        if(CollectionUtils.isNotEmpty(existIrsubableSlabList)){
            throw new CommonException("pm.irsubable_slab_002",existIrsubableSlabList.get(0).getSlabNo());
        }

        List<PmIrsubableSlab> pmIrsubableSlabList = pmIrsubableSlabMapper.querySaveList(slabNoList,tenantId);
        if(CollectionUtils.isEmpty(pmIrsubableSlabList)){
            throw new CommonException("pm.irsubable_slab_003");
        }
        pmIrsubableSlabs.forEach(line -> {
            PmIrsubableSlab pmIrsubableSlab = pmIrsubableSlabList.stream().filter(slab -> slab.getSlabNo().equals(line.getSlabNo())).findFirst().orElse(null);
            if (pmIrsubableSlab == null) {
                throw new CommonException("pm.irsubable_slab_004");
            }
            line.setTenantId(tenantId);
            line.setSlabNo(pmIrsubableSlab.getSlabNo());
            line.setMatActThick(pmIrsubableSlab.getMatActThick());
            line.setMatActWidth(pmIrsubableSlab.getMatActWidth());
            line.setMatActLen(pmIrsubableSlab.getMatActLen());
            line.setSgSign(pmIrsubableSlab.getSgSign());
            line.setStNo(pmIrsubableSlab.getStNo());
            line.setDstockCode(pmIrsubableSlab.getDstockCode());
        });

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

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

    @Override
    public List<PmIrsubableSlabDTO> exportList(Long organizationId, PmIrsubableSlabRequestDTO pmIrsubableSlab) {
        pmIrsubableSlab.setTenantId(organizationId);
        return pmIrsubableSlabMapper.exportList(pmIrsubableSlab);
    }

    @Override
    public void xx() {
        mmEventClient.getPassQualifiedPro(1L, Collections.singletonList(14572L));
    }
}

