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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bonus.canteen.core.supply.purchase.domain.bo.DrpMaterialLatestPrice;
import com.bonus.canteen.core.supply.purchase.domain.bo.DrpMaterialLatestPriceDTO;
import com.bonus.canteen.core.supply.purchase.domain.vo.DrpMaterialLatestPriceVO;
import com.bonus.canteen.core.supply.purchase.mapper.DrpMaterialLatestPriceMapper;
import com.bonus.canteen.core.supply.purchase.service.DrpMaterialLatestPriceService;
import com.bonus.common.houqin.utils.id.Id;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.lang.invoke.SerializedLambda;
import java.util.List;

@Service
public class DrpMaterialLatestPriceServiceImpl implements DrpMaterialLatestPriceService {
   private static final Logger log = LoggerFactory.getLogger(DrpMaterialLatestPriceServiceImpl.class);
   @Autowired
   @Lazy
   private DrpMaterialLatestPriceMapper drpMaterialLatestPriceMapper;

   public List<DrpMaterialLatestPriceVO> getMaterialLatestPrice(List<Long> materialIdList, Integer priceType, Long supplierId) {
      return this.drpMaterialLatestPriceMapper.getPriceListByMaterialIdList(materialIdList, priceType, supplierId);
   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void addMaterialLatestPrice(DrpMaterialLatestPriceDTO drpMaterialLatestPriceDTO) {
      List<DrpMaterialLatestPrice> drpMaterialLatestPrices = this.drpMaterialLatestPriceMapper.selectList((((Wrappers.lambdaQuery(DrpMaterialLatestPrice.class).eq(DrpMaterialLatestPrice::getMaterialId, drpMaterialLatestPriceDTO.getMaterialId())).eq(DrpMaterialLatestPrice::getUnitId, drpMaterialLatestPriceDTO.getUnitId())).eq(drpMaterialLatestPriceDTO.getSupplierId() != null, DrpMaterialLatestPrice::getSupplierId, drpMaterialLatestPriceDTO.getSupplierId())).eq(DrpMaterialLatestPrice::getPriceType, drpMaterialLatestPriceDTO.getPriceType()));
      if (CollUtil.isNotEmpty(drpMaterialLatestPrices)) {
         drpMaterialLatestPrices.forEach((drpMaterialLatestPricex) -> {
            this.drpMaterialLatestPriceMapper.deleteById(drpMaterialLatestPricex.getId());
         });
      }

      DrpMaterialLatestPrice drpMaterialLatestPrice = new DrpMaterialLatestPrice();
      BeanUtil.copyProperties(drpMaterialLatestPriceDTO, drpMaterialLatestPrice, new String[0]);
      drpMaterialLatestPrice.setId(Id.next());
      this.drpMaterialLatestPriceMapper.insert(drpMaterialLatestPrice);
   }

   public void delete(String relatedId) {
      this.drpMaterialLatestPriceMapper.delete(Wrappers.lambdaQuery(DrpMaterialLatestPrice.class).eq(DrpMaterialLatestPrice::getRelatedId, relatedId));
   }
}
