
package com.tmerclub.cloud.delivery.controller.supplier;

import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.delivery.dto.PrinterDTO;
import com.tmerclub.cloud.delivery.model.Printer;
import com.tmerclub.cloud.delivery.service.PrinterService;
import com.tmerclub.cloud.delivery.vo.PrinterVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 打印机
 *
 * @author TRACK
 * @date 2023-08-23 14:48:23
 */
@RestController("supplierPrinterController")
@RequestMapping("/s/printer")
@Tag(name = "打印机")
public class PrinterController {

    @Autowired
    private PrinterService printerService;

    @GetMapping("/page")
    @Operation(summary = "获取打印机列表", description = "分页获取打印机列表")
    public ServerResponseEntity<PageVO<Printer>> page(@Valid PageDTO pageDTO, PrinterDTO printerDTO) {
        printerDTO.setSupplierId(AuthUserContext.get().getTenantId());
        PageVO<Printer> printerPage = printerService.page(pageDTO, printerDTO);
        return ServerResponseEntity.success(printerPage);
    }

    @GetMapping
    @Operation(summary = "获取打印机", description = "根据printerId获取打印机")
    public ServerResponseEntity<Printer> getByPrinterId(@RequestParam Long printerId) {
        return ServerResponseEntity.success(printerService.getByPrinterId(printerId));
    }

    @PostMapping
    @Operation(summary = "保存打印机", description = "保存打印机")
    public ServerResponseEntity<Void> save(@Valid @RequestBody PrinterDTO printerDTO) {
        Printer printer = BeanUtil.map(printerDTO, Printer.class);
        printer.setPrinterId(null);
        printer.setShopId(null);
        printer.setSupplierId(AuthUserContext.get().getTenantId());
        printerService.save(printer);
        printerService.removeCache(AuthUserContext.get().getTenantId(), AuthUserContext.get().getSysType());
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新打印机", description = "更新打印机")
    public ServerResponseEntity<Void> update(@Valid @RequestBody PrinterDTO printerDTO) {
        Printer printer = BeanUtil.map(printerDTO, Printer.class);
        printer.setShopId(null);
        printer.setSupplierId(AuthUserContext.get().getTenantId());
        printerService.update(printer);
        printerService.removeCache(AuthUserContext.get().getTenantId(), AuthUserContext.get().getSysType());
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除打印机", description = "根据打印机id删除打印机")
    public ServerResponseEntity<Void> delete(@RequestParam Long printerId) {
        printerService.deleteById(printerId);
        printerService.removeCache(AuthUserContext.get().getTenantId(), AuthUserContext.get().getSysType());
        return ServerResponseEntity.success();
    }

    @PutMapping("/setDefault")
    @Operation(summary = "设打印机信息为默认/取消默认")
    public ServerResponseEntity<Void> setDefault(@RequestParam Long printerId) {
        printerService.setDefault(printerId);
        printerService.removeCache(AuthUserContext.get().getTenantId(), AuthUserContext.get().getSysType());
        return ServerResponseEntity.success();
    }

    @GetMapping("/printerList")
    @Operation(summary = "获取打印机信息列表")
    public ServerResponseEntity<List<PrinterVO>> printerList() {
        List<PrinterVO> list = printerService.listByShopId(AuthUserContext.get().getTenantId(), AuthUserContext.get().getSysType());
        return ServerResponseEntity.success(list);
    }
}
