ProjectMaterialService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package platform.modules.company.service;
  2. import java.io.File;
  3. import java.util.List;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.transaction.annotation.Transactional;
  8. import org.springframework.ui.ModelMap;
  9. import platform.common.Constant;
  10. import platform.common.base.model.Template;
  11. import platform.common.base.service.BaseService;
  12. import platform.common.base.service.DictionaryItemService;
  13. import platform.common.base.service.TemplateService;
  14. import platform.modules.company.dao.ProjectMaterialDao;
  15. import platform.modules.company.entity.*;
  16. import platform.modules.government.dao.AttachmentDao;
  17. import platform.modules.government.entity.Attachment;
  18. import platform.modules.government.entity.FileDown;
  19. import platform.modules.government.entity.Project;
  20. import platform.modules.government.service.AttachmentService;
  21. import platform.modules.government.service.ProjectService;
  22. import tk.mybatis.mapper.entity.Example;
  23. /**
  24. * 项目申报材料
  25. *
  26. * @author yl
  27. */
  28. @Service
  29. @Transactional
  30. public class ProjectMaterialService extends BaseService<ProjectMaterial> {
  31. @Autowired
  32. private ProjectMaterialDao projectMaterialDao;
  33. @Autowired
  34. private AttachmentService attachmentService;
  35. @Autowired
  36. private DictionaryItemService dictionaryItemService;
  37. @Autowired
  38. private AttachmentDao attachmentDao;
  39. @Autowired
  40. private ProjectService projectService;
  41. //全部
  42. public List<ProjectMaterial> findProjectMaterialByType(Integer projectId) {
  43. Integer itemId = 0;
  44. Project project = projectService.findById(projectId);
  45. if (null != project) {
  46. itemId = dictionaryItemService.findIdByTypeAndName(Constant.DictionaryType.PROJECT_NAME, project.getProject_name());
  47. }
  48. List<ProjectMaterial> materials = projectMaterialDao.findMaterialTemplateByType(itemId);
  49. for (ProjectMaterial material : materials) {
  50. //绑定模板文件
  51. List<Attachment> templateFile = attachmentService.selectByIdAndBusinessId(Constant.Attachment.TEMPLATE, material.getTemplate_id(), null);
  52. if (null != templateFile && templateFile.size() > 0) {
  53. FileDown fileDown = new FileDown(templateFile.get(0).getId(), templateFile.get(0).getFile_name(), templateFile.get(0).getFile_url(), templateFile.get(0).getDownload_uri());
  54. material.setTemplateFile(fileDown);
  55. } else {
  56. material.setTemplateFile(new FileDown(null, material.getContent(), null, null));
  57. }
  58. }
  59. return materials;
  60. }
  61. //仅查询字典表item激活的
  62. public List<ProjectMaterial> findProjectMaterialByTypeActive(Integer projectId) {
  63. Integer itemId = 0;
  64. Project project = projectService.findById(projectId);
  65. if (null != project) {
  66. itemId = dictionaryItemService.findIdByTypeAndNameActive(Constant.DictionaryType.PROJECT_NAME, project.getProject_name());
  67. }
  68. List<ProjectMaterial> materials = projectMaterialDao.findMaterialTemplateByType(itemId);
  69. for (ProjectMaterial material : materials) {
  70. //绑定模板文件
  71. List<Attachment> templateFile = attachmentService.selectByIdAndBusinessId(Constant.Attachment.TEMPLATE, material.getTemplate_id(), null);
  72. if (null != templateFile && templateFile.size() > 0) {
  73. FileDown fileDown = new FileDown(templateFile.get(0).getId(), templateFile.get(0).getFile_name(), templateFile.get(0).getFile_url(), templateFile.get(0).getDownload_uri());
  74. material.setTemplateFile(fileDown);
  75. } else {
  76. material.setTemplateFile(new FileDown(null, material.getContent(), null, null));
  77. }
  78. }
  79. return materials;
  80. }
  81. //仅查询离创建时间最近且小于创建时间的
  82. public List<ProjectMaterial> findProjectMaterialByTypeRecent(Integer projectId) {
  83. Integer itemId = 0;
  84. Project project = projectService.findById(projectId);
  85. if (null != project) {
  86. itemId = dictionaryItemService.findIdByTypeAndNameRecent(Constant.DictionaryType.PROJECT_NAME, project.getProject_name(), project.getCreate_time());
  87. }
  88. List<ProjectMaterial> materials = projectMaterialDao.findMaterialTemplateByType(itemId);
  89. for (ProjectMaterial material : materials) {
  90. //绑定模板文件
  91. List<Attachment> templateFile = attachmentService.selectByIdAndBusinessId(Constant.Attachment.TEMPLATE, material.getTemplate_id(), null);
  92. if (null != templateFile && templateFile.size() > 0) {
  93. FileDown fileDown = new FileDown(templateFile.get(0).getId(), templateFile.get(0).getFile_name(), templateFile.get(0).getFile_url(), templateFile.get(0).getDownload_uri());
  94. material.setTemplateFile(fileDown);
  95. } else {
  96. material.setTemplateFile(new FileDown(null, material.getContent(), null, null));
  97. }
  98. }
  99. return materials;
  100. }
  101. public List<ProjectMaterial> getApplyMaterials(ProjectApplication projectApply) {
  102. //申请材料
  103. // List<ProjectMaterial> materials = this.findProjectMaterialByApply(projectApply);
  104. //20200805修改逻辑获取最近的 因为可能存在第二年同名的
  105. List<ProjectMaterial> materials = this.findProjectMaterialByApplyRecent(projectApply);
  106. return materials;
  107. }
  108. /**
  109. * 2019 附件
  110. *
  111. * @param applyId
  112. * @return
  113. */
  114. public List<ProjectMaterial> getAnnexMaterial(Integer applyId) {
  115. List<ProjectMaterial> materials = projectMaterialDao.findByContentAndApplyId("annex_template", applyId);
  116. for (ProjectMaterial material : materials) {
  117. //模版文件
  118. Attachment template = attachmentService.findById(material.getTemplate_id());
  119. material.setTemplateFile(new FileDown(template.getId(), template.getFile_name(), template.getFile_url(), null));
  120. //上传文件
  121. List<Attachment> file = attachmentService.selectByIdAndBusinessId(Constant.Attachment.PROJECT, material.getId(), null);
  122. if (null != file && file.size() > 0) {
  123. Attachment attachment = file.get(0);
  124. FileDown fileDown = new FileDown(attachment.getId(), attachment.getFile_name(),
  125. StringUtils.isNotBlank(attachment.getFile_url()) ? attachment.getFile_url() : "", attachment.getDownload_uri());
  126. material.setFileDown(fileDown);
  127. }
  128. }
  129. return materials;
  130. }
  131. /**
  132. * 查找申请文件模板和匹配的已经上传的材料
  133. *
  134. * @param projectApply
  135. * @return
  136. */
  137. public List<ProjectMaterial> findProjectMaterialByApply(ProjectApplication projectApply) {
  138. //找出模板,再匹配上传材料
  139. List<ProjectMaterial> materialTemplates = findProjectMaterialByType(projectApply.getProject_id());
  140. for (ProjectMaterial material : materialTemplates) {
  141. ProjectMaterial existMaterial = findMaterialByApplyIdAndTemplateId(projectApply.getId(), material.getTemplate_id());
  142. if (null != existMaterial) {
  143. material.setId(existMaterial.getId());
  144. material.setApply_id(existMaterial.getApply_id());
  145. //绑定文件
  146. List<Attachment> file = attachmentService.selectByIdAndBusinessId(Constant.Attachment.PROJECT, existMaterial.getId(), null);
  147. if (null != file && file.size() > 0) {
  148. Attachment attachment = file.get(0);
  149. FileDown fileDown = new FileDown(attachment.getId(), attachment.getFile_name(),
  150. StringUtils.isNotBlank(attachment.getFile_url()) ? attachment.getFile_url() : "", attachment.getDownload_uri());
  151. material.setFileDown(fileDown);
  152. }
  153. }
  154. }
  155. return materialTemplates;
  156. }
  157. /**
  158. * 查找申请文件模板和匹配的已经上传的材料 最近的
  159. *
  160. * @param projectApply
  161. * @return
  162. */
  163. public List<ProjectMaterial> findProjectMaterialByApplyRecent(ProjectApplication projectApply) {
  164. //找出模板,再匹配上传材料
  165. List<ProjectMaterial> materialTemplates = findProjectMaterialByTypeRecent(projectApply.getProject_id());
  166. for (ProjectMaterial material : materialTemplates) {
  167. ProjectMaterial existMaterial = findMaterialByApplyIdAndTemplateId(projectApply.getId(), material.getTemplate_id());
  168. if (null != existMaterial) {
  169. material.setId(existMaterial.getId());
  170. material.setApply_id(existMaterial.getApply_id());
  171. //绑定文件
  172. List<Attachment> file = attachmentService.selectByIdAndBusinessId(Constant.Attachment.PROJECT, existMaterial.getId(), null);
  173. if (null != file && file.size() > 0) {
  174. Attachment attachment = file.get(0);
  175. FileDown fileDown = new FileDown(attachment.getId(), attachment.getFile_name(),
  176. StringUtils.isNotBlank(attachment.getFile_url()) ? attachment.getFile_url() : "", attachment.getDownload_uri());
  177. material.setFileDown(fileDown);
  178. }
  179. }
  180. }
  181. return materialTemplates;
  182. }
  183. //查找申请的相应模板的上传材料
  184. public ProjectMaterial findMaterialByApplyIdAndTemplateId(Integer id, Integer template_id) {
  185. Example example = new Example(ProjectMaterial.class);
  186. Example.Criteria criteria = example.createCriteria();
  187. criteria.andEqualTo("del_flag", false);
  188. if (null != id && id > 0) {
  189. criteria.andEqualTo("apply_id", id);
  190. }
  191. if (null != template_id && template_id > 0) {
  192. criteria.andEqualTo("template_id", template_id);
  193. }
  194. List<ProjectMaterial> materials = this.selectByExample(example);
  195. if (null != materials && materials.size() > 0) {
  196. return materials.get(0);
  197. }
  198. return null;
  199. }
  200. public Boolean saveMaterial(ProjectMaterial material) {
  201. if (this.saveSelective(material) > 0) {
  202. if (null != material.getFileDown() && null != material.getFileDown().getFile_id()) {
  203. attachmentDao.updateAttachment(Constant.Attachment.PROJECT, material.getId(), material.getFileDown().getFile_id());
  204. }
  205. return true;
  206. }
  207. return false;
  208. }
  209. public Boolean updateMaterial(ProjectMaterial material) {
  210. if (this.updateSelective(material) > 0) {
  211. if (null != material.getFileDown() && null != material.getFileDown().getFile_id()) {
  212. List<Attachment> attachments = attachmentDao.selectByIdAndBusinessId(Constant.Attachment.PROJECT, material.getId(), material.getFileDown().getFile_id());
  213. if (attachments.size() == 0) {
  214. //size为0,则是重新上传的文件,更新附件
  215. attachmentDao.deleteByBusiness(Constant.Attachment.PROJECT, material.getId());
  216. attachmentDao.updateAttachment(Constant.Attachment.PROJECT, material.getId(), material.getFileDown().getFile_id());
  217. }
  218. }
  219. return true;
  220. }
  221. return false;
  222. }
  223. public List<ProjectMaterial> gfindProvincesMaterials(ProjectProvinces projectApply) {
  224. return null;
  225. }
  226. /**
  227. * 删除原有附件
  228. *
  229. * @param id
  230. */
  231. public void deleteByApplyId(Integer id) {
  232. projectMaterialDao.deleteByApplyId(id);
  233. }
  234. }