package com.caimei.www.service.impl;

import com.caimei.www.mapper.SupplierDao;
import com.caimei.www.pojo.JsonModel;
import com.caimei.www.pojo.content.ProductList;
import com.caimei.www.pojo.content.SupplierDetail;
import com.caimei.www.service.SupplierService;
import com.caimei.www.utils.ImageUtil;
import com.caimei.www.utils.PriceUtil;
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/22
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Value("${caimei.wwwDomain}")
    private String domain;
    @Resource
    private SupplierDao supplierDao;

    /**
     * 供应商详情
     *
     * @param supplierId
     * @return
     */
    @Override
    public SupplierDetail getSupplierById(Integer supplierId) {
        SupplierDetail supplier = supplierDao.getSupplierById(supplierId);
        String[] businessScope = supplier.getBusinessScope().split("/");
        supplier.setBusinessScopeArr(businessScope);
        return supplier;
    }

    /**
     * 供应商-轮播图片
     *
     * @param supplierId
     * @return
     */
    @Override
    public JsonModel<List<String>> getSupplierDetailImages(Integer supplierId) {
        if (supplierId == null) { return JsonModel.error("参数异常", null);}
        List<String> images = supplierDao.getSupplierDetailImages(supplierId);
        return JsonModel.success(images);
    }

    /**
     * 供应商-主推商品
     *
     * @param supplierId
     * @return
     */
    @Override
    public JsonModel<List<ProductList>> getSupplierMainProducts(Integer supplierId) {
        if (supplierId == null) { return JsonModel.error("参数异常", null);}
        List<ProductList> list = supplierDao.getSupplierMainProducts(supplierId);
        // 设置价格等级 及 老图片路径
        list.forEach(product -> {
             product.setPricegrade(PriceUtil.getPriceGrade(product.getPrice()));
             product.setPrice(0d);
             product.setImage(ImageUtil.getImageURL("product", product.getImage(), 0, domain));
        });
        return JsonModel.success(list);
    }
}
