ProductController.java 5.14 KB
package com.glsx.admin.controller;

import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.platform.shopmall.dto.CreateMallGoodsRequestDTO;
import cn.com.glsx.platform.shopmall.remote.ShopMallGoodsRemote;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.glsx.admin.exception.BusinessException;
import com.glsx.admin.exception.ResultEnum;
import com.glsx.admin.pojo.GroupA;
import com.glsx.admin.pojo.request.ProductReq;
import com.glsx.admin.pojo.response.DeviceOrderResp;
import com.glsx.admin.pojo.response.ProductResp;
import com.glsx.admin.remote.ProductFeignClient;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.groups.Default;
import java.util.List;

/**
 * <p>Description: 产品列表业务
 * <p>Project: chejia-smart
 *
 * @author wbh
 * @date 2021-03-15
 */
@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductFeignClient productFeignClient;

    @DubboReference(version = "1.0.0", check = false, registry = {"shopmall"})
    private ShopMallGoodsRemote shopMallGoodsRemote;

    /**
     * 分页获取产品列表。
     *
     * @param req 请求体
     * @return 结果
     */
    @RequestMapping(value = "/getPageList", method = {RequestMethod.GET, RequestMethod.POST})
    public R getPageList(ProductReq req) {
        log.info("$$$$ /product/getPageList param: {}", JSON.toJSONString(req));
        PageInfo<ProductResp> pageInfo = productFeignClient.getPageList(req);
        return R.ok().putPageData(pageInfo);
    }

    /**
     * 获取产品列表,不分页。
     *
     * @param req 请求体
     * @return 结果
     */
    @RequestMapping(value = "/getList", method = {RequestMethod.GET, RequestMethod.POST})
    public R getList(ProductReq req) {
        log.info("$$$$ /product/getList param: {}", JSON.toJSONString(req));
        List<ProductResp> respList = productFeignClient.getList(req);
        return R.ok().data(respList);
    }

    /**
     * 根据 id 查询产品详情。
     *
     * @param req 请求体
     * @return 结果
     */
    @RequestMapping(value = "/getById", method = {RequestMethod.GET, RequestMethod.POST})
    public R getById(@Validated({Default.class}) ProductReq req) {
        log.info("$$$$ /product/getById param: {}", JSON.toJSONString(req));
        ProductResp resp = productFeignClient.getById(req);
        return R.ok().data(resp);
    }

    /**
     * 保存产品。无则插入,有则更新。
     *
     * @param req 产品
     * @return 结果
     */
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    public R save(@RequestBody @Validated({GroupA.class}) ProductReq req) {
        log.info("$$$$ /product/save param: {}", JSON.toJSONString(req));
        Boolean success = productFeignClient.save(req);
        return R.ok().data(success);
    }

    /**
     * 根据产品 id 获取指令集。
     *
     * @param req 请求体
     * @return 结果
     */
    @RequestMapping(value = "/getCommandSetById", method = {RequestMethod.GET, RequestMethod.POST})
    public R getCommandSetById(@Validated({Default.class}) ProductReq req) {
        log.info("$$$$ /product/getCommandSetById param: {}", JSON.toJSONString(req));
        List<DeviceOrderResp> respList = productFeignClient.getCommandSetById(req);
        return R.ok().data(respList);
    }

    /**
     * 校验商城商品 id。
     *
     * @param req 请求体
     * @return 结果
     */
    @RequestMapping(value = "/checkShopProductId", method = {RequestMethod.GET, RequestMethod.POST})
    public R checkShopProductId(ProductReq req) {
        log.info("$$$$ /product/checkShopProductId param: {}", JSON.toJSONString(req));
        if (StringUtils.isBlank(req.getShopProductId())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR);
        }
        RpcResponse<CreateMallGoodsRequestDTO> rpcResponse = shopMallGoodsRemote
                .selectOneById(Integer.valueOf(req.getShopProductId()), false);
        log.info("$$$$ shopMallGoodsRemote.selectOneById response: {}",
                JSON.toJSONString(rpcResponse));
        boolean productIdValid = rpcResponse.isSuccess() && rpcResponse.getResult() != null;
        return R.ok().data(productIdValid);
    }

    /**
     * 上下架产品。
     *
     * @param req 请求体
     * @return 结果
     */
    @RequestMapping(value = "/putOnOff", method = {RequestMethod.GET, RequestMethod.POST})
    public R putOnOff(@Validated({Default.class}) ProductReq req) {
        log.info("$$$$ /product/putOnOff param: {}", JSON.toJSONString(req));
        Boolean success = productFeignClient.putOnOff(req);
        return R.ok().data(success);
    }
}