package com.hwrj.cloud.portal.controller;

import com.hwrj.cloud.common.api.CommonPage;
import com.hwrj.cloud.common.api.CommonResult;
import com.hwrj.cloud.portal.domain.PmsProductVo;
import com.hwrj.cloud.portal.model.PmsProduct;
import com.hwrj.cloud.portal.service.PortalProductPulishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "h5端产品发布接口",description = "h5端产品发布接口")
@RestController
@RequestMapping("/prod/pub")
public class PortalProductPublishController {

    @Autowired
    private PortalProductPulishService portalProductPulishService;


    @ApiOperation("发布产品")
    @PostMapping("add")
    public CommonResult addProd(@RequestBody PmsProductVo productVo){

        int i = portalProductPulishService.create(productVo);
        if (i==0){
            return CommonResult.failed("发布失败");
        }

        return CommonResult.success();
    }


    @ApiOperation("修改产品")
    @PostMapping("update")
    public CommonResult updateProd(@RequestBody PmsProductVo productVo){
        Long id = productVo.getId();
        if (id==null){
            return CommonResult.failed("修改失败");
        }
        int i = portalProductPulishService.update(id,productVo);
        if (i==0){
            return CommonResult.failed("发布失败");
        }

        return CommonResult.success();
    }

    @ApiOperation("产品列表")
    @GetMapping("list")
    public CommonResult listProd(@RequestParam(value = "pageNo",required = false,defaultValue = "0")int pageNo,@RequestParam(value = "pageSize",required = false,defaultValue = "10")int pageSize){

        List<PmsProduct> list = portalProductPulishService.list(pageSize, pageNo);

        return CommonResult.success(CommonPage.restPage(list));
    }


    @ApiOperation("修改产品")
    @GetMapping("detail")
    public CommonResult detailProd(@RequestParam(value = "id",required = true,defaultValue = "0")Long id){

        PmsProduct detail = portalProductPulishService.detail(id);

        return CommonResult.success(detail);
    }

}
