package com.zhgd.xmgl.push.controller;

import com.getui.push.v2.sdk.common.ApiResult;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.xmgl.push.service.UniPushService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @program: wisdomSite
 * @description: 消息推送
 * @author: Mr.Peng
 * @create: 2022-04-20 10:41
 **/
@RestController
@RequestMapping("/xmgl/push")
@Slf4j
@Api(tags = "push消息推送")
public class UniPushController {
    @Autowired
    private UniPushService uniPushService;

    @ApiOperation(value = " 消息推送", notes = "消息推送", httpMethod="POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "alias", value = "别名", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "title", value = "标题", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "content", value = "标题", paramType = "query", required = true, dataType = "String"),
    })
    @PostMapping("/pushMess")
    public Result pushMessage(@RequestBody Map<String,Object> map) {
        Result<String> result=new Result();
        try {
            ApiResult apiResult=uniPushService.pushToSingleByAlias(MapUtils.getString(map,"alias"),MapUtils.getString(map,"title"),MapUtils.getString(map,"content"),null);
            if (apiResult.isSuccess()){
                result.successMsg("操作成功");
            }else{
                result.error500(apiResult.getMsg());
            }
        }catch (Exception e){
            e.printStackTrace();
            result.error500("操作失败");
        }
        return result;
    }

    @ApiOperation(value = " 别名绑定", notes = "别名绑定", httpMethod="POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "alias", value = "别名", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "clientId", value = "客户端ID", paramType = "query", required = true, dataType = "String"),
    })
    @PostMapping("/bindAlias")
    public Result bindAlias(@RequestBody Map<String,Object> map) {
        Result<String> result=new Result();
        try {
            ApiResult apiResult=uniPushService.bindAlias(MapUtils.getString(map,"alias"),MapUtils.getString(map,"clientId"));
            if (apiResult.isSuccess()){
                result.successMsg("操作成功");
            }else{
                result.error500(apiResult.getMsg());
            }
        }catch (Exception e){
            e.printStackTrace();
            result.error500("操作失败");
        }
        return result;
    }


    @ApiOperation(value = " 查询用户状态", notes = "查询用户状态", httpMethod="GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端ID", paramType = "query", required = true, dataType = "String"),
    })
    @GetMapping("/queryUserStatus")
    public Result queryUserStatus(String clientId) {
        Result<Object> result=new Result();
        try {
            ApiResult apiResult=uniPushService.queryUserStatus(clientId);
            if (apiResult.isSuccess()){
                result.successMsg("操作成功");
                result.data(apiResult.getData());
            }else{
                result.error500(apiResult.getMsg());
            }
        }catch (Exception e){
            e.printStackTrace();
            result.error500("操作失败");
        }
        return result;
    }
}
