package com.nmgs.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nmgs.config.ResultData;
import com.nmgs.service.AppointmentInfoService;
import com.nmgs.service.IntegralLevelMessageService;
import com.nmgs.util.LogUtil;
import com.nmgs.util.PubTools;
import com.nmgs.util.WebsocketUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value="reservateManager")
@Api(tags = "车辆预约")
public class AppointmentInfoController {
    public AppointmentInfoService appointmentInfoService;
    @Autowired
    public void setAppointmentInfoService(AppointmentInfoService appointmentInfoServiceTT) {
        this.appointmentInfoService = appointmentInfoServiceTT;
    }
    @RequestMapping(value = "getStaList",
            method = {RequestMethod.POST}
    )
    @ApiOperation(httpMethod="POST",value="查询线路信息")
    public ResultData<List> getStaList(){
        List integrationList = this.appointmentInfoService.getStaList();
        if(integrationList != null){
            return ResultData.success("查询成功",integrationList,integrationList.size());
        }else{
            return ResultData.fail("查询失败");
        }
    }
    @RequestMapping(value = "getList",
            method = {RequestMethod.POST}
    )
    @ApiOperation(httpMethod="POST",value="查询用户预约信息")
    public ResultData<List> getList(@RequestParam(value = "userId") String userId,
                                    @RequestParam(value = "appointmentType") String appointmentType,
                                    @RequestParam(value = "orderScope",required = false) String orderScope,
                                    @RequestParam(value = "pageSize",required = false) String pageSize,
                                    @RequestParam(value = "currentPage",required = false) String currentPage,
                                    @RequestParam(value = "id",required = false) String id,
                                    @RequestParam(value = "orderId",required = false) String orderId){
        int start=0;
        int end=1000000000;
        List integrationList =null;
        List totalList =null;
        if(!PubTools.isNull(pageSize) && !PubTools.isNull(currentPage)){
            start=(Integer.parseInt(currentPage)-1)* Integer.parseInt(pageSize)+1;
            end=Integer.parseInt(currentPage)*Integer.parseInt(pageSize);
            integrationList = this.appointmentInfoService.getList(id,orderId, userId,appointmentType,orderScope,start,end);
            totalList = this.appointmentInfoService.getList(id,orderId, userId,appointmentType,orderScope,0,0);
        }else{
            integrationList = this.appointmentInfoService.getList(id,orderId, userId,appointmentType,orderScope,start,end);
            totalList=integrationList;
        }
        if(integrationList != null){
            return ResultData.success("查询成功",integrationList,totalList.size());
        }else{
            return ResultData.fail("查询失败");
        }
    }

    @RequestMapping(value = "commit",
            method = {RequestMethod.POST}
    )
    @ApiOperation(httpMethod="POST",value="提交预约信息")
    public ResultData<String> commit(@RequestParam(value = "userId") String userId,
                                   @RequestParam(value = "appointmentType") String appointmentType,
                                   @RequestParam(value = "dateTime",required = true) String dateTime,
                                   @RequestParam(value = "appointmentId",required = false) String appointmentId,
                                   @RequestParam(value = "mealId",required = false) String mealId,
                                   @RequestParam(value = "mealIdCount",required = false) String mealIdCount,
                                   @RequestParam(value = "mealTotalMoney",required = false) String mealTotalMoney,
                                   @RequestParam(value = "serviceId",required = false) String serviceId,
                                   @RequestParam(value = "operatorId",required = false) String operatorId,
                                   @RequestParam(value = "aCarNo",required = false) String aCarNo){
        if(PubTools.isNull(appointmentId)){
            appointmentId="";
        }
        if(PubTools.isNull(aCarNo)){
            aCarNo="";
        }
        if(PubTools.isNull(serviceId)){
            serviceId="";
        }
        if(PubTools.isNull(mealId)){
            mealId="";
        }
        if(PubTools.isNull(mealIdCount)){
            mealIdCount="";
        }
        if(PubTools.isNull(operatorId)){
            operatorId="";
        }
        if(PubTools.isNull(mealTotalMoney)){
            mealTotalMoney="";
        }
        LogUtil.WriteLog_Info("预约提交开始执行======>"+PubTools.getCurrentDate(),"QcodeInfoController");
        JSONObject retStrJSON = this.appointmentInfoService.commit(userId, appointmentType,appointmentId,aCarNo,serviceId,operatorId,mealId,mealIdCount,mealTotalMoney,dateTime);
        String retStr= retStrJSON.get("ret").toString();
        LogUtil.WriteLog_Info("预约提交结果==="+PubTools.getCurrentDate()+"==="+retStrJSON.toString(),"QcodeInfoController");
        JSONArray ja=new JSONArray();
        ja.add(retStrJSON);
        if("1".equals(retStr)){
            String orderId=retStrJSON.get("ORDERID").toString();
            return ResultData.success("预约成功",orderId,1);
        }else if("2".equals(retStr)){
            return ResultData.success(2,"用户不存在");
        }else if("3".equals(retStr)){
            return ResultData.fail(3,"用户可用积分不足",ja.toString());
        }else if("8".equals(retStr)){
            return ResultData.success(8,"用户已被冻结");
        }else if("-1".equals(retStr)){
            return ResultData.fail("接口出错了");
        }
        return ResultData.success(1,"预约成功");
    }

    @RequestMapping(value = "getAppointListByServiceId",
            method = {RequestMethod.POST}
    )
    @ApiOperation(httpMethod="POST",value="服务区预约信息查询")
    public ResultData<List> getAppointListByServiceId(@RequestParam(value = "serviceAreaId",required = false) String serviceAreaId,
                                   @RequestParam(value = "beginDate",required = false) String beginDate,
                                   @RequestParam(value = "operatorId",required = false) String operatorId,
                                   @RequestParam(value = "endDate",required = false) String endDate,
                                   @RequestParam(value = "appointmentType") String appointmentType,
                                                      @RequestParam(value = "completeStatus", required = false) String completeStatus,
                                  @RequestParam(value = "isCurrentDateAfter") String isCurrentDateAfter,
                                   @RequestParam(value = "refreshDate",required = false) String refreshDate){
        List ret = this.appointmentInfoService.getAppointListByServiceId(serviceAreaId, operatorId, beginDate, endDate, appointmentType, isCurrentDateAfter, refreshDate, completeStatus);
        if(ret ==null){
            return ResultData.fail("查询失败");
        }else{
            return ResultData.success("查询成功",ret,ret.size());
        }
    }

    @RequestMapping(value = "getQcodeInfoById",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="根据预约记录ID获取二维码内容",httpMethod = "POST")
    public ResultData<String> getQcodeInfoByUserId(@RequestParam("openId") String openId,@RequestParam("integrateType") String integrateType,@RequestParam("appointId") String appointId){
        String qcodeInfoByUserId = appointmentInfoService.getQcodeInfoById(openId, integrateType, appointId);
        if("-2".equals(qcodeInfoByUserId)){
            return ResultData.success(2,"用户未找到");
        }else if("-3".equals(qcodeInfoByUserId)){
            return ResultData.success(3,"用户已被冻结");
        }else{
            return ResultData.success("生成二维码成功",qcodeInfoByUserId,1);
        }
    }
}
