package com.nmgs.controller;

import com.alibaba.fastjson.JSONObject;
import com.nmgs.config.ResultData;
import com.nmgs.entity.Consistant;
import com.nmgs.service.UserTableService;
import com.nmgs.service.impl.IntegralSeriveImpl;
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;

import static dm.jdbc.a.b.i;

@RestController
@RequestMapping("userInfo")
@Api(tags="用户信息操作")
public class UserController {

    public UserTableService userTableService;
    @Autowired
    public void setUserTableService(UserTableService userTableServiceT) {
        this.userTableService = userTableServiceT;
    }

    @RequestMapping(value = "oathuUser",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="用户初始化注册",httpMethod = "POST")
    public ResultData<String> oathuUser(@RequestParam("openId") String openId, @RequestParam(value = "wxName", required = false) String wxName) throws InterruptedException {
        LogUtil.WriteLog_Info("用户注册开始====" + PubTools.getCurrentDate() + "====" + openId, "UserController");
        List userByOpenId = userTableService.getUserByOpenId(openId);
       if(userByOpenId != null && userByOpenId.size()>0 ){
           return ResultData.success(2,"人员已经存在");
       }else{
           if(PubTools.isNull(wxName)){
               wxName="";
           }
           int i = userTableService.addUser(openId,wxName);
           if(i<0){
               return ResultData.fail("注册失败");
           }else if(i>0){
               return ResultData.success(1,"注册成功");
           }
       }

        return ResultData.success(1,"注册成功");
    }

    @RequestMapping(value = "identifyCommit",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="用户实名认证",httpMethod = "POST")
    public ResultData<String> identifyCommit(@RequestParam("USERID") String USERID,
                                             @RequestParam("IDNUMBER") String IDNUMBER,
                                             @RequestParam("REALNAME") String REALNAME,
                                             @RequestParam(value = "PHONE",required = false) String PHONE,
                                             @RequestParam("NUMBERPIC") String NUMBERPIC){
        LogUtil.WriteLog_Info("用户实名认证开始====" + PubTools.getCurrentDate() + "====" + USERID, "UserController");
        int i = userTableService.updateUser(USERID,IDNUMBER,PHONE,REALNAME,NUMBERPIC);
        LogUtil.WriteLog_Info("用户实名认证结束========" + PubTools.getCurrentDate(), "UserController");
        if(i<0){
            return ResultData.fail("认证失败");
        }else if(i>0){
            //WebsocketUtil.sendCheckMessageForAll("user");
            redisConfigUtil.publish("USER", "user");
            //测试取消订阅
            // redisConfigUtil.publish(Consistant.USER + USERID, "unSubscribe_" + USERID);
            return ResultData.success(1,"已提交");
        }
        return ResultData.success(1,"已提交");
    }

    @RequestMapping(value = "getUserInfoByOepnId",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="根据用户微信ID获取用户基本信息(不包含图片)",httpMethod = "POST")
    public ResultData<List> getUserInfoByOepnId(@RequestParam("openId") String openId){
        List userByOpenId = userTableService.getUserByOpenId(openId);
        if(userByOpenId ==null){
            return ResultData.fail("查询失败");
        }else if(userByOpenId !=null){
            return ResultData.success("查询成功",userByOpenId,userByOpenId.size());
        }
        return ResultData.success(1,"查询成功");
    }

    @RequestMapping(value = "getUserStarByOepnId",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="根据用户微信ID获取用户星级信息",httpMethod = "POST")
    public ResultData<List> getUserStarByOepnId(@RequestParam("openId") String openId){
        List userByOpenId = userTableService.getUserStarByOepnId(openId);
        if(userByOpenId ==null ){
            return ResultData.fail("查询失败");
        }else if(userByOpenId !=null){
            return ResultData.success("查询成功",userByOpenId,userByOpenId.size());
        }
        return ResultData.success(1,"查询成功");
    }

    @RequestMapping(value = "getUserDetailByOpenId",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value="根据用户微信ID获取用户详情信息",httpMethod = "POST")
    public ResultData<List> getUserDetailByOpenId(@RequestParam("openId") String openId){
        List userByOpenId = userTableService.getUserDetailByOpenId(openId);
        if(userByOpenId ==null){
            return ResultData.fail("查询失败");
        }else if(userByOpenId !=null){
            return ResultData.success("查询成功",userByOpenId,userByOpenId.size());
        }
        return ResultData.success(1,"查询成功");
    }

    @RequestMapping(value = "checkUserInfo",
            method = {RequestMethod.POST}
    )
    @ApiOperation(value = "根据用户信息进行校验", httpMethod = "POST")
    public ResultData<List> checkUserInfo(@RequestParam(value = "PHONE", required = false) String PHONE,
                                          @RequestParam(value = "IDNUMBER", required = false) String IDNUMBER,
                                          @RequestParam(value = "REALNAME", required = false) String REALNAME) {
        if (PubTools.isNull(PHONE) && PubTools.isNull(IDNUMBER) && PubTools.isNull(REALNAME)) {
            return ResultData.fail("查询失败：姓名、身份证号和手机号至少提供一个");
        }
        List userByOpenId = userTableService.checkUserInfo(PHONE, IDNUMBER, REALNAME);
        if (userByOpenId == null) {
            return ResultData.fail("查询失败");
        } else if (userByOpenId != null) {
            return ResultData.success("查询成功", userByOpenId, userByOpenId.size());
        }
        return ResultData.success("查询成功", userByOpenId, userByOpenId.size());
    }
}
