package com.iamberry.rent.handlers.efast;

import com.iamberry.rent.face.efast.EfastRentService;
import com.iamberry.rent.face.machine.MachineService;
import com.iamberry.rent.face.order.RentOrderService;
import com.iamberry.wechat.core.entity.giftCard.AgentMachine;
import com.iamberry.wechat.core.entity.giftCard.UserMachineInfo;
import com.iamberry.wechat.core.entity.order.Order;
import com.iamberry.wechat.core.entity.rent.RentChangeLogs;
import com.iamberry.wechat.core.entity.rent.RentOrder;
import com.iamberry.wechat.face.giftCard.AgentMachineService;
import com.iamberry.wechat.face.giftCard.GiftCardService;
import com.iamberry.wechat.face.order.AdminOrderService;

import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created by Administrator on 2017/4/27.
 */
public class EfastTask implements Runnable {

    public static BlockingQueue<Map<String,Object>> queues = new ArrayBlockingQueue<Map<String,Object>>(500);

    private EfastRentService efastRentService;
    private AdminOrderService adminOrderService;
    private GiftCardService giftCardService;
    private AgentMachineService agentMachineService;
    private RentOrderService rentOrderService;
    private MachineService machineService;

    public EfastTask(EfastRentService efastRentService,AdminOrderService adminOrderService,
                     GiftCardService giftCardService,AgentMachineService agentMachineService,
                     RentOrderService rentOrderService,MachineService machineService) {
        this.efastRentService = efastRentService;
        this.adminOrderService = adminOrderService;
        this.giftCardService = giftCardService;
        this.agentMachineService = agentMachineService;
        this.rentOrderService = rentOrderService;
        this.machineService = machineService;
    }

    @Override
    public void run() {
        try {
            //获取队列中的map集合
            Map<String,Object> queuesMap = queues.take();
            int type = (int)queuesMap.get("type");
            String id = (String)queuesMap.get("id");
            String orderId = (String)queuesMap.get("orderId");
            //查询efast系统该订单的状态
            Map<String,Object> map = efastRentService.getEfastSendOrderInfo(id);
            if (!(boolean)map.get("status")) {
                //类型为2可能有两种单号，如果第一种为空，则查询第二种，如果都为空，则直接返回
                if (type == 2) {
                    id = (String)queuesMap.get("donateId");
                    map = efastRentService.getEfastSendOrderInfo(id);
                    if (!(boolean)map.get("status")) {
                        return;
                    }
                }
            }
            switch (type) {
                case 1:
                    Order order = new Order();
                    order.setSalesOrderid(orderId);
                    order.setSalesStatus(5);
                    order.setSalesPostFirm((String)map.get("postFirm"));
                    order.setSalesPostNum((String)map.get("postNum"));
                    order.setOldStatus(2);
                    adminOrderService.updateOrderDeliver(order);
                    break;
                case 2:
                    AgentMachine agentMachineInfo = new AgentMachine();
                    agentMachineInfo.setAgentMachineId(Integer.parseInt(orderId));
                    agentMachineInfo.setAgentMachinePostName((String)map.get("postFirm"));
                    agentMachineInfo.setAgentMachinePostNum((String)map.get("postNum"));
                    agentMachineInfo.setAgentMachineStatus(3);
                    agentMachineService.updateAgentMachine(agentMachineInfo);
                    break;
                case 3:
                    UserMachineInfo userMachine = new UserMachineInfo();
                    userMachine.setUserMachineId(Integer.parseInt(orderId));
                    userMachine.setUserMachinePostName((String)map.get("postFirm"));
                    userMachine.setUserMachinePostNum((String)map.get("postNum"));
                    userMachine.setUserMachineStatus(2);
                    giftCardService.update(userMachine);
                    break;
                case 4:
                    AgentMachine agentMachine = new AgentMachine();
                    agentMachine.setAgentMachineId(Integer.parseInt(orderId));
                    agentMachine.setAgentMachinePostName((String)map.get("postFirm"));
                    agentMachine.setAgentMachinePostNum((String)map.get("postNum"));
                    agentMachine.setAgentMachineStatus(3);
                    agentMachineService.updateAgentMachine(agentMachine);
                    break;
                case 5:
                    RentOrder rentOrder = new RentOrder();
                    rentOrder.setRentOrderId(orderId);
                    rentOrder.setRentPostFirm((String)map.get("postFirm"));
                    rentOrder.setRentPostNum((String)map.get("postNum"));
                    rentOrder.setRentStatus(5);
                    rentOrder.setRentOldStatus(2);
                    rentOrderService.deliverOrderByOrderId(rentOrder);
                    break;
                case 6:
                    RentChangeLogs logs = new RentChangeLogs();
                    logs.setLogsStatus(4);
                    logs.setLogsPostFirm((String)map.get("postFirm"));
                    logs.setLogsPostNum((String)map.get("postNum"));
                    logs.setLogsId(Integer.parseInt(orderId));
                    machineService.updateChangeLogsById(logs);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
