package com.sifang.modules.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.entity.CustomerWarranty;
import com.sifang.modules.order.dao.OrdeCallbackDao;
import com.sifang.modules.order.dao.OrderDao;
import com.sifang.modules.order.entity.CrmCustomerWarrantyCard;
import com.sifang.modules.platform.service.WarrantyService;
import com.sifang.modules.sunshine.SystemSetting.utils.CustomException;
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.entity.OrderCallback;
import com.sifang.modules.order.service.OrdeCallbackService;
import com.sifang.modules.order.service.OrderService;
import com.sifang.modules.sys.common.utils.DateUtils;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 工单回访信息
 * @author lzp
 * @version 2019-11-13
 */
@Controller
@RequestMapping(value = "${adminPath}/callback")
public class OrdeCallbackController extends BaseController{
    @Autowired
    private OrdeCallbackService callService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrdeCallbackDao ordeCallbackDao;

    @Autowired
    private WarrantyService warrantyService;
    @Autowired
    private OrderDao orderDao;
    //平台给服务商回访
    @RequestMapping(value = "getSystemCallback")
    public String getCallback(HttpServletRequest request,Model model){
            //获取里程费
        String orderId = request.getParameter("orderId");
        String type = request.getParameter("type");
        model.addAttribute("call", ordeCallbackDao.getOrderCallback(orderId));
        if(StringUtil.isNotBlank(orderId)){
            CrmOrder order = orderService.getOrderById(orderId);
            Map<String ,Object> map = callService.getMileagePayment(order,"2");
            model.addAttribute("map",map);
            model.addAttribute("order",order);
            if(order.getEndTime() !=null && order.getProcessTime() !=null){
                String hour= DateUtils.pastHour(order.getEndTime(),order.getProcessTime());
                model.addAttribute("hour",hour);
            }else{
                model.addAttribute("hour",0);
            }
        }
        model.addAttribute("type",type);
        return "modules/order/platform/platformCallBack";
    }


    /**
     * 平台重新回访页面
    */
    @RequestMapping(value = "anewCallbackOrder")
    public String anewCallbackOrder(HttpServletRequest request, Model model){
        String orderId = request.getParameter("orderId");
        if(StringUtil.isNotBlank(orderId)){
            //查询工单回访信息
            model.addAttribute("call", ordeCallbackDao.getOrderCallback(orderId));
            CrmOrder or= orderService.getOrderById(orderId);
            model.addAttribute("order",or);
            if(or.getEndTime() !=null && or.getProcessTime() !=null){
                String hour= DateUtils.pastHour(or.getEndTime(),or.getProcessTime());
                model.addAttribute("hour",hour);
            }else{
                model.addAttribute("hour",0);
            }
            Map<String ,Object> map = callService.getMileagePayment(or,"1");
            model.addAttribute("map",map);
        }
        return "modules/order/platform/anewCallbackOrder";
    }

    /**
     * 平台回访
    */
    @ResponseBody
    @RequestMapping(value = "CallbackOrder")
    public Object CallbackOrder(HttpServletRequest request, OrderCallback call){
            Map<String,String> map = getParams(request);
            Result result = new Result();
          try {
               return  callService.callbackOrder(call,map);
            }catch (CustomException cue){
                cue.printStackTrace();
                result.setCode(cue.getCode());
            }catch (Exception e){
                result.setCode("404");
                e.printStackTrace();
                throw e;
            }
            return result;
    }




    /**
     * 工单详情中查看回访信息
    */
    @RequestMapping(value = "getCallbackOrder")
    public Object getCallbackOrder(HttpServletRequest request){
        String orderId = request.getParameter("orderId");
        if(StringUtil.isBlank(orderId)){
            return null;
        }
        return callService.getCallbackOrder(orderId);
    }

    /**
     * 重新回访保存
    */
    @ResponseBody
    @RequestMapping(value = "anewCallbackUpdate")
    public Object anewCallbackUpdate(HttpServletRequest request){
        Map<String,String> map = getParams(request);

        return callService.anewCallbackUpdate(map);
    }

    /**
     * 判断工单配件需要返厂的是否已经返厂
    */
    @ResponseBody
    @RequestMapping(value = "getOrderFittingCount")
    public Object getOrderFittingCount(HttpServletRequest request){
        String orderId = request.getParameter("orderId");
        long count =callService.getOrderFittingCount(orderId);

        return count;
    }


        /***
        * 平台生成保修卡页面
        */
        @RequestMapping(value= "buildWarranty")
        public String toBuildWarranty(HttpServletRequest request,Model model){
           String orderId = request.getParameter("orderId");
           String type=request.getParameter("type");
           CrmOrder order = orderDao.get(orderId);
           model.addAttribute("order",order);
           List<Record> list=warrantyService.getFeedBack(orderId);
           model.addAttribute("feedBacks",list);
           return "modules/order/platform/warrantyForm";
        }

        /**
         * 平台查看保修卡页面
         */
        @RequestMapping(value="lookWarranty")
        public String toLookWarranty(HttpServletRequest request,Model model){
            String orderId = request.getParameter("orderId");
            CustomerWarranty warranty=null;
            warranty = warrantyService.getByOrderId(orderId);
            model.addAttribute("order",warranty);
            //查看保修卡
            List<Record> list=warrantyService.getWarranty(orderId);
            model.addAttribute("feedBacks",list);
            return "modules/order/platform/lookForm";
        }

            /**
         * 平台保存生成的保修卡
         */
        @ResponseBody
        @RequestMapping(value="savaWarranty")
        public Result savaWarranty(HttpServletRequest request, HttpServletResponse response, CustomerWarranty warranty){
            String str = request.getParameter("list");
               List<CrmCustomerWarrantyCard> list=JSONObject.parseArray(str, CrmCustomerWarrantyCard.class);
               String orderId=request.getParameter("orderId");
            return warrantyService.saveWarranty(list,orderId,warranty);
        }

}
