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

import com.xisc.pm.api.dto.PmIrsubableContRequestDTO;
import com.xisc.pm.api.dto.export.PmIrsubableContDTO;
import com.xisc.pm.domain.entity.PmIngotYieldBt;
import com.xisc.pm.domain.entity.PmProContProdTtr;
import com.xisc.pm.infra.mapper.PmIrsubableContMapper;
import com.xisc.pm.infra.mapper.PmProContProdTtrMapper;
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.PmIrsubableContService;
import org.springframework.stereotype.Service;
import com.xisc.pm.domain.entity.PmIrsubableCont;
import com.xisc.pm.domain.repository.PmIrsubableContRepository;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 不可替代订单表(PmIrsubableCont)应用服务
 *
 * @author BKGY
 * @since 2024-12-16 15:44:40
 */
@Service
public class PmIrsubableContServiceImpl implements PmIrsubableContService {
    @Autowired
    private PmIrsubableContRepository pmIrsubableContRepository;

    @Autowired
    private PmProContProdTtrMapper pmProContProdTtrMapper;

    @Autowired
    private PmIrsubableContMapper pmIrsubableContMapper;

    @Override
    public Page<PmIrsubableCont> selectList(PageRequest pageRequest, PmIrsubableContRequestDTO pmIrsubableCont) {
        return PageHelper.doPageAndSort(pageRequest, () -> pmIrsubableContRepository.selectList(pmIrsubableCont));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveData(Long tenantId , List<PmIrsubableCont> pmIrsubableConts) {
        if(CollectionUtils.isEmpty(pmIrsubableConts)){
            return;
        }
        // 校验唯一性
        List<String> orderNoList = pmIrsubableConts.stream().map(PmIrsubableCont::getOrderNo).distinct().collect(Collectors.toList());
        if (orderNoList.size() < pmIrsubableConts.size()) {
            throw new CommonException("pm.irsubable_cont_001");
        }
        List<PmIrsubableCont> existIrsubableContList = pmIrsubableContRepository.selectByCondition(Condition.builder(PmIrsubableCont.class)
                .andWhere(Sqls.custom().andIn(PmIrsubableCont.FIELD_ORDER_NO, orderNoList))
                .andWhere(Sqls.custom().andEqualTo(PmIrsubableCont.FIELD_TENANT_ID, tenantId)).build());
        if(CollectionUtils.isNotEmpty(existIrsubableContList)){
            throw new CommonException("pm.irsubable_cont_002",existIrsubableContList.get(0).getOrderNo());
        }

        List<PmProContProdTtr> pmProContProdTtrList = pmProContProdTtrMapper.queryList(orderNoList,tenantId);
        if(CollectionUtils.isEmpty(pmProContProdTtrList)){
            throw new CommonException("pm.irsubable_cont_003");
        }
        pmIrsubableConts.forEach(line -> {
            PmProContProdTtr pmProContProdTtr = pmProContProdTtrList.stream().filter(ttr -> ttr.getOrderNo().equals(line.getOrderNo())).findFirst().orElse(null);
            if (pmProContProdTtr == null) {
                throw new CommonException("pm.irsubable_cont_004");
            }
            line.setTenantId(tenantId);
            line.setDelivyDate(pmProContProdTtr.getDelivyDate());
            line.setOrderThick(pmProContProdTtr.getOrderThick());
            line.setOrderWidth(pmProContProdTtr.getOrderWidth());
            line.setOrderLen(pmProContProdTtr.getOrderLen());
            line.setSgSign(pmProContProdTtr.getSgSign());
            line.setSgStd(pmProContProdTtr.getSgStd());
        });

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

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

    @Override
    public List<PmIrsubableContDTO> exportList(Long organizationId, PmIrsubableContRequestDTO pmIrsubableCont) {
        pmIrsubableCont.setTenantId(organizationId);
        return pmIrsubableContMapper.exportList(pmIrsubableCont);
    }
}

