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


import com.neusoft.smart.pos.printer.persistent.domain.PrinterRuleAttribute;
import com.neusoft.smart.pos.printer.persistent.ext.domain.Printer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @author zcf
 * @date 2018-10-18 10：43
 * @description 单菜品打印规则实现使用Repository
 */
@Component("singleGoodsPrinterRuleRepository")
public class SingleGoodsPrinterRuleRepository implements IPrinterRuleRepository {

	@Autowired
	private PrinterRuleAttributeRepository printerRuleAttributeRepository;

	private String goodsPrinterType = "2";	//菜品打印类型@1:单菜品打印@2:多菜品打印

	public List<Printer> executeRule(Integer orgId,Integer shopId,Printer printer,Long ruleId,Long printerRuleId){
		//1.处理当前规则属性值信息
		ruleAttributeHandle(ruleId,printerRuleId);

		//2.处理规则业务逻辑,默认即为多菜品打印，所以无需处理
		List<Printer> printersTemp = new ArrayList<>();
		if(goodsPrinterType.equals("1")){
			List<Printer.PrinterDetail> printerDetails = printer.getPrinterDetails();
			for(Printer.PrinterDetail printerDetailTemp:printerDetails){
				Printer printerTemp = new Printer();
				BeanUtils.copyProperties(printer,printerTemp);
				printerTemp.setPrinterDetails(new ArrayList<>());
				printerTemp.getPrinterDetails().add(printerDetailTemp);
				printersTemp.add(printerTemp);
			}
			return printersTemp;
		}else{
			printersTemp.add(printer);
			return printersTemp;
		}
	}

	/**
	 * 处理规则属性方法
	 * @param ruleId
	 */
	private void ruleAttributeHandle(Long ruleId,Long printerRuleId){
		List<PrinterRuleAttribute> printerRuleAttributeList = printerRuleAttributeRepository.queryPrinterRuleAttributeByRuleId(ruleId,printerRuleId);
		if(null != printerRuleAttributeList && printerRuleAttributeList.size() > 0){
			goodsPrinterType = printerRuleAttributeList.get(0).getAttributeValue();
		}
	}

}
