package cn.ysk.cashier.cons.rest;

import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.cons.domain.TbProskuCon;
import cn.ysk.cashier.cons.service.TbProskuConService;
import cn.ysk.cashier.cons.service.dto.ProskuInfo;
import cn.ysk.cashier.cons.service.dto.TbProskuConQueryCriteria;
import cn.ysk.cashier.exception.BadRequestException;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
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 java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @author admin
* @date 2024-06-22
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "商品规格耗材信息管理")
@RequestMapping("/api/tbProskuCon")
public class TbProskuConController {

    private final TbProskuConService tbProskuConService;

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

    @GetMapping
    @ApiOperation("查询商品规格耗材信息")
    public ResponseEntity<Object> queryTbProskuCon(TbProskuConQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(tbProskuConService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增商品规格耗材信息")
    @ApiOperation("新增商品规格耗材信息")
    public ResponseEntity<Object> createTbProskuCon(@Validated @RequestBody List<ProskuInfo> resources) throws Exception {
        try {
            return new ResponseEntity<>(tbProskuConService.create(resources),HttpStatus.CREATED);
        } catch (BadRequestException be) {
            throw new Exception(be.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception("相同商品耗材信息不允许添加");
        }
    }

    @PutMapping
    @Log("修改商品规格耗材信息")
    @ApiOperation("修改商品规格耗材信息")
    public ResponseEntity<Object> updateTbProskuCon(@Validated @RequestBody TbProskuCon resources) throws Exception {
        tbProskuConService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

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