package cn.iocoder.yudao.module.member.controller.app.storegoods;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.storegoods.vo.StoreSaleGoodsPageVO;
import cn.iocoder.yudao.module.member.controller.app.storegoods.vo.StoreGoodsVO;
import cn.iocoder.yudao.module.member.dal.dataobject.storegoods.StoreGoodsDO;
import cn.iocoder.yudao.module.member.service.storegoods.StoreGoodsService;
import cn.iocoder.yudao.module.member.service.storegoodsinventory.StoreGoodsInventoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "APP超市 - 商品")
@RestController
@RequestMapping("/member/store-goods")
@Validated
public class AppStoreGoodsController {

    @Resource
    private StoreGoodsService storeGoodsService;
    @Resource
    private StoreGoodsInventoryService storeGoodsInventoryService;


    @GetMapping("/page")
    @Operation(summary = "获得商品分页")
    public CommonResult<PageResult<StoreSaleGoodsPageVO>> getStoreGoodsPage(@Valid StoreSaleGoodsPageVO pageReqVO) {
        PageResult<StoreSaleGoodsPageVO> pageResult = storeGoodsService.getSaleGoodsPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品详情")
    public CommonResult<StoreGoodsVO> getStoreGoods(Integer goodsId,Long carteenId ) {
        StoreGoodsDO storeGoods = storeGoodsService.getStoreGoods(goodsId);
        StoreGoodsVO bean = BeanUtils.toBean(storeGoods, StoreGoodsVO.class);
        Integer inventory = storeGoodsInventoryService.getInventory(goodsId, carteenId);
        bean.setInventory(inventory);
        return success(bean);
    }

}