package com.ycwl.basic.service.printer.impl;

import com.ycwl.basic.mapper.PrinterMapper;
import com.ycwl.basic.model.pc.printer.entity.PrintTaskEntity;
import com.ycwl.basic.model.pc.printer.entity.PrinterEntity;
import com.ycwl.basic.model.printer.req.PrinterSyncReq;
import com.ycwl.basic.model.printer.req.WorkerAuthReqVo;
import com.ycwl.basic.model.printer.resp.PrintTaskResp;
import com.ycwl.basic.service.printer.PrinterService;
import com.ycwl.basic.utils.ApiResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class PrinterServiceImpl implements PrinterService {
    @Autowired
    private PrinterMapper printerMapper;

    @Override
    public ApiResponse<List<PrinterEntity>> list(PrinterEntity condition) {
        return ApiResponse.success(printerMapper.list(condition));
    }

    @Override
    public ApiResponse<PrinterEntity> get(Integer id) {
        return ApiResponse.success(printerMapper.getById(id));
    }

    @Override
    public ApiResponse<Integer> add(PrinterEntity entity) {
        return ApiResponse.success(printerMapper.add(entity));
    }

    @Override
    public ApiResponse<Integer> update(PrinterEntity entity) {
        return ApiResponse.success(printerMapper.update(entity));
    }

    @Override
    public ApiResponse<Integer> delete(Integer id) {
        return ApiResponse.success(printerMapper.deleteById(id));
    }

    private PrinterEntity getPrinter(WorkerAuthReqVo req) {
        String accessKey = req.getAccessKey();
        if (accessKey == null) {
            return null;
        }
        PrinterEntity printer = printerMapper.findByAccessKey(accessKey);
        if (printer == null) {
            return null;
        }
        if (printer.getStatus() != 1) {
            return null;
        }
        return printer;
    }

    @Override
    public List<PrintTaskResp> sync(PrinterSyncReq req) {
        PrinterEntity printer = getPrinter(req);
        if (printer == null) {
            return Collections.emptyList();
        }
        List<String> printers = req.getPrinters();
        String printersStr = StringUtils.join(printers, ",");
        if (!StringUtils.equals(printersStr, printer.getPrinters())) {
            printer.setPrinters(printersStr);
            printerMapper.update(printer);
        }
        PrintTaskResp task = printerMapper.findTaskByPrinterId(printer.getId());
        if (task == null) {
            return Collections.emptyList();
        }
        return Collections.singletonList(task);
    }

    @Override
    public void taskSuccess(Integer taskId, WorkerAuthReqVo req) {
        PrintTaskEntity task = printerMapper.getTaskById(taskId);
        if (task == null) {
            return;
        }
        PrinterEntity printer = getPrinter(req);
        if (printer == null) {
            return;
        }
        if (printer.getId().equals(task.getPrinterId())) {
            printerMapper.updateTaskStatus(taskId, 1);
        }
    }

    @Override
    public void taskFail(Integer taskId, WorkerAuthReqVo req) {
        PrintTaskEntity task = printerMapper.getTaskById(taskId);
        if (task == null) {
            return;
        }
        PrinterEntity printer = getPrinter(req);
        if (printer == null) {
            return;
        }
        if (printer.getId().equals(task.getPrinterId())) {
            printerMapper.updateTaskStatus(taskId, 2);
        }
    }
}