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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.basic.controller.admin.goodsinfo.vo.*;
import cn.iocoder.yudao.module.basic.convert.goodsinfo.GoodsInfoConvert;
import cn.iocoder.yudao.module.basic.dal.dataobject.goodsinfo.GoodsInfoDO;
import cn.iocoder.yudao.module.basic.service.goodsinfo.GoodsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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-info")
@Validated
public class GoodsInfoController {

    @Resource
    private GoodsInfoService goodsInfoService;

    @PostMapping("/create")
    @ApiOperation("创建商品")
    @PreAuthorize("@ss.hasPermission('basic:goods-info:create')")
    public CommonResult<Long> createGoodsInfo(@Valid @RequestBody GoodsInfoCreateReqVO createReqVO) {
        return success(goodsInfoService.createGoodsInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新商品")
    @PreAuthorize("@ss.hasPermission('basic:goods-info:update')")
    public CommonResult<Boolean> updateGoodsInfo(@Valid @RequestBody GoodsInfoUpdateReqVO updateReqVO) {
        goodsInfoService.updateGoodsInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除商品")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class),
                        @ApiImplicitParam(name = "goodsId", value = "商品编码", required = true, dataTypeClass = String.class)})
    @PreAuthorize("@ss.hasPermission('basic:goods-info:delete')")
    public CommonResult<Boolean> deleteGoodsInfo(@RequestParam("id") Long id,@RequestParam("goodsId") String goodsId) {
        goodsInfoService.deleteGoodsInfo(id,goodsId);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得商品")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('basic:goods-info:query')")
    public CommonResult<GoodsInfoRespVO> getGoodsInfo(@RequestParam("id") Long id) {
        GoodsInfoDO goodsInfo = goodsInfoService.getGoodsInfo(id);
        return success(GoodsInfoConvert.INSTANCE.convert(goodsInfo));
    }

    @GetMapping("/listQuery")
    @ApiOperation("不分页获得商品列表，根据条件动态获取 GoodsInfoListReqVO")
    @PreAuthorize("@ss.hasPermission('basic:goods-info:query')")
    public CommonResult<List<GoodsInfoRespVO>> getGoodsInfoList(GoodsInfoListReqVO goodsInfoListReqVO) {
        List<GoodsInfoDO> list = goodsInfoService.getGoodsInfoListQuery(goodsInfoListReqVO);
        return success(GoodsInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得商品分页")
    @PreAuthorize("@ss.hasPermission('basic:goods-info:query')")
    public CommonResult<PageResult<GoodsInfoRespVO>> getGoodsInfoPage(@Valid GoodsInfoPageReqVO pageVO) {
        PageResult<GoodsInfoDO> pageResult = goodsInfoService.getGoodsInfoPage(pageVO);
        return success(GoodsInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getGoodsNameList")
    @ApiOperation("根据指定的商户id 获取所有名下多个商品")
    @ApiImplicitParam(name = "ids", value = "商户id", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('basic:goods-info:query')")
    public CommonResult<List<GoodsInfoRespVO>> getGoodsNameList(@RequestParam("ids") List<Long> ids) {
        List<GoodsInfoDO> list = goodsInfoService.getGoodsNameListByBusinessInfoId(ids);
        return success(GoodsInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/nameList")
    @ApiOperation("获得所有商品")
    @PreAuthorize("@ss.hasPermission('basic:goods-info:query')")
    public CommonResult<List<GoodsInfoRespVO>> getGoodsNameLists() {
        List<GoodsInfoDO> list = goodsInfoService.getGoodsNameList();
        return success(GoodsInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getGoodsListByBelongBusinessId")
    @ApiOperation("根据指定的商户id 获取所有名下多个商品")
    @ApiImplicitParam(name = "belongBusinessId", value = "商户id", required = true, example = "1", dataTypeClass = List.class)
    public CommonResult<List<GoodsInfoRespVO>> getGoodsListByBelongBusinessId(@RequestParam("belongBusinessId") List<Long> belongBusinessId) {
        List<GoodsInfoDO> goodsListByBelongBusinessId = goodsInfoService.getGoodsListByBelongBusinessId(belongBusinessId);
        return success(GoodsInfoConvert.INSTANCE.convertList(goodsListByBelongBusinessId));
    }

    @GetMapping("/getByBusinessInfoId")
    @ApiOperation("根据商户id 获得所有商品")
    @ApiImplicitParam(name = "businessInfoId", value = "商户信息id", required = true, example = "1", dataTypeClass = Long.class)
    public CommonResult<List<GoodsInfoRespVO>> getByBusinessInfoId(@RequestParam("businessInfoId") Long businessInfoId) {
        return success(goodsInfoService.getGoodsListByBusinessInfoId(businessInfoId));
    }

    @GetMapping("/queryListMiniResult")
    @ApiOperation("获得商品信息（返回字段做精简化）")
    @ApiImplicitParam(name = "belongBusinessId", value = "商户信息ID", required = true, example = "1", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('basic:goods-info:query')")
    public CommonResult<List<GoodsInfoForTransferRespVO>> queryListMiniResult(@RequestParam("belongBusinessId") Long belongBusinessId) {
        return success(goodsInfoService.queryListMiniResult(belongBusinessId));
    }

    @GetMapping("/getByGoodsId")
    @ApiOperation("根据商品编码获得商品信息")
    @ApiImplicitParam(name = "goodsId", value = "商品编码", required = true, example = "GOCD202305291459QRSJ", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('basic:goods-info:query')")
    public CommonResult<GoodsInfoRespVO> getByGoodsId(@RequestParam("goodsId") String goodsId) {
        return success(goodsInfoService.getByGoodsId(goodsId));
    }
}
