package cn.ysk.cashier.controller.product;

import cn.ysk.cashier.annotation.AnonymousAccess;
import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.dto.product.TbProductSortCriteria;
import cn.ysk.cashier.vo.TbProductVo;
import cn.ysk.cashier.service.product.TbProductService;
import cn.ysk.cashier.dto.product.TbProductQueryCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.util.List;

/**
* @website https://eladmin.vip
* @author lyf
* @date 2023-12-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "商品管理")
@RequestMapping("/api/tbProduct")
public class TbProductController {

    private final TbProductService tbProductService;

    @GetMapping
    @ApiOperation("查询/product")
    public ResponseEntity<Object> queryTbProduct(TbProductQueryCriteria criteria){
        return new ResponseEntity<>(tbProductService.queryAll(criteria, false),HttpStatus.OK);
    }



    @GetMapping("/list")
    @ApiOperation("查询/productForAdmin")
    public ResponseEntity<Object> queryTbProductForAdmin(TbProductQueryCriteria criteria){
        return new ResponseEntity<>(tbProductService.queryAll(criteria, true),HttpStatus.OK);
    }

    @GetMapping("/isHot")
    public ResponseEntity<Object> updateIsHot(@RequestParam Integer isHot, @RequestParam Integer id){
        tbProductService.updateIsHot(id,isHot);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping("/{product}")
    @ApiOperation("查询/product")
    public Object queryTbProductInfo(@PathVariable("product") Integer product)throws Exception{
        return tbProductService.findByProductId(product);
    }

    @GetMapping ("/productList")
    @ApiOperation("查询/product")
    public Object queryTbProductInfo(@RequestParam List<String> productList){
        return tbProductService.findByProductList(productList);
    }



    @PostMapping("/upProSort")
    @ApiOperation("修改商品排序")
    public ResponseEntity<Object> upProSort(@RequestBody TbProductSortCriteria param){
        tbProductService.upProSort(param);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping
    @Log("新增商品:#resources.name")
    @ApiOperation("新增/product")
    public ResponseEntity<Object> createTbProduct(@Validated @RequestBody TbProductVo resources){
        return new ResponseEntity<>(tbProductService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改商品:#resources.name")
    @ApiOperation("修改/product")
    public ResponseEntity<Object> updateTbProduct(@Validated @RequestBody TbProductVo resources){
        tbProductService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除商品:#ids")
    @ApiOperation("删除/product")
    public ResponseEntity<Object> deleteTbProduct(@RequestBody Integer[] ids) {
        tbProductService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
