package com.iamberry.wechat.core.entity;

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

import org.springframework.stereotype.Component;

import com.iamberry.app.tool.des.MD5;
import com.iamberry.wechat.core.entity.order.OrderOperation;

@Component
public class ProbationOrderUtil {
	
	// 订单状态:下标对应数据库中记录状态, 此用于请求操作时
			public final String[] orderStatusStrList = 
				{
					"quitOrder", "staySendOrder", "sendOrder", "stayPayOrder",
					"probationPause", "paySuccess"
				};
			
			public String[] getOrderStatusStrList() {
				return orderStatusStrList;
			}
			
			// 订单状态文字，显示给用户看
			private final String[] orderStatusView = 
				{
					"取消订单", "待派送", "派送中", "待支付","试用暂停", "支付完成"
				};
			
			public String[] getOrderStatusView() {
				return orderStatusView;
			}
			
			// 订单操作状态
			public final String [] orderActin = 
				{
					"quitOrderByOrderId", 			// 取消订单操作
					"getProbationOrderByOrderId",	// 查看订单详情操作
					"sendOrder", 				// 派送中
					"remark", 					// 订单备注
					"stayPayOrder",		//待支付
					"probationPause",		//试用暂停
					"paySuccess",			//支付完成
					"addCourierInfo",		//录入派送员信息
					"delayOrder",				//订单延时
					"addGiftInfo"				//录入礼品信息
				};
			
			public String[] getOrderActin() {
				return orderActin;
			}
			
			// 加密
			public final MD5 md5 = new MD5("orderact");
			
			// 安全的随机数
			private AdminUtils adminUtils;
			public void setAdminUtils(AdminUtils adminUtils) {
				this.adminUtils = adminUtils;
			}
			
			/**
			 * 根据订单因为标识，获取订单的数字标识
			 * @param status
			 * @return
			 */
			public Integer getOrderStatusByStr(String status) {
				for(int i = 0; i < orderStatusStrList.length; i++) {
					if (orderStatusStrList[i].equals(status)) {
						return i;
					}
				}
				return 2;
			}
			
			/**
			 * 转换订单类型为字母表示
			 * @param orderStatus
			 * @return
			 */
			public String orderStatusIntToOrderStatusString(Integer orderStatus) {
				if (orderStatus == null || orderStatus >= orderStatusStrList.length) {
					return "订单状态有误";
				}
				if (orderStatus == -1) {
					return "allOrder";
				}
				return orderStatusStrList[orderStatus];
			}
			
			/**
			 * 订单状态修改为文字
			 * @param status
			 * @return
			 */
			public String orderStatusToString(Integer status) {
				
				if (status == null || status >= orderStatusView.length) {
					return "订单状态有误";
				}
				
				if (status == -1) {
					return "所有订单";
				}
				
				return orderStatusView[status];
			}
			
		/**
		 * 后台：根据订单状态，获取订单的操作
		 * @param status
		 * @return
		 * @throws Exception 
		 */
		public List<OrderOperation> orderOperationByStatus(Integer status, String orderId) throws Exception {
			if (status == null) {
				return null;
			}
			// 返回的操作
			List<OrderOperation> operations = new ArrayList<OrderOperation>();
			// 每个订单默认有查看详情和备注的功能权限
			OrderOperation orderDesc = new OrderOperation("&#xe60c;", "查看订单详情",  "/admin/probationOrder/" + orderActin[1] + "/" + orderId);
			operations.add(orderDesc);// 订单详情
			OrderOperation orderRemank = new OrderOperation("&#xe6de;", "备注",  "/admin/probationOrder/" + orderActin[3] + "/" + orderId);
			operations.add(orderRemank);// 订单备注
			// 根据状态分发权限
			switch (status) {
				case 0:
					return operations;
				case 1: // 待派送
					
					OrderOperation operation1 = new OrderOperation("&#xe60c;", "关闭订单" ,  "/admin/probationOrder/" + orderActin[0] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=quitOrder&osid=" + md5.encrypt(orderId + "_" + orderActin[0] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
					operations.add(operation1);
					OrderOperation sendOrder = new OrderOperation("&#xe60c;", "确认派送" ,  "/admin/probationOrder/" + orderActin[2] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=sendOrder&osid=" + md5.encrypt(orderId + "_" + orderActin[2] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
					operations.add(sendOrder);
					return operations;
				case 2: // 派送中
					OrderOperation cancelOperation = new OrderOperation("&#xe60c;", "关闭订单" ,  "/admin/probationOrder/" + orderActin[0] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=quitOrder&osid=" + md5.encrypt(orderId + "_" + orderActin[0] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
					operations.add(cancelOperation);
					OrderOperation addCourierInfo = new OrderOperation("&#xe60c;", "录入派送员信息" ,  "/admin/probationOrder/" + orderActin[7] + "/" + orderId);
					operations.add(addCourierInfo);
					OrderOperation stayPayOrder = new OrderOperation("&#xe60c;", "派送完成，进入试用环节" ,  "/admin/probationOrder/" + orderActin[4] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=stayPayOrder&osid=" + md5.encrypt(orderId + "_" + orderActin[4] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
					operations.add(stayPayOrder);
					return operations;
				case 3: // 待支付
					OrderOperation cancelOperation3 = new OrderOperation("&#xe60c;", "关闭订单" ,  "/admin/probationOrder/" + orderActin[0] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=quitOrder&osid=" + md5.encrypt(orderId + "_" + orderActin[0] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
					operations.add(cancelOperation3);
					OrderOperation probationPause = new OrderOperation("&#xe60c;", "试用暂停" , "/admin/probationOrder/" + orderActin[5] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=probationPause&osid=" + md5.encrypt(orderId + "_" + orderActin[5] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
					operations.add(probationPause);
					OrderOperation delayOrder = new OrderOperation("&#xe6de;", "订单延时" , "/admin/probationOrder/" + orderActin[8] + "/" +orderId);
					operations.add(delayOrder);
					return operations;
				case 4: // 试用暂停
					OrderOperation operation4 = new OrderOperation("&#xe60c;", "暂停结束，开始试用" ,  "/admin/probationOrder/" + orderActin[4] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=stayPayOrder&osid=" + md5.encrypt(orderId + "_" + orderActin[4] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
					operations.add(operation4);
					return operations;
				case 5: // 支付完成
					return operations;
				default:
					break;
			}
			return operations;
		}
}
