package com.bonus.canteen.core.supply.purchase.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.supply.purchase.api.DrpAuthorityPurchaseApi;
import com.bonus.canteen.core.supply.purchase.constants.*;
import com.bonus.canteen.core.supply.purchase.domain.bo.*;
import com.bonus.canteen.core.supply.purchase.domain.param.*;
import com.bonus.canteen.core.supply.purchase.domain.vo.*;
import com.bonus.canteen.core.supply.purchase.mapper.*;
import com.bonus.canteen.core.supply.purchase.service.DrpInquiryService;
import com.bonus.canteen.core.supply.purchase.service.DrpMaterialLatestPriceService;
import com.bonus.canteen.core.supply.purchase.service.DrpPurchasePurNoticeService;
import com.bonus.canteen.core.supply.purchase.utils.CustomIdGenerateUtil;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.constant.DelFlagEnum;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.common.houqin.utils.AesEncryptUtil;
import com.bonus.common.houqin.utils.LeBeanUtil;
import com.bonus.common.houqin.utils.id.Id;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DrpInquiryServiceImpl extends ServiceImpl<DrpPurchaseInquiryMapper, DrpInquiry> implements DrpInquiryService {
   @Autowired
   @Lazy
   private DrpPurchaseInquiryDetailMapper drpPurchaseInquiryDetailMapper;
   @Autowired
   @Lazy
   private DrpPurchaseQuoteMapper drpPurchaseQuoteMapper;
   @Autowired
   @Lazy
   private DrpPurchaseInquirySupplierMapper drpPurchaseInquirySupplierMapper;
   @Autowired
   @Lazy
   private DrpPurchasePurNoticeService drpPurchasePurNoticeService;
   @Autowired
   @Lazy
   private DrpPurchaseOrderGoodsMapper drpPurchaseOrderGoodsMapper;
   @Autowired
   @Lazy
   private DrpOrderGoodsDetailMapper drpOrderGoodsDetailMapper;
   @Resource(
      name = "smartCanteenTaskExecutor"
   )
   @Lazy
   private AsyncTaskExecutor asyncTaskExecutor;
   @Autowired
   @Lazy
   private DrpPurchaseQuoteInquiryPicMapper drpPurchaseQuoteInquiryPicMapper;
   @Autowired
   @Lazy
   private DrpMaterialLatestPriceService drpMaterialLatestPriceService;
   @Autowired
   @Lazy
   DrpAuthorityPurchaseApi drpAuthorityPurchaseApi;


   public void cancelInquiry(DrpInquiryAddVO content) {
      ((DrpPurchaseInquiryMapper)this.baseMapper).cancelInquiry(content.getInquiryId(), DrpInquiryStatusEnum.LOSE_THE_BID.key());
   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public DrpInquiryAddVO saveInquiryAndDetails(DrpInquiryAddDTO content) {
      content.setPhone(AesEncryptUtil.aesDecode(content.getPhone()));
      DrpInquiry drpInquiry = new DrpInquiry();
      BeanUtil.copyProperties(content, drpInquiry);
      drpInquiry.setStatus(DrpInquiryStatusEnum.NO_COMMIT.key());
      if (CharSequenceUtil.isNotEmpty(drpInquiry.getPhone())) {
         drpInquiry.setPhone(AesEncryptUtil.aesDecode(drpInquiry.getPhone()));
      }

      String inquiryId = content.getInquiryId();
      if (CharSequenceUtil.isNotBlank(inquiryId)) {
         drpInquiry.setInquiryId(inquiryId);
         this.baseMapper.update(drpInquiry, Wrappers.lambdaUpdate(DrpInquiry.class).eq(DrpInquiry::getInquiryId, inquiryId));
      } else {
         inquiryId = CustomIdGenerateUtil.generateInquiryId();
         drpInquiry.setInquiryId(inquiryId);
         this.baseMapper.insert(drpInquiry);
      }

      this.drpPurchaseInquiryDetailMapper.delete(Wrappers.lambdaQuery(DrpInquiryDetail.class).eq(DrpInquiryDetail::getInquiryId, inquiryId));
      this.drpPurchaseInquirySupplierMapper.delete(Wrappers.lambdaQuery(DrpInquirySupplier.class).eq(DrpInquirySupplier::getInquiryId, inquiryId));
      List<DrpInquiryDetailAddDTO> drpInquiryDetails = content.getDetailList();
      Iterator var5 = drpInquiryDetails.iterator();

      while(var5.hasNext()) {
         DrpInquiryDetailAddDTO drpInquiryDetailAddDTO = (DrpInquiryDetailAddDTO)var5.next();
         DrpInquiryDetail drpInquiryDetail = new DrpInquiryDetail();
         BeanUtil.copyProperties(drpInquiryDetailAddDTO, drpInquiryDetail, new String[0]);
         drpInquiryDetail.setInquiryDetailId(Id.next());
         drpInquiryDetail.setInquiryId(inquiryId);
         this.drpPurchaseInquiryDetailMapper.insert(drpInquiryDetail);
      }

      List<Long> supplierIds = content.getSupplierIds();
      Iterator var10 = supplierIds.iterator();

      while(var10.hasNext()) {
         Long supplierId = (Long)var10.next();
         DrpInquirySupplier drpInquirySupplier = new DrpInquirySupplier();
         drpInquirySupplier.setInquirySupplierId(Id.next());
         drpInquirySupplier.setInquiryId(inquiryId);
         drpInquirySupplier.setSupplierId(supplierId);
         drpInquirySupplier.setBidStatus(DrpQuoteStatusEnum.SAVE_NOT_COMMIT.key());
         this.drpPurchaseInquirySupplierMapper.insert(drpInquirySupplier);
      }

      DrpInquiryAddVO drpInquiryAddVO = new DrpInquiryAddVO();
      drpInquiryAddVO.setInquiryId(inquiryId);
      return drpInquiryAddVO;
   }

   public List<DrpInquiryPageVO> queryInquiry(DrpInquiryPageDTO content) {
      List<DrpInquiryPageVO> records = this.baseMapper.selectAllList(content);
      if (CollUtil.isEmpty(records)) {
         return records;
      } else {
         List<Long> bidIdList = records.stream().map(DrpInquiryPageVO::getBidSupplierId).filter(Objects::nonNull).collect(Collectors.toList());
         Map<String, List<DrpInquiryDetailPageVO>> collect = Maps.newHashMap();
         List<DrpInquiryDetailPageVO> supplierIds;
         if (CollUtil.isNotEmpty(bidIdList)) {
            supplierIds = this.drpPurchaseInquiryDetailMapper.selectByInquiryIdPage(bidIdList);
            collect = supplierIds.stream().collect(Collectors.groupingBy(DrpInquiryDetailPageVO::getInquiryId));
         }

         List<DrpInquirySupplier> supplierIdsNew = this.drpPurchaseInquirySupplierMapper.selectList(Wrappers.lambdaQuery(DrpInquirySupplier.class).in(DrpInquirySupplier::getInquiryId, records.stream().map(DrpInquiryPageVO::getInquiryId).collect(Collectors.toList())));
         Map<String, List<DrpInquirySupplier>> collect1 = supplierIdsNew.stream().collect(Collectors.groupingBy(DrpInquirySupplier::getInquiryId));
         List<DrpInquirySupplier> collect2 = supplierIdsNew.stream().filter((d) -> {
            return d.getQuoteTime() != null;
         }).collect(Collectors.toList());
         Map<String, List<DrpInquirySupplier>> collect3 = collect2.stream().collect(Collectors.groupingBy(DrpInquirySupplier::getInquiryId));
         Iterator var11 = records.iterator();

         while(var11.hasNext()) {
            DrpInquiryPageVO drpInquiryPageVO = (DrpInquiryPageVO)var11.next();
            LocalDateTime now = LocalDateTime.now();
            if (!Objects.equals(drpInquiryPageVO.getStatus(), LeConstants.COMMON_YES)) {
               if (drpInquiryPageVO.getStartTime().isBefore(now)) {
                  drpInquiryPageVO.setInquiryState(2);
               } else if (drpInquiryPageVO.getStartTime().isAfter(now)) {
                  drpInquiryPageVO.setInquiryState(1);
               }

               if (drpInquiryPageVO.getEndTime().isBefore(now)) {
                  drpInquiryPageVO.setInquiryState(3);
               }

               if (drpInquiryPageVO.getStatus().equals(DrpInquiryStatusEnum.LOSE_THE_BID.key())) {
                  drpInquiryPageVO.setInquiryState(4);
               }
            }

            String inquiryId = drpInquiryPageVO.getInquiryId();
            drpInquiryPageVO.setSupplierIds(CollUtil.isNotEmpty(collect1.get(drpInquiryPageVO.getInquiryId())) ? (collect1.get(drpInquiryPageVO.getInquiryId())).stream().map(DrpInquirySupplier::getSupplierId).collect(Collectors.toList()) : null);
            List drpQuoteInquiryPics;
            if (CollUtil.isNotEmpty(collect.get(drpInquiryPageVO.getInquiryId()))) {
               drpInquiryPageVO.setDetailList((collect.get(drpInquiryPageVO.getInquiryId())));
            } else {
               drpQuoteInquiryPics = this.drpPurchaseInquiryDetailMapper.selectDetailListByInquiryId(drpInquiryPageVO.getInquiryId());
               drpInquiryPageVO.setDetailList(drpQuoteInquiryPics);
            }

            drpInquiryPageVO.setPhone(AesEncryptUtil.aesEncrypt(drpInquiryPageVO.getPhone()));
            if (CollUtil.isNotEmpty(collect3.get(drpInquiryPageVO.getInquiryId()))) {
               drpQuoteInquiryPics = (collect3.get(drpInquiryPageVO.getInquiryId())).stream().map(DrpInquirySupplier::getSupplierId).collect(Collectors.toList());
               drpInquiryPageVO.setQuoteSupplierIdList(drpQuoteInquiryPics);
               MinQuoteSupplierModel minQuoteSupplier = this.drpPurchaseInquirySupplierMapper.getMinQuoteSupplier(inquiryId);
               drpInquiryPageVO.setMinQuoteSupplierId(minQuoteSupplier.getSupplierId());
               drpInquiryPageVO.setMinQuoteSupplierName(minQuoteSupplier.getSupplierName());
            }

            drpQuoteInquiryPics = this.drpPurchaseQuoteInquiryPicMapper.selectByInquiryIdAndSupplierId(drpInquiryPageVO.getInquiryId(), (Long)null);
            drpInquiryPageVO.setDrpQuoteInquiryPics(drpQuoteInquiryPics);
         }

         return records;
      }
   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void deleteNotCommitInquiry(DrpInquiryAddVO content) {
      String inquiryId = content.getInquiryId();
      HashMap<String, Object> deleteMap = new HashMap();
      deleteMap.put("inquiry_id", inquiryId);
      ((DrpPurchaseInquiryMapper)this.baseMapper).deleteByInquiryId(inquiryId, DelFlagEnum.DEL_TRUE.key());
   }

   public List<DrpSupplierQuoteVO> listSupplierQuote(String inquiryId) {
      List<DrpSupplierQuoteVO> list = this.drpPurchaseInquirySupplierMapper.listSupplierQuote(inquiryId);
      if (CollUtil.isNotEmpty(list)) {
         list.forEach((d) -> {
            List<DrpQuoteInquiryPicVO> drpQuoteInquiryPics = this.drpPurchaseQuoteInquiryPicMapper.selectByInquiryIdAndSupplierId(inquiryId, d.getSupplierId());
            d.setDrpQuoteInquiryPics(drpQuoteInquiryPics);
         });
      }

      return list;
   }

   public List<DrpFinalBidsQuotePageVO> finalBidsQuote(DrpFinalBidsDTO content) {
      if (CharSequenceUtil.isNotEmpty(content.getMaterialName())) {
         content.setMaterialName(LeBeanUtil.fieldLikeHandle(content.getMaterialName()));
      }

      List<DrpFinalBidsQuotePageVO> resultList = this.drpPurchaseInquiryDetailMapper.selectByDrpFinalBidsDTO(content);
      Iterator var3 = resultList.iterator();

      while(var3.hasNext()) {
         DrpFinalBidsQuotePageVO drpFinalBidsQuotePageVO = (DrpFinalBidsQuotePageVO)var3.next();
         List<DrpFinalBidsQuotePageModel> detailList = this.drpPurchaseQuoteMapper.listSupplierQuote(drpFinalBidsQuotePageVO.getInquiryDetailId());
         drpFinalBidsQuotePageVO.setDetailList(detailList);
         Integer minSinglePrice = detailList.stream().mapToInt(DrpFinalBidsQuotePageModel::getSinglePrice).min().orElse(0);
         drpFinalBidsQuotePageVO.setLowestPrice(minSinglePrice);
         Iterator var7 = detailList.iterator();

         while(var7.hasNext()) {
            DrpFinalBidsQuotePageModel drpFinalBidsQuotePageModel = (DrpFinalBidsQuotePageModel)var7.next();
            if (drpFinalBidsQuotePageVO.getGuidePrice() != null && drpFinalBidsQuotePageVO.getGuidePrice() != 0) {
               drpFinalBidsQuotePageModel.setDiffPrice(drpFinalBidsQuotePageModel.getSinglePrice() - drpFinalBidsQuotePageVO.getGuidePrice());
               drpFinalBidsQuotePageModel.setDiffRate((new BigDecimal(drpFinalBidsQuotePageModel.getDiffPrice())).multiply(new BigDecimal(100)).divide(new BigDecimal(drpFinalBidsQuotePageVO.getGuidePrice()), 2, RoundingMode.HALF_UP));
               drpFinalBidsQuotePageModel.setDiffCost((new BigDecimal(drpFinalBidsQuotePageModel.getDiffPrice())).multiply(drpFinalBidsQuotePageVO.getPurNum()));
            }
         }
      }

      return resultList;
   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void commitFinalBids(DrpCommitFinalBidsDTO content) {
      DrpInquiry drpInquiry = this.baseMapper.selectOne((Wrappers.lambdaQuery(DrpInquiry.class).eq(DrpInquiry::getInquiryId, content.getInquiryId())).select(DrpInquiry::getEndTime));
      LocalDateTime bidTime = LocalDateTime.now();
      if (bidTime.isBefore(drpInquiry.getEndTime())) {
         throw new ServiceException("报价未结束，无法定标");
      } else if (0 == content.getBidTotalPrice()) {
         throw new ServiceException("定标报价不能为空");
      } else {
         this.updateInquiryStatus(content, bidTime);
         this.inquiryGenerateOrderGoods(content);
         this.asyncTaskExecutor.execute(() -> {
            this.drpPurchasePurNoticeService.sendSmsNoticeGeneral(content.getSupplierId(), (String)null, content.getInquiryId(), NoticeTemplateKeyEnum.DRP_SUPPLIER_JB.getKey());
         });
      }
   }

   protected void inquiryGenerateOrderGoods(DrpCommitFinalBidsDTO content) {
      DrpInquiry drpInquiry = this.baseMapper.selectOne(Wrappers.lambdaQuery(DrpInquiry.class).eq(DrpInquiry::getInquiryId, content.getInquiryId()));
      DrpOrderGoods drpOrderGoods = new DrpOrderGoods();
      String orderGoodsId = CustomIdGenerateUtil.generateOrderGoodsId();
      drpOrderGoods.setOrderGoodsId(orderGoodsId);
      drpOrderGoods.setSupplierId(content.getSupplierId());
      drpOrderGoods.setOrderStatus(DrpOrderGoodsStatusEnum.WAIT_COMMIT.getKey());
      drpOrderGoods.setOrderAmount((long)content.getBidTotalPrice());
      drpOrderGoods.setRequestArrivalTime(drpInquiry.getRequestArrivalTime());
      drpOrderGoods.setOrderTitle(drpInquiry.getTitle());
      drpOrderGoods.setSupplyAddress(drpInquiry.getAddress());
      drpOrderGoods.setAreaId(drpInquiry.getAreaId());
      this.drpPurchaseOrderGoodsMapper.insert(drpOrderGoods);
      List<DrpInquiryDetailPageVO> drpInquiryDetailPageVOList = this.drpPurchaseInquiryDetailMapper.selectByInquiryId(content.getInquiryId(), content.getSupplierId());
      Iterator var6 = drpInquiryDetailPageVOList.iterator();

      while(var6.hasNext()) {
         DrpInquiryDetailPageVO drpInquiryDetailPageVO = (DrpInquiryDetailPageVO)var6.next();
         DrpOrderGoodsDetail drpOrderGoodsDetail = new DrpOrderGoodsDetail();
         BeanUtil.copyProperties(drpInquiryDetailPageVO, drpOrderGoodsDetail, new String[0]);
         drpOrderGoodsDetail.setOrderGoodsDetailId(Id.next());
         drpOrderGoodsDetail.setOrderGoodsId(orderGoodsId);
         drpOrderGoodsDetail.setOrderNum(drpInquiryDetailPageVO.getQuoteNum());
         this.drpOrderGoodsDetailMapper.insert(drpOrderGoodsDetail);
         this.drpMaterialLatestPriceService.addMaterialLatestPrice(DrpMaterialLatestPriceDTO.builder().materialId(drpInquiryDetailPageVO.getMaterialId()).unitId(drpInquiryDetailPageVO.getUnitId()).priceType(DrpMaterialPriceTypeEnum.MATERIAL_INQUIRY.key()).latestPrice(drpInquiryDetailPageVO.getSinglePrice()).supplierId(content.getSupplierId()).relatedId(content.getInquiryId()).remark(DrpMaterialPriceTypeEnum.MATERIAL_INQUIRY.value()).build());
      }

   }

   protected void updateInquiryStatus(DrpCommitFinalBidsDTO content, LocalDateTime bidTime) {
      ((DrpPurchaseInquiryMapper)this.baseMapper).update(null, ((((Wrappers.lambdaUpdate(DrpInquiry.class).eq(DrpInquiry::getInquiryId, content.getInquiryId())).set(DrpInquiry::getStatus, DrpInquiryStatusEnum.ALREADY_QUOTE.key())).set(DrpInquiry::getBidTotalPrice, content.getBidTotalPrice())).set(DrpInquiry::getSupplierId, content.getSupplierId())).set(DrpInquiry::getBidTime, bidTime));
      this.drpPurchaseInquirySupplierMapper.update(null, (Wrappers.lambdaUpdate(DrpInquirySupplier.class).eq(DrpInquirySupplier::getInquiryId, content.getInquiryId())).set(DrpInquirySupplier::getBidStatus, DrpQuoteStatusEnum.LOSE_THE_BID.key()));
      this.drpPurchaseInquirySupplierMapper.update(null, ((Wrappers.lambdaUpdate(DrpInquirySupplier.class).eq(DrpInquirySupplier::getInquiryId, content.getInquiryId())).eq(DrpInquirySupplier::getSupplierId, content.getSupplierId())).set(DrpInquirySupplier::getBidStatus, DrpQuoteStatusEnum.WON_THE_BID.key()));
   }

   public QuerySupplierQuoteVO querySupplierQuoteDetail(Long inquirySupplierId) {
      QuerySupplierQuoteVO querySupplierQuoteVO = this.drpPurchaseInquirySupplierMapper.querySupplierQuote(inquirySupplierId);
      List<DrpQuerySupplierQuoteDetailVO> detailList = this.drpPurchaseQuoteMapper.querySupplierQuoteDetail(inquirySupplierId);
      querySupplierQuoteVO.setDetailList(detailList);
      return querySupplierQuoteVO;
   }

   public void sendNotice(String content) {
      List<DrpSupplierQuoteVO> list = this.drpPurchaseInquirySupplierMapper.listSupplierQuoteAll(content);
      List<Long> collect = list.stream().map(DrpSupplierQuoteVO::getSupplierId).collect(Collectors.toList());
      this.asyncTaskExecutor.execute(() -> {
         collect.forEach((d) -> {
            this.drpPurchasePurNoticeService.sendSmsNoticeGeneral(d, (String)null, content, NoticeTemplateKeyEnum.DRP_GOODS_QUOTE.getKey());
         });
      });
   }

   public void upload(DrpQuoteInquiryPicDTO content) {
      Long supplierAuthority = this.drpAuthorityPurchaseApi.listSupplierAuthority();
      List<DrpQuoteInquiryPic> drpQuoteInquiryPics = this.drpPurchaseQuoteInquiryPicMapper.selectList((Wrappers.lambdaQuery(DrpQuoteInquiryPic.class).eq(DrpQuoteInquiryPic::getInquiryId, content.getInquiryId())).eq(DrpQuoteInquiryPic::getSupplierId, supplierAuthority));
      if (CollUtil.isNotEmpty(drpQuoteInquiryPics)) {
         drpQuoteInquiryPics.forEach((d) -> {
            this.drpPurchaseQuoteInquiryPicMapper.deleteById(d.getId());
         });
      }

      if (CollUtil.isNotEmpty(content.getImgUrls())) {
         content.getImgUrls().forEach((d) -> {
            DrpQuoteInquiryPic drpQuoteInquiryPic = new DrpQuoteInquiryPic();
            drpQuoteInquiryPic.setInquiryId(content.getInquiryId());
            drpQuoteInquiryPic.setImgUrl(d);
            drpQuoteInquiryPic.setSupplierId(supplierAuthority);
            this.drpPurchaseQuoteInquiryPicMapper.insert(drpQuoteInquiryPic);
         });
      }

   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public DrpInquiryAddVO commitInquiryAndDetails(DrpInquiryAddDTO content) {
      if (content.getEndTime().isBefore(LocalDateTime.now())) {
         throw new ServiceException("报价结束时间不能小于当前时间");
      } else if (content.getEndTime().isBefore(content.getStartTime())) {
         throw new ServiceException("报价结束时间不能小于开始时间");
      } else {
         content.setPhone(AesEncryptUtil.aesDecode(content.getPhone()));
         String inquiryId = content.getInquiryId();
         DrpInquiry drpInquiry = new DrpInquiry();
         BeanUtil.copyProperties(content, drpInquiry, new String[0]);
         if (CharSequenceUtil.isNotEmpty(drpInquiry.getPhone())) {
            drpInquiry.setPhone(AesEncryptUtil.aesDecode(drpInquiry.getPhone()));
         }

         HashMap<String, Object> deleteMap = new HashMap();
         deleteMap.put("inquiry_id", inquiryId);
         this.drpPurchaseInquiryDetailMapper.deleteByMap(deleteMap);
         this.drpPurchaseInquirySupplierMapper.deleteByMap(deleteMap);
         drpInquiry.setStatus(DrpInquiryStatusEnum.DOING_QUOTE.key());
         if (CharSequenceUtil.isNotBlank(inquiryId)) {
            drpInquiry.setInquiryId(inquiryId);
            ((DrpPurchaseInquiryMapper)this.baseMapper).update(drpInquiry, Wrappers.lambdaUpdate(DrpInquiry.class).eq(DrpInquiry::getInquiryId, inquiryId));
         } else {
            inquiryId = CustomIdGenerateUtil.generateInquiryId();
            drpInquiry.setInquiryId(inquiryId);
            ((DrpPurchaseInquiryMapper)this.baseMapper).insert(drpInquiry);
         }

         List<DrpInquiryDetailAddDTO> detailList = content.getDetailList();
         List<DrpInquiryDetail> inquiryDetails = new ArrayList(64);
         Iterator var7 = detailList.iterator();

         while(var7.hasNext()) {
            DrpInquiryDetailAddDTO detail = (DrpInquiryDetailAddDTO)var7.next();
            DrpInquiryDetail drpInquiryDetail = new DrpInquiryDetail();
            BeanUtil.copyProperties(detail, drpInquiryDetail, new String[0]);
            drpInquiryDetail.setInquiryDetailId(Id.next());
            drpInquiryDetail.setInquiryId(inquiryId);
            this.drpPurchaseInquiryDetailMapper.insert(drpInquiryDetail);
            inquiryDetails.add(drpInquiryDetail);
         }

         List<Long> supplierIds = content.getSupplierIds();
         Iterator var16 = supplierIds.iterator();

         while(var16.hasNext()) {
            Long supplierId = (Long)var16.next();
            DrpInquirySupplier drpInquirySupplier = new DrpInquirySupplier();
            drpInquirySupplier.setInquirySupplierId(Id.next());
            drpInquirySupplier.setInquiryId(inquiryId);
            drpInquirySupplier.setSupplierId(supplierId);
            this.drpPurchaseInquirySupplierMapper.insert(drpInquirySupplier);
            Long inquirySupplierId = drpInquirySupplier.getInquirySupplierId();
            Iterator var12 = inquiryDetails.iterator();

            while(var12.hasNext()) {
               DrpInquiryDetail detail = (DrpInquiryDetail)var12.next();
               DrpQuote quote = new DrpQuote();
               BeanUtil.copyProperties(detail, quote, new String[0]);
               quote.setId((Long)null);
               quote.setQuoteId(Id.next());
               quote.setInquirySupplierId(inquirySupplierId);
               quote.setQuoteNum(detail.getPurNum());
               this.drpPurchaseQuoteMapper.insert(quote);
            }

            DrpNoticeParam drpNoticeParam = new DrpNoticeParam();
            drpNoticeParam.setMainId(inquiryId);
            drpNoticeParam.setSupplierId(supplierId);
            drpNoticeParam.setContentId(inquiryId);
            drpNoticeParam.setNoticeProcess(DrpNoticeProcessEnum.PUR_TO_SUPPLIER_CHANGE.key());
            drpNoticeParam.setDrpNotice(DrpNoticeEnum.INQUIRY_QUOTE_NOTICE.key());
            drpNoticeParam.setInquirySendNoticeType(DrpNoticeSendFinalBidsQuoteEnum.SEND_QUOTE.key());
            drpNoticeParam.setInquirySendNoticeType(DrpNoticeSendFinalBidsQuoteEnum.SEND_QUOTE.key());
            drpNoticeParam.setCode(inquiryId);
            drpNoticeParam.setDrpInquirySendNoticeType(NoticeTemplateKeyEnum.DRP_GOODS_QUOTE.getKey());
         }

         String finalInquiryId = inquiryId;
         this.asyncTaskExecutor.execute(() -> {
            supplierIds.forEach((d) -> {
               this.drpPurchasePurNoticeService.sendSmsNoticeGeneral(d, null, finalInquiryId, NoticeTemplateKeyEnum.DRP_GOODS_QUOTE.getKey());
            });
         });
         DrpInquiryAddVO drpInquiryAddVO = new DrpInquiryAddVO();
         drpInquiryAddVO.setInquiryId(inquiryId);
         return drpInquiryAddVO;
      }
   }
}
