DemandApiController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. package com.goafanti.demand.controller;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import javax.annotation.Resource;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.validation.Valid;
  8. import org.springframework.beans.BeanUtils;
  9. import org.springframework.validation.BindingResult;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import com.goafanti.admin.service.AftFileService;
  15. import com.goafanti.common.bo.Result;
  16. import com.goafanti.common.constant.AFTConstants;
  17. import com.goafanti.common.constant.ErrorConstants;
  18. import com.goafanti.common.controller.CertifyApiController;
  19. import com.goafanti.common.enums.AttachmentType;
  20. import com.goafanti.common.enums.DemandAuditStatus;
  21. import com.goafanti.common.enums.DemandDataCategory;
  22. import com.goafanti.common.enums.DemandFields;
  23. import com.goafanti.common.enums.DemandOrderStatus;
  24. import com.goafanti.common.model.AftFile;
  25. import com.goafanti.common.model.Demand;
  26. import com.goafanti.common.model.DemandOrder;
  27. import com.goafanti.common.utils.StringUtils;
  28. import com.goafanti.core.shiro.token.TokenManager;
  29. import com.goafanti.demand.bo.InputDemand;
  30. import com.goafanti.demand.service.DemandOrderService;
  31. import com.goafanti.demand.service.DemandService;
  32. @RestController
  33. @RequestMapping(value = "/api/user/demand")
  34. public class DemandApiController extends CertifyApiController {
  35. @Resource
  36. private DemandService demandService;
  37. @Resource
  38. private AftFileService aftFileService;
  39. @Resource
  40. private DemandOrderService demandOrderService;
  41. /**
  42. * 成果需求匹配列表
  43. */
  44. @RequestMapping(value = "/achievementDemand", method = RequestMethod.GET)
  45. public Result achievementDemand(String id) {
  46. Result res = new Result();
  47. res.setData(demandService.selectAchievementDemandListByDemandId(id));
  48. return res;
  49. }
  50. /**
  51. * 下载技术需求批量导入Excel模板
  52. */
  53. @RequestMapping(value = "/downloadTemplate", method = RequestMethod.GET)
  54. public Result downloadTemplateFile(HttpServletResponse response, String sign) {
  55. Result res = new Result();
  56. AttachmentType attachmentType = AttachmentType.getField(sign);
  57. if (attachmentType == AttachmentType.DEMAND_TEMPLATE) {
  58. String fileName = "";
  59. AftFile af = aftFileService.selectAftFileBySign(sign);
  60. if (null == af) {
  61. res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "", "找不到文件!"));
  62. } else {
  63. String path = af.getFilePath();
  64. String suffix = path.substring(path.lastIndexOf("."));
  65. fileName = AttachmentType.DEMAND_TEMPLATE.getDesc() + suffix;
  66. downloadFile(response, fileName, path);
  67. }
  68. } else {
  69. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
  70. }
  71. return res;
  72. }
  73. /**
  74. * 用户需求列表
  75. */
  76. @RequestMapping(value = "/list", method = RequestMethod.GET)
  77. public Result list(Integer auditStatus, Integer serialNumber, String name, String keyword, Integer demandType,
  78. String validityPeriodStartDate, String validityPeriodEndDate, Integer status, Integer releaseStatus,
  79. String releaseDateStartDate, String releaseDateEndDate, String pageNo, String pageSize) {
  80. Result res = new Result();
  81. Integer pNo = 1;
  82. Integer pSize = 10;
  83. if (StringUtils.isNumeric(pageSize)) {
  84. pSize = Integer.parseInt(pageSize);
  85. }
  86. if (StringUtils.isNumeric(pageNo)) {
  87. pNo = Integer.parseInt(pageNo);
  88. }
  89. res.setData(demandService.listDemand(auditStatus, serialNumber, name, keyword, demandType,
  90. validityPeriodStartDate, validityPeriodEndDate, status, releaseStatus, releaseDateStartDate,
  91. releaseDateEndDate, pNo, pSize));
  92. return res;
  93. }
  94. /**
  95. * 新增需求
  96. */
  97. @RequestMapping(value = "/apply", method = RequestMethod.POST)
  98. public Result userApply(@Valid InputDemand demand, BindingResult bindingResult,
  99. @RequestParam(name = "keywords[]", required = false) String[] keywords,
  100. String validityPeriodFormattedDate) {
  101. Result res = new Result();
  102. if (bindingResult.hasErrors()) {
  103. res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
  104. DemandFields.getFieldDesc(bindingResult.getFieldError().getField())));
  105. return res;
  106. }
  107. res = disposeDemand(res, demand, keywords);
  108. if (!res.getError().isEmpty()) {
  109. return res;
  110. }
  111. Demand d = new Demand();
  112. BeanUtils.copyProperties(demand, d);
  113. d.setEmployerId(TokenManager.getUserId());
  114. demandService.saveDemand(d, validityPeriodFormattedDate, keywords);
  115. return res;
  116. }
  117. /**
  118. * 组织用户详情
  119. */
  120. @RequestMapping(value = "/orgDemandDetail", method = RequestMethod.GET)
  121. public Result orgDemandDetail(String id) {
  122. Result res = new Result();
  123. if (StringUtils.isBlank(id)) {
  124. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
  125. return res;
  126. }
  127. res.setData(demandService.selectOrgDemandDetail(id));
  128. return res;
  129. }
  130. /**
  131. * 个人需求详情
  132. */
  133. @RequestMapping(value = "/userDemandDetail", method = RequestMethod.GET)
  134. public Result userDemandDetail(String id) {
  135. Result res = new Result();
  136. if (StringUtils.isBlank(id)) {
  137. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
  138. return res;
  139. }
  140. res.setData(demandService.selectUserDemandDetail(id));
  141. return res;
  142. }
  143. /**
  144. * 修改需求
  145. */
  146. @RequestMapping(value = "/update", method = RequestMethod.POST)
  147. public Result updateUser(@Valid InputDemand demand, BindingResult bindingResult,
  148. @RequestParam(name = "keywords[]", required = false) String[] keywords,
  149. String validityPeriodFormattedDate) {
  150. Result res = new Result();
  151. if (bindingResult.hasErrors()) {
  152. res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
  153. DemandFields.getFieldDesc(bindingResult.getFieldError().getField())));
  154. return res;
  155. }
  156. if (StringUtils.isBlank(demand.getId())) {
  157. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
  158. return res;
  159. }
  160. if (!DemandAuditStatus.CREATE.getCode().equals(demand.getAuditStatus())
  161. && !DemandAuditStatus.SUBMIT.getCode().equals(demand.getAuditStatus())
  162. && !DemandAuditStatus.UNAUDITED.getCode().equals(demand.getAuditStatus())) {
  163. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "当前状态无法提交审核!"));
  164. return res;
  165. }
  166. res = disposeDemand(res, demand, keywords);
  167. if (!res.getError().isEmpty()) {
  168. return res;
  169. }
  170. Demand d = new Demand();
  171. BeanUtils.copyProperties(demand, d);
  172. d.setEmployerId(TokenManager.getUserId());
  173. res.setData(demandService.updateUserDemand(d, validityPeriodFormattedDate, keywords));
  174. return res;
  175. }
  176. /**
  177. * 需求撤消发布(下架)
  178. */
  179. @RequestMapping(value = "/offShelf", method = RequestMethod.POST)
  180. public Result offShelf(String id) {
  181. Result res = new Result();
  182. if (StringUtils.isBlank(id)) {
  183. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
  184. return res;
  185. }
  186. Demand d = demandService.selectByPrimaryKey(id);
  187. if (null == d) {
  188. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
  189. return res;
  190. }
  191. res.setData(demandService.updateReleaseStatus(d));
  192. return res;
  193. }
  194. /**
  195. * 删除需求(个人&团体)
  196. */
  197. @RequestMapping(value = "/delete", method = RequestMethod.POST)
  198. public Result delete(@RequestParam(name = "ids[]", required = false) String[] ids) {
  199. Result res = new Result();
  200. if (ids == null || ids.length != 1) {
  201. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", ""));
  202. } else {
  203. List<DemandOrder> list = demandOrderService.selectDemandOrderByDemandId(ids[0]);
  204. for (DemandOrder order : list){
  205. if (!DemandOrderStatus.CREATE.getCode().equals(order.getStatus())){
  206. res.getError().add(buildError("", "当前科技需求有订单,无法删除!"));
  207. return res;
  208. }
  209. }
  210. res.setData(demandService.deleteByPrimaryKey(Arrays.asList(ids)));
  211. }
  212. return res;
  213. }
  214. /**
  215. * 需求资料--图片上传
  216. */
  217. @RequestMapping(value = "/uploadPicture", method = RequestMethod.POST)
  218. public Result uploadPicture(HttpServletRequest req, String sign) {
  219. Result res = new Result();
  220. AttachmentType attachmentType = AttachmentType.getField(sign);
  221. if (attachmentType == AttachmentType.DEMAND_PICTURE) {
  222. res.setData(handleFiles(res, "/demand/", false, req, sign, TokenManager.getUserId()));
  223. } else {
  224. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
  225. }
  226. return res;
  227. }
  228. /**
  229. * 需求资料--文本文件上传
  230. */
  231. @RequestMapping(value = "/uploadTextFile", method = RequestMethod.POST)
  232. public Result uploadTextFile(HttpServletRequest req, String sign) {
  233. Result res = new Result();
  234. AttachmentType attachmentType = AttachmentType.getField(sign);
  235. if (attachmentType == AttachmentType.DEMAND_TEXT_FILE) {
  236. res.setData(handleFiles(res, "/demand/", false, req, sign, TokenManager.getUserId()));
  237. } else {
  238. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
  239. }
  240. return res;
  241. }
  242. /**
  243. * 下载需求文件--文本文件
  244. */
  245. @RequestMapping(value = "/download", method = RequestMethod.GET)
  246. public Result download(HttpServletResponse response, String id) {
  247. Result res = new Result();
  248. if (StringUtils.isEmpty(id)) {
  249. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
  250. return res;
  251. }
  252. Demand d = demandService.selectByPrimaryKey(id);
  253. if (null == d) {
  254. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
  255. return res;
  256. }
  257. downloadUnPrivateFile(response, d.getTextFileDownloadFileName(), d.getTextFileUrl());
  258. return res;
  259. }
  260. private Result disposeDemand(Result res, InputDemand demand, String[] keywords) {
  261. if (StringUtils.isBlank(demand.getName())) {
  262. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求名称", "需求名称"));
  263. return res;
  264. }
  265. if (!DemandDataCategory.USERDEMAND.getCode().equals(demand.getDataCategory())
  266. && !DemandDataCategory.ORGDEMAND.getCode().equals(demand.getDataCategory())) {
  267. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "数据类型"));
  268. return res;
  269. }
  270. if (StringUtils.isBlank(demand.getKeyword())) {
  271. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到关键词", "关键词"));
  272. return res;
  273. }
  274. if (null == keywords || keywords.length < 1) {
  275. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到关键词", "关键词"));
  276. return res;
  277. }
  278. if (null == demand.getIndustryCategoryA()) {
  279. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到行业类别", "行业类别"));
  280. return res;
  281. }
  282. if (null == demand.getDemandType()) {
  283. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求类型", "需求类型"));
  284. return res;
  285. }
  286. if (StringUtils.isBlank(demand.getProblemDes())) {
  287. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到问题说明", "问题说明"));
  288. return res;
  289. }
  290. for (int i = 0; i < keywords.length; i++) {
  291. if (AFTConstants.KEYWORDLENTH < keywords[i].length()) {
  292. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "关键词长度"));
  293. return res;
  294. }
  295. }
  296. return res;
  297. }
  298. }