| | |
| | | package com.highdatas.mdm.controller; |
| | | |
| | | import com.highdatas.mdm.entity.TUser; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.highdatas.mdm.entity.*; |
| | | import com.highdatas.mdm.pojo.CodeMsg; |
| | | import com.highdatas.mdm.util.pool.MqEntity; |
| | | import com.highdatas.mdm.pojo.Result; |
| | | import com.highdatas.mdm.service.DispenseService; |
| | | import com.highdatas.mdm.service.MasterDataService; |
| | | 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.MqMessage; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RequestMethod; |
| | | import org.springframework.web.bind.annotation.RequestParam; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | * @since 2019-12-31 |
| | | */ |
| | | @RestController |
| | | @RequestMapping("/dispense") |
| | | @RequestMapping("/subscribe") |
| | | public class DispenseController { |
| | | @Autowired |
| | | DispenseService dispenseService; |
| | |
| | | RedisClient redisClient; |
| | | @Autowired |
| | | MasterDataService masterDataService; |
| | | @Autowired |
| | | IMasterAuthorService masterAuthorService; |
| | | @Autowired |
| | | IMaintainFieldService maintainFieldService; |
| | | @Autowired |
| | | IMaintainService maintainService; |
| | | @Autowired |
| | | ISysMenuService menuService; |
| | | @Autowired |
| | | ISysViewService viewService; |
| | | @Autowired |
| | | IMasterAuthorSubscribeService masterAuthorSubscribeService; |
| | | @Autowired |
| | | IMasterAuthorUnactiveService masterAuthorUnactiveService; |
| | | @Autowired |
| | | ISysDispenseLogsService dispenseLogsService; |
| | | |
| | | @RequestMapping(value = "/add", method = RequestMethod.GET) |
| | | public void getAll(@RequestParam String code, HttpServletResponse response) { |
| | | dispenseService.pushPassiveMq(new MqMessage( code, response)); |
| | | @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"); |
| | | } |
| | | |
| | | @RequestMapping(value = "/dispense/surface/muti", method = RequestMethod.POST) |
| | | public Result surface(@RequestBody JSONObject objectList, HttpServletRequest request, HttpServletResponse response) { |
| | | String userId = objectList.getString("userId"); |
| | | JSONArray datas = objectList.getJSONArray("datas"); |
| | | ArrayList<Result> results = new ArrayList<>(); |
| | | for (int i = 0; i < datas.size(); i++) { |
| | | JSONObject oneRequest = datas.getJSONObject(i); |
| | | String type = oneRequest.getString("type"); |
| | | String dataId = oneRequest.getString("dataId"); |
| | | String versionId = oneRequest.getString("versionId"); |
| | | Result result = addPassiveMq(userId, type, dataId, versionId, request, "muti"); |
| | | results.add(result); |
| | | } |
| | | return Result.success(results); |
| | | |
| | | } |
| | | |
| | | private Result addPassiveMq(@RequestParam String userId, @RequestParam String type, @RequestParam String dataId, String versionId, HttpServletRequest request, String touchType) { |
| | | TUser user = DbUtils.getUserById(userId); |
| | | if (user == null) { |
| | | return Result.error(CodeMsg.USER_NOT_MATHED); |
| | | } |
| | | String incrementStr = request.getParameter("increment"); |
| | | boolean increment = true; |
| | | if (!StringUtils.isEmpty(incrementStr)) { |
| | | increment = Boolean.valueOf(incrementStr); |
| | | } |
| | | MqEntity mqEntity = new MqEntity(); |
| | | mqEntity.setUserId(userId); |
| | | mqEntity.setIncrement(increment); |
| | | mqEntity.setMsgTopicName(type); |
| | | mqEntity.setType(type); |
| | | mqEntity.setDataId(dataId); |
| | | if(type.equalsIgnoreCase(Constant.Master)) { |
| | | |
| | | if (StringUtils.isEmpty(versionId)) { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | mqEntity.setMaintainId(versionId); |
| | | SysMenu sysMenu = menuService.selectById(dataId); |
| | | if (sysMenu == null) { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | int i = masterAuthorUnactiveService.selectCount(new EntityWrapper<MasterAuthorUnactive>().eq("maintain_id", versionId).eq("user_id", userId)); |
| | | if (i > 0) { |
| | | return Result.error(CodeMsg.Active_UN_ERROR); |
| | | } |
| | | |
| | | mqEntity.setMsgTagName(sysMenu.getId()); |
| | | } else if (type.equalsIgnoreCase(Constant.View)) { |
| | | SysView sysView = viewService.selectById(dataId); |
| | | if (sysView == null) { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | Boolean active = sysView.getActive(); |
| | | if (!active) { |
| | | return Result.error(CodeMsg.Active_UN_ERROR); |
| | | } |
| | | mqEntity.setMsgTagName(sysView.getId()); |
| | | } else { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | mqEntity.setMsgKey(DbUtils.getUUID(16)); |
| | | MqEntity.MsgBodyBean msgBody = mqEntity.getMsgBody(); |
| | | msgBody.setVersion(versionId); |
| | | MqMessage mqMessage = new MqMessage(mqEntity); |
| | | |
| | | dispenseService.pushPassiveMq(mqMessage,touchType); //被动返回 |
| | | // dispenseService.pushActiveMq(mqMessage); 主动推 |
| | | return Result.success(CodeMsg.ADDQUEUE_SUCCESS); |
| | | |
| | | } |
| | | |
| | | @RequestMapping(value = "/dispense/api", method = RequestMethod.GET) |
| | | public Result api(@RequestParam String token, @RequestParam String type, @RequestParam String dataId, HttpServletRequest request, HttpServletResponse response) { |
| | | String userId = redisClient.getRedisVal(token); |
| | | if (StringUtils.isEmpty(userId)) { |
| | | return Result.error(CodeMsg.ERROR_FIND_TOKEN); |
| | | } |
| | | String versionId = request.getParameter("versionId"); |
| | | return addPassiveMq(userId, type, dataId, versionId, request, "api"); |
| | | } |
| | | |
| | | @RequestMapping(value = "/add", method = RequestMethod.GET) |
| | | public Result add(@RequestParam String token, @RequestParam String type, @RequestParam String dataId) { |
| | | try{ |
| | | String redisVal = redisClient.getRedisVal(token); |
| | | if (StringUtils.isEmpty(redisVal)) { |
| | | return Result.error(CodeMsg.ERROR_FIND_TOKEN); |
| | | } |
| | | if(type.equalsIgnoreCase(Constant.Master)) { |
| | | MasterAuthor masterAuthor = masterAuthorService.selectById(dataId); |
| | | if (masterAuthor == null) { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | boolean updated = new MasterAuthorSubscribe().setId(DbUtils.getUUID()).setMenuId(masterAuthor.getId()).setUserId(redisVal).insert(); |
| | | if (updated) { |
| | | return Result.success(null); |
| | | } else { |
| | | return Result.error(CodeMsg.SUBSCRIBE_SAVE_ERROR); |
| | | } |
| | | } else if (type.equalsIgnoreCase(Constant.View)) { |
| | | SysView sysView = viewService.selectById(dataId); |
| | | if (sysView == null) { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | boolean updated = sysView.setSubscribe(true).updateById(); |
| | | if (updated) { |
| | | return Result.success(null); |
| | | } else { |
| | | return Result.error(CodeMsg.SUBSCRIBE_SAVE_ERROR); |
| | | } |
| | | } else { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | } |
| | | catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return Result.error(CodeMsg.ERROR_SAVE_TOKEN); |
| | | } |
| | | } |
| | | |
| | | @RequestMapping(value = "/refreshList", method = RequestMethod.POST) |
| | | public Result refreshList(@RequestBody JSONObject requestBody) { |
| | | String token = requestBody.getString(Constant.Token); |
| | | if (StringUtils.isEmpty(token)) { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | try{ |
| | | String userId = redisClient.getRedisVal(token); |
| | | if (StringUtils.isEmpty(userId)) { |
| | | return Result.error(CodeMsg.ERROR_FIND_TOKEN); |
| | | } |
| | | JSONArray dataList = requestBody.getJSONArray("dataList"); |
| | | if (dataList == null) { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | //un subscribe |
| | | List<SysView> sysViews = viewService.selectList(new EntityWrapper<SysView>().eq(Constant.USERID, userId)); |
| | | for (SysView sysView : sysViews) { |
| | | sysView.setSubscribe(false).updateById(); |
| | | } |
| | | |
| | | masterAuthorSubscribeService.delete(new EntityWrapper<MasterAuthorSubscribe>().eq(Constant.USERID, userId)); |
| | | |
| | | for (int i = 0; i < dataList.size(); i++) { |
| | | JSONObject oneObj = dataList.getJSONObject(i); |
| | | String dataId = oneObj.getString("dataId"); |
| | | String type = oneObj.getString("type"); |
| | | if (StringUtils.isEmpty(dataId) || StringUtils.isEmpty(type)) { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | if(type.equalsIgnoreCase(Constant.Master)) { |
| | | MasterAuthor masterAuthor = masterAuthorService.selectById(dataId); |
| | | if (masterAuthor == null) { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | boolean updated = new MasterAuthorSubscribe().setId(DbUtils.getUUID()).setMenuId(masterAuthor.getId()).setUserId(userId).insert(); |
| | | if (updated) { |
| | | return Result.success(null); |
| | | } else { |
| | | return Result.error(CodeMsg.SUBSCRIBE_SAVE_ERROR); |
| | | } |
| | | } else if (type.equalsIgnoreCase(Constant.View)) { |
| | | SysView sysView = viewService.selectById(dataId); |
| | | if (sysView == null) { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | boolean updated = sysView.setSubscribe(true).updateById(); |
| | | if (updated) { |
| | | return Result.success(null); |
| | | } else { |
| | | return Result.error(CodeMsg.SUBSCRIBE_SAVE_ERROR); |
| | | } |
| | | } else { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | } |
| | | return Result.success(null); |
| | | } |
| | | catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return Result.error(CodeMsg.ERROR_SAVE_TOKEN); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | @RequestMapping(value = "/delete", method = RequestMethod.GET) |
| | | public Result delete(@RequestParam String token, @RequestParam String type, @RequestParam String dataId) { |
| | | try{ |
| | | String redisVal = redisClient.getRedisVal(token); |
| | | if (StringUtils.isEmpty(redisVal)) { |
| | | return Result.error(CodeMsg.ERROR_FIND_TOKEN); |
| | | } |
| | | if(type.equalsIgnoreCase(Constant.Master)) { |
| | | MasterAuthor masterAuthor = masterAuthorService.selectById(dataId); |
| | | if (masterAuthor == null) { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | boolean updated = masterAuthorSubscribeService.delete(new EntityWrapper<MasterAuthorSubscribe>().eq("master_author_id", masterAuthor.getId()).eq(Constant.USERID, redisVal)); |
| | | |
| | | if (updated) { |
| | | return Result.success(null); |
| | | } else { |
| | | return Result.error(CodeMsg.SUBSCRIBE_SAVE_ERROR); |
| | | } |
| | | } else if (type.equalsIgnoreCase(Constant.View)) { |
| | | SysView sysView = viewService.selectById(dataId); |
| | | if (sysView == null) { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | boolean updated = sysView.setSubscribe(false).updateById(); |
| | | if (updated) { |
| | | return Result.success(null); |
| | | } else { |
| | | return Result.error(CodeMsg.SUBSCRIBE_SAVE_ERROR); |
| | | } |
| | | } else { |
| | | return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); |
| | | } |
| | | } |
| | | catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return Result.error(CodeMsg.ERROR_SAVE_TOKEN); |
| | | } |
| | | } |
| | | |
| | | @RequestMapping(value = "/authors", method = RequestMethod.GET) |
| | | public Result authors(@RequestParam String token) { |
| | | try{ |
| | | String userId = redisClient.getRedisVal(token); |
| | | if (StringUtils.isEmpty(userId)) { |
| | | return Result.error(CodeMsg.ERROR_FIND_TOKEN); |
| | | } |
| | | TUser user = DbUtils.getUserById(userId); |
| | | if (user == null) { |
| | | return Result.error(CodeMsg.USER_NOT_MATHED); |
| | | } |
| | | List<SubscribeEntity> result = new ArrayList<>(); |
| | | |
| | | // author 只有拥有最新权限的才能订阅 |
| | | List<MasterAuthor> userAuthor = masterAuthorService.getUserAuthor(userId, null); |
| | | userAuthor = userAuthor.stream() |
| | | .filter(masterAuthor -> !StringUtils.isEmpty(masterAuthor.getMaintainFieldId())) |
| | | // .filter(masterAuthor -> masterAuthor.getMaintainFieldId().equalsIgnoreCase(Constant.All)) |
| | | .collect(Collectors.toList()); |
| | | |
| | | for (MasterAuthor masterAuthor : userAuthor) { |
| | | SubscribeEntity subscribeEntity = new SubscribeEntity(); |
| | | subscribeEntity.setId(masterAuthor.getId()); |
| | | subscribeEntity.setType(Constant.Master); |
| | | String menuId = masterAuthor.getMenuId(); |
| | | SysMenu sysMenu = menuService.selectById(menuId); |
| | | if (sysMenu == null) { |
| | | continue; |
| | | } |
| | | subscribeEntity.setName(sysMenu.getName()); |
| | | String maintainFieldId = masterAuthor.getMaintainFieldId(); |
| | | String tableName = masterAuthor.getTableName(); |
| | | Maintain nowVersion = maintainService.getNowVersion(tableName); |
| | | if (nowVersion == null) { |
| | | continue; |
| | | } |
| | | List<Maintain> maintainByMaintainField = maintainFieldService.getMaintainByMaintainField(maintainFieldId, tableName); |
| | | long count = maintainByMaintainField.stream().filter(maintain -> maintain.getId().equalsIgnoreCase(nowVersion.getId())).count(); |
| | | if (count == 1) { |
| | | result.add(subscribeEntity); |
| | | } |
| | | |
| | | } |
| | | |
| | | //view |
| | | List<SysView> sysViews = viewService.selectList(new EntityWrapper<SysView>().eq(Constant.USERID, userId)); |
| | | List<SubscribeEntity> viewList = sysViews.stream() |
| | | .map(view -> new SubscribeEntity().setId(view.getId()).setName(view.getName()).setType(Constant.View)) |
| | | .collect(Collectors.toList()); |
| | | result.addAll(viewList); |
| | | |
| | | return Result.success(result); |
| | | } |
| | | catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return Result.error(CodeMsg.ERROR_SAVE_TOKEN); |
| | | } |
| | | |
| | | } |
| | | |
| | | @RequestMapping(value = "/getSubscribeList", method = RequestMethod.GET) |
| | | public Result getSubscribeList(@RequestParam String token, HttpServletResponse response) { |
| | | String userId = redisClient.getRedisVal(token); |
| | | if (StringUtils.isEmpty(userId)) { |
| | | return Result.error(CodeMsg.ERROR_FIND_TOKEN); |
| | | } |
| | | TUser user = DbUtils.getUserById(userId); |
| | | if (user == null) { |
| | | return Result.error(CodeMsg.USER_NOT_MATHED); |
| | | } |
| | | List<SysMenu> menuList = masterAuthorService.getMenu(user); |
| | | |
| | | // maintainFieldService.getMaintainByMaintainField() |
| | | return null; |
| | | } |
| | | |
| | | @RequestMapping(value = "/test", method = RequestMethod.GET) |
| | | public String test(@RequestParam String tableName, HttpServletRequest request) { |
| | | TUser user = DbUtils.getUser(request); |
| | |
| | | public Result login(HttpServletRequest request) { |
| | | TUser user = DbUtils.getUser(request); |
| | | String token = DbUtils.getUUID(32); |
| | | boolean b = redisClient.putRedisVal(user.getId(), token); |
| | | boolean b = redisClient.putRedisVal(token, user.getId()); |
| | | if (b) { |
| | | return Result.success(token); |
| | | }else { |
| | | return Result.success(CodeMsg.ERROR_SAVE_TOKEN); |
| | | } |
| | | } |
| | | |
| | | |
| | | @RequestMapping(value = "/key", method = RequestMethod.GET) |
| | | public Result key(@RequestParam String token, HttpServletRequest request) { |
| | | try{ |
| | | String redisVal = redisClient.getRedisVal(token); |
| | | if (StringUtils.isEmpty(redisVal)) { |
| | | return Result.error(CodeMsg.ERROR_FIND_TOKEN); |
| | | } |
| | | //todo aes KEY; |
| | | return null; |
| | | } |
| | | catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return Result.error(CodeMsg.ERROR_SAVE_TOKEN); |
| | | } |
| | | |
| | | } |
| | | } |