package com.neusoft.smart.pos.printer.service;

import com.neusoft.smart.pos.printer.persistent.domain.PrinterTaskInfo;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterTaskLog;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterTaskLogExample;
import com.neusoft.smart.pos.printer.repository.PrinterTaskInfoRepository;
import com.neusoft.smart.pos.printer.repository.PrinterTaskLogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PrinterTaskService {
    
    @Autowired
    private PrinterTaskInfoRepository printerTaskInfoRepository;
    
    @Autowired
    private PrinterTaskLogRepository printerTaskLogRepository;
    
    /**
     * 使用数据库实体，保存打印任务和日志
     *
     * @param printerTaskInfo
     */
    public void createInfo(PrinterTaskInfo printerTaskInfo) {
        printerTaskInfoRepository.create(printerTaskInfo);
    }
    
    public void updateTaskStatus(Integer taskId, String taskStatus) {
        printerTaskInfoRepository.updateTaskStatus(taskId, taskStatus);
    }
    
    /**
     *
     * @param taskId
     * @return
     */
    public PrinterTaskInfo getInfo(Integer taskId) {
        return printerTaskInfoRepository.get(taskId);
    }
    
    
    /**
     * 保存打印日志
     *
     * @param printerTaskLog
     */
    public void createLog(PrinterTaskLog printerTaskLog) {
        PrinterTaskLogExample printerTaskLogExample = new PrinterTaskLogExample();
        printerTaskLogExample.createCriteria().andTaskStatusEqualTo(printerTaskLog.getTaskStatus()).andTaskIdEqualTo(printerTaskLog.getTaskId());
        //如果该状态没有日志数据,进行保存
        if (printerTaskLogRepository.getCountByExample(printerTaskLogExample) == 0) {
            printerTaskLogRepository.create(printerTaskLog);
        }
    }
    
}
