package cn.ysk.cashier.controller.shop;

import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.dto.shop.TbShopStorageNumDto;
import cn.ysk.cashier.dto.shop.TbShopStorageQueryCriteria;
import cn.ysk.cashier.dto.shop.TbShopStorageRecordQueryCriteria;
import cn.ysk.cashier.pojo.shop.TbShopStorage;
import cn.ysk.cashier.service.shop.TbShopStorageRecordService;
import cn.ysk.cashier.service.shop.TbShopStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
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.*;

@RestController
@RequiredArgsConstructor
@Api(tags = "存酒管理")
@RequestMapping("/api/storage")
@Slf4j
public class TbShopStorageController {

    private final TbShopStorageService tbShopStorageService;
    private final TbShopStorageRecordService tbShopStorageRecordService;
//
//    @ApiOperation("导出数据")
//    @GetMapping(value = "/download")
//    public void exportTbShopStorage(HttpServletResponse response, TbShopStorageQueryCriteria criteria) throws IOException {
//        tbShopStorageService.download(tbShopStorageService.queryAll(criteria), response);
//    }

    @PostMapping("list")
    @ApiOperation("查询存酒列表")
    public ResponseEntity<Object> queryTbShopStorage(@RequestBody TbShopStorageQueryCriteria criteria){
        Pageable pageable = PageRequest.of(criteria.getPage(), criteria.getSize(),Sort.by(Sort.Direction.DESC, "id"));
        return new ResponseEntity<>(tbShopStorageService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增存酒:#resources.name")
    @ApiOperation("新增存酒")
    public ResponseEntity<Object> createTbShopStorage(@Validated @RequestBody TbShopStorage resources){
        return new ResponseEntity<>(tbShopStorageService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("存取酒")
    @ApiOperation("存取酒")
    public ResponseEntity<Object> updateTbShopStorage(@Validated @RequestBody TbShopStorageNumDto resources) {
        tbShopStorageService.updateNum(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping("recordList")
    @ApiOperation("查询存取酒记录")
    public ResponseEntity<Object> queryTbShopStorageRecord(TbShopStorageRecordQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(tbShopStorageRecordService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping("countRecord")
    @ApiOperation("存酒统计")
    public ResponseEntity<Object> queryTbOrderPayCount(@RequestBody TbShopStorageQueryCriteria criteria){
        Pageable pageable = PageRequest.of(criteria.getPage(), criteria.getSize());
        return new ResponseEntity<>(tbShopStorageService.countStorage(criteria,pageable),HttpStatus.OK);
    }
}
