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

import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.dto.printer.redis.PrinterTaskBean;
import com.neusoft.smart.pos.dto.printer.response.GetPrinterTemplateResponse;
import com.neusoft.smart.pos.dto.printer.response.GetPrinterTemplateTypesResponse;
import com.neusoft.smart.pos.dto.printer.response.GetPrinterTemplatesResponse;
import com.neusoft.smart.pos.framework.utils.ListUtils;
import com.neusoft.smart.pos.framework.utils.SpringUtils;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterTemplate;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterTemplateRef;
import com.neusoft.smart.pos.printer.persistent.ext.domain.Printer;
import com.neusoft.smart.pos.printer.persistent.ext.domain.PrinterObject;
import com.neusoft.smart.pos.printer.repository.IPrinterTemplateRepository;
import com.neusoft.smart.pos.printer.repository.PrinterTemplateRefRepository;
import com.neusoft.smart.pos.printer.repository.PrinterTemplateRepository;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service("printerTemplateService")
public class PrinterTemplateService {

	@Autowired
	private PrinterTemplateRefRepository printerTemplateRefRepository;

	@Autowired
	private PrinterTemplateRepository printerTemplateRepository;

	/**
	 * 打印模板执行方法
	 * @return
	 */
	public List<PrinterTaskBean> executeTemplate(Integer orgId, Integer shopId, PrinterObject printerObject) {
		List<PrinterTaskBean> printerTaskBeanList = new ArrayList<>();

		//1.查询规则关系列表数据
		for(Printer printer:printerObject.getPrinterTarget().getPrinters()){
			PrinterTemplateRef printerTemplateRefs = printerTemplateRefRepository.queryPrinterTemplateByPrinterId(printer.getPrinterId());
			//1.1 查询规则字典bean信息
			PrinterTemplate printerTemplate = printerTemplateRepository.queryPrinterTemplateByTemplateId(printerTemplateRefs.getTemplateId());
			val printerTemplateRepository = SpringUtils.getBean(printerTemplate.getBeanName(),IPrinterTemplateRepository.class);

			//1.2 执行规则
			PrinterTaskBean printerTaskBean = printerTemplateRepository.executeTemplate(orgId,shopId,printer);
			printerTaskBeanList.add(printerTaskBean);
		}
		return printerTaskBeanList;
	}
	
	/**
	 * 打印模板字典列表查询方法
	 * @return
	 */
	public GetPrinterTemplatesResponse queryPrinterTemplates(String isEnable, String templateType) {
		GetPrinterTemplatesResponse printerTemplatesResponse = new GetPrinterTemplatesResponse();
		List<PrinterTemplate> printerTemplateList = printerTemplateRepository.queryPrinterTemplateList(isEnable,templateType);
		//根据模板类型进行分组
		List<GetPrinterTemplateTypesResponse> printerTemplateTypeList = new ArrayList<>();
		if(null != printerTemplateList){
			for(int i=0;i<printerTemplateList.size();i++){
				GetPrinterTemplateResponse printerTemplate = new GetPrinterTemplateResponse();
				BeanUtils.copyProperties(printerTemplateList.get(i),printerTemplate);
				DictionaryConstant dict = DictionaryConstant.getEnumByCode(DictionaryConstant.PRINTER_TEMPLATE_TYPE.getCode(), printerTemplate.getTemplateType());
				printerTemplate.setTemplateTypeName(dict.getValue());
				//遍历模板类型列表，如果存在则追加，否则新增
				Boolean exist = false;  //是否存在
				for(int j=0;j<printerTemplateTypeList.size();j++){
					if(printerTemplateTypeList.get(j).getTemplateType().equals(printerTemplate.getTemplateType())){
						exist = true;
						printerTemplateTypeList.get(j).getPrinterTemplateList().add(printerTemplate);
					}
				}
				if(!exist){
					//不存在新增
					GetPrinterTemplateTypesResponse printerTemplateTypes = new GetPrinterTemplateTypesResponse();
					printerTemplateTypes.setTemplateType(printerTemplate.getTemplateType());
					printerTemplateTypes.setTemplateTypeName(printerTemplate.getTemplateTypeName());
					printerTemplateTypes.getPrinterTemplateList().add(printerTemplate);
					printerTemplateTypeList.add(printerTemplateTypes);
				}
			}
		}
		
		printerTemplatesResponse.setPrinterTemplateTypeList(printerTemplateTypeList);
		return printerTemplatesResponse;
	}
	
}
