/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package cn.ysk.cashier.controller.product;

import cn.ysk.cashier.annotation.AnonymousAccess;
import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.pojo.product.TbProductStockDetail;
import cn.ysk.cashier.service.product.TbProductStockDetailService;
import cn.ysk.cashier.dto.product.TbProductStockDetailQueryCriteria;
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.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author lyf
* @date 2024-01-19
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "/product/Stock管理")
@RequestMapping("/api/tbProductStockDetail")
public class TbProductStockDetailController {

    private final TbProductStockDetailService tbProductStockDetailService;

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void exportTbProductStockDetail(HttpServletResponse response, TbProductStockDetailQueryCriteria criteria) throws IOException {
        tbProductStockDetailService.download(tbProductStockDetailService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询/product/Stock")
    public ResponseEntity<Object> queryTbProductStockDetail(TbProductStockDetailQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(tbProductStockDetailService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping("/stock")
    @ApiOperation("查询/product/Stock")
    public ResponseEntity<Object> queryPage(@RequestBody TbProductStockDetailQueryCriteria criteria){
        return new ResponseEntity<>(tbProductStockDetailService.queryPage(criteria),HttpStatus.OK);
    }
    @GetMapping("/sum")
    public ResponseEntity<Object> sumType(TbProductStockDetailQueryCriteria criteria){
        return new ResponseEntity<>(tbProductStockDetailService.sumStockNumber(criteria.getProductId()),HttpStatus.OK);
    }


    /**
     * 出库/入库
     * @param resources
     * @return
     */
    @PostMapping
    @Log("出入库:#resources.productName")
    @ApiOperation("新增/product/Stock")
    public ResponseEntity<Object> createTbProductStockDetail(@Validated @RequestBody TbProductStockDetail resources){
        return new ResponseEntity<>(tbProductStockDetailService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @ApiOperation("修改/product/Stock")
    public ResponseEntity<Object> updateTbProductStockDetail(@Validated @RequestBody TbProductStockDetail resources){
        tbProductStockDetailService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @ApiOperation("删除/product/Stock")
    public ResponseEntity<Object> deleteTbProductStockDetail(@RequestBody Long[] ids) {
        tbProductStockDetailService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
