package cn.iocoder.yudao.module.basic.controller.admin.goodsverify;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.basic.controller.admin.goodsverify.vo.GoodsVerifyCreateReqVO;
import cn.iocoder.yudao.module.basic.controller.admin.goodsverify.vo.GoodsVerifyPageReqVO;
import cn.iocoder.yudao.module.basic.controller.admin.goodsverify.vo.GoodsVerifyRespVO;
import cn.iocoder.yudao.module.basic.controller.admin.goodsverify.vo.GoodsVerifyUpdateReqVO;
import cn.iocoder.yudao.module.basic.convert.goodsverify.GoodsVerifyConvert;
import cn.iocoder.yudao.module.basic.dal.dataobject.goodsverify.GoodsVerifyDO;
import cn.iocoder.yudao.module.basic.service.goodsverify.GoodsVerifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

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

@Api(tags = "管理后台 - 商品认证")
@RestController
@RequestMapping("/basic/goods-verify")
@Validated
public class GoodsVerifyController {

    @Resource
    private GoodsVerifyService goodsVerifyService;

    @PostMapping("/create")
    @ApiOperation("创建商品认证")
    @PreAuthorize("@ss.hasPermission('basic:goods-verify:create')")
    public CommonResult<Long> createGoodsVerify(@Valid @RequestBody GoodsVerifyCreateReqVO createReqVO) {
        return success(goodsVerifyService.createGoodsVerify(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新商品认证")
    @PreAuthorize("@ss.hasPermission('basic:goods-verify:update')")
    public CommonResult<Boolean> updateGoodsVerify(@Valid @RequestBody GoodsVerifyUpdateReqVO updateReqVO) {
        goodsVerifyService.updateGoodsVerify(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除商品认证")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('basic:goods-verify:delete')")
    public CommonResult<Boolean> deleteGoodsVerify(@RequestParam("id") Long id) {
        goodsVerifyService.deleteGoodsVerify(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得商品认证")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('basic:goods-verify:query')")
    public CommonResult<GoodsVerifyRespVO> getGoodsVerify(@RequestParam("id") Long id) {
        GoodsVerifyDO goodsVerify = goodsVerifyService.getGoodsVerify(id);
        return success(GoodsVerifyConvert.INSTANCE.convert(goodsVerify));
    }

    @GetMapping("/page")
    @ApiOperation("获得商品认证分页")
    @PreAuthorize("@ss.hasPermission('basic:goods-verify:query')")
    public CommonResult<PageResult<GoodsVerifyRespVO>> getGoodsVerifyPage(@Valid GoodsVerifyPageReqVO pageVO) {
        PageResult<GoodsVerifyDO> pageResult = goodsVerifyService.getGoodsVerifyPage(pageVO);
        return success(GoodsVerifyConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getByGoodsId")
    @ApiOperation("根据商品编码获得商品认证")
    @ApiImplicitParam(name = "goodsId", value = "商品编码", required = true, example = "GOCD202305061351B2PO", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('basic:goods-verify:query')")
    public CommonResult<List<GoodsVerifyRespVO>> getByGoodsId(@RequestParam("goodsId") String goodsId) {
        return success(GoodsVerifyConvert.INSTANCE.convertList(goodsVerifyService.getByGoodsId(goodsId)));
    }

}
