package cn.ysk.cashier.controller.product;

import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.dto.product.TbProductFrmLossDto;
import cn.ysk.cashier.dto.product.TbProductStockCountQueryCriteria;
import cn.ysk.cashier.dto.product.TbProductStockDetailQueryCriteria;
import cn.ysk.cashier.service.product.TbProductStockDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;

@RestController
@RequiredArgsConstructor
@Api(tags = "商品库存记录管理")
@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("查询商品库存记录")
    public ResponseEntity<Object> queryTbProductStockDetail(TbProductStockDetailQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(tbProductStockDetailService.queryAll(criteria,pageable),HttpStatus.OK);
    }


    @PostMapping("/stock/count")
    @ApiOperation("商品库存统计")
    public ResponseEntity<Object> stockCount(@RequestBody TbProductStockCountQueryCriteria criteria) throws ParseException {
        return new ResponseEntity<>(tbProductStockDetailService.stockCount(criteria),HttpStatus.OK);
    }


    @PostMapping("/stock/list")
    @ApiOperation("商品库存统计列表")
    public ResponseEntity<Object> stockList(@RequestBody TbProductStockCountQueryCriteria criteria) throws ParseException {
        return new ResponseEntity<>(tbProductStockDetailService.stockList(criteria),HttpStatus.OK);
    }


    @PostMapping("/stock")
    @ApiOperation("商品库存记录列表")
    public ResponseEntity<Object> queryPage(@RequestBody TbProductStockDetailQueryCriteria criteria) throws ParseException {
        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);
//    }


    @Log("商品报损")
    @PostMapping("frmLoss")
    @ApiOperation("商品报损")
    public ResponseEntity<Object> frmLoss(@RequestBody TbProductFrmLossDto resources){
        tbProductStockDetailService.frmLoss(resources);
        return new ResponseEntity<>("ok",HttpStatus.OK);
    }

//    /**
//     * 商品库存记录
//     * @param resources
//     * @return
//     */
//    @PostMapping
//    @Log("报损")
//    @ApiOperation("新增商品库存记录")
//    public ResponseEntity<Object> createTbProductStockDetail(@Validated @RequestBody TbProductStockDetail resources){
//        return new ResponseEntity<>(tbProductStockDetailService.create(resources),HttpStatus.CREATED);
//    }
//
//    @PutMapping
//    @ApiOperation("修改商品库存记录")
//    public ResponseEntity<Object> updateTbProductStockDetail(@Validated @RequestBody TbProductStockDetail resources){
//        tbProductStockDetailService.update(resources);
//        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
//    }
//
//    @DeleteMapping
//    @ApiOperation("删除商品库存记录")
//    public ResponseEntity<Object> deleteTbProductStockDetail(@RequestBody Long[] ids) {
//        tbProductStockDetailService.deleteAll(ids);
//        return new ResponseEntity<>(HttpStatus.OK);
//    }
}
