|
|
|
package com.zhonglai.luhui.api.controller.user;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.ruoyi.common.annotation.Log;
|
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
import com.ruoyi.common.enums.BusinessType;
|
|
|
|
import com.ruoyi.common.utils.DateUtils;
|
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
|
import com.ruoyi.system.domain.user.UserOpenid;
|
|
|
|
import com.zhonglai.luhui.action.BaseController;
|
|
|
|
import com.zhonglai.luhui.dao.service.PublicService;
|
|
|
|
import com.zhonglai.luhui.device.domain.IotAlert;
|
|
|
|
import com.zhonglai.luhui.device.domain.IotAlertNoticeChannel;
|
|
|
|
import com.zhonglai.luhui.device.domain.IotAlertUserNotice;
|
|
|
|
import com.zhonglai.luhui.device.dto.WeiXinGongZhongHaoNoticeDto;
|
|
|
|
import com.zhonglai.luhui.device.dto.WeiXinXiaoChengXuNoticeDto;
|
|
|
|
import com.zhonglai.luhui.security.utils.SecurityUtils;
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
import io.swagger.annotations.ApiImplicitParam;
|
|
|
|
import io.swagger.annotations.ApiImplicitParams;
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import weixin.popular.api.SnsAPI;
|
|
|
|
import weixin.popular.bean.sns.SnsToken;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
@Api(tags = "用户告警管理")
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/user/alarm")
|
|
|
|
public class UserInfoAlarmController extends BaseController {
|
|
|
|
@Autowired
|
|
|
|
private PublicService publicService;
|
|
|
|
|
|
|
|
@ApiOperation("提交公众号通知服务")
|
|
|
|
@ApiImplicitParams({
|
|
|
|
@ApiImplicitParam(value = "渠道id(管理员提供)",name = "channel_id"),
|
|
|
|
@ApiImplicitParam(value = "告警类型(1系统告警,2用户告警)",name = "type"),
|
|
|
|
@ApiImplicitParam(value = "授权获取到的code",name = "code"),
|
|
|
|
})
|
|
|
|
@Log(title = "提交公众号通知服务", businessType = BusinessType.INSERT)
|
|
|
|
@Transactional
|
|
|
|
@PostMapping(value = "subGzhNoticeService/{channel_id}/{type}")
|
|
|
|
public AjaxResult subGzhNoticeService(@PathVariable Integer channel_id,@PathVariable Integer type, String code)
|
|
|
|
{
|
|
|
|
//检查渠道是否存在
|
|
|
|
IotAlertNoticeChannel iotAlertNoticeChannel = publicService.getObject(IotAlertNoticeChannel.class,"id",channel_id+"");
|
|
|
|
if(null == iotAlertNoticeChannel)
|
|
|
|
{
|
|
|
|
return AjaxResult.error("通知渠道不存在");
|
|
|
|
}
|
|
|
|
if(iotAlertNoticeChannel.getType()!=1)
|
|
|
|
{
|
|
|
|
return AjaxResult.error("该渠道不是公众号通知渠道");
|
|
|
|
}
|
|
|
|
WeiXinGongZhongHaoNoticeDto weiXinGongZhongHaoNoticeDto = JSONObject.parseObject(iotAlertNoticeChannel.getConfig(),WeiXinGongZhongHaoNoticeDto.class);
|
|
|
|
|
|
|
|
Integer user_id = SecurityUtils.getUserId().intValue();
|
|
|
|
SnsToken snsToken = SnsAPI.oauth2AccessToken(weiXinGongZhongHaoNoticeDto.getGzhappid(), weiXinGongZhongHaoNoticeDto.getGzhsecret(),code);
|
|
|
|
if(StringUtils.isEmpty(snsToken.getOpenid()))
|
|
|
|
{
|
|
|
|
return AjaxResult.error("公众号授权失败");
|
|
|
|
}
|
|
|
|
upOpenid(user_id,snsToken.getOpenid(),weiXinGongZhongHaoNoticeDto.getXcxConfigId());
|
|
|
|
|
|
|
|
return upIotAlertUserNotice(user_id,channel_id,type);
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("提交小程序通知服务")
|
|
|
|
@ApiImplicitParams({
|
|
|
|
@ApiImplicitParam(value = "渠道id(管理员提供)",name = "channel_id"),
|
|
|
|
@ApiImplicitParam(value = "告警类型(1系统告警,2用户告警)",name = "type"),
|
|
|
|
@ApiImplicitParam(value = "小程序用户openid",name = "openid"),
|
|
|
|
})
|
|
|
|
@Log(title = "提交公众号通知服务", businessType = BusinessType.INSERT)
|
|
|
|
@Transactional
|
|
|
|
@PostMapping(value = "subXcxNoticeService/{channel_id}/{type}")
|
|
|
|
public AjaxResult subXcxNoticeService(@PathVariable Integer channel_id,@PathVariable Integer type, String openid)
|
|
|
|
{
|
|
|
|
//检查渠道是否存在
|
|
|
|
IotAlertNoticeChannel iotAlertNoticeChannel = publicService.getObject(IotAlertNoticeChannel.class,"id",channel_id+"");
|
|
|
|
if(null == iotAlertNoticeChannel)
|
|
|
|
{
|
|
|
|
return AjaxResult.error("通知渠道不存在");
|
|
|
|
}
|
|
|
|
if(iotAlertNoticeChannel.getType()!=2)
|
|
|
|
{
|
|
|
|
return AjaxResult.error("该渠道不是小程序通知渠道");
|
|
|
|
}
|
|
|
|
WeiXinXiaoChengXuNoticeDto weiXinXiaoChengXuNoticeDto = JSONObject.parseObject(iotAlertNoticeChannel.getConfig(),WeiXinXiaoChengXuNoticeDto.class);
|
|
|
|
|
|
|
|
Integer user_id = SecurityUtils.getUserId().intValue();
|
|
|
|
|
|
|
|
upOpenid(user_id,openid,weiXinXiaoChengXuNoticeDto.getXcxConfigId());
|
|
|
|
|
|
|
|
return upIotAlertUserNotice(user_id,channel_id,type);
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("获取告警通知渠道配置")
|
|
|
|
@ApiImplicitParam(value = "产品id",name = "product_id")
|
|
|
|
@GetMapping(value = "getIotAlertUserNoticeList/{product_id}")
|
|
|
|
public AjaxResult getIotAlertUserNoticeList(@PathVariable Integer product_id)
|
|
|
|
{
|
|
|
|
Integer user_id = SecurityUtils.getUserId().intValue();
|
|
|
|
IotAlertUserNotice iotAlertUserNotice = new IotAlertUserNotice();
|
|
|
|
iotAlertUserNotice.setUser_id(user_id);
|
|
|
|
List<Map<String,Object>> list = publicService.getObjectList(iotAlertUserNotice,"*",null,null,0,0);
|
|
|
|
if (null != list && list.size() !=0)
|
|
|
|
{
|
|
|
|
for (Map<String,Object> map:list)
|
|
|
|
{
|
|
|
|
switch ((int)map.get("type")) //告警类型(1系统告警,2用户告警)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
if ((map.get("alert_ids")).equals("*"))
|
|
|
|
{
|
|
|
|
map.put("iotAlertList",publicService.getObjectListBySQL("select * from `iot_alert` where product_id="+product_id));
|
|
|
|
}else {
|
|
|
|
map.put("iotAlertList",publicService.getObjectListBySQL("select * from `iot_alert` where product_id="+product_id+"alert_id in("+map.get("alert_ids")+")"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if ((map.get("alert_ids")).equals("*"))
|
|
|
|
{
|
|
|
|
map.put("iotAlertList",publicService.getObjectListBySQL("select * from `iot_alert_user` where product_id="+product_id));
|
|
|
|
}else {
|
|
|
|
map.put("iotAlertList",publicService.getObjectListBySQL("select * from `iot_alert_user` where product_id="+product_id+"alert_id in("+map.get("alert_ids")+")"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
String channels = (String) map.get("channels");
|
|
|
|
map.put("channelList",publicService.getObjectListBySQL("select `id`,`type`,`name`,`state` from `iot_alert_notice_channel` where id in("+channels+")"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AjaxResult.success(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("获取告警通知渠道")
|
|
|
|
@GetMapping(value = "getIotAlertNoticeChannelList")
|
|
|
|
public AjaxResult getIotAlertNoticeChannelList()
|
|
|
|
{
|
|
|
|
IotAlertNoticeChannel iotAlertNoticeChannel = new IotAlertNoticeChannel();
|
|
|
|
iotAlertNoticeChannel.setState(1);
|
|
|
|
List<Map<String,Object>> list = publicService.getObjectList(iotAlertNoticeChannel,"`id`,`name`",null,null,0,0);
|
|
|
|
return AjaxResult.success(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("获取指定产品的系统告警")
|
|
|
|
@ApiImplicitParam(value = "产品id",name = "product_id")
|
|
|
|
@GetMapping(value = "getIotAlertByProduct/{product_id}")
|
|
|
|
public AjaxResult getIotAlertByProduct(@PathVariable Long product_id)
|
|
|
|
{
|
|
|
|
IotAlert iotAlert = new IotAlert();
|
|
|
|
iotAlert.setProductId(product_id);
|
|
|
|
List<Map<String,Object>> list = publicService.getObjectList(iotAlert,"*",null,null,0,0);
|
|
|
|
return AjaxResult.success(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("更新告警通知渠道配置")
|
|
|
|
@ApiImplicitParams({
|
|
|
|
@ApiImplicitParam(value = "通知渠道(iot_alert_notice_channel表的id集合,多个英文逗号分割)",name = "channels"),
|
|
|
|
@ApiImplicitParam(value = "告警id集合(关联iot_alert和iot_alert_user表的id,多个英文逗号分割,*表示所有)",name = "alert_ids"),
|
|
|
|
@ApiImplicitParam(value = "主键id",name = "id"),
|
|
|
|
})
|
|
|
|
@Log(title = "更新告警通知渠道配置", businessType = BusinessType.UPDATE)
|
|
|
|
@Transactional
|
|
|
|
@PostMapping(value = "upIotAlertUserNotice/{id}")
|
|
|
|
public AjaxResult upIotAlertUserNotice(@PathVariable Integer id,String channels, String alert_ids)
|
|
|
|
{
|
|
|
|
IotAlertUserNotice iotAlertUserNotice = new IotAlertUserNotice();
|
|
|
|
iotAlertUserNotice.setId(id);
|
|
|
|
iotAlertUserNotice.setChannels(channels);
|
|
|
|
iotAlertUserNotice.setAlert_ids(alert_ids);
|
|
|
|
int i = publicService.updateObject(iotAlertUserNotice,"id");
|
|
|
|
return AjaxResult.success(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("更新指定产品的告警通知渠道配置")
|
|
|
|
@ApiImplicitParams({
|
|
|
|
@ApiImplicitParam(value = "通知渠道(iot_alert_notice_channel表的id集合,多个英文逗号分割)",name = "channels"),
|
|
|
|
@ApiImplicitParam(value = "告警id集合(关联iot_alert和iot_alert_user表的id,多个英文逗号分割,*表示所有)",name = "alert_ids"),
|
|
|
|
@ApiImplicitParam(value = "主键id",name = "id"),
|
|
|
|
@ApiImplicitParam(value = "产品id",name = "product_id"),
|
|
|
|
})
|
|
|
|
@Log(title = "更新指定产品的告警通知渠道配置", businessType = BusinessType.UPDATE)
|
|
|
|
@Transactional
|
|
|
|
@PostMapping(value = "upIotAlertUserNoticeByProduct/{id}/{product_id}")
|
|
|
|
public AjaxResult upIotAlertUserNoticeByProduct(@PathVariable Integer id,@PathVariable Long product_id,String channels, String alert_ids)
|
|
|
|
{
|
|
|
|
IotAlertUserNotice iotAlertUserNotice = publicService.getObject(IotAlertUserNotice.class,"id",id+"");
|
|
|
|
|
|
|
|
IotAlertUserNotice uPiotAlertUserNotice = new IotAlertUserNotice();
|
|
|
|
uPiotAlertUserNotice.setId(id);
|
|
|
|
if(StringUtils.isNotEmpty(channels))
|
|
|
|
{
|
|
|
|
uPiotAlertUserNotice.setChannels(channels);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(StringUtils.isNotEmpty(alert_ids))
|
|
|
|
{
|
|
|
|
if("*".equals(iotAlertUserNotice.getAlert_ids()))
|
|
|
|
{
|
|
|
|
uPiotAlertUserNotice.setAlert_ids(alert_ids);
|
|
|
|
}else {
|
|
|
|
List<Map<String,Object>> aidsList = publicService.getObjectListBySQL("SELECT GROUP_CONCAT(alert_id) aids FROM `iot_alert` WHERE product_id<>"+product_id+" AND alert_id IN("+iotAlertUserNotice.getAlert_ids()+")");
|
|
|
|
if(null != aidsList && aidsList.size() != 0)
|
|
|
|
{
|
|
|
|
Object aids = aidsList.get(0).get("aids");
|
|
|
|
if(null != aids && !aids.equals(""))
|
|
|
|
{
|
|
|
|
uPiotAlertUserNotice.setAlert_ids(aids+","+alert_ids);
|
|
|
|
}else {
|
|
|
|
uPiotAlertUserNotice.setAlert_ids(alert_ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int upi = 0;
|
|
|
|
if(StringUtils.isNotEmpty(uPiotAlertUserNotice.getChannels()) || StringUtils.isNotEmpty(uPiotAlertUserNotice.getAlert_ids()))
|
|
|
|
{
|
|
|
|
upi = publicService.updateObject(uPiotAlertUserNotice,"id");
|
|
|
|
}
|
|
|
|
return AjaxResult.success(upi);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void upOpenid(Integer user_id,String openid,Integer xcxConfigid)
|
|
|
|
{
|
|
|
|
//更新用户openid表
|
|
|
|
UserOpenid userOpenid = new UserOpenid();
|
|
|
|
userOpenid.setUser_id(user_id);
|
|
|
|
userOpenid.setOpenid(openid);
|
|
|
|
userOpenid.setXcx_config_id(xcxConfigid);
|
|
|
|
Long ct = publicService.getObjectListTotle(userOpenid,null);
|
|
|
|
if(0 == ct)
|
|
|
|
{
|
|
|
|
publicService.insert(userOpenid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private AjaxResult upIotAlertUserNotice(Integer user_id,Integer channel_id,Integer type)
|
|
|
|
{
|
|
|
|
//更新用户通知
|
|
|
|
IotAlertUserNotice iotAlertUserNotice= publicService.getObject(IotAlertUserNotice.class,"user_id,type",user_id+","+type);
|
|
|
|
if(null != iotAlertUserNotice)
|
|
|
|
{
|
|
|
|
if((","+iotAlertUserNotice.getChannels()+",").indexOf(","+channel_id+",")>=0)
|
|
|
|
{
|
|
|
|
return AjaxResult.error("您已经开通过通知服务了,请到通知服务编辑页面进行编辑");
|
|
|
|
}else {
|
|
|
|
IotAlertUserNotice upIotAlertUserNotice = new IotAlertUserNotice();
|
|
|
|
upIotAlertUserNotice.setId(iotAlertUserNotice.getId());
|
|
|
|
upIotAlertUserNotice.setChannels(iotAlertUserNotice.getChannels()+","+channel_id);
|
|
|
|
upIotAlertUserNotice.setUpdate_time(DateUtils.getTime());
|
|
|
|
return AjaxResult.success(publicService.updateObject(upIotAlertUserNotice,"id"));
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
IotAlertUserNotice sysIotAlertUserNotice = new IotAlertUserNotice();
|
|
|
|
sysIotAlertUserNotice.setChannels(channel_id+"");
|
|
|
|
sysIotAlertUserNotice.setType(type);
|
|
|
|
sysIotAlertUserNotice.setUser_id(user_id);
|
|
|
|
|
|
|
|
return AjaxResult.success(publicService.insert(sysIotAlertUserNotice));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
@ApiOperation("生成告警授权连接")
|
|
|
|
@RequestMapping(value = "getAlarmOauth2URL/{channel_id}", method = RequestMethod.GET)
|
|
|
|
public AjaxResult getAlarmOauth2URL(@PathVariable Integer channel_id, @RequestParam(value="redirect_uri") String redirect_uri,String state)
|
|
|
|
{
|
|
|
|
//检查渠道是否存在
|
|
|
|
IotAlertNoticeChannel iotAlertNoticeChannel = publicService.getObject(IotAlertNoticeChannel.class,"id",channel_id+"");
|
|
|
|
if(null == iotAlertNoticeChannel)
|
|
|
|
{
|
|
|
|
return AjaxResult.error("通知渠道不存在");
|
|
|
|
}
|
|
|
|
if(iotAlertNoticeChannel.getType()!=1)
|
|
|
|
{
|
|
|
|
return AjaxResult.error("该渠道不是公众号通知渠道");
|
|
|
|
}
|
|
|
|
WeiXinGongZhongHaoNoticeDto weiXinGongZhongHaoNoticeDto = JSONObject.parseObject(iotAlertNoticeChannel.getConfig(),WeiXinGongZhongHaoNoticeDto.class);
|
|
|
|
String authorizeUrl = SnsAPI.connectOauth2Authorize(weiXinGongZhongHaoNoticeDto.getGzhappid(), redirect_uri, true, state);
|
|
|
|
return AjaxResult.success("",authorizeUrl);
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|