package com.cxyxiaomo.epp.shop.controller;

import com.cxyxiaomo.epp.common.response.Res;
import com.cxyxiaomo.epp.common.vo.GoodCategoryVO;
import com.cxyxiaomo.epp.common.vo.GoodVO;
import com.cxyxiaomo.epp.shop.service.GoodsServiceImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/shop/good")
public class GoodsController {

    @Resource
    GoodsServiceImpl goodsService;

    /**
     * 小程序端商品列表
     *
     * @return
     */
    @GetMapping("/miniprogram/list")
    public Res list(@RequestParam(value = "cateId", required = false) Integer cateId,
                    @RequestParam(value = "searchText", required = false) String searchText) {
        if (cateId != null && cateId <= 0) {
            cateId = null;
        }
        if (searchText != null) {
            searchText = searchText.trim();
            if (searchText.contains("%") || searchText.contains("_")) {
                searchText = searchText
                        .replaceAll("[_%]", "");
            }
            if ("".equals(searchText)) {
                searchText = null;
            }
        }
        List<GoodVO> list = goodsService.list(cateId, searchText);
        return Res.success(list);
    }

    /**
     * 小程序端商品详情
     *
     * @return
     */
    @GetMapping("/miniprogram/detail")
    public Res detail(@RequestParam("id") Long id) {
        GoodVO goodVO = goodsService.getById(id);
        return Res.success(goodVO);
    }

    /**
     * 小程序端商品分类列表
     *
     * @return
     */
    @GetMapping("/miniprogram/cateList")
    public Res cateList() {
        List<GoodCategoryVO> list = goodsService.cateList();
        return Res.success(list);
    }
}
