package com.bonus.canteen.core.order.deprecated.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.order.common.business.OrderRefundBusiness;
import com.bonus.canteen.core.order.common.constants.OrderRefundBizEnum;
import com.bonus.canteen.core.order.common.dto.OrderRefundDTO;
import com.bonus.canteen.core.order.common.vo.OrderRefundResultVO;
import com.bonus.canteen.core.order.deprecated.constants.OrdBackTypeEnum;
import com.bonus.canteen.core.order.deprecated.dto.OrderBackForMacDTO;
import com.bonus.canteen.core.order.deprecated.model.OrdDetail;
import com.bonus.canteen.core.order.deprecated.param.OrdParam;
import com.bonus.canteen.core.order.deprecated.service.OrdAppInfoService;
import com.bonus.canteen.core.order.deprecated.service.OrdInfoBusiness;
import com.bonus.canteen.core.order.deprecated.vo.OrdInfoVO;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.common.houqin.utils.LeBeanUtil;
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 java.lang.invoke.SerializedLambda;
import java.util.Iterator;
import java.util.List;

@Service
public class OrdInfoBusinessImpl implements OrdInfoBusiness {
   private static final Logger log = LoggerFactory.getLogger(OrdInfoBusinessImpl.class);
   @Autowired
   @Lazy
   private OrdAppInfoService ordAppInfoService;
   @Autowired
   @Lazy
   private OrderRefundBusiness orderRefundBusiness;

   public Page<OrdInfoVO> queryPageOrdInfoDetail(Page<?> page, OrdParam ordParam) {
      if (StrUtil.isNotBlank(ordParam.getCustName())) {
         ordParam.setCustName(LeBeanUtil.fieldLikeHandle(ordParam.getCustName()));
      }

      if (StrUtil.isNotBlank(ordParam.getMchName())) {
         ordParam.setMchName(LeBeanUtil.fieldLikeHandle(ordParam.getMchName()));
      }

      Page<OrdInfoVO> resultPage = this.ordAppInfoService.queryPageOrdInfoDetail(page, ordParam);
      List<OrdInfoVO> ordInfoVOList = resultPage.getRecords();
      Iterator var5 = ordInfoVOList.iterator();

      while(var5.hasNext()) {
         OrdInfoVO infoVO = (OrdInfoVO)var5.next();
         LambdaQueryWrapper<OrdDetail> queryWrapper = Wrappers.lambdaQuery(OrdDetail.class);
         if (LeConstants.COMMON_YES.equals(infoVO.getIfReverse())) {
            (queryWrapper.eq(ObjectUtil.isNotNull(infoVO.getRelateOrdId()), OrdDetail::getOrdId, infoVO.getRelateOrdId())).eq(OrdDetail::getRefundOrdId, infoVO.getOrdId());
         } else {
            queryWrapper.eq(OrdDetail::getOrdId, infoVO.getOrdId());
         }
      }

      return resultPage;
   }

   public void orderBackForMac(OrderBackForMacDTO macDTO) {
      OrderRefundDTO content = new OrderRefundDTO();
      content.setMacOrderId(macDTO.getMacOrdId());
      content.setOrderId(macDTO.getOrdId());
      content.setRemark(OrdBackTypeEnum.MERCHANT_BACK.value());
      content.setRestoreInventory(macDTO.getRestoreInventory());
      OrderRefundResultVO orderRefundResultVO = this.orderRefundBusiness.orderRefund(content.convertToOrderRefundParam(), OrderRefundBizEnum.ANDROID);
      if (!orderRefundResultVO.ifSuccess()) {
         throw new ServiceException(orderRefundResultVO.getResultMsg(), orderRefundResultVO.getResultCode());
      }
   }
}
