UserDemandApiController.java 11 KB

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