package com.caimei.www.service.impl;

import com.caimei.www.pojo.JsonModel;
import com.caimei.www.mapper.ProductDao;
import com.caimei.www.pojo.content.ProductDetail;
import com.caimei.www.pojo.content.ProductList;
import com.caimei.www.pojo.content.Parameter;
import com.caimei.www.service.ProductService;
import com.caimei.www.utils.ImageUtil;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Description
 *
 * @author : Charles
 * @date : 2020/7/8
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductDao productDao;
    @Value("${caimei.wwwDomain}")
    private String domain;
    /**
     * 根据商品Id获取详情
     *
     * @param productId
     * @return
     */
    @Override
    public ProductDetail getProductDetailById(Integer productId) {
        ProductDetail product = productDao.getProductDetailById(productId);
        if(!StringUtil.isNullOrEmpty(product.getTags())){
            product.setTagList(product.getTags().split(","));
        }
        // 价格等级
        if (product.getPrice() > 50 * 5000) {
            product.setPricegrade(5);
        } else if (product.getPrice() > 25 * 5000) {
            product.setPricegrade(4);
        } else if (product.getPrice() > 10 * 5000) {
            product.setPricegrade(3);
        } else if (product.getPrice() > 2.5 * 5000) {
            product.setPricegrade(2);
        } else {
            product.setPricegrade(1);
        }
        return product;
    }

    /**
     * 详情-相关推荐
     *
     * @return
     */
    @Override
    public JsonModel<List<ProductList>> getProductDetailRecommends(Integer productId, Integer recommendType) {
        List<ProductList> list = null;
        //相关推荐类型 0自动选择; 1手动推荐
        if (1 == recommendType) {
            list = productDao.getProductRecommendsById(productId);
        } else {
            list = productDao.getAutoProductRecommends(productId);
        }
		return JsonModel.success(list);
    }

    /**
     * 详情-相关参数
     *
     * @param productId
     * @return
     */
    @Override
    public JsonModel<List<Parameter>> getProductParameters(Integer productId) {
        if (productId == null) { return JsonModel.error("参数异常", null);}
        List<Parameter> list = productDao.getProductParameters(productId);
		return JsonModel.success(list);
    }

    /**
     * 详情-图片
     *
     * @param productId
     * @return
     */
    @Override
    public JsonModel<List<String>> getProductDetailImages(Integer productId) {
        if (productId == null) { return JsonModel.error("参数异常", null);}
        List<String> list = productDao.getProductDetailImages(productId);
        if (list.size() > 0) {
            // 设置价格等级 及 老图片路径
            list.forEach(image -> {
                image = ImageUtil.getImageURL("product", image, 0, domain);
            });
        }
		return JsonModel.success(list);
    }
}
