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.bonus.canteen.core.supply.purchase.constants.DrpCommitStatusEnum;
import com.bonus.canteen.core.supply.purchase.constants.DrpMaterialPriceTypeEnum;
import com.bonus.canteen.core.supply.purchase.domain.bo.*;
import com.bonus.canteen.core.supply.purchase.domain.param.DrpMarketPriceCommitDTO;
import com.bonus.canteen.core.supply.purchase.domain.param.DrpMarketPricePageDTO;
import com.bonus.canteen.core.supply.purchase.domain.vo.DrpMarketPriceDetailPageVO;
import com.bonus.canteen.core.supply.purchase.domain.vo.DrpMarketPricePageVO;
import com.bonus.canteen.core.supply.purchase.mapper.DrpMarketPriceDetailMapper;
import com.bonus.canteen.core.supply.purchase.mapper.DrpMarketPriceMapper;
import com.bonus.canteen.core.supply.purchase.service.DrpMarketPriceService;
import com.bonus.canteen.core.supply.purchase.service.DrpMaterialLatestPriceService;
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.utils.LeBeanUtil;
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 java.util.Iterator;
import java.util.List;
import java.util.Objects;

@Service
public class DrpMarketPriceServiceImpl implements DrpMarketPriceService {
   @Autowired
   @Lazy
   private DrpMarketPriceMapper drpMarketPriceMapper;
   @Autowired
   @Lazy
   private DrpMarketPriceDetailMapper drpMarketPriceDetailMapper;
   @Autowired
   @Lazy
   private DrpMaterialLatestPriceService drpMaterialLatestPriceService;

   public String commit(DrpMarketPriceCommitDTO content) {
      DrpMarketPrice drpMarketPrice;
      if (CharSequenceUtil.isEmpty(content.getHarvestId())) {
         String harvestId = CustomIdGenerateUtil.generateMarketPriceId();
         drpMarketPrice = new DrpMarketPrice();
         BeanUtil.copyProperties(content, drpMarketPrice);
         drpMarketPrice.setDelFlag(DelFlagEnum.DEL_FALSE.key());
         drpMarketPrice.setHarvestId(harvestId);
         this.drpMarketPriceMapper.insert(drpMarketPrice);
      } else {
         drpMarketPrice = this.drpMarketPriceMapper.selectById(content.getHarvestId());
         BeanUtil.copyProperties(content, drpMarketPrice);
         drpMarketPrice.setDelFlag(DelFlagEnum.DEL_FALSE.key());
         this.drpMarketPriceMapper.updateById(drpMarketPrice);
         this.drpMarketPriceDetailMapper.delete(Wrappers.lambdaQuery(DrpMarketPriceDetail.class).eq(DrpMarketPriceDetail::getHarvestId, content.getHarvestId()));
      }

      if (CollUtil.isNotEmpty(content.getDetailList())) {
         Iterator var6 = content.getDetailList().iterator();

         while(var6.hasNext()) {
            DrpMarketPriceDetailCommitDTO detail = (DrpMarketPriceDetailCommitDTO)var6.next();
            DrpMarketPriceDetail drpMarketPriceDetail = new DrpMarketPriceDetail();
            BeanUtil.copyProperties(detail, drpMarketPriceDetail);
            drpMarketPriceDetail.setHarvestDetailId(Id.next());
            drpMarketPriceDetail.setHarvestId(drpMarketPrice.getHarvestId());
            this.drpMarketPriceDetailMapper.insert(drpMarketPriceDetail);
            if (content.getCommitStatus().equals(DrpCommitStatusEnum.COMMIT.key())) {
               this.drpMaterialLatestPriceService.addMaterialLatestPrice(DrpMaterialLatestPriceDTO.builder().materialId(detail.getMaterialId()).unitId(detail.getUnitId()).priceType(DrpMaterialPriceTypeEnum.MATERIAL_MARKET.key()).latestPrice(detail.getUnitPrice()).relatedId(drpMarketPrice.getHarvestId()).remark(DrpMaterialPriceTypeEnum.MATERIAL_MARKET.value()).build());
            }
         }
      }

      return drpMarketPrice.getHarvestId();
   }

   public List<DrpMarketPricePageVO> page(DrpMarketPricePageDTO content) {
      if (CharSequenceUtil.isNotBlank(content.getHarvestId())) {
         content.setHarvestId(LeBeanUtil.fieldLikeHandle(content.getHarvestId()));
      }
      return this.drpMarketPriceMapper.page(content, DelFlagEnum.DEL_FALSE.key());
   }

   public DrpMarketPricePageVO detail(String content) {
      List<DrpMarketPricePageVO> drpMarketPricePageVOList = this.page((new DrpMarketPricePageDTO()).setHarvestId(content));
      if (CollUtil.isNotEmpty(drpMarketPricePageVOList)) {
         DrpMarketPricePageVO drpMarketPricePageVO = drpMarketPricePageVOList.get(0);
         List<DrpMarketPriceDetailPageVO> detailList = this.drpMarketPriceMapper.getDetailListById(content);
         drpMarketPricePageVO.setDetailList(detailList);
         return drpMarketPricePageVO;
      } else {
         return null;
      }
   }

   public void remove(String content) {
      if (Objects.isNull(content)) {
         throw new ServiceException("id不能为空！");
      } else {
         this.drpMarketPriceMapper.update(null, (Wrappers.lambdaUpdate(DrpMarketPrice.class).set(DrpMarketPrice::getDelFlag, DelFlagEnum.DEL_TRUE.key())).eq(DrpMarketPrice::getHarvestId, content));
      }
   }
}
