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

import cn.hutool.core.bean.BeanUtil;
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.DrpInspectGoodsStatusEnum;
import com.bonus.canteen.core.supply.purchase.domain.bo.DrpInspectGoods;
import com.bonus.canteen.core.supply.purchase.domain.bo.DrpInspectGoodsDetail;
import com.bonus.canteen.core.supply.purchase.domain.bo.DrpQualityInspect;
import com.bonus.canteen.core.supply.purchase.domain.param.DrpInspectGoodsCommitAddDTO;
import com.bonus.canteen.core.supply.purchase.domain.param.DrpInspectGoodsDetailCommitAddDTO;
import com.bonus.canteen.core.supply.purchase.domain.param.DrpInspectGoodsDetailPageDTO;
import com.bonus.canteen.core.supply.purchase.domain.param.DrpInspectGoodsPageDTO;
import com.bonus.canteen.core.supply.purchase.domain.vo.DrpInspectGoodsAddVO;
import com.bonus.canteen.core.supply.purchase.domain.vo.DrpInspectGoodsDetailPageVO;
import com.bonus.canteen.core.supply.purchase.domain.vo.DrpInspectGoodsPageVO;
import com.bonus.canteen.core.supply.purchase.mapper.*;
import com.bonus.canteen.core.supply.purchase.service.DrpInspectGoodsService;
import com.bonus.canteen.core.supply.purchase.utils.CustomIdGenerateUtil;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.common.houqin.utils.id.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;

@Service
public class DrpInspectGoodsServiceImpl extends ServiceImpl<DrpInspectGoodsMapper, DrpInspectGoods> implements DrpInspectGoodsService {
   @Autowired
   @Lazy
   private DrpInspectGoodsDetailMapper drpInspectGoodsDetailMapper;
   @Autowired
   @Lazy
   private DrpDeliveryGoodsDetailMapper drpDeliveryGoodsDetailMapper;
   @Autowired
   @Lazy
   private DrpDeliveryGoodsMapper drpDeliveryGoodsMapper;
   @Autowired
   @Lazy
   private DrpQualityInspectMapper drpQualityInspectMapper;
   @Autowired
   @Lazy
   private DrpAuthorityPurchaseApi drpAuthorityPurchaseApi;

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public DrpInspectGoodsAddVO addCommitInspectGoods(DrpInspectGoodsCommitAddDTO content) {
      DrpInspectGoods drpInspectGoods = new DrpInspectGoods();
      BeanUtil.copyProperties(content, drpInspectGoods);
      drpInspectGoods.setStatus(DrpInspectGoodsStatusEnum.COMMIT.key());
      String inspectGoodsId = content.getInspectGoodsId();
      if (CharSequenceUtil.isBlank(inspectGoodsId)) {
         inspectGoodsId = CustomIdGenerateUtil.generateInspectGoodsId();
         drpInspectGoods.setInspectGoodsId(inspectGoodsId);
         this.baseMapper.insert(drpInspectGoods);
      } else {
         this.baseMapper.update(drpInspectGoods, Wrappers.lambdaQuery(DrpInspectGoods.class).eq(DrpInspectGoods::getInspectGoodsId, inspectGoodsId));
         this.drpInspectGoodsDetailMapper.delete(Wrappers.lambdaQuery(DrpInspectGoodsDetail.class).eq(DrpInspectGoodsDetail::getInspectGoodsId, inspectGoodsId));
      }

      if (CharSequenceUtil.isNotEmpty(content.getRelateDeliveryGoodsId())) {
         this.drpDeliveryGoodsMapper.plusInspectTotalNum(content.getRelateDeliveryGoodsId(), content.getTotalQualifiedNum());
      }

      List<DrpInspectGoodsDetailCommitAddDTO> detailList = content.getDetailList();
      Iterator var5 = detailList.iterator();

      while(var5.hasNext()) {
         DrpInspectGoodsDetailCommitAddDTO detail = (DrpInspectGoodsDetailCommitAddDTO)var5.next();
         DrpInspectGoodsDetail drpInspectGoodsDetail = new DrpInspectGoodsDetail();
         BeanUtil.copyProperties(detail, drpInspectGoodsDetail, new String[0]);
         drpInspectGoodsDetail.setInspectGoodsId(inspectGoodsId);
         drpInspectGoodsDetail.setInspectGoodsDetailId(Id.next());
         this.drpInspectGoodsDetailMapper.insert(drpInspectGoodsDetail);
         this.drpDeliveryGoodsDetailMapper.plusDeliveryDetailQualifiedNum(detail.getDeliveryGoodsDetailId(), detail.getQualifiedNum());
      }

      return (new DrpInspectGoodsAddVO()).setInspectGoodsId(inspectGoodsId);
   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void addSaveInspectGoods(DrpInspectGoodsCommitAddDTO content) {
      DrpInspectGoods drpInspectGoods = new DrpInspectGoods();
      BeanUtil.copyProperties(content, drpInspectGoods, new String[0]);
      String inspectGoodsId = content.getInspectGoodsId();
      if (CharSequenceUtil.isBlank(inspectGoodsId)) {
         inspectGoodsId = CustomIdGenerateUtil.generateInspectGoodsId();
         drpInspectGoods.setInspectGoodsId(inspectGoodsId);
         this.baseMapper.insert(drpInspectGoods);
      } else {
         this.baseMapper.update(drpInspectGoods, Wrappers.lambdaQuery(DrpInspectGoods.class).eq(DrpInspectGoods::getInspectGoodsId, inspectGoodsId));
         this.drpInspectGoodsDetailMapper.delete(Wrappers.lambdaQuery(DrpInspectGoodsDetail.class).eq(DrpInspectGoodsDetail::getInspectGoodsId, inspectGoodsId));
      }

      List<DrpInspectGoodsDetailCommitAddDTO> detailList = content.getDetailList();
      Iterator var5 = detailList.iterator();

      while(var5.hasNext()) {
         DrpInspectGoodsDetailCommitAddDTO detail = (DrpInspectGoodsDetailCommitAddDTO)var5.next();
         DrpInspectGoodsDetail drpInspectGoodsDetail = new DrpInspectGoodsDetail();
         BeanUtil.copyProperties(detail, drpInspectGoodsDetail, new String[0]);
         drpInspectGoodsDetail.setInspectGoodsId(inspectGoodsId);
         drpInspectGoodsDetail.setInspectGoodsDetailId(Id.next());
         this.drpInspectGoodsDetailMapper.insert(drpInspectGoodsDetail);
      }

   }

   public List<DrpInspectGoodsPageVO> pageInspectGoods(DrpInspectGoodsPageDTO content) {
      List<DrpInspectGoodsPageVO> resultList = this.baseMapper.pageInspectGoods(content);
      Iterator var5 = resultList.iterator();

      while(var5.hasNext()) {
         DrpInspectGoodsPageVO result = (DrpInspectGoodsPageVO)var5.next();
         result.setIfUpload(LeConstants.COMMON_NO);
         Long checkIfUpload = this.drpQualityInspectMapper.selectCount(Wrappers.lambdaQuery(DrpQualityInspect.class).eq(DrpQualityInspect::getDrpOrderId, result.getInspectGoodsId()));
         if (checkIfUpload > 0L) {
            result.setIfUpload(LeConstants.COMMON_YES);
         }
      }

      return resultList;
   }

   public List<DrpInspectGoodsDetailPageVO> pageInspectGoodsDetail(DrpInspectGoodsDetailPageDTO content) {
      return this.drpInspectGoodsDetailMapper.pageInspectGoodsDetail(content);
   }

//   public DrpInspectGoodsTotalVO getTotal() {
//      LocalDateTime startTime = DrpDateUtil.getTodayStartTime();
//      LocalDateTime endTime = DrpDateUtil.getTodayEndTime();
//      return ((DrpInspectGoodsMapper)this.baseMapper).queryTotal(startTime, endTime);
//   }
}
