package com.sifang.modules.order.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.order.entity.CrmOrder;
import com.sifang.modules.order.service.CrmOrderDispatchService;
import com.sifang.modules.order.service.OrderService;
import com.sifang.modules.sys.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
 *平台派工服务商
 * @author lzp
 * @version 2019-11-13
 */
@Controller
@RequestMapping(value = "${adminPath}/dispatch")
public class OrderDispatchController extends BaseController{

    @Autowired
    private CrmOrderDispatchService disService;
    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "getOrderSite")
    public String getOrderSite(HttpServletRequest request, Model model){
        String orderId = request.getParameter("orderId");
        String where = request.getParameter("where");
        List<Record> list = disService.getDisOrderSite(orderId);
        model.addAttribute("sites",list);
        model.addAttribute("orderId",orderId);
        model.addAttribute("where",where);
        return "modules/order/platform/disOrderSite";
    }

    /**
     * 转派页面
    */
    @RequestMapping(value = "getturnDisOrderSite")
    public String getturnDisOrderSite(HttpServletRequest request, Model model){
        String orderId = request.getParameter("orderId");
        String where = request.getParameter("where");
        CrmOrder order = orderService.getOrderById(orderId);
        List<Record> list = disService.getDisOrderSite(orderId);
        model.addAttribute("sites",list);
        model.addAttribute("order",order);
        model.addAttribute("where",where);
        return "modules/order/platform/turnDisOrderSite";
    }

    /**
     * 派工服务商 ，根据服务商名称搜索
    */
    @ResponseBody
    @RequestMapping(value = "getOrderConditionSiteList")
    public Object getOrderConditionSiteList(HttpServletRequest request ){
        String siteName = request.getParameter("siteName");
        String orderId = request.getParameter("orderId");
        if(StringUtil.isBlank(orderId)){
            return Result.fail("202","工单ID为空");
        }else if(StringUtil.isBlank(siteName)){
            return Result.fail("202","服务商名称为空");
        }
        return disService.getOrderConditionSiteList(orderId,siteName);
    }



   // 指派工单
    @ResponseBody
    @RequestMapping(value = "dispatchOrderSite")
    public Object dispatchOrderSite(HttpServletRequest request ){
        String orderId = request.getParameter("orderId");
        String siteId = request.getParameter("siteId");
        if(StringUtil.isBlank(orderId)){
            return Result.fail("202","工单ID为空");
        }else if(StringUtil.isBlank(siteId)){
            return Result.fail("204","服务商ID为空");
        }
        return disService.dispatchOrderSite(orderId,siteId);
    }

// 转派工单
    @ResponseBody
    @RequestMapping(value = "turnDispatchOrderSite")
    public Object turnDispatchOrderSite(HttpServletRequest request ){
        String orderId = request.getParameter("orderId");
        String siteId = request.getParameter("siteId");
        String remarks = request.getParameter("remarks");
        if(StringUtil.isBlank(orderId)){
            return Result.fail("202","工单ID为空");
        }else if(StringUtil.isBlank(siteId)){
            return Result.fail("204","服务商ID为空");
        }else if(StringUtil.isBlank(remarks)){
            return Result.fail("203","转派原因为空");
        }
        return disService.turnDispatchOrderSite(orderId,siteId,remarks);
    }


}
