package platform.modules.company.service; import java.io.File; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.ModelMap; import platform.common.Constant; import platform.common.base.model.Template; import platform.common.base.service.BaseService; import platform.common.base.service.DictionaryItemService; import platform.common.base.service.TemplateService; import platform.modules.company.dao.ProjectMaterialDao; import platform.modules.company.entity.*; import platform.modules.government.dao.AttachmentDao; import platform.modules.government.entity.Attachment; import platform.modules.government.entity.FileDown; import platform.modules.government.entity.Project; import platform.modules.government.service.AttachmentService; import platform.modules.government.service.ProjectService; import tk.mybatis.mapper.entity.Example; /** * 项目申报材料 * * @author yl */ @Service @Transactional public class ProjectMaterialService extends BaseService { @Autowired private ProjectMaterialDao projectMaterialDao; @Autowired private AttachmentService attachmentService; @Autowired private DictionaryItemService dictionaryItemService; @Autowired private AttachmentDao attachmentDao; @Autowired private ProjectService projectService; //全部 public List findProjectMaterialByType(Integer projectId) { Integer itemId = 0; Project project = projectService.findById(projectId); if (null != project) { itemId = dictionaryItemService.findIdByTypeAndName(Constant.DictionaryType.PROJECT_NAME, project.getProject_name()); } List materials = projectMaterialDao.findMaterialTemplateByType(itemId); for (ProjectMaterial material : materials) { //绑定模板文件 List templateFile = attachmentService.selectByIdAndBusinessId(Constant.Attachment.TEMPLATE, material.getTemplate_id(), null); if (null != templateFile && templateFile.size() > 0) { FileDown fileDown = new FileDown(templateFile.get(0).getId(), templateFile.get(0).getFile_name(), templateFile.get(0).getFile_url(), templateFile.get(0).getDownload_uri()); material.setTemplateFile(fileDown); } else { material.setTemplateFile(new FileDown(null, material.getContent(), null, null)); } } return materials; } //仅查询字典表item激活的 public List findProjectMaterialByTypeActive(Integer projectId) { Integer itemId = 0; Project project = projectService.findById(projectId); if (null != project) { itemId = dictionaryItemService.findIdByTypeAndNameActive(Constant.DictionaryType.PROJECT_NAME, project.getProject_name()); } List materials = projectMaterialDao.findMaterialTemplateByType(itemId); for (ProjectMaterial material : materials) { //绑定模板文件 List templateFile = attachmentService.selectByIdAndBusinessId(Constant.Attachment.TEMPLATE, material.getTemplate_id(), null); if (null != templateFile && templateFile.size() > 0) { FileDown fileDown = new FileDown(templateFile.get(0).getId(), templateFile.get(0).getFile_name(), templateFile.get(0).getFile_url(), templateFile.get(0).getDownload_uri()); material.setTemplateFile(fileDown); } else { material.setTemplateFile(new FileDown(null, material.getContent(), null, null)); } } return materials; } //仅查询离创建时间最近且小于创建时间的 public List findProjectMaterialByTypeRecent(Integer projectId) { Integer itemId = 0; Project project = projectService.findById(projectId); if (null != project) { itemId = dictionaryItemService.findIdByTypeAndNameRecent(Constant.DictionaryType.PROJECT_NAME, project.getProject_name(), project.getCreate_time()); } List materials = projectMaterialDao.findMaterialTemplateByType(itemId); for (ProjectMaterial material : materials) { //绑定模板文件 List templateFile = attachmentService.selectByIdAndBusinessId(Constant.Attachment.TEMPLATE, material.getTemplate_id(), null); if (null != templateFile && templateFile.size() > 0) { FileDown fileDown = new FileDown(templateFile.get(0).getId(), templateFile.get(0).getFile_name(), templateFile.get(0).getFile_url(), templateFile.get(0).getDownload_uri()); material.setTemplateFile(fileDown); } else { material.setTemplateFile(new FileDown(null, material.getContent(), null, null)); } } return materials; } public List getApplyMaterials(ProjectApplication projectApply) { //申请材料 // List materials = this.findProjectMaterialByApply(projectApply); //20200805修改逻辑获取最近的 因为可能存在第二年同名的 List materials = this.findProjectMaterialByApplyRecent(projectApply); return materials; } /** * 2019 附件 * * @param applyId * @return */ public List getAnnexMaterial(Integer applyId) { List materials = projectMaterialDao.findByContentAndApplyId("annex_template", applyId); for (ProjectMaterial material : materials) { //模版文件 Attachment template = attachmentService.findById(material.getTemplate_id()); material.setTemplateFile(new FileDown(template.getId(), template.getFile_name(), template.getFile_url(), null)); //上传文件 List file = attachmentService.selectByIdAndBusinessId(Constant.Attachment.PROJECT, material.getId(), null); if (null != file && file.size() > 0) { Attachment attachment = file.get(0); FileDown fileDown = new FileDown(attachment.getId(), attachment.getFile_name(), StringUtils.isNotBlank(attachment.getFile_url()) ? attachment.getFile_url() : "", attachment.getDownload_uri()); material.setFileDown(fileDown); } } return materials; } /** * 查找申请文件模板和匹配的已经上传的材料 * * @param projectApply * @return */ public List findProjectMaterialByApply(ProjectApplication projectApply) { //找出模板,再匹配上传材料 List materialTemplates = findProjectMaterialByType(projectApply.getProject_id()); for (ProjectMaterial material : materialTemplates) { ProjectMaterial existMaterial = findMaterialByApplyIdAndTemplateId(projectApply.getId(), material.getTemplate_id()); if (null != existMaterial) { material.setId(existMaterial.getId()); material.setApply_id(existMaterial.getApply_id()); //绑定文件 List file = attachmentService.selectByIdAndBusinessId(Constant.Attachment.PROJECT, existMaterial.getId(), null); if (null != file && file.size() > 0) { Attachment attachment = file.get(0); FileDown fileDown = new FileDown(attachment.getId(), attachment.getFile_name(), StringUtils.isNotBlank(attachment.getFile_url()) ? attachment.getFile_url() : "", attachment.getDownload_uri()); material.setFileDown(fileDown); } } } return materialTemplates; } /** * 查找申请文件模板和匹配的已经上传的材料 最近的 * * @param projectApply * @return */ public List findProjectMaterialByApplyRecent(ProjectApplication projectApply) { //找出模板,再匹配上传材料 List materialTemplates = findProjectMaterialByTypeRecent(projectApply.getProject_id()); for (ProjectMaterial material : materialTemplates) { ProjectMaterial existMaterial = findMaterialByApplyIdAndTemplateId(projectApply.getId(), material.getTemplate_id()); if (null != existMaterial) { material.setId(existMaterial.getId()); material.setApply_id(existMaterial.getApply_id()); //绑定文件 List file = attachmentService.selectByIdAndBusinessId(Constant.Attachment.PROJECT, existMaterial.getId(), null); if (null != file && file.size() > 0) { Attachment attachment = file.get(0); FileDown fileDown = new FileDown(attachment.getId(), attachment.getFile_name(), StringUtils.isNotBlank(attachment.getFile_url()) ? attachment.getFile_url() : "", attachment.getDownload_uri()); material.setFileDown(fileDown); } } } return materialTemplates; } //查找申请的相应模板的上传材料 public ProjectMaterial findMaterialByApplyIdAndTemplateId(Integer id, Integer template_id) { Example example = new Example(ProjectMaterial.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("del_flag", false); if (null != id && id > 0) { criteria.andEqualTo("apply_id", id); } if (null != template_id && template_id > 0) { criteria.andEqualTo("template_id", template_id); } List materials = this.selectByExample(example); if (null != materials && materials.size() > 0) { return materials.get(0); } return null; } public Boolean saveMaterial(ProjectMaterial material) { if (this.saveSelective(material) > 0) { if (null != material.getFileDown() && null != material.getFileDown().getFile_id()) { attachmentDao.updateAttachment(Constant.Attachment.PROJECT, material.getId(), material.getFileDown().getFile_id()); } return true; } return false; } public Boolean updateMaterial(ProjectMaterial material) { if (this.updateSelective(material) > 0) { if (null != material.getFileDown() && null != material.getFileDown().getFile_id()) { List attachments = attachmentDao.selectByIdAndBusinessId(Constant.Attachment.PROJECT, material.getId(), material.getFileDown().getFile_id()); if (attachments.size() == 0) { //size为0,则是重新上传的文件,更新附件 attachmentDao.deleteByBusiness(Constant.Attachment.PROJECT, material.getId()); attachmentDao.updateAttachment(Constant.Attachment.PROJECT, material.getId(), material.getFileDown().getFile_id()); } } return true; } return false; } public List gfindProvincesMaterials(ProjectProvinces projectApply) { return null; } /** * 删除原有附件 * * @param id */ public void deleteByApplyId(Integer id) { projectMaterialDao.deleteByApplyId(id); } }