package com.iamberry.wechat.core.entity;

import com.iamberry.app.tool.des.MD5;
import com.iamberry.wechat.core.entity.order.OrderOperation;
import org.springframework.stereotype.Component;

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

/**
 * Created by Administrator on 2017/4/24.
 */
@Component
public class RentFilterUtil {
    // 滤芯状态:下标对应数据库中记录状态, 此用于请求操作时
    public final String[] filterStatusStrList =
            {
                    "quitFilter", "inFilter", "theFilterSuccess", "rejectFilter", "sendFilter", "inFinish"
            };

    public String[] getfilterStatusStrList() {
        return filterStatusStrList;
    }

    // 滤芯状态文字，显示给用户看
    public final String[] filterStatusView =
            {
                    "取消更换", "更换申请中", "更换中", "驳回", "已发货", "已完成"
            };

    // 滤芯操作状态
    private final String [] filterActin =
            {
                    "quitFilterAction", 	    // 取消更换滤芯操作
                    "getFilterById",	        // 查看更换滤芯详情操作
                    "sendFilter", 				// 发货
                    "filterRemark", 			// 更换备注
                    "successInFilter", 			// 同意换货
                    "savePostInfoAndSend", 	    // 录入物流信息并发货
                    "rejectedInFilter",			//拒绝换货
                    "select_efast_order"         //同步百胜订单状态
            };

    public String[] getFilterActin() {
        return filterActin;
    }

    // 加密
    public final MD5 md5 = new MD5("orderact");

    // 安全的随机数
    private AdminUtils adminUtils;
    public void setAdminUtils(AdminUtils adminUtils) {
        this.adminUtils = adminUtils;
    }

    /**
     * 根据订单因为标识，获取订单的数字标识
     * @param status
     * @return
     */
    public Integer getFilterStatusByStr(String status) {
        for(int i = 0; i < filterStatusStrList.length; i++) {
            if (filterStatusStrList[i].equals(status)) {
                return i;
            }
        }
        return 2;
    }

    /**
     * 转换订单类型为字母表示
     * @param filterStatus
     * @return
     */
    public String filterStatusIntToOrderStatusString(Integer filterStatus) {
        if (filterStatus == null || filterStatus >= filterStatusStrList.length) {
            return "滤芯状态有误";
        }
        if (filterStatus == -1) {
            return "allFilter";
        }
        return filterStatusStrList[filterStatus];
    }

    /**
     * 订单状态修改为文字
     * @param status
     * @return
     */
    public String filterStatusToString(Integer status) {

        if (status == null || status >= filterStatusView.length) {
            return "滤芯状态有误";
        }

        if (status == -1) {
            return "所有滤芯";
        }

        return filterStatusView[status];
    }

    /**
     * 后台：根据订单状态，获取订单的操作
     * @param status
     * @return
     * @throws Exception
     */
    public List<OrderOperation> filterStatusToOperation(Integer status, Integer filterId) throws Exception {
        if (status == null) {
            return null;
        }
        // 返回的操作
        List<OrderOperation> operations = new ArrayList<OrderOperation>();
        // 每个订单默认有查看详情和备注的功能权限
        OrderOperation orderDesc = new OrderOperation("&#xe60c;", "查看订单详情",  "/admin/admin_machine/" + filterActin[1] + "/" + filterId);// + "&orderStatus=" + filterStatusStrList[status]
        operations.add(orderDesc);// 订单详情
        OrderOperation orderSync = new OrderOperation("&#xe60c;", "同步订单状态",  "/admin/rent/efast/" + filterActin[7] + "/" + filterId + "/6");
        operations.add(orderSync);// 订单同步
        /*OrderOperation orderRemank = new OrderOperation("&#xe6de;", "备注",  "/admin/admin_machine/" + filterActin[3] + "/" + filterId);
        operations.add(orderRemank);// 订单备注*/
        // 根据状态分发权限
        switch (status) {
            case 0:
                return operations;
            case 1: // 更换申请中

                OrderOperation inOrder = new OrderOperation("&#xe60c;", "同意换货" , "/admin/admin_machine/" + filterActin[4] + "/" + filterId + "?orderStatus=" + filterStatusStrList[status] + "&toOrderStatus=theFilterSuccess&osid=" + md5.encrypt(filterId + "_" + filterActin[4] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
                operations.add(inOrder);
                OrderOperation operation1 = new OrderOperation("&#xe60c;", "取消订单" ,  "/admin/admin_machine/" + filterActin[0] + "/" + filterId + "?orderStatus=" + filterStatusStrList[status] + "&toOrderStatus=quitFilter&osid=" + md5.encrypt(filterId + "_" + filterActin[0] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
                operations.add(operation1);
                //OrderOperation rejectedInOrder = new OrderOperation("&#xe60c;", "驳回换货" , "/admin/admin_machine/" + filterActin[6] + "/" + filterId + "?orderStatus=" + filterStatusStrList[status] + "&toOrderStatus=sendOrder&osid=" + md5.encrypt(filterId + "_" + filterActin[6] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
                //operations.add(rejectedInOrder);
                return operations;
            case 2: // 更换中

                OrderOperation inOrderSuccess = new OrderOperation("&#xe60c;", "录入物流信息并发货" ,"/admin/admin_machine/" + filterActin[2] + "/" + filterId + "?orderStatus=" + filterStatusStrList[status] + "&toOrderStatus=sendFilter&osid=" + md5.encrypt(filterId + "_" + filterActin[2] + "-" + adminUtils.byte2hex(adminUtils.createRandomByte())));
                operations.add(inOrderSuccess);
                return operations;
            case 3: // 更换驳回

                return operations;
            case 4: // 无操作权限

                return operations;
            default:
                break;
        }
        return operations;
    }
}
