package com.czg.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.czg.account.dto.print.PrinterDelDTO;
import com.czg.account.entity.PrintMachine;
import com.czg.account.service.PrintMachineService;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 打印机管理
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/printer")
public class PrintMachineController {
    @Resource
    private PrintMachineService printMachineService;


    /**
     * 打印机列表
     * @param name 名称
     * @param connectionType 类型 USB 网络 蓝牙
     * @return 打印机列表
     */
    @SaAdminCheckPermission(parentName = "打印机管理", value = "printer:list", name = "打印机列表获取")
    @GetMapping
    public CzgResult<Page<PrintMachine>> list(String name, String connectionType, String subType) {
        QueryWrapper queryWrapper = new QueryWrapper().eq(PrintMachine::getShopId, StpKit.USER.getShopId());
        if (StrUtil.isNotBlank(name)) {
            queryWrapper.like(PrintMachine::getName, name);
        }

        if (StrUtil.isNotBlank(connectionType)) {
            queryWrapper.eq(PrintMachine::getConnectionType, connectionType);
        }

        if (StrUtil.isNotBlank(subType)) {
            queryWrapper.eq(PrintMachine::getPrintType, subType);
        }
        queryWrapper.orderBy(PrintMachine::getSort, true).orderBy(PrintMachine::getId, false);
        Page<PrintMachine> page = printMachineService.page(PageUtil.buildPage(), queryWrapper);
        return CzgResult.success(page);
    }

    /**
     * 打印机列表
     * @param name 名称
     * @return 打印机列表
     */
    @GetMapping("/getPrintLocal")
    public CzgResult<Page<PrintMachine>> getPrintLocal(String name, String subType) {
        QueryWrapper queryWrapper = new QueryWrapper().eq(PrintMachine::getShopId, StpKit.USER.getShopId());
        if (StrUtil.isNotBlank(name)) {
            queryWrapper.like(PrintMachine::getName, name);
        }
        queryWrapper.in(PrintMachine::getConnectionType, "USB", "局域网");
        if (StrUtil.isNotBlank(subType)) {
            queryWrapper.eq(PrintMachine::getPrintType, subType);
        }
        queryWrapper.orderBy(PrintMachine::getSort, true).orderBy(PrintMachine::getId, false);
        Page<PrintMachine> page = printMachineService.page(PageUtil.buildPage(), queryWrapper);
        return CzgResult.success(page);
    }

    /**
     * 打印机详情
     * @param id 打印机id
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "打印机管理", value = "printer:detail", name = "打印机详情获取")
    @GetMapping("/detail")
    public CzgResult<PrintMachine> detail(@RequestParam Integer id) {
        PrintMachine printMachine = printMachineService.getOne(new QueryWrapper().eq(PrintMachine::getId, id).eq(PrintMachine::getShopId, StpKit.USER.getShopId()));
        return CzgResult.success(printMachine);
    }


    /**
     * 打印机新增
     * @return 打印机列表
     */
    @SaAdminCheckPermission(parentName = "打印机管理", value = "printer:add", name = "打印机新增")
    @PostMapping
    public CzgResult<Boolean> add(@RequestBody @Validated PrintMachine printerAddDTO) {
        return CzgResult.success(printMachineService.add(StpKit.USER.getShopId(), printerAddDTO));
    }

    /**
     * 打印机修改
     * @return 打印机列表
     */
    @SaAdminCheckPermission(parentName = "打印机管理", value = "printer:edit", name = "打印机编辑")
    @PutMapping
    public CzgResult<Boolean> edit(@RequestBody @Validated PrintMachine printerEditDTO) {
        return CzgResult.success(printMachineService.edit(StpKit.USER.getShopId(), printerEditDTO));
    }

    /**
     * 打印机删除
     * @return 打印机列表
     */
    @SaAdminCheckPermission(parentName = "打印机管理", value = "printer:del", name = "打印机删除")
    @DeleteMapping
    public CzgResult<Boolean> edit(@RequestBody @Validated PrinterDelDTO printerDelDTO) {
        return CzgResult.success(printMachineService.remove(new QueryWrapper().eq(PrintMachine::getShopId, StpKit.USER.getShopId()).eq(PrintMachine::getId, printerDelDTO.getId())));
    }

}
