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

import com.neusoft.smart.pos.dto.printer.redis.PrinterBean;
import com.neusoft.smart.pos.framework.task.RedisTask;
import com.neusoft.smart.pos.framework.utils.BeanUtil;
import com.neusoft.smart.pos.framework.utils.SpringUtils;
import com.neusoft.smart.pos.printer.constants.PrintConstants;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterGoodsRefInfo;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterInfo;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterRule;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterRuleAttribute;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterRuleRef;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterTemplate;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterTemplateRef;
import com.neusoft.smart.pos.printer.repository.PrinterGoodsRefInfoRepository;
import com.neusoft.smart.pos.printer.repository.PrinterInfoRepository;
import com.neusoft.smart.pos.printer.repository.PrinterRuleAttributeRepository;
import com.neusoft.smart.pos.printer.repository.PrinterRuleRefRepository;
import com.neusoft.smart.pos.printer.repository.PrinterRuleRepository;
import com.neusoft.smart.pos.printer.repository.PrinterTemplateRefRepository;
import com.neusoft.smart.pos.printer.repository.PrinterTemplateRepository;
import com.neusoft.smart.pos.printer.service.PrinterRedisService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class PrinterCreateOrUpdateTasks extends RedisTask {
    
    @Setter
    private Long printerId;
    
    private PrinterRuleRefRepository printerRuleRefRepository = SpringUtils.getBean("printerRuleRefRepository", PrinterRuleRefRepository.class);
    
    @Autowired
    private PrinterRuleRepository printerRuleRepository = SpringUtils.getBean("printerRuleRepository", PrinterRuleRepository.class);
    
    @Autowired
    private PrinterRuleAttributeRepository printerRuleAttributeRepository = SpringUtils.getBean("printerRuleAttributeRepository", PrinterRuleAttributeRepository.class);
    
    @Autowired
    private PrinterTemplateRefRepository printerTemplateRefRepository = SpringUtils.getBean("printerTemplateRefRepository", PrinterTemplateRefRepository.class);
    
    @Autowired
    private PrinterTemplateRepository printerTemplateRepository = SpringUtils.getBean("printerTemplateRepository", PrinterTemplateRepository.class);
    
    @Autowired
    private PrinterGoodsRefInfoRepository printerGoodsRefInfoRepository = SpringUtils.getBean("printerGoodsRefInfoRepository", PrinterGoodsRefInfoRepository.class);
    
    @Autowired
    private PrinterInfoRepository printerInfoRepository = SpringUtils.getBean("printerInfoRepository", PrinterInfoRepository.class);
    
    @Autowired
    private PrinterRedisService printerRedisService = SpringUtils.getBean("printerRedisService", PrinterRedisService.class);
    
    @Override
    public void executeTask() {
        PrinterBean printerBean = generate (printerId);
        printerRedisService.insertOrUpdate(printerBean);
    }
    
    
    private PrinterBean generate (Long printerId) {
        PrinterBean printerBean = new PrinterBean();
        PrinterInfo printerInfo = printerInfoRepository.queryPrinterByPrinterId(printerId);
        BeanUtils.copyProperties(printerInfo, printerBean);
        //中转设备
        if (PrintConstants.TRANSFER.getCode().equals(printerInfo.getDeviceType())) {
            List<PrinterInfo> children = printerInfoRepository.queryPrinterByParentId(printerId);
            children.forEach(child ->
                    printerBean.getLinkedPrinters().add(BeanUtil.parse(generate (child.getId()), PrinterBean.class)));
            return printerBean;
        }
        //组装规则
        List<PrinterRuleRef> printerRuleRefs = printerRuleRefRepository.queryPrinterRulesByPrinterId(printerId);
        if (!printerRuleRefs.isEmpty()) {
            List<Long> ruleIds = printerRuleRefs.parallelStream().map(PrinterRuleRef::getRuleId).collect(Collectors.toList());
            List<PrinterRule> printerRules = printerRuleRepository.queryPrinterRulesByRuleIds(ruleIds);
            printerBean.setPrinterRules(BeanUtil.parseList(printerRules, PrinterBean.PrintRuleBean.class));
            //拼规则下的值
            List<Long> ruleRefIds = printerRuleRefs.parallelStream().map(PrinterRuleRef::getId).collect(Collectors.toList());
            List<PrinterRuleAttribute> printerRuleAttributes = printerRuleAttributeRepository.queryPrinterRuleAttributeByRuleRefIds(ruleRefIds);
            printerBean.getPrinterRules().forEach(rule -> {
                List<PrinterRuleAttribute> temp = printerRuleAttributes.parallelStream().filter(attr -> attr.getRuleId().equals(rule.getId())).collect(Collectors.toList());
                rule.getRuleAttributes().addAll(BeanUtil.parseList(temp, PrinterBean.PrintRuleAttributeBean.class));
            });
        }
        //拼下模板
        PrinterTemplateRef printerTemplateRef = printerTemplateRefRepository.queryPrinterTemplateByPrinterId(printerId);
        PrinterTemplate printerTemplate = printerTemplateRepository.queryPrinterTemplateByTemplateId(printerTemplateRef.getTemplateId());
        printerBean.setPrinterTemplate(BeanUtil.parse(printerTemplate, PrinterBean.PrinterTemplateBean.class));
        //组装下与菜单或者菜单分类的关系
        List<PrinterGoodsRefInfo> printerGoodsRefInfos = printerGoodsRefInfoRepository.queryPrinterGoodsRefInfoByPrinterId(printerId);
        if (!printerGoodsRefInfos.isEmpty()) {
            printerBean.setPrinterGoods(BeanUtil.parseList(printerGoodsRefInfos, PrinterBean.PrinterGoodsBean.class));
        }
        
        return printerBean;
    }
}
