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


import com.alibaba.fastjson.JSON;
import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.dto.printer.redis.PrinterTaskBean;
import com.neusoft.smart.pos.framework.utils.DateUtils;
import com.neusoft.smart.pos.printer.persistent.ext.domain.Printer;
import com.neusoft.smart.pos.printer.service.bean.OrderBean;
import org.springframework.stereotype.Component;

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

/**
 * @author zcf
 * @date 2018-10-19 10：43
 * @description 后厨打印模板实现使用Repository
 */
@Component("kitchenTemplateRepository")
public class KitchenTemplateRepository implements IPrinterTemplateRepository {

	/**
	 * 执行打印模板方法
	 * @param orgId
	 * @param shopId
	 * @return
	 */
	public PrinterTaskBean executeTemplate(Integer orgId, Integer shopId, Printer printer) {
		PrinterTaskBean printerTaskBean = new PrinterTaskBean();
		//1.整理打印任务对象数据
		printerTaskBean.setPrinterId(printer.getPrinterId().intValue());
		printerTaskBean.setPrinterType(DictionaryConstant.PRINTER_PRINTER_TYPE_1.getCode());
		printerTaskBean.setOrderNumber(printer.getOrderNumber());
		printerTaskBean.setOrderType(DictionaryConstant.PRINTER_ORDER_TYPE_1.getCode());
		printerTaskBean.setTaskStatus(DictionaryConstant.PRINTER_TASK_STATUS_1.getCode());
		
		//2.整理打印订单对象数据
		OrderBean orderBean = getPrinterOrderHandle(printer);
		printerTaskBean.setPrinterJson(JSON.toJSONString(orderBean));
		
		return printerTaskBean;
	}
	
	/**
	 * 处理打印订单方法
	 * @param printer
	 * @return
	 */
	private OrderBean getPrinterOrderHandle(Printer printer){
		OrderBean orderBean = new OrderBean();
		//1.1 整理打印数据
		orderBean.setOrderNumber(printer.getOrderNumber());
		orderBean.setLocationNo(printer.getLocationNo());
		orderBean.setOrderAmount(printer.getOrderAmount());
		orderBean.setRealPayAmount(printer.getRealPayAmount());
		orderBean.setDiscountAmount(printer.getDiscountAmount());
		orderBean.setOrderStatus(printer.getOrderStatus());
		orderBean.setOrderStatusName(printer.getOrderStatusName());
		orderBean.setOrderType(printer.getOrderType());
		orderBean.setPaidTime(DateUtils.parse(printer.getPaidTime()).getTime());
		
		//1.2 整理打印明细数据
		List<Printer.PrinterDetail> printerDetailList = printer.getPrinterDetails();
		List<OrderBean.OrderDetailBean> orderDetailBeanList = new ArrayList<>();
		printerDetailList.forEach(printerDetail -> {
			OrderBean.OrderDetailBean OrderDetailBean = new OrderBean.OrderDetailBean();
			OrderDetailBean.setItemName(printerDetail.getItemName());
			OrderDetailBean.setUnitPrice(printerDetail.getUnitPrice());
			OrderDetailBean.setQuantity(printerDetail.getQuantity());
			OrderDetailBean.setDiscountAmount(printerDetail.getDiscountAmount());
			OrderDetailBean.setFinalPrice(printerDetail.getFinalPrice());
			OrderDetailBean.setCategoryName(printerDetail.getCategoryName());
			OrderDetailBean.setCategoryNumber(printerDetail.getCategoryNumber());
			OrderDetailBean.setSelectedFlavors(printerDetail.getSelectedFlavors());
			OrderDetailBean.setRemark(printerDetail.getRemark());
			orderDetailBeanList.add(OrderDetailBean);
		});
		orderBean.setOrderDetails(orderDetailBeanList);
		return orderBean;
	}

}
