package cn.ysk.cashier.cons.rest;

import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.cons.domain.SuppFlow;
import cn.ysk.cashier.cons.domain.TbConsInfo;
import cn.ysk.cashier.cons.domain.TbConsSuppFlow;
import cn.ysk.cashier.cons.service.TbConsInfoService;
import cn.ysk.cashier.cons.service.dto.TbConsInfoQueryCriteria;
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.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @author admin
* @date 2024-06-22
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "耗材信息管理")
@RequestMapping("/api/tbConsInfo")
public class TbConsInfoController {

    private final TbConsInfoService tbConsInfoService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void exportTbConsInfo(HttpServletResponse response, TbConsInfoQueryCriteria criteria) throws IOException {
        tbConsInfoService.download(tbConsInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询耗材信息")
    public ResponseEntity<Object> queryTbConsInfo(TbConsInfoQueryCriteria criteria, Pageable pageable){
        pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by("createTime").descending());
        return new ResponseEntity<>(tbConsInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增耗材信息")
    @ApiOperation("新增耗材信息")
    public ResponseEntity<Object> createTbConsInfo(@Validated @RequestBody List<TbConsInfo> resources) throws Exception {
        return new ResponseEntity<>(tbConsInfoService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改耗材信息")
    @ApiOperation("修改耗材信息")
    public ResponseEntity<Object> updateTbConsInfo(@Validated @RequestBody List<TbConsInfo> resources) throws Exception {
        tbConsInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除耗材信息")
    @ApiOperation("删除耗材信息")
    public ResponseEntity<Object> deleteTbConsInfo(@RequestBody Integer[] ids) {
        tbConsInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "stockInOut")
    @Log("耗材::#resources.type")
    @ApiOperation("耗材出入库")
    public ResponseEntity<Object> stockInOut(@Validated @RequestBody SuppFlow resources) throws Exception {

        tbConsInfoService.stockInOut(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }



    @PostMapping("inputStock")
    public ResponseEntity<Object> inputStock(@RequestParam String shopId,@RequestParam("file") MultipartFile file){
        try {
            tbConsInfoService.inputStock(shopId,file);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
