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.platform.utils.PlantformUtils;
import com.sifang.modules.order.entity.CrmOrder;
import com.sifang.modules.order.service.CrmOrderDispatchService;
import com.sifang.modules.order.service.OrderFeedbackService;
import com.sifang.modules.order.service.OrderService;
import com.sifang.modules.order.utils.OrderUtils;
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}/feedback")
public class OrderFeedbackController extends BaseController{

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


    /**
     * 过程反馈
    */
    @ResponseBody
    @RequestMapping(value = "processFeedback")
    public Object processFeedback(HttpServletRequest request ){
        String orderId = request.getParameter("orderId");//工单id
        String textarea = request.getParameter("textarea");//反馈内容
        String imgs = request.getParameter("imgs");//反馈图片
        String serviceMeasures = request.getParameter("serviceMeasures");
        if(StringUtil.isBlank(orderId)){
            return Result.fail("202","工单ID为空");
        }else if(StringUtil.isBlank(textarea)){
            return Result.fail("204","反馈内容为空");
        }
        return feedbackService.processFeedback(orderId,textarea,imgs,serviceMeasures);
    }

    /**
     * 反馈封单完成
    */
    @ResponseBody
    @RequestMapping(value = "feedbackSealingSheet")
    public Object feedbackSealingSheet(HttpServletRequest request ){
        String orderId = request.getParameter("orderId");
        String textarea = request.getParameter("textarea");
        String imgs = request.getParameter("imgs");
        String serviceMeasures = request.getParameter("serviceMeasures");
        if(StringUtil.isBlank(orderId)){
            return Result.fail("202","工单ID为空");
        }else if(StringUtil.isBlank(textarea)){
            return Result.fail("204","反馈内容为空");
        }else if(StringUtil.isBlank(serviceMeasures)){
            return Result.fail("203","请选择服务措施");
        }
        List<Record> fittingStatus = PlantformUtils.getFittingApply(orderId);
        if(fittingStatus.size()>0){
            return Result.fail("205","备件申请状态为已完成时才能执行服务完成操作!");
        }
        return feedbackService.feedbackSealingSheet(orderId,textarea,imgs,serviceMeasures);
    }

    /**
     * 平台端和厂家端已完成工单可以进行反馈------反馈封单完成
     */
    @ResponseBody
    @RequestMapping(value = "feedbackSealingSheetYwc")
    public Object feedbackSealingSheetYwc(HttpServletRequest request ){
        String orderId = request.getParameter("orderId");
        String textarea = request.getParameter("textarea");
        String imgs = request.getParameter("imgs");
        if(StringUtil.isBlank(orderId)){
            return Result.fail("202","工单ID为空");
        }else if(StringUtil.isBlank(textarea)){
            return Result.fail("204","反馈内容为空");
        }
        List<Record> fittingStatus = PlantformUtils.getFittingApply(orderId);
        if(fittingStatus.size()>0){
            return Result.fail("205","备件申请状态为已完成时才能执行服务完成操作!");
        }
        return feedbackService.feedbackSealingSheetYwc(orderId,textarea,imgs);
    }

    @RequestMapping(value = "againPlatFormFeedback")
    public String platFormFeedback(HttpServletRequest request,Model model){
        String orderId = request.getParameter("orderId");
        if(StringUtil.isNotBlank(orderId)){
            CrmOrder or= orderService.getOrderById(orderId);
            //获取服务措施
            List<Record> list = OrderUtils.getOrderServiceMeasures(or.getServiceType());
            model.addAttribute("rds",list);
            model.addAttribute("order",or);
        }
        return "modules/order/platform/againFeedbackOrder";
    }



}
