package com.ycwl.basic.controller.pc;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.mapper.PrintTaskMapper;
import com.ycwl.basic.model.pc.printer.entity.PrintTaskEntity;
import com.ycwl.basic.model.pc.printer.entity.PrinterEntity;
import com.ycwl.basic.model.pc.printer.req.PrintTaskReqQuery;
import com.ycwl.basic.model.pc.printer.req.ReprintRequest;
import com.ycwl.basic.service.printer.PrinterService;
import com.ycwl.basic.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/printer/v1")
public class PrinterController {
    @Autowired
    private PrinterService printerService;

    @Autowired
    private PrintTaskMapper printTaskMapper;

    // 查询列表
    @PostMapping("/list")
    public ApiResponse<List<PrinterEntity>> list(@RequestBody PrinterEntity condition) {
        return printerService.list(condition);
    }

    // 获取详情
    @GetMapping("/get/{id}")
    public ApiResponse<PrinterEntity> get(@PathVariable("id") Integer id) {
        return printerService.get(id);
    }

    // 新增
    @PostMapping("/add")
    public ApiResponse<Integer> add(@RequestBody PrinterEntity entity) {
        return printerService.add(entity);
    }

    // 更新
    @PostMapping("/update")
    public ApiResponse<Integer> update(@RequestBody PrinterEntity entity) {
        return printerService.update(entity);
    }

    // 删除
    @DeleteMapping("/delete/{id}")
    public ApiResponse<Integer> delete(@PathVariable("id") Integer id) {
        return printerService.delete(id);
    }

    // 分页查询打印任务
    @PostMapping("/task/page")
    public ApiResponse<PageInfo<PrintTaskEntity>> taskPage(@RequestBody PrintTaskReqQuery req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<PrintTaskEntity> list = printTaskMapper.queryByCondition(req.getPrinterId(), req.getStatus());
        PageInfo<PrintTaskEntity> pageInfo = new PageInfo<>(list);
        return ApiResponse.success(pageInfo);
    }

    // 重新打印(将状态设置为0-未开始,并更新打印机名称)
    @PostMapping("/task/reprint/{id}")
    public ApiResponse<Integer> reprint(@PathVariable("id") Integer id, @RequestBody ReprintRequest request) {
        int result = printTaskMapper.updateStatusAndPrinter(id, 0, request.getPrinterName());
        return ApiResponse.success(result);
    }

    /**
     * 查询待审核的打印任务
     * @param printerId 打印机ID(可选)
     * @return 待审核任务列表
     */
    @GetMapping("/task/pending-review")
    public ApiResponse<List<PrintTaskEntity>> getPendingReviewTasks(Integer printerId) {
        List<PrintTaskEntity> tasks = printerService.getPendingReviewTasks(printerId);
        return ApiResponse.success(tasks);
    }

    /**
     * 更新待审核任务的URL（重新处理水印等）
     * @param taskId 任务ID
     * @param url 新的打印URL
     * @return 操作结果
     */
    @PostMapping("/task/{taskId}/url")
    public ApiResponse<Boolean> updateTaskUrl(@PathVariable("taskId") Integer taskId, @RequestBody String url) {
        boolean success = printerService.updatePendingReviewTaskUrl(taskId, url);
        return ApiResponse.success(success);
    }

    /**
     * 批准待审核任务，下发到打印队列
     * @param taskIds 任务ID列表
     * @return 成功数量
     */
    @PostMapping("/task/approve")
    public ApiResponse<Integer> approveTasks(@RequestBody List<Integer> taskIds) {
        int count = printerService.approvePrintTasks(taskIds);
        return ApiResponse.success(count);
    }

    /**
     * 拒绝待审核任务
     * @param taskIds 任务ID列表
     * @return 成功数量
     */
    @PostMapping("/task/reject")
    public ApiResponse<Integer> rejectTasks(@RequestBody List<Integer> taskIds) {
        int count = printerService.rejectPrintTasks(taskIds);
        return ApiResponse.success(count);
    }
}