package com.fzm.mall.server.front.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.front.goods.model.vo.SkuVo;
import com.fzm.mall.server.front.goods.service.ISkuService;
import com.fzm.mall.server.front.order.mapper.DeliveryMapper;
import com.fzm.mall.server.front.order.model.DeliveryRefund;
import com.fzm.mall.server.front.order.mapper.DeliveryRefundMapper;
import com.fzm.mall.server.front.order.model.vo.ExpressVo;
import com.fzm.mall.server.front.order.model.vo.ReturnGoodsDetail;
import com.fzm.mall.server.front.order.model.vo.ShopInfoVo;
import com.fzm.mall.server.front.order.service.IDeliveryRefundService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 提货退货单表 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-04-26
 */
@Service
public class DeliveryRefundServiceImpl extends ServiceImpl<DeliveryRefundMapper, DeliveryRefund> implements IDeliveryRefundService {
    @Autowired
    private DeliveryRefundMapper deliveryRefundMapper;

    @Autowired
    private WuLiuService wuLiuService;

    @Autowired
    private ISkuService skuService;

    @Autowired
    private DeliveryMapper deliveryMapper;


    @Override
    public List<ExpressVo> getExpress(String searchKey) {
        return deliveryRefundMapper.getExpress(searchKey);
    }

    @Override
    public DeliveryRefund getByUidAndOrderDeliveryId(String uid, String orderDeliveryId) {
        QueryWrapper<DeliveryRefund> q  = new QueryWrapper<>();
        q.eq("uid",uid);
        q.eq("order_delivery_id",orderDeliveryId);
        return getOne(q);
    }

    @Override
    public ReturnGoodsDetail getDetail(String orderDeliveryId) {
        ReturnGoodsDetail returnGoodsDetail = deliveryRefundMapper.getDetail(orderDeliveryId);
        if(returnGoodsDetail == null){
            return  null;
        }
        String expressName = skuService.getExpressName(returnGoodsDetail.getReturnExpressName());
        returnGoodsDetail.setExpressName(expressName);
        returnGoodsDetail.setExpress(wuLiuService.getWuliu(orderDeliveryId,returnGoodsDetail.getReturnExpressName(),returnGoodsDetail.getReturnExpressNo()));
        SkuVo skuVo = skuService.getBySkuId(returnGoodsDetail.getSkuId());
        returnGoodsDetail.setSkuVo(skuVo);
        ShopInfoVo shopInfoVo = skuService.getShopInfoVo(returnGoodsDetail.getAddressId());
        returnGoodsDetail.setShopInfoVo(shopInfoVo);
        return returnGoodsDetail;
    }

    @Override
    public int revokeReturn(String uid, String orderDeliveryId) {
        int i = deliveryMapper.revokeReturn(uid,orderDeliveryId);
        if(i == 1){
            deliveryRefundMapper.revokeReturn(orderDeliveryId);
        }
        return i;
    }
}
