package com.fjhx.sale.service.purchase.impl;


import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.fjhx.purchase.entity.purchase.dto.PurchaseDto;
import com.fjhx.purchase.entity.purchase.enums.PurchaseStatusEnum;
import com.fjhx.purchase.entity.purchase.po.Purchase;
import com.fjhx.purchase.entity.purchase.po.PurchaseDetail;
import com.fjhx.purchase.mapper.purchase.PurchaseMapper;
import com.fjhx.purchase.service.purchase.PurchaseDetailService;
import com.fjhx.purchase.service.purchase.PurchaseService;
import com.fjhx.sale.entity.contract.po.ContractProduct;
import com.fjhx.sale.entity.sample.po.SampleProduct;
import com.fjhx.sale.service.contract.ContractProductService;
import com.fjhx.sale.service.purchase.SalePurchaseService;
import com.fjhx.sale.service.sample.SampleProductService;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;


/**
 * <p>
 * 采购 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-06
 */
@Service
public class SalePurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase>
        implements SalePurchaseService {

    @Autowired
    private PurchaseService purchaseService;

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Autowired
    private ContractProductService contractProductService;

    @Autowired
    private SampleProductService sampleProductService;

    /**
     * 采购作废
     * @param purchaseDto
     */
    @Override
    @DSTransactional
    public void cancellation(PurchaseDto purchaseDto) {
        if(purchaseDto.getPurchaseStatus()==PurchaseStatusEnum.CENCELLATION.getKey()){//如果采购合同=作废//归还交接单的数量
            //查询出当前采购的明细
            Purchase purchase = purchaseService.getById(purchaseDto.getId());
            if(purchase.getPurchaseStatus()==PurchaseStatusEnum.CENCELLATION.getKey()){
                throw new ServiceException("采购合同已作废");
            }
            List<ContractProduct> upContractProductList = new ArrayList<>();
            List<SampleProduct> upSampleProductList = new ArrayList<>();
            if(ObjectUtils.isNotEmpty(purchase)){
                List<PurchaseDetail> purchaseDetailList = purchaseDetailService.list(Wrappers.<PurchaseDetail>query().lambda().eq(PurchaseDetail::getPurchaseId,purchase.getId()));
                for(PurchaseDetail p:purchaseDetailList){
                    if(p.getDataResource()==1){//业务来源 0自建  1外销合同采购 2:样品单
                        //查询出外销合同产品
                        ContractProduct contractProduct = contractProductService.getById(p.getDataResourceId());
                        if(ObjectUtils.isEmpty(contractProduct)){
                            throw new ServiceException("作废合同失败,当前采购产品数据有误");
                        }
                        BigDecimal sumExpendQuantity = contractProduct.getExpendQuantity().add(p.getCount());
                        contractProduct.setExpendQuantity(sumExpendQuantity);
                        upContractProductList.add(contractProduct);
                    }
                    if(p.getDataResource()==2){
                        //查询出样品单产品
                        SampleProduct sampleProduct = sampleProductService.getById(p.getDataResourceId());
                        if(ObjectUtils.isEmpty(sampleProduct)){
                            throw new ServiceException("作废合同失败,当前采购产品数据有误");
                        }
                        BigDecimal sumExpendQuantity = sampleProduct.getExpendQuantity().add(p.getCount());
                        sampleProduct.setExpendQuantity(sumExpendQuantity);
                        upSampleProductList.add(sampleProduct);
                    }
                }
            }
            if(CollectionUtils.isNotEmpty(upContractProductList)){
                contractProductService.updateBatchById(upContractProductList);
            }
            if(CollectionUtils.isNotEmpty(upSampleProductList)){
                sampleProductService.updateBatchById(upSampleProductList);
            }
        }
        purchaseService.updateById(purchaseDto);
    }
}
