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

import ch.qos.logback.core.net.SyslogOutputStream;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.front.asset.model.po.OrderDetailPO;
import com.fzm.mall.server.front.goods.model.GoodSku;
import com.fzm.mall.server.front.goods.model.Shop;
import com.fzm.mall.server.front.goods.service.IGoodCommentService;
import com.fzm.mall.server.front.goods.service.IGoodSkuService;
import com.fzm.mall.server.front.goods.service.IShopService;
import com.fzm.mall.server.front.order.model.OrderDetail;
import com.fzm.mall.server.front.order.mapper.OrderDetailMapper;
import com.fzm.mall.server.front.order.service.IOrderDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.front.redis.OrderRedis;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 用户购物订单详情表 服务实现类
 * </p>
 *
 * @author tdz
 * @since 2021-03-03
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements IOrderDetailService {
    private final OrderRedis orderRedis;
    private final OrderDetailMapper orderDetailMapper;
    private final IGoodSkuService skuService;
    private final IShopService shopService;

    @Override
    public List<OrderDetail> listOrderDetailService(String oid) {
        QueryWrapper<OrderDetail> qw = new QueryWrapper<>();
        qw.eq("oid", oid);
        return list(qw);
    }

    @Override
    public List<OrderDetail> getWaitsByUid(String uid) {
        List<OrderDetail> waitsByUid = orderDetailMapper.getWaitsByUid(uid);
        for (OrderDetail orderDetail : waitsByUid) {
            String skuId = orderDetail.getSkuId();
            GoodSku sku = skuService.getBySkuId(skuId);
            if (sku != null) {
                orderDetail.setThumb(sku.getThumb());
            }
            Shop shop = shopService.getByMerchantId(orderDetail.getMerchantId());
            if (shop != null) {
                orderDetail.setShopName(shop.getShopName());
            }
        }
        return waitsByUid;
    }

    /**
     * 订单详情列表
     */
    @Override
    public List<OrderDetail> listOrderDetail(final String oid) {
        List<OrderDetail> listOrderDetail = orderRedis.getListOrderDetail(oid);
        if (listOrderDetail != null) {
            return listOrderDetail;
        }
        QueryWrapper<OrderDetail> qw = new QueryWrapper();
        qw.eq("oid", oid);
        listOrderDetail = list(qw);
        if (!CollectionUtils.isEmpty(listOrderDetail)) {
            orderRedis.setListOrderDetail(oid, listOrderDetail);
        }
        return listOrderDetail;
    }

    @Override
    public List<OrderDetail> getList(String oid) {
        QueryWrapper<OrderDetail> qw = new QueryWrapper();
        qw.eq("oid", oid);
        return list(qw);
    }

    @Override
    public OrderDetail getByUidAndOidAndSkuId(String oid, String skuId) {
        QueryWrapper<OrderDetail> qw = new QueryWrapper();
        qw.eq("oid", oid);
        qw.eq("sku_id", skuId);
        return getOne(qw);
    }

    @Override
    public int updateCommend(String uid, String oid, String skuId) {
        return orderDetailMapper.updateCommend(uid, oid, skuId);
    }

    @Override
    public int updateCommendAppend(String commentId) {
        return orderDetailMapper.updateCommendAppend(commentId);
    }

    @Override
    public List<OrderDetail> getByUidAndSkuId(String uid, String skuId) {
        return  orderDetailMapper.getByUidAndSkuId(uid,skuId);
    }
}
