UserDemandApiController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package com.goafanti.demand.controller;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Date;
  5. import java.util.List;
  6. import javax.annotation.Resource;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import javax.validation.Valid;
  10. import org.springframework.beans.BeanUtils;
  11. import org.springframework.validation.BindingResult;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RequestMethod;
  14. import org.springframework.web.bind.annotation.RequestParam;
  15. import org.springframework.web.bind.annotation.RestController;
  16. import com.goafanti.admin.service.AftFileService;
  17. import com.goafanti.common.bo.Result;
  18. import com.goafanti.common.constant.AFTConstants;
  19. import com.goafanti.common.constant.ErrorConstants;
  20. import com.goafanti.common.constant.PageConstants;
  21. import com.goafanti.common.controller.CertifyApiController;
  22. import com.goafanti.common.enums.AttachmentType;
  23. import com.goafanti.common.enums.DemandAuditStatus;
  24. import com.goafanti.common.enums.DemandDataCategory;
  25. import com.goafanti.common.enums.DemandFields;
  26. import com.goafanti.common.enums.DemandOrderStatus;
  27. import com.goafanti.common.enums.UserType;
  28. import com.goafanti.common.model.AftFile;
  29. import com.goafanti.common.model.Demand;
  30. import com.goafanti.common.model.DemandOrder;
  31. import com.goafanti.common.utils.StringUtils;
  32. import com.goafanti.core.shiro.token.TokenManager;
  33. import com.goafanti.demand.bo.InputDemand;
  34. import com.goafanti.demand.service.DemandOrderService;
  35. import com.goafanti.demand.service.DemandService;
  36. @RestController
  37. @RequestMapping(value = "/api/user/demand")
  38. public class UserDemandApiController extends CertifyApiController {
  39. @Resource
  40. private DemandService demandService;
  41. @Resource
  42. private AftFileService aftFileService;
  43. @Resource
  44. private DemandOrderService demandOrderService;
  45. /**
  46. * 下载技术需求批量导入Excel模板
  47. */
  48. @RequestMapping(value = "/downloadTemplate", method = RequestMethod.GET)
  49. public Result downloadTemplateFile(HttpServletResponse response, String sign) {
  50. Result res = new Result();
  51. AttachmentType attachmentType = AttachmentType.getField(sign);
  52. if (attachmentType == AttachmentType.DEMAND_TEMPLATE) {
  53. String fileName = "";
  54. AftFile af = aftFileService.selectAftFileBySign(sign);
  55. if (null == af) {
  56. res.getError().add(buildError(ErrorConstants.FILE_NON_EXISTENT, "", "找不到文件!"));
  57. } else {
  58. String path = af.getFilePath();
  59. String suffix = path.substring(path.lastIndexOf("."));
  60. fileName = AttachmentType.DEMAND_TEMPLATE.getDesc() + suffix;
  61. downloadFile(response, fileName, path);
  62. }
  63. } else {
  64. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
  65. }
  66. return res;
  67. }
  68. /**
  69. * 用户需求列表
  70. */
  71. @RequestMapping(value = "/list", method = RequestMethod.GET)
  72. public Result list(String name,String startDate,String endDate,Integer pageNo, Integer pageSize) {
  73. Result res = new Result();
  74. res.setData(demandService.listMyDemand(name, startDate, endDate, pageNo, pageSize));
  75. return res;
  76. }
  77. /**
  78. * 新增需求
  79. */
  80. @RequestMapping(value = "/apply", method = RequestMethod.POST)
  81. public Result userApply(@Valid InputDemand demand, BindingResult bindingResult,
  82. @RequestParam(name = "keywords[]", required = false) String[] keywords,
  83. @RequestParam(name = "publishPages[]", required = false) String[] publishPages,
  84. String validityPeriodFormattedDate) {
  85. Result res = new Result();
  86. System.out.println("a1");
  87. if (bindingResult.hasErrors()) {
  88. res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
  89. DemandFields.getFieldDesc(bindingResult.getFieldError().getField())));
  90. return res;
  91. }
  92. res = disposeDemand(res, demand, keywords,publishPages);
  93. if (!res.getError().isEmpty()) {
  94. return res;
  95. }
  96. Demand d = new Demand();
  97. BeanUtils.copyProperties(demand, d);
  98. d.setEmployerId(TokenManager.getUserId());
  99. if(TokenManager.getUserToken().getType().intValue() == UserType.PERSONAL.getCode().intValue()){
  100. d.setDataCategory(DemandDataCategory.USERDEMAND.getCode());
  101. }else if(TokenManager.getUserToken().getType().intValue() == UserType.ORGANIZATION.getCode().intValue()){
  102. d.setDataCategory(DemandDataCategory.ORGDEMAND.getCode());
  103. }else{
  104. res.getError().add(buildError(ErrorConstants.USER_UN_AUTHENTICATION,"","用户未认证"));
  105. return res;
  106. }
  107. List<String> webPages = new ArrayList<String>();
  108. List<String> appPages = new ArrayList<String>();
  109. PageConstants.putDemand(publishPages, webPages, appPages);
  110. if(webPages.size()==0 && appPages.size() == 0){
  111. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "页面参数错误"));
  112. return res;
  113. }
  114. demandService.saveDemand(d, validityPeriodFormattedDate, keywords, webPages, appPages);
  115. return res;
  116. }
  117. @RequestMapping(value = "/demandDetail", method = RequestMethod.GET)
  118. public Result demandDetail(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.selectDemandDetail(id));
  125. return res;
  126. }
  127. /**
  128. * 组织用户详情
  129. */
  130. @RequestMapping(value = "/orgDemandDetail", method = RequestMethod.GET)
  131. public Result orgDemandDetail(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.selectOrgDemandDetail(id));
  138. return res;
  139. }
  140. /**
  141. * 个人需求详情
  142. */
  143. @RequestMapping(value = "/userDemandDetail", method = RequestMethod.GET)
  144. public Result userDemandDetail(String id) {
  145. Result res = new Result();
  146. if (StringUtils.isBlank(id)) {
  147. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
  148. return res;
  149. }
  150. res.setData(demandService.selectUserDemandDetail(id));
  151. return res;
  152. }
  153. /**
  154. * 修改需求
  155. */
  156. @RequestMapping(value = "/updateDemand", method = RequestMethod.POST)
  157. public Result updateDemand(@Valid InputDemand demand, BindingResult bindingResult,
  158. @RequestParam(name = "keywords[]", required = false) String[] keywords,
  159. @RequestParam(name = "publishPages[]", required = false) String[] publishPages,
  160. String validityPeriodFormattedDate) {
  161. Result res = new Result();
  162. if (bindingResult.hasErrors()) {
  163. res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
  164. DemandFields.getFieldDesc(bindingResult.getFieldError().getField())));
  165. return res;
  166. }
  167. if (StringUtils.isBlank(demand.getId())) {
  168. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
  169. return res;
  170. }
  171. if (!DemandAuditStatus.CREATE.getCode().equals(demand.getAuditStatus())
  172. && !DemandAuditStatus.UNAUDITED.getCode().equals(demand.getAuditStatus())
  173. && !DemandAuditStatus.REVOKE.getCode().equals(demand.getAuditStatus())) {
  174. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "当前状态无法提交审核!"));
  175. return res;
  176. }
  177. //demand.setAuditStatus(DemandAuditStatus.INAUDIT.getCode()); 两步操作,先保存后提交
  178. res = disposeDemand(res, demand, keywords, publishPages);
  179. if (!res.getError().isEmpty()) {
  180. return res;
  181. }
  182. List<String> webPages = new ArrayList<String>();
  183. List<String> appPages = new ArrayList<String>();
  184. PageConstants.putDemand(publishPages, webPages, appPages);
  185. if(webPages.size()==0 && appPages.size() == 0){
  186. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "页面参数错误"));
  187. return res;
  188. }
  189. Demand d = new Demand();
  190. BeanUtils.copyProperties(demand, d);
  191. d.setEmployerId(TokenManager.getUserId());
  192. res.setData(demandService.updateUserDemand(d, validityPeriodFormattedDate, keywords, webPages,appPages));
  193. return res;
  194. }
  195. /**
  196. * 发布需求
  197. * @param id
  198. * @param auditStatus
  199. * @return
  200. */
  201. @RequestMapping(value = "/publishDemand", method = RequestMethod.POST)
  202. public Result publishDemand(String id,Integer auditStatus){
  203. Result res = new Result();
  204. if (StringUtils.isBlank(id)) {
  205. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
  206. return res;
  207. }
  208. if (null == auditStatus) {
  209. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "需求状态错误", "需求状态"));
  210. return res;
  211. }
  212. if (DemandAuditStatus.CREATE.getCode() != auditStatus
  213. && DemandAuditStatus.UNAUDITED.getCode() != auditStatus
  214. && DemandAuditStatus.REVOKE.getCode() != auditStatus) {
  215. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "当前状态无法提交审核!"));
  216. return res;
  217. }
  218. Demand d = new Demand();
  219. d.setId(id);
  220. // d.setReleaseDate(new Date());
  221. d.setAuditStatus(DemandAuditStatus.INAUDIT.getCode());
  222. demandService.updateByPrimaryKeySelective(d);
  223. return res;
  224. }
  225. /**
  226. * 刷新发布时间
  227. * @param id
  228. * @param auditStatus
  229. * @return
  230. */
  231. @RequestMapping(value = "/refreshPublish", method = RequestMethod.POST)
  232. public Result refreshPublish(String id,Integer auditStatus){
  233. Result res = new Result();
  234. if (StringUtils.isBlank(id)) {
  235. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
  236. return res;
  237. }
  238. if (null == auditStatus) {
  239. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "需求状态错误", "需求状态"));
  240. return res;
  241. }
  242. if (DemandAuditStatus.AUDITED.getCode() != auditStatus) {
  243. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "当前状态无法刷新发布"));
  244. return res;
  245. }
  246. Demand d = new Demand();
  247. d.setId(id);
  248. d.setReleaseDate(new Date());
  249. demandService.updateByPrimaryKeySelective(d);
  250. return res;
  251. }
  252. /**
  253. * 需求撤消发布(下架)
  254. */
  255. @RequestMapping(value = "/offShelf", method = RequestMethod.POST)
  256. public Result offShelf(String id,Integer auditStatus) {
  257. Result res = new Result();
  258. if (StringUtils.isBlank(id)) {
  259. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
  260. return res;
  261. }
  262. Demand d = demandService.selectByPrimaryKey(id);
  263. if (null == d) {
  264. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
  265. return res;
  266. }
  267. if(DemandAuditStatus.AUDITED.getCode() != auditStatus){
  268. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "当前状态无法撤销发布"));
  269. return res;
  270. }
  271. demandService.updateReleaseStatus(d);
  272. return res;
  273. }
  274. /**
  275. * 删除需求(个人&团体)
  276. */
  277. @RequestMapping(value = "/delete", method = RequestMethod.POST)
  278. public Result delete(@RequestParam(name = "ids[]", required = false) String[] ids) {
  279. Result res = new Result();
  280. if (ids == null || ids.length != 1) {
  281. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", ""));
  282. } else {
  283. /* List<DemandOrder> list = demandOrderService.selectDemandOrderByDemandId(ids[0]);
  284. for (DemandOrder order : list){
  285. if (!DemandOrderStatus.CREATE.getCode().equals(order.getStatus())){
  286. res.getError().add(buildError("", "当前科技需求有订单,无法删除!"));
  287. return res;
  288. }
  289. }*/
  290. res.setData(demandService.deleteByPrimaryKey(Arrays.asList(ids)));
  291. }
  292. return res;
  293. }
  294. /**
  295. * 需求资料--图片上传
  296. */
  297. @RequestMapping(value = "/uploadPicture", method = RequestMethod.POST)
  298. public Result uploadPicture(HttpServletRequest req, String sign) {
  299. Result res = new Result();
  300. AttachmentType attachmentType = AttachmentType.getField(sign);
  301. if (attachmentType == AttachmentType.DEMAND_PICTURE) {
  302. res.setData(handleFiles(res, "/demand/", false, req, sign, TokenManager.getUserId()));
  303. } else {
  304. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
  305. }
  306. return res;
  307. }
  308. /**
  309. * 需求资料--文本文件上传
  310. */
  311. @RequestMapping(value = "/uploadTextFile", method = RequestMethod.POST)
  312. public Result uploadTextFile(HttpServletRequest req, String sign) {
  313. Result res = new Result();
  314. AttachmentType attachmentType = AttachmentType.getField(sign);
  315. /*if (attachmentType == AttachmentType.DEMAND_TEXT_FILE) {
  316. res.setData(handleFiles(res, "/demand/", false, req, sign, TokenManager.getUserId()));
  317. } else {
  318. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
  319. }*/
  320. return res;
  321. }
  322. /**
  323. * 下载需求文件--文本文件
  324. */
  325. @RequestMapping(value = "/download", method = RequestMethod.GET)
  326. public Result download(HttpServletResponse response, String id) {
  327. Result res = new Result();
  328. if (StringUtils.isEmpty(id)) {
  329. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
  330. return res;
  331. }
  332. Demand d = demandService.selectByPrimaryKey(id);
  333. if (null == d) {
  334. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
  335. return res;
  336. }
  337. downloadUnPrivateFile(response, d.getTextFileDownloadFileName(), d.getTextFileUrl());
  338. return res;
  339. }
  340. private Result disposeDemand(Result res, InputDemand demand, String[] keywords,String[] publishPages) {
  341. if (StringUtils.isBlank(demand.getName())) {
  342. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求名称", "需求名称"));
  343. return res;
  344. }
  345. /*if (!DemandDataCategory.USERDEMAND.getCode().equals(demand.getDataCategory())
  346. && !DemandDataCategory.ORGDEMAND.getCode().equals(demand.getDataCategory())) {
  347. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "数据类型"));
  348. return res;
  349. }*/
  350. if (StringUtils.isBlank(demand.getKeyword())) {
  351. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到关键词", "关键词"));
  352. return res;
  353. }
  354. if (null == keywords || keywords.length < 1) {
  355. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到关键词", "关键词"));
  356. return res;
  357. }
  358. if (null == demand.getIndustryCategoryA()) {
  359. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到行业类别", "行业类别"));
  360. return res;
  361. }
  362. if (null == demand.getDemandType()) {
  363. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求类型", "需求类型"));
  364. return res;
  365. }
  366. if (StringUtils.isBlank(demand.getProblemDes())) {
  367. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到问题说明", "问题说明"));
  368. return res;
  369. }
  370. for (int i = 0; i < keywords.length; i++) {
  371. if (AFTConstants.KEYWORDLENTH < keywords[i].length()) {
  372. res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "关键词长度"));
  373. return res;
  374. }
  375. }
  376. return res;
  377. }
  378. }