RwdObjectController.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package com.persagy.calendar.controller;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import org.springframework.beans.BeanUtils;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.validation.annotation.Validated;
  8. import org.springframework.web.bind.annotation.RequestBody;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import com.alibaba.fastjson.JSON;
  13. import com.alibaba.fastjson.JSONArray;
  14. import com.alibaba.fastjson.JSONObject;
  15. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  16. import com.google.common.collect.Lists;
  17. import com.google.common.collect.Sets;
  18. import com.persagy.calendar.client.model.BaseModel;
  19. import com.persagy.calendar.client.model.RwdObjectModel;
  20. import com.persagy.calendar.constant.WorkCalendarConstant;
  21. import com.persagy.calendar.pojo.dto.WorkCalendar;
  22. import com.persagy.calendar.pojo.dto.WorkCalendarObject;
  23. import com.persagy.calendar.pojo.vo.RwdObjectQueryVO;
  24. import com.persagy.calendar.pojo.vo.WorkCalendarObjectUpdateVO;
  25. import com.persagy.calendar.pojo.vo.WorkCalendarQueryVO;
  26. import com.persagy.calendar.service.IWorkCalendarObjectService;
  27. import com.persagy.calendar.service.IWorkCalendarService;
  28. import com.persagy.calendar.service.RwdObjectService;
  29. import com.persagy.common.enums.ResponseCode;
  30. import com.persagy.common.utils.DateUtil;
  31. import com.persagy.common.utils.ResponseResult;
  32. import com.persagy.common.utils.ResponseUtil;
  33. import com.persagy.common.utils.StringUtil;
  34. import cn.hutool.core.collection.CollectionUtil;
  35. import io.swagger.annotations.Api;
  36. import io.swagger.annotations.ApiOperation;
  37. /**
  38. * @version
  39. * @description
  40. * @company persagy
  41. * @author zhangqiankun
  42. * @since 2021年3月2日: 下午3:16:15
  43. */
  44. @RestController
  45. @Api(tags = "物理世界对象管理")
  46. @RequestMapping(value = "/v2.0/rwdObject", method = RequestMethod.POST)
  47. public class RwdObjectController {
  48. @Autowired
  49. private RwdObjectService rwdObjectService;
  50. @Autowired
  51. private IWorkCalendarService workCalendarService;
  52. @Autowired
  53. private IWorkCalendarObjectService workCalendarObjectService;
  54. /**
  55. * 物理世界对象列表查询
  56. * @param queryVO
  57. * @return
  58. */
  59. @ApiOperation(value = "物理世界对象列表查询")
  60. @RequestMapping(value = "queryRwdObjects")
  61. public ResponseResult queryRwdObjects(@RequestBody @Validated RwdObjectQueryVO queryVO) {
  62. BaseModel baseModel = new BaseModel();
  63. BeanUtils.copyProperties(queryVO, baseModel);
  64. RwdObjectModel objectModel = new RwdObjectModel();
  65. BeanUtils.copyProperties(queryVO, objectModel);
  66. return this.rwdObjectService.queryRwdObjects(baseModel, objectModel);
  67. }
  68. /**
  69. * 查询工作历对象列表
  70. * @param queryVO
  71. * @return
  72. */
  73. @ApiOperation(value = "查询工作历对象列表")
  74. @RequestMapping(value = "queryCalendarObjectList")
  75. public ResponseResult queryCalendarObjectList(@RequestBody @Validated WorkCalendarQueryVO queryVO) {
  76. QueryWrapper<WorkCalendarObject> queryWrapper = null;
  77. if (StringUtil.isNotBlank(queryVO.getObjId()) && StringUtil.isBlank(queryVO.getId())) {
  78. queryWrapper = new WorkCalendarObject.Builder().createQueryWrapper()
  79. .groupCodeEq(queryVO.getGroupCode()).projectIdEq(queryVO.getProjectId())
  80. .objectIdEq(queryVO.getObjId()).builderQueryWrapper();
  81. WorkCalendarObject calendarObject = this.workCalendarObjectService.getOne(queryWrapper);
  82. if (calendarObject == null) {
  83. return ResponseUtil.successResult(Lists.newArrayList(), 0L);
  84. }
  85. queryVO.setId(calendarObject.getCalendarId());
  86. }
  87. // 默认只查正常的
  88. QueryWrapper<WorkCalendar> calendarWrapper = new WorkCalendar.Builder().createQueryWrapper()
  89. .idEq(queryVO.getId()).groupCodeEq(queryVO.getGroupCode())
  90. .projectIdEq(queryVO.getProjectId()).calendarNameLike(queryVO.getCalendarName())
  91. .delFlagEq(WorkCalendarConstant.WORK_CALENDAR_DEL_FLAG_NOR).builderQueryWrapper();
  92. List<WorkCalendar> list = this.workCalendarService.list(calendarWrapper);
  93. if (CollectionUtil.isEmpty(list)) {
  94. return ResponseUtil.successResult(Lists.newArrayList(), 0L);
  95. }
  96. // 遍历获取对象名称
  97. BaseModel baseModel = new BaseModel();
  98. BeanUtils.copyProperties(queryVO, baseModel);
  99. RwdObjectModel objectModel = new RwdObjectModel();
  100. objectModel.setObjType(Sets.newHashSet("space", "floor", "building", "project"));
  101. for (WorkCalendar workCalendar : list) {
  102. queryWrapper = new WorkCalendarObject.Builder().createQueryWrapper()
  103. .groupCodeEq(queryVO.getGroupCode()).projectIdEq(queryVO.getProjectId())
  104. .calendarIdEq(workCalendar.getId()).builderQueryWrapper();
  105. List<WorkCalendarObject> calendarObjects = this.workCalendarObjectService.list(queryWrapper);
  106. if (CollectionUtil.isNotEmpty(calendarObjects)) {
  107. List<JSONObject> objects = workCalendar.getObjects();
  108. objects = CollectionUtil.isEmpty(objects) ? new ArrayList<JSONObject>() : objects;
  109. for (WorkCalendarObject object : calendarObjects) {
  110. objectModel.setId(object.getObjectId());
  111. ResponseResult result = this.rwdObjectService.queryRwdObjects(baseModel, objectModel);
  112. Object content = result.getContent();
  113. if (content != null) {
  114. JSONArray array = JSONArray.parseArray(JSON.toJSONString(content));
  115. if (array != null && array.size() > 0) {
  116. JSONObject jsonObject = array.getJSONObject(0);
  117. String objectName = jsonObject.getString("localName");
  118. jsonObject = new JSONObject();
  119. jsonObject.put("objectName", objectName);
  120. jsonObject.put("objectId", object.getObjectId());
  121. objects.add(jsonObject);
  122. }
  123. }
  124. }
  125. workCalendar.setObjects(objects);
  126. }
  127. }
  128. return ResponseUtil.successResult(list, (long)list.size());
  129. }
  130. /**
  131. * 添加对象工作历
  132. */
  133. @ApiOperation(value = "添加对象工作历")
  134. @RequestMapping(value = "addCalendarObject")
  135. public ResponseResult addCalendarObject(@RequestBody @Validated WorkCalendarObjectUpdateVO updateVO) {
  136. if (StringUtil.isBlank(updateVO.getCalendarId())) {
  137. return ResponseUtil.errorResult(ResponseCode.A0402.getCode(), "工作历ID不可为空");
  138. }
  139. if (StringUtil.isBlank(updateVO.getObjId()) && CollectionUtil.isEmpty(updateVO.getObjIds())) {
  140. return ResponseUtil.errorResult(ResponseCode.A0402.getCode(), "对象ID或对象ID集合,必须存在其一");
  141. }
  142. WorkCalendarObject calendarObject = new WorkCalendarObject();
  143. BeanUtils.copyProperties(updateVO, calendarObject);
  144. if (CollectionUtil.isEmpty(updateVO.getObjIds())) {
  145. updateVO.setObjIds(Sets.newHashSet(updateVO.getObjId()));
  146. } else {
  147. updateVO.getObjIds().add(updateVO.getObjId());
  148. }
  149. this.workCalendarObjectService.batchInsertObjects(calendarObject, updateVO.getObjIds());
  150. return ResponseUtil.successResult("添加成功");
  151. }
  152. /**
  153. * 删除加对象工作历
  154. */
  155. @ApiOperation(value = "删除加对象工作历")
  156. @RequestMapping(value = "deleteCalendarObject")
  157. public ResponseResult deleteCalendarObject(@RequestBody @Validated WorkCalendarObjectUpdateVO updateVO) {
  158. if (StringUtil.isBlank(updateVO.getObjId()) && CollectionUtil.isEmpty(updateVO.getObjIds())) {
  159. return ResponseUtil.errorResult(ResponseCode.A0402.getCode(), "对象ID或对象ID集合,必须存在其一");
  160. }
  161. WorkCalendarObject calendarObject = new WorkCalendarObject();
  162. BeanUtils.copyProperties(updateVO, calendarObject);
  163. if (CollectionUtil.isEmpty(updateVO.getObjIds())) {
  164. updateVO.setObjIds(Sets.newHashSet(updateVO.getObjId()));
  165. } else {
  166. updateVO.getObjIds().add(updateVO.getObjId());
  167. }
  168. QueryWrapper<WorkCalendarObject> queryWrapper = new WorkCalendarObject.Builder().createQueryWrapper()
  169. .groupCodeEq(updateVO.getGroupCode()).calendarIdEq(updateVO.getCalendarId())
  170. .projectIdEq(updateVO.getProjectId()).objectIdIn(updateVO.getObjIds()).builderQueryWrapper();
  171. boolean result = this.workCalendarObjectService.remove(queryWrapper);
  172. return result ? ResponseUtil.successResult("删除成功") : ResponseUtil.errorResult("删除失败");
  173. }
  174. /**
  175. * 对象变更所属工作历
  176. */
  177. @ApiOperation(value = "对象变更所属工作历")
  178. @RequestMapping(value = "changeCalendarObject")
  179. public ResponseResult changeCalendarObject(@RequestBody @Validated WorkCalendarObjectUpdateVO updateVO) {
  180. if (StringUtil.isBlank(updateVO.getCalendarId())) {
  181. return ResponseUtil.errorResult(ResponseCode.A0402.getCode(), "工作历ID不可为空");
  182. }
  183. if (CollectionUtil.isEmpty(updateVO.getObjIds())) {
  184. return ResponseUtil.errorResult(ResponseCode.A0402.getCode(), "对象ID集合不可为空");
  185. }
  186. WorkCalendarObject calendarObject = new WorkCalendarObject();
  187. BeanUtils.copyProperties(updateVO, calendarObject);
  188. calendarObject.setUpdateUser(updateVO.getUserId());
  189. calendarObject.setCreateTime(DateUtil.format(new Date(), DateUtil.FORMAT_DATE_YYYYMMDDHHMMSS));
  190. boolean result = this.workCalendarObjectService.replaceCalendarObject(calendarObject, updateVO.getObjIds());
  191. return result ? ResponseUtil.successResult("操作成功") : ResponseUtil.errorResult("操作失败");
  192. }
  193. }