package com.nmgs.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nmgs.config.ResultData;
import com.nmgs.entity.Consistant;
import com.nmgs.service.UserTableService;
import com.nmgs.service.UserVehicleService;
import com.nmgs.util.LogUtil;
import com.nmgs.util.PubTools;
import com.nmgs.util.WebsocketUtil;
import com.nmgs.util.redisConfigUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("userVehicle")
@Api(tags="用户车辆信息")
public class UserVehicleController {

    public UserVehicleService userVehicleService;
    @Autowired
    public void setUserVehicleService(UserVehicleService userVehicleServiceT) {
        this.userVehicleService = userVehicleServiceT;
    }

    /**
     * 根据用户id 获取已经绑定的车牌
     * @param userId  用户id
     * @param certification 认证状态
     * @return
     */
    @RequestMapping(value = "getUserVehicleByUserId",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="根据用户id查询车牌",httpMethod = "POST")
    public ResultData<List> getUserVehicleByUserId(@RequestParam("userId")  String userId,
                                                   @RequestParam(value = "certification",required = false) String certification){
        LogUtil.WriteLog_Info("根据用户id查询车牌===userId====>"+userId+"=====certification===="+certification+"==成功："+ PubTools.getCurrentDate(),"UserVehicleController");
        List userVehicleByUserId = this.userVehicleService.getUserVehicleByUserId(userId, certification);
        if(userVehicleByUserId != null){
            return ResultData.success("查询成功",userVehicleByUserId,userVehicleByUserId.size());
        }else{
            return ResultData.fail("接口查询出错");
        }
    }

    /**
     * 批量添加用户车牌
     * @return
     */
    @RequestMapping(value = "addUserVehicle",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="新增用户车牌",httpMethod = "POST")
    public ResultData<String> addUserVehicle(@RequestParam("jsonArray") String jsonArray){
        LogUtil.WriteLog_Info("批量插入用户车牌信息===userId====>"+jsonArray.toString(),"UserVehicleController");
        int ret = this.userVehicleService.addUserVehicle(JSONArray.parseArray(jsonArray));
        if(ret >0){
            if(ret==2){
                return ResultData.success(2,"车辆信息已经存在");
            }else  if(ret==3){
                return ResultData.success(3,"车辆在本月已解绑，下个月才可以继续绑定");
            }else  if(ret==4){
                return ResultData.success(4,"每个人认证的车辆数不能超过3辆");
            }else {
                return ResultData.success(1,"新增成功");
            }

        }else{
            return ResultData.fail("接口查询出错");
        }
    }

    /**
     * 车牌取消绑定
     * @param userId
     * @param aCarNo
     * @return
     */
    @RequestMapping(value = "cancelUserVehicle",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="解除车牌绑定",httpMethod = "POST")
    public ResultData<String> cancelUserVehicle(@RequestParam("userId") String userId,@RequestParam("aCarNo") String aCarNo){
        LogUtil.WriteLog_Info("解除用户车牌信息===userId====>"+userId+"====aCarNo======="+aCarNo,"UserVehicleController");
        int ret = this.userVehicleService.cancelUserVehicle(userId,aCarNo);
        if(ret >-1){
            return ResultData.success("解除成功");
        }else{
            return ResultData.fail("车牌解除出错");
        }
    }
    /**
     * 查询车辆详细信息
     * @param userId
     * @param aCarNo
     * @return
     */
    @RequestMapping(value = "getUserVehicle",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="查询车辆详情",httpMethod = "POST")
    public ResultData<List> getUserVehicle(@RequestParam("userId") String userId,@RequestParam("aCarNo") String aCarNo){
        LogUtil.WriteLog_Info("查询车辆信息===userId====>"+userId+"====aCarNo======="+aCarNo,"UserVehicleController");
        List ret = this.userVehicleService.getUserVehicle(userId,aCarNo);
        if(ret != null){
            return ResultData.success("查询成功",ret,ret.size());
        }else{
            return ResultData.fail("查询车辆详情失败");
        }
    }

    /**
     * 修改用户车牌信息
     * @param USERID
     * @param ACARNO
     * @param CHECKUSERNAME
     * @param CHECKUSERNUMBER
     * @param LICENSEPIC
     * @param PERMITPIC
     * @param VEHICLEPIC
     * @param CERTIFICATION
     * @return
     */
    @RequestMapping(value = "updateUserVehicle",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="更新（认证）车牌信息",httpMethod = "POST")
    public ResultData<String> updateUserVehicle(@RequestParam("USERID") String USERID,
                                                @RequestParam("ACARNO") String ACARNO,
                                                @RequestParam(value = "CHECKUSERNAME",required = false) String CHECKUSERNAME,
                                                @RequestParam(value ="CHECKUSERNUMBER",required = false) String CHECKUSERNUMBER,
                                                @RequestParam(value = "LICENSEPIC", required = true) String LICENSEPIC,
                                                @RequestParam(value = "PERMITPIC", required = true) String PERMITPIC,
                                                @RequestParam(value = "VEHICLEPIC", required = true) String VEHICLEPIC,
                                                @RequestParam(value ="CERTIFICATION",required = false) String CERTIFICATION,
                                                @RequestParam(value ="REFERRERCODE",required = false) String REFERRERCODE
                                                ){
        LogUtil.WriteLog_Info("更新用户车牌信息=====USERID==>" + USERID + "=====ACARNO======>" + ACARNO + "======CHECKUSERNAME======" + CHECKUSERNAME + "====CHECKUSERNUMBER=====" + CHECKUSERNUMBER, "UserVehicleController");
        if(PubTools.isNull(LICENSEPIC) || PubTools.isNull(PERMITPIC) || PubTools.isNull(VEHICLEPIC)){
            return ResultData.fail("附件不允许为空");
        }
        if(PubTools.isNull(CHECKUSERNAME)){
            CHECKUSERNAME="";
        }
        if(PubTools.isNull(CHECKUSERNUMBER)){
            CHECKUSERNUMBER="";
        }
        if(PubTools.isNull(LICENSEPIC)){
            LICENSEPIC="";
        }
        if(PubTools.isNull(PERMITPIC)){
            PERMITPIC="";
        }
        if(PubTools.isNull(VEHICLEPIC)){
            VEHICLEPIC="";
        }
        if(PubTools.isNull(CERTIFICATION)){
            CERTIFICATION="2";
        }
        if(PubTools.isNull(REFERRERCODE)){
            REFERRERCODE="";
        }
        int ret = this.userVehicleService.updateUserVehicle(USERID,ACARNO,CHECKUSERNAME,CHECKUSERNUMBER,LICENSEPIC,PERMITPIC,VEHICLEPIC,CERTIFICATION,REFERRERCODE);
        if(ret >0){
            // WebsocketUtil.sendCheckMessageForAll("car");
            redisConfigUtil.publish("CAR", "car");
            return ResultData.success("更新成功");
        }else{
            return ResultData.fail("更新车牌信息出错");
        }
    }
}
