package com.iamberry.rent.handlers.efast;

import cn.jpush.api.utils.StringUtils;
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.ResultMsg;
import com.iamberry.wechat.face.giftCard.AgentMachineService;
import com.iamberry.wechat.face.giftCard.GiftCardService;
import com.iamberry.wechat.face.order.AdminOrderService;
import com.iamberry.wechat.tools.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2017/4/27.
 * @author 刘志伟
 */
@Controller
@RequestMapping("/admin/rent/efast")
public class EfastRentHandler {

    //创建线程池,通过线程池管理线程，此线程池可灵活回收空闲线程，若无可回收，则新建线程
    public static ExecutorService service = Executors.newCachedThreadPool();

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

    /**
     *
     * @param request
     * @param orderId
     * @param type 1:销售订单;2:万元小代理赠送机器;3:万元小代理兑换机器;4:万元小代理奖励机器;5:租赁订单;6:更换滤芯
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/select_efast_order/{orderId}/{type}")
    public ResultMsg selectEfastOrder(HttpServletRequest request,
                                      @PathVariable("orderId") String orderId,
                                      @PathVariable("type") Integer type) throws Exception {
        ResultMsg msg = new ResultMsg();
        Map<String,Object> map = new HashMap<String,Object>();
        StringBuilder id = new StringBuilder();
        boolean flag = false;
        if (!StringUtils.isNotEmpty(orderId) || type == null) {
            msg.setMessage("订单号或类型为空!");
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            return msg;
        }
        //根据类型重组订单号，该订单号只在efast系统中存在
        switch (type) {
            case 1:
                break;
            case 2:
                id.append("ZS1111111_");
                map.put("donateId","ZS11111111" + orderId);
                break;
            case 3:
                id.append("DH00000000");
                break;
            case 4:
                id.append("ZS22222222");
                break;
            case 5:
                break;
            case 6:
                id.append("RF00000000");
                break;
        }
        id.append(orderId);
        map.put("orderId",orderId);
        map.put("id",id.toString());
        map.put("type",type);
        while (!flag) {
            //如果此collection包含指定的元素，则返回 true，如果有则不再插入队列
            if (EfastTask.queues.contains(map)) {
                msg.setStatus(true);
                msg.setResultCode(ResultInfo.SUCCESSCODE);
                msg.setMessage("已存在该同步任务，不需再次插入队列中");
                return msg;
            }
            flag = EfastTask.queues.offer(map);
            //如果参数没有添加到队列中，则该线程停止1s后继续插入队列，直到插入进去为止
            if (!flag) {
                Thread.sleep(1000);
            }
        }
        EfastTask task = new EfastTask(efastRentService,adminOrderService,giftCardService,agentMachineService,rentOrderService,machineService);
        service.submit(task);
        return msg;
    }
}
