package com.highdatas.mdm.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.highdatas.mdm.entity.*;
import com.highdatas.mdm.entity.Character;
import com.highdatas.mdm.pojo.CodeMsg;
import com.highdatas.mdm.pojo.DispenseField;
import com.highdatas.mdm.pojo.Result;
import com.highdatas.mdm.pojo.SubscribeEntity;
import com.highdatas.mdm.service.*;
import com.highdatas.mdm.util.Constant;
import com.highdatas.mdm.util.DbUtils;
import com.highdatas.mdm.util.RedisClient;
import com.highdatas.mdm.util.pool.MqEntity;
import com.highdatas.mdm.util.pool.MqMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 前端控制器
*
* @description 分发接口
* @author kimi
* @since 2019-12-31
*/
@RestController
@RequestMapping("/subscribe")
public class DispenseController {
@Autowired
DispenseService dispenseService;
@Autowired
RedisClient redisClient;
@Autowired
MasterDataService masterDataService;
@Autowired
IMasterAuthorService masterAuthorService;
@Autowired
IMaintainFieldService maintainFieldService;
@Autowired
IMaintainService maintainService;
@Autowired
IMenuMappingService menuMappingService;
@Autowired
IMasterAuthorSubscribeService subscribeService;
@Autowired
ISysMenuService menuService;
@Autowired
ISysViewService viewService;
@Autowired
IMasterAuthorSubscribeService masterAuthorSubscribeService;
@Autowired
IMasterAuthorUnactiveService masterAuthorUnactiveService;
@Autowired
ISysDispenseLogsService dispenseLogsService;
@Autowired
ISysFieldService fieldService;
/**
*
* @description: 页面调用分发接口
* @param userId 分发用户
* @param dataId 分发的dataId
* @param type 分发类型, 视图还是主题
* @param increment 增量/全量
* @return: 是否添加到队列
*
*/
@RequestMapping(value = "/dispense/surface", method = RequestMethod.GET)
public Result surface(@RequestParam String userId, @RequestParam String type, @RequestParam String dataId, HttpServletRequest request, HttpServletResponse response) {
//类型为 主题的时候需要传分发哪个版本的数据
String versionId = request.getParameter("versionId");
//添加到分发队列
return addPassiveMq(userId, type, dataId, versionId, request, "surface");
}
/**
*
* @description: 获取当前分发任务数据总条数,供外部系统调用
* @param reqObj 参数对象
* token 通过token获取user信息
* type 数据类型 视图,主题
* dataId 分发的dataId
* versionId 主题的版本id
* increment 增量/全量
* @return: 记录数
*
*/
@RequestMapping(value = "/api/count", method = RequestMethod.POST)
public Result count(@RequestBody JSONObject reqObj) {
String token = reqObj.getString("token");
String type = reqObj.getString("type");
String dataId = reqObj.getString("dataId");
String versionId = reqObj.getString("versionId");
String incrementStr = reqObj.getString("increment");
//校验必传字段
if (StringUtils.isEmpty(token) || StringUtils.isEmpty(type) || StringUtils.isEmpty(dataId)) {
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
//获取用户信息
String userId = redisClient.getRedisVal(token);
if (StringUtils.isEmpty(userId)) {
return Result.error(CodeMsg.ERROR_FIND_TOKEN);
}
//转换increment类型
boolean increment = true;
if (!StringUtils.isEmpty(incrementStr)) {
increment = Boolean.valueOf(incrementStr);
}
if (type.equalsIgnoreCase(Constant.Master)) {
//主题 需校验版本字段
if (StringUtils.isEmpty(versionId)) {
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
//获取主题版本信息
Maintain maintain = maintainService.selectById(versionId);
if (maintain == null) {
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
//获取主题信息
SysMenu sysMenu = menuService.selectById(dataId);
if (sysMenu == null) {
return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND);
}
//校验是否已经注册
int subscribeCnt = subscribeService.selectCount(new EntityWrapper().eq("menu_id", dataId).eq("user_id", userId));
if (subscribeCnt < 1) {
return Result.error(CodeMsg.SUBSCRIBE_UN_ERROR);
}
//校验是否激活
int i = masterAuthorUnactiveService.selectCount(new EntityWrapper().eq("maintain_id", versionId).eq("user_id", userId));
if (i > 0) {
return Result.error(CodeMsg.Active_UN_ERROR);
}
//获取权限信息
MasterAuthor masterAuthor = new MasterAuthor().setTableName(maintain.getTableName());
TUser user = new TUser().setUserId(userId);
//获取理论上的页码数,记录信息
com.highdatas.mdm.pojo.Page initPageInfo = DbUtils.masterAuthorService.getInitPageInfo(masterAuthor, maintain, user, increment);
if (initPageInfo != null) {
return Result.success(initPageInfo.getRecordCount());
}
return Result.success(0);
} else if (type.equalsIgnoreCase(Constant.View)) {
//获取视图信息
SysView sysView = viewService.selectById(dataId);
if (sysView == null) {
return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND);
}
//校验是否激活
Boolean active = sysView.getActive();
if (!active) {
return Result.error(CodeMsg.Active_UN_ERROR);
}
//获取视图记录数
long viewCount = viewService.getViewCount(sysView);
return Result.success(viewCount);
} else {
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
}
/**
*
* @description: 获取当前分发任务数据数据,供外部系统调用
* @param reqObj 参数对象
* token 通过token获取user信息
* beginNo 起始条数
* endNo 结束条数
* type 数据类型 视图,主题
* dataId 分发的dataId
* versionId 主题的版本id
* increment 增量/全量
* @return: 数据记录
*
*/
@RequestMapping(value = "/api/data", method = RequestMethod.POST)
public Result data(@RequestBody JSONObject reqObj) {
//创建分发日志
SysDispenseLogs logs = new SysDispenseLogs().setId(DbUtils.getUUID()).setCreateTime(new Date());
//设置日志类型
logs.setTouchType("rest");
logs.insert();
String token = reqObj.getString("token");
String type = reqObj.getString("type");
String dataId = reqObj.getString("dataId");
String versionId = reqObj.getString("versionId");
String incrementStr = reqObj.getString("increment");
Integer beginNo = reqObj.getInteger("beginNo");
Integer endNo = reqObj.getInteger("endNo");
logs.setTopicId(type).setTagId(dataId).setDataType(type);
//beginNo endNo字段 是否合理
if (beginNo == null || endNo == null) {
logs.setErrorInfo("beginNo or endNo not exists").setStatus("false").updateById();
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
} else if (endNo < beginNo) {
logs.setErrorInfo("beginNo > endNo ").setStatus("false").updateById();
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
if (beginNo < 0 || endNo < 0) {
logs.setErrorInfo("beginNo or endNo < 0").setStatus("false").updateById();
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
//校验必传字段
if (StringUtils.isEmpty(token) || StringUtils.isEmpty(type) || StringUtils.isEmpty(dataId)) {
logs.setErrorInfo("token or type or dataId not found").setStatus("false").updateById();
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
//获取用户信息
String userId = redisClient.getRedisVal(token);
if (StringUtils.isEmpty(userId)) {
logs.setErrorInfo(CodeMsg.ERROR_FIND_TOKEN.getMsg()).setStatus("false").updateById();
return Result.error(CodeMsg.ERROR_FIND_TOKEN);
}
TUser userById = DbUtils.getUserById(userId);
//更新日志信息
logs.setUserName(userById.getUserName());
logs.setUserId(userId).updateById();
boolean increment = true;
if (!StringUtils.isEmpty(incrementStr)) {
increment = Boolean.valueOf(incrementStr);
}
com.highdatas.mdm.pojo.Page initPageInfo = null;
if (type.equalsIgnoreCase(Constant.Master)) {
//校验版本字段
if (StringUtils.isEmpty(versionId)) {
logs.setErrorInfo("versionId not exists").setStatus("false").updateById();
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
//获取版本信息
Maintain maintain = maintainService.selectById(versionId);
if (maintain == null) {
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
//更新日志信息
logs.setMaintainId(maintain.getId()).setVersion(maintain.getVersion());
logs.updateById();
//获取主题信息
SysMenu sysMenu = menuService.selectById(dataId);
if (sysMenu == null) {
return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND);
}
//更新主题信息
logs.setName(sysMenu.getName());
logs.updateById();
//检验是否已注册
int subscribeCnt = subscribeService.selectCount(new EntityWrapper().eq("menu_id", dataId).eq("user_id", userId));
if (subscribeCnt < 1) {
logs.setErrorInfo(CodeMsg.SUBSCRIBE_UN_ERROR.getMsg()).updateById();
return Result.error(CodeMsg.SUBSCRIBE_UN_ERROR);
}
//检验是否已激活
int i = masterAuthorUnactiveService.selectCount(new EntityWrapper().eq("maintain_id", versionId).eq("user_id", userId));
if (i > 0) {
logs.setErrorInfo(CodeMsg.Active_UN_ERROR.getMsg()).updateById();
return Result.error(CodeMsg.Active_UN_ERROR);
}
//获取权限
MasterAuthor masterAuthor = new MasterAuthor().setTableName(maintain.getTableName());
TUser user = new TUser().setUserId(userId);
//获取理论上的页数,记录数
initPageInfo = masterAuthorService.getInitPageInfo(masterAuthor, maintain, user, increment);
if (initPageInfo == null) {
return Result.success(null);
}
//通过 beginNo,endNo更新page信息
boolean b = refreshInitPage(initPageInfo, beginNo, endNo);
if(!b) {
return Result.error(CodeMsg.ERROR_SIZE_TOO_LONG);
}
if (increment) {
//获取增量数据
String maintainTableName = maintain.getTableName();
//获取记录表
String tempTableName = maintainTableName + Constant.RECORD;
//组装字段
List fieldByMaintain = DbUtils.fieldService.getFieldByMaintain(maintain.getId());
List fieldList = fieldByMaintain.stream().map(sysField -> sysField.getField()).collect(Collectors.toList());
String fields = fieldByMaintain.stream().map(sysField -> sysField.getField()).collect(Collectors.joining(Constant.COMMA));
//组装筛选条件
String filter = DbUtils.masterAuthorService.getFilter(user, maintain.getId());
if (StringUtils.isEmpty(filter)) {
filter = Constant.WHERE_DEFAULT;
}
//获取数据
List