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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.front.constant.GoodMainConst;
import com.fzm.mall.server.front.goods.model.GoodSkuProp;
import com.fzm.mall.server.front.goods.mapper.GoodSkuPropMapper;
import com.fzm.mall.server.front.goods.service.IGoodSkuPropService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.front.order.mapper.OrderDetailMapper;
import com.fzm.mall.server.front.order.model.OrderDetail;
import com.fzm.mall.server.front.redis.GoodRedis;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品规格属性 服务实现类
 * </p>
 *
 * @author tdz
 * @since 2021-03-02
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GoodSkuPropServiceImpl extends ServiceImpl<GoodSkuPropMapper, GoodSkuProp> implements IGoodSkuPropService {
    private final GoodRedis goodRedis;
    private final OrderDetailMapper orderDetailMapper;

    public static void main(String[] args) {
        List<Map<String, Object>> skuProp = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("name", "AZX");
        map.put("value", "1");
        skuProp.add(map);
        System.out.println(skuProp);
    }

    @Override
    public List<Map<String, Object>> getSkuPropListBySkuId(String skuId) {
        List<Map<String, Object>> skuProp = goodRedis.getSkuProp(skuId);
        if (skuProp != null) {
            return skuProp;
        } else {
            skuProp = new ArrayList<>();
        }
        List<GoodSkuProp> list = new ArrayList<>();
        QueryWrapper<GoodSkuProp> qw = new QueryWrapper();
        qw.eq("sku_id", skuId);
        list = list(qw);
        if (list != null) {
            for (GoodSkuProp prop : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", prop.getPropKey());
                map.put("value", prop.getPropVal());
                skuProp.add(map);
            }
            goodRedis.setSkuProp(skuId, skuProp);
        }
        return skuProp;
    }

    @Override
    public List<GoodSkuProp> getList(String string) {
        QueryWrapper<GoodSkuProp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sku_id", string);
        return list(queryWrapper);
    }

    @Override
    public void delSkuProCachByOid(String oid) {
        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("oid", oid);
        List<OrderDetail> detailList = orderDetailMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(detailList)){
            detailList.forEach(orderDetail -> {
                goodRedis.delSkuProp(orderDetail.getSkuId());
            });
        }
    }
}
