ProductController.java 2.9 KB
Newer Older
H
haoxr 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
package com.youlai.mall.pms.controller.app;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.common.result.Result;
import com.youlai.mall.pms.pojo.bo.app.ProductBO;
import com.youlai.mall.pms.pojo.domain.PmsSpu;
import com.youlai.mall.pms.pojo.dto.app.ProductDTO;
import com.youlai.mall.pms.service.IPmsSpuService;
import com.youlai.mall.pms.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "【移动端】商品信息")
@RestController("AppSpuController")
@RequestMapping("/api.app/v1/products")
@AllArgsConstructor
public class ProductController {

    private IPmsSpuService iPmsSpuService;

    private IProductService iProductService;

    @ApiOperation(value = "列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数量", defaultValue = "10", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "name", value = "商品名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "categoryId", value = "商品类目", paramType = "query", dataType = "Long")
    })
    @GetMapping
    public Result list(
            Integer page,
            Integer limit,
            String name,
            Long categoryId
    ) {
        Page<PmsSpu> result = iPmsSpuService.page(new Page<>(page, limit), new LambdaQueryWrapper<PmsSpu>()
                .eq(categoryId != null, PmsSpu::getCategoryId, categoryId)
                .like(StrUtil.isNotBlank(name), PmsSpu::getName, name)
                .select(PmsSpu::getId,
                        PmsSpu::getName,
                        PmsSpu::getPic,
                        PmsSpu::getPrice,
                        PmsSpu::getSales
                )
        );
        List<ProductDTO> list = result.getRecords().stream()
                .map(item -> JSONUtil.toBean(JSONUtil.toJsonStr(item), ProductDTO.class))
                .collect(Collectors.toList());
        return Result.success(list, result.getTotal());
    }

    @ApiOperation(value = "商品详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "商品ID", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/{id}")
    public Result detail(@PathVariable Long id) {
        ProductBO product = iProductService.getProductById(id);
        return Result.success(product);
    }

}