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

import com.neusoft.smart.pos.dto.printer.redis.PrinterBean;
import com.neusoft.smart.pos.dto.printer.redis.PrinterTaskBean;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.printer.constants.PrintConstants;
import com.neusoft.smart.pos.printer.constants.PrinterErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.LinkedList;
import java.util.Set;

/**
 * 打印机或者中转设备redis操作service
 */
@Service
public class PrinterRedisService {
    
    @Autowired
    private RedisTemplate redisTemplate;
    
    public void insertOrUpdate(PrinterBean printerBean) {
        if (PrintConstants.TRANSFER.getCode().equals(printerBean.getDeviceType()) && StringUtils.isEmpty(printerBean.getPrinterSn())) {
            throw new BusinessCommonException(PrinterErrorCode.EMPTY_TRANSFER_SN_ERROR);
        }
        String sn = StringUtils.isEmpty(printerBean.getPrinterSn()) ? "@@" : printerBean.getPrinterSn();
        BoundValueOperations<String, PrinterBean> boundValueOperations = redisTemplate.boundValueOps("printer_" + printerBean.getId() + "_" + sn);
        boundValueOperations.set(printerBean);
        //中转设备
        if (PrintConstants.TRANSFER.getCode().equals(printerBean.getDeviceType())) {
            printerBean.getLinkedPrinters().forEach(linkedPrinter -> {
                Set<String> keySet = redisTemplate.keys("printer_" + linkedPrinter.getId() + "_*");
                //改一下连接打印机的parentId
                if (!keySet.isEmpty()) {
                    String key = keySet.iterator().next();
                    PrinterBean bean = (PrinterBean) redisTemplate.boundValueOps(key).get();
                    bean.setParentId(printerBean.getId());
                    redisTemplate.boundValueOps(key).set(bean);
                }
                BoundValueOperations<String, LinkedList<PrinterTaskBean>> tasks = redisTemplate.boundValueOps("printer_" + linkedPrinter.getId() + "_tasks");
                tasks.setIfAbsent(new LinkedList<>());
            });
        } else {
            BoundValueOperations<String, LinkedList<PrinterTaskBean>> tasks = redisTemplate.boundValueOps("printer_" + printerBean.getId() + "_tasks");
            tasks.setIfAbsent(new LinkedList<>());
        }
    }
    
    
    public void delete(Integer printerId) {
        Set<String> keySet = redisTemplate.keys("printer_" + printerId + "_*");
        if (!keySet.isEmpty()) {
            keySet.forEach(key -> {
                //如果是中转设备,也要把连接的打印机队列删除掉
                BoundValueOperations<String, PrinterBean> boundValueOperations = redisTemplate.boundValueOps(key);
                boundValueOperations.get().getLinkedPrinters().forEach(linkedPrinter -> redisTemplate.delete("printer_" + linkedPrinter.getId() + "_tasks"));
            });
            redisTemplate.delete(keySet);
        }
    }
    
    public PrinterBean getBySn(String sn) {
        Set<String> keySet = redisTemplate.keys("printer_*_" + sn);
        if (keySet.isEmpty()) {
            throw new BusinessCommonException(PrinterErrorCode.INVALIDATE_SN_ERROR);
        }
        return (PrinterBean) redisTemplate.boundValueOps(keySet.iterator().next()).get();
    }
    
}
