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 OrderUtil {

	
		// 订单状态:下标对应数据库中记录状态, 此用于请求操作时
		public final String[] orderStatusStrList = 
			{
				"quitOrder", "etcOrder", "paySuccess", "returnOrder",
				"returnOrderSuccess", "sendOrder", "returnOrderEtc", "returnOrderEtcAdmin", "returnOrderThe",
				"theOrderSuccess", "theOrderSuccessAdmin", "inOrder", "inFinish"
			};
		
		// 订单状态文字，显示给用户看
		public final String[] orderStatusView =
			{
				"取消订单", "待支付", "已支付、待发货", "退款申请中",
				"已退款", "已发货", "申请退货中", "退货中", "订单退货，订单关闭" ,
				"换货中", "换货完成", "申请换货中", "已完成"
			};
		
		// 订单操作状态
		private final String [] orderActin =
			{
				"quitOrderAction", 			// 取消订单操作
				"getShopOrderByOrderId",	// 查看订单详情操作
				"sendOrder", 				// 发货
				"remark", 					// 订单备注
				"rejectedRefund", 			// 驳回退款
				"agreedRefund", 			// 同意退款
				"rejectedOrder", 			// 同意退货，等等收取货物
				"agreedOrder", 				// 确认退货
				"successInOrder", 			// 同意换货
				"insertPostInfoAndSend", 	// 录入物流信息并发货
				"rejectedReturn",			//拒绝退货
				"rejectedInOrder"			//拒绝换货
			};

		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> orderStatusToOperation(Integer status, String orderId) throws Exception {
		if (status == null) {
			return null;
		}
		// 返回的操作
		List<OrderOperation> operations = new ArrayList<OrderOperation>();
		// 每个订单默认有查看详情和备注的功能权限
		OrderOperation orderDesc = new OrderOperation("&#xe60c;", "查看订单详情",  "/admin/order/" + orderActin[1] + "/" + orderId);// + "&orderStatus=" + orderStatusStrList[status]
		operations.add(orderDesc);// 订单详情
		OrderOperation orderRemank = new OrderOperation("&#xe6de;", "备注",  "/admin/order/" + orderActin[3] + "/" + orderId);
		operations.add(orderRemank);// 订单备注
		// 根据状态分发权限
		switch (status) {
			case 0:
				return operations;
			case 1: // 待支付
				
				OrderOperation operation1 = new OrderOperation("&#xe60c;", "取消订单" ,  "/admin/order/" + orderActin[0] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=quitOrder&osid=" + md5.encrypt(orderId + "_" + orderActin[0] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
				operations.add(operation1);
			case 2: // 已支付，待发货
				
				OrderOperation operation3 = new OrderOperation("&#xe60c;", "发货" ,  "/admin/order/" + orderActin[2] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=sendOrder&osid=" + md5.encrypt(orderId + "_" + orderActin[2] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
				operations.add(operation3);
				return operations;
			case 3: // 申请退款中
				
				OrderOperation returnPay = new OrderOperation("&#xe60c;", "驳回退款" , "/admin/order/" + orderActin[4] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=paySuccess&osid=" + md5.encrypt(orderId + "_" + orderActin[4] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
				operations.add(returnPay);
				OrderOperation agreedRefund = new OrderOperation("&#xe60c;", "确认退款" , "/admin/order/" + orderActin[5] + "/" +orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=returnOrderSuccess&osid=" + md5.encrypt(orderId + "_" + orderActin[5] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
				operations.add(agreedRefund);
				return operations;
			case 4: // 无操作权限
				
				return operations;
			case 5: // 无操作权限
				
				return operations;
			case 6: // 申请退货中
				
				OrderOperation ruturnOrderError = new OrderOperation("&#xe60c;", "同意退货，等待收货" , "/admin/order/" + orderActin[6] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=returnOrderEtcAdmin&osid=" + md5.encrypt(orderId + "_" + orderActin[6] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
				operations.add(ruturnOrderError);
				OrderOperation rejectedReturn = new OrderOperation("&#xe60c;", "驳回退货" , "/admin/order/" + orderActin[10] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=sendOrder&osid=" + md5.encrypt(orderId + "_" + orderActin[10] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
				operations.add(rejectedReturn);
				return operations;
			case 7: // 退货中
				
				OrderOperation quitOrderSuccess = new OrderOperation("&#xe60c;", "确认退货，已经收到货物" , "/admin/order/" + orderActin[7] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=returnOrderThe&osid=" + md5.encrypt(orderId + "_" + orderActin[7] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
				operations.add(quitOrderSuccess);
				return operations;
			case 8: // 无操作权限
				
				return operations;
			case 9: // 换货中
				
				OrderOperation inOrderSuccess = new OrderOperation("&#xe60c;", "录入物流信息并发货" ,"/admin/order/" + orderActin[9] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=sendOrder&osid=" + md5.encrypt(orderId + "_" + orderActin[9] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
				operations.add(inOrderSuccess);
				return operations;
			case 11: // 申请换货
				
				OrderOperation inOrder = new OrderOperation("&#xe60c;", "同意换货" , "/admin/order/" + orderActin[8] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=theOrderSuccess&osid=" + md5.encrypt(orderId + "_" + orderActin[8] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
				operations.add(inOrder);
				OrderOperation rejectedInOrder = new OrderOperation("&#xe60c;", "驳回换货" , "/admin/order/" + orderActin[11] + "/" + orderId + "?orderStatus=" + orderStatusStrList[status] + "&toOrderStatus=sendOrder&osid=" + md5.encrypt(orderId + "_" + orderActin[11] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
				operations.add(rejectedInOrder);
				return operations;
				
			case 12://已完成
				
				return operations;
			default:
				break;
		}
		return operations;
	}
}
