package cn.ysk.cashier.controller.product;

import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.dto.product.TbProductSortCriteria;
import cn.ysk.cashier.service.product.TbProductService;
import cn.ysk.cashier.dto.product.TbProductQueryCriteria;
import cn.ysk.cashier.pojo.product.TbProductGroup;
import cn.ysk.cashier.service.product.TbProductGroupService;
import cn.ysk.cashier.dto.product.TbProductGroupQueryCriteria;
import cn.ysk.cashier.vo.AddProduct;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author lyf
* @date 2023-12-16
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "product/group管理")
@RequestMapping("/api/tbProductGroup")
public class TbProductGroupController {

    private final TbProductGroupService tbProductGroupService;

    @Resource
    private TbProductService tbProductService;

//    @ApiOperation("导出数据")
//    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('tbProductGroup:list')")
//    public void exportTbProductGroup(HttpServletResponse response, TbProductGroupQueryCriteria criteria) throws IOException {
//        tbProductGroupService.download(tbProductGroupService.queryAll(criteria), response);
//    }

    @GetMapping
    @ApiOperation("查询product/group")
    public ResponseEntity<Object> queryTbProductGroup(TbProductGroupQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(tbProductGroupService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/{productGroup}")
    @ApiOperation("查询product/group")
    public ResponseEntity<Object> queryTbProductGroup(@PathVariable("productGroup") Integer productGroup){
        return new ResponseEntity<>(tbProductGroupService.findByIdProduct(productGroup),HttpStatus.OK);
    }

    @PostMapping("/upGroupSort")
    @ApiOperation("修改分组排序")
    public ResponseEntity<Object> upProSort(@RequestBody TbProductSortCriteria param){
        tbProductGroupService.upSort(param);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @PostMapping
    @Log("新增商品分组:#resources.name")
    @ApiOperation("新增商品分组")
    public ResponseEntity<Object> createTbProductGroup(@Validated @RequestBody TbProductGroup resources){
        return new ResponseEntity<>(tbProductGroupService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改商品分组:#resources.name")
    @ApiOperation("修改商品分组")
    public ResponseEntity<Object> updateTbProductGroup(@Validated @RequestBody TbProductGroup resources){
        tbProductGroupService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

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

    /**
     *添加商品（商品列表）
     * @return
     */
    @GetMapping("/addProduct")
    public ResponseEntity<Object> ProductList(TbProductQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(tbProductService.queryAll(criteria, false),HttpStatus.OK);
    }

    /**
     * 商品分类增加商品
     * @param addProduct
     * @param userName
     * @return
     */
    @Log("商品分组增加商品")
    @PostMapping("/addProductInfo")
    public ResponseEntity<Object> addProductInfo(@RequestBody AddProduct addProduct,@RequestAttribute(value = "userName", required = false) String userName){
        return new ResponseEntity<>(tbProductGroupService.updateProductIds(addProduct,userName),HttpStatus.OK);
    }

}
