package com.foreverwin.mesnac.common.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.common.model.Printer;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.R;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mesnac.common.service.PrinterService;
import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * @author Leon.L
 * @since 2021-08-11
 */
@RestController
@RequestMapping("/PRINTER")
public class PrinterController {

    @Autowired
    public PrinterService printerService;

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getPrinterList(Printer printer){
        List<Printer> result;
        try {
            String site = CommonMethods.getSite();
            printer.setSite(site);

            QueryWrapper<Printer> queryWrapper = new QueryWrapper<>();
            queryWrapper.setEntity(printer);
            result = printerService.list(queryWrapper);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }

    @ResponseBody
    @GetMapping("/getPrinter")
    public R savePrinter(String printer) {
        Printer result = null;
        try {
            String site = CommonMethods.getSite();
            String handle = HandleEnum.PRINT.getHandle(site, printer);
            result = printerService.getById(handle);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }

    @ResponseBody
    @PostMapping("/savePrinter")
    public R savePrinter(@RequestBody Printer printer) {
        try {
            LocalDateTime nowDate = LocalDateTime.now();
            String site = CommonMethods.getSite();

            String handle = HandleEnum.PRINT.getHandle(site, printer.getPrinter());
            Printer printerModel = printerService.getById(handle);
            if (printerModel == null) {
                printer.setHandle(handle);
                printer.setSite(site);
                printer.setCreatedDateTime(nowDate);
                printer.setModifiedDateTime(nowDate);
                printerService.save(printer);
                return R.ok("保存成功");
            } else {
                printerModel.setDescription(printer.getDescription());
                printerModel.setEnabled(printer.getEnabled());
                printerModel.setModifiedDateTime(nowDate);
                printerService.updateById(printerModel);
                return R.ok("修改成功");
            }
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
    }
    @ResponseBody
    @PostMapping("/delPrinter")
    public R delPrinter(Printer printer) {
        try {
            String site = CommonMethods.getSite();

            String handle = HandleEnum.PRINT.getHandle(site, printer.getPrinter());
            Printer printerModel = printerService.getById(handle);
            if (printerModel == null) {
                throw BusinessException.build("请先检索再删除！");
            } else {
                printerService.removeById(printerModel);
            }
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
        return R.ok();
    }
}