UserDemandApiController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. * 下载技术需求批量导入Excel模板
  45. */
  46. @RequestMapping(value = "/downloadTemplate", method = RequestMethod.GET)
  47. public Result downloadTemplateFile(HttpServletResponse response, String sign) {
  48. Result res = new Result();
  49. AttachmentType attachmentType = AttachmentType.getField(sign);
  50. if (attachmentType == AttachmentType.DEMAND_TEMPLATE) {
  51. String fileName = "";
  52. AftFile af = aftFileService.selectAftFileBySign(sign);
  53. if (null == af) {
  54. res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "", "找不到文件!"));
  55. } else {
  56. String path = af.getFilePath();
  57. String suffix = path.substring(path.lastIndexOf("."));
  58. fileName = AttachmentType.DEMAND_TEMPLATE.getDesc() + suffix;
  59. downloadFile(response, fileName, path);
  60. }
  61. } else {
  62. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
  63. }
  64. return res;
  65. }
  66. /**
  67. * 用户需求列表
  68. */
  69. @RequestMapping(value = "/list", method = RequestMethod.GET)
  70. public Result list(String name,String startDate,String endDate,Integer pageNo, Integer pageSize) {
  71. Result res = new Result();
  72. res.setData(demandService.listMyDemand(name, startDate, endDate, pageNo, pageSize));
  73. return res;
  74. }
  75. /**
  76. * 新增需求
  77. */
  78. @RequestMapping(value = "/apply", method = RequestMethod.POST)
  79. public Result userApply(@Valid InputDemand demand, BindingResult bindingResult,
  80. @RequestParam(name = "keywords[]", required = false) String[] keywords,
  81. @RequestParam(value = "publishPages[]", required = false)String[] publishPages,
  82. String validityPeriodFormattedDate) {
  83. Result res = new Result();
  84. if (bindingResult.hasErrors()) {
  85. res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
  86. DemandFields.getFieldDesc(bindingResult.getFieldError().getField())));
  87. return res;
  88. }
  89. res = disposeDemand(res, demand, keywords,publishPages);
  90. if (!res.getError().isEmpty()) {
  91. return res;
  92. }
  93. Demand d = new Demand();
  94. BeanUtils.copyProperties(demand, d);
  95. d.setEmployerId(TokenManager.getUserId());
  96. List<String> webPages = new ArrayList<String>();
  97. List<String> appPages = new ArrayList<String>();
  98. PageConstants.putDemand(publishPages, webPages, appPages);
  99. if(webPages.size()==0 && appPages.size() == 0){
  100. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "页面参数错误"));
  101. return res;
  102. }
  103. demandService.saveDemand(d, validityPeriodFormattedDate, keywords,webPages, appPages);
  104. return res;
  105. }
  106. @RequestMapping(value = "/demandDetail", method = RequestMethod.GET)
  107. public Result demandDetail(String id){
  108. Result res = new Result();
  109. if (StringUtils.isBlank(id)) {
  110. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
  111. return res;
  112. }
  113. res.setData(demandService.selectDemandDetail(id));
  114. return res;
  115. }
  116. /**
  117. * 组织用户详情
  118. */
  119. @RequestMapping(value = "/orgDemandDetail", method = RequestMethod.GET)
  120. public Result orgDemandDetail(String id) {
  121. Result res = new Result();
  122. if (StringUtils.isBlank(id)) {
  123. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
  124. return res;
  125. }
  126. res.setData(demandService.selectOrgDemandDetail(id));
  127. return res;
  128. }
  129. /**
  130. * 个人需求详情
  131. */
  132. @RequestMapping(value = "/userDemandDetail", method = RequestMethod.GET)
  133. public Result userDemandDetail(String id) {
  134. Result res = new Result();
  135. if (StringUtils.isBlank(id)) {
  136. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
  137. return res;
  138. }
  139. res.setData(demandService.selectUserDemandDetail(id));
  140. return res;
  141. }
  142. /**
  143. * 修改需求
  144. */
  145. @RequestMapping(value = "/updateDemand", method = RequestMethod.POST)
  146. public Result updateDemand(@Valid InputDemand demand, BindingResult bindingResult,
  147. @RequestParam(name = "keywords[]", required = false) String[] keywords,
  148. @RequestParam(value = "publishPages[]", required = false) String[] publishPages,
  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.UNAUDITED.getCode().equals(demand.getAuditStatus())
  162. && !DemandAuditStatus.REVOKE.getCode().equals(demand.getAuditStatus())) {
  163. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "当前状态无法提交审核!"));
  164. return res;
  165. }
  166. //demand.setAuditStatus(DemandAuditStatus.INAUDIT.getCode()); 两步操作,先保存后提交
  167. res = disposeDemand(res, demand, keywords,publishPages);
  168. if (!res.getError().isEmpty()) {
  169. return res;
  170. }
  171. List<String> webPages = new ArrayList<String>();
  172. List<String> appPages = new ArrayList<String>();
  173. PageConstants.putDemand(publishPages, webPages, appPages);
  174. if(webPages.size()==0 && appPages.size() == 0){
  175. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "页面参数错误"));
  176. return res;
  177. }
  178. Demand d = new Demand();
  179. BeanUtils.copyProperties(demand, d);
  180. d.setEmployerId(TokenManager.getUserId());
  181. res.setData(demandService.updateUserDemand(d, validityPeriodFormattedDate, keywords, webPages,appPages));
  182. return res;
  183. }
  184. @RequestMapping(value = "/publishDemand", method = RequestMethod.POST)
  185. public Result publishDemand(String id,Integer auditStatus){
  186. Result res = new Result();
  187. if (StringUtils.isBlank(id)) {
  188. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
  189. return res;
  190. }
  191. if (null == auditStatus) {
  192. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "需求状态错误", "需求状态"));
  193. return res;
  194. }
  195. if (DemandAuditStatus.CREATE.getCode() != auditStatus
  196. && DemandAuditStatus.UNAUDITED.getCode() != auditStatus
  197. && DemandAuditStatus.REVOKE.getCode() != auditStatus) {
  198. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "当前状态无法提交审核!"));
  199. return res;
  200. }
  201. Demand d = new Demand();
  202. d.setId(id);
  203. d.setAuditStatus(DemandAuditStatus.INAUDIT.getCode());
  204. demandService.updateByPrimaryKeySelective(d);
  205. return res;
  206. }
  207. /**
  208. * 需求撤消发布(下架)
  209. */
  210. @RequestMapping(value = "/offShelf", method = RequestMethod.POST)
  211. public Result offShelf(String id) {
  212. Result res = new Result();
  213. if (StringUtils.isBlank(id)) {
  214. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
  215. return res;
  216. }
  217. Demand d = demandService.selectByPrimaryKey(id);
  218. if (null == d) {
  219. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
  220. return res;
  221. }
  222. res.setData(demandService.updateReleaseStatus(d));
  223. return res;
  224. }
  225. /**
  226. * 删除需求(个人&团体)
  227. */
  228. @RequestMapping(value = "/delete", method = RequestMethod.POST)
  229. public Result delete(@RequestParam(name = "ids[]", required = false) String[] ids) {
  230. Result res = new Result();
  231. if (ids == null || ids.length != 1) {
  232. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", ""));
  233. } else {
  234. List<DemandOrder> list = demandOrderService.selectDemandOrderByDemandId(ids[0]);
  235. for (DemandOrder order : list){
  236. if (!DemandOrderStatus.CREATE.getCode().equals(order.getStatus())){
  237. res.getError().add(buildError("", "当前科技需求有订单,无法删除!"));
  238. return res;
  239. }
  240. }
  241. res.setData(demandService.deleteByPrimaryKey(Arrays.asList(ids)));
  242. }
  243. return res;
  244. }
  245. /**
  246. * 需求资料--图片上传
  247. */
  248. @RequestMapping(value = "/uploadPicture", method = RequestMethod.POST)
  249. public Result uploadPicture(HttpServletRequest req, String sign) {
  250. Result res = new Result();
  251. AttachmentType attachmentType = AttachmentType.getField(sign);
  252. if (attachmentType == AttachmentType.DEMAND_PICTURE) {
  253. res.setData(handleFiles(res, "/demand/", false, req, sign, TokenManager.getUserId()));
  254. } else {
  255. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
  256. }
  257. return res;
  258. }
  259. /**
  260. * 需求资料--文本文件上传
  261. */
  262. @RequestMapping(value = "/uploadTextFile", method = RequestMethod.POST)
  263. public Result uploadTextFile(HttpServletRequest req, String sign) {
  264. Result res = new Result();
  265. AttachmentType attachmentType = AttachmentType.getField(sign);
  266. if (attachmentType == AttachmentType.DEMAND_TEXT_FILE) {
  267. res.setData(handleFiles(res, "/demand/", false, req, sign, TokenManager.getUserId()));
  268. } else {
  269. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
  270. }
  271. return res;
  272. }
  273. /**
  274. * 下载需求文件--文本文件
  275. */
  276. @RequestMapping(value = "/download", method = RequestMethod.GET)
  277. public Result download(HttpServletResponse response, String id) {
  278. Result res = new Result();
  279. if (StringUtils.isEmpty(id)) {
  280. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
  281. return res;
  282. }
  283. Demand d = demandService.selectByPrimaryKey(id);
  284. if (null == d) {
  285. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
  286. return res;
  287. }
  288. downloadUnPrivateFile(response, d.getTextFileDownloadFileName(), d.getTextFileUrl());
  289. return res;
  290. }
  291. private Result disposeDemand(Result res, InputDemand demand, String[] keywords,String[] publishPages) {
  292. if (StringUtils.isBlank(demand.getName())) {
  293. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求名称", "需求名称"));
  294. return res;
  295. }
  296. if (!DemandDataCategory.USERDEMAND.getCode().equals(demand.getDataCategory())
  297. && !DemandDataCategory.ORGDEMAND.getCode().equals(demand.getDataCategory())) {
  298. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "数据类型"));
  299. return res;
  300. }
  301. /*if (StringUtils.isBlank(demand.getKeyword())) {
  302. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到关键词", "关键词"));
  303. return res;
  304. }
  305. if (null == keywords || keywords.length < 1) {
  306. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到关键词", "关键词"));
  307. return res;
  308. }*/
  309. if (null == demand.getIndustryCategoryA()) {
  310. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到行业类别", "行业类别"));
  311. return res;
  312. }
  313. if (null == demand.getDemandType()) {
  314. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求类型", "需求类型"));
  315. return res;
  316. }
  317. if (StringUtils.isBlank(demand.getProblemDes())) {
  318. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到问题说明", "问题说明"));
  319. return res;
  320. }
  321. for (int i = 0; i < keywords.length; i++) {
  322. if (AFTConstants.KEYWORDLENTH < keywords[i].length()) {
  323. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "关键词长度"));
  324. return res;
  325. }
  326. }
  327. return res;
  328. }
  329. }