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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fzm.mall.server.front.goods.mapper.GoodSkuMapper;
import com.fzm.mall.server.front.goods.model.Category;
import com.fzm.mall.server.front.goods.model.OprationFloorGoods;
import com.fzm.mall.server.front.goods.mapper.OprationFloorGoodsMapper;
import com.fzm.mall.server.front.goods.model.vo.PageVo;
import com.fzm.mall.server.front.goods.model.vo.PageVoWithOne;
import com.fzm.mall.server.front.goods.model.vo.SaleHotVo;
import com.fzm.mall.server.front.goods.service.ICategoryService;
import com.fzm.mall.server.front.goods.service.IOprationFloorGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 运营中心-首页楼层 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-04-02
 */
@Service
public class OprationFloorGoodsServiceImpl extends ServiceImpl<OprationFloorGoodsMapper, OprationFloorGoods> implements IOprationFloorGoodsService {

    @Autowired
    private OprationFloorGoodsMapper oprationFloorGoodsMapper;

    @Autowired
    private GoodSkuMapper goodSkuMapper;

    @Autowired
    private ICategoryService categoryService;

    @Override
    public List<SaleHotVo> getByOperationFloorId(String operationFloorId, PageVoWithOne pageVo) {
        int start = (pageVo.getPage()-1)*pageVo.getSize();
        int end = pageVo.getSize();
        List<SaleHotVo> list = oprationFloorGoodsMapper.getList(operationFloorId, start, end);
        if(!CollectionUtils.isEmpty(list)){
            for(SaleHotVo i:list){
                String str = i.getCategoryId();
                JSONArray objects = JSONArray.parseArray(str);
                String categoryId = objects.get(objects.size() - 1).toString();
                Category category = categoryService.getByCategoryId(categoryId);
                if(category!=null){
                    i.setCategoryName(category.getName());
                }

                SaleHotVo countResult = goodSkuMapper.count(i.getGoodsId());
                i.setStockCount(countResult.getStockCount());
                i.setCirculationCount(countResult.getCirculationCount());
            }
        }
        return list;
    }

    @Override
    public Integer getCount(String operationFloorId) {
        return oprationFloorGoodsMapper.getCount(operationFloorId);
    }
}
