package platform.modules.carrier.service; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import platform.common.Constant; import platform.common.base.model.DictionaryItem; import platform.common.base.service.BaseService; import platform.common.base.service.DictionaryItemService; import platform.common.util.Reflections; import platform.common.util.ShiroUtils; import platform.modules.carrier.dao.ParkDao; import platform.modules.carrier.dto.CarrierLibraryResult; import platform.modules.carrier.dto.CustomSearchCondition; import platform.modules.carrier.dto.ParkDto; import platform.modules.carrier.dto.SearchCondition; import platform.modules.carrier.entity.*; import platform.modules.government.entity.Attachment; import platform.modules.government.entity.Street; import platform.modules.government.entity.User; import platform.modules.government.service.AttachmentService; import platform.modules.government.service.StreetService; import platform.modules.sys.web.ResponseMessage; import tk.mybatis.mapper.entity.Example; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * @author kevin * @since 2019/3/31 3:04 PM */ @Service @Transactional public class ParkService extends BaseService { @Autowired private ParkDao parkDao; @Autowired private StreetService streetService; @Autowired private AttachmentService attachmentService; @Autowired private ParkAttachmentService parkAttachmentService; @Autowired private ParkSupportService parkSupportService; @Autowired private ParkSupportAttachmentService parkSupportAttachmentService; @Autowired private ParkTypicalCompanyService parkTypicalCompanyService; @Autowired private ParkTypicalCompanyAttachmentService parkTypicalCompanyAttachmentService; @Autowired private DictionaryItemService dictionaryItemService; @Autowired private CarrierStatisticService carrierStatisticService; @Autowired private BuildingService buildingService; @Autowired private ParkDraftService parkDraftService; /** * 通过组合条件查询工业小区,返回符合条件的id集合 * * @param condition * @return */ public List getParkListByCondition(SearchCondition condition) { return parkDao.getParkListByCondition(condition); } /** * 获取所有园区 * * @param pageNum * @param pageSize * @return */ public PageInfo findParkList(Integer pageNum, Integer pageSize, SearchCondition condition) { if (!condition.getIs_front()) { User user = ShiroUtils.getUserEntity(); if (user.getUser_type() != null && user.getUser_type().equals(Constant.UserType.STREET)) { condition.setStreetId(user.getStreet_id()); } if (user.getUser_type() != null && user.getUser_type().equals(Constant.UserType.BUILD)) { condition.setParkId(user.getBuild_id()); } } PageHelper.startPage(pageNum, pageSize); List parkList = parkDao.findParkList(condition); for (Park park : parkList) { double sumBuildingArea = carrierStatisticService.getSumParkBuildingArea(park.getId()); park.setSumBuildArea(sumBuildingArea); park.setFile_down_url(condition.getFileUrl()); Attachment attachment = attachmentService.findMainPicture(park.getId()); if (attachment != null) { park.setMain_pic_url(attachment.getFile_url()); } Double sumRentArea = getParkRentAreaStatistic(park.getId()); park.setSumRentArea(sumRentArea); if (park.getType() != null){ park.setType_desc(dictionaryItemService.findNameByTypeAndValue(Constant.DictionaryType.PARK_TYPE, park.getType().toString())); } } return new PageInfo<>(parkList); } /** * 获取小区信息 * * @param id * @return */ public Park parkDetailById(Integer id) { ParkDraft draft = parkDraftService.findById(id); if (draft != null){ Park park = new Park(); Reflections.copyFieldToBean(draft, park); park.setId(id); park.setStreetName(streetService.findById(park.getStreet_id()).getName()); getParkDraftInfo(park); return park; } Park park = this.findById(id); if (park != null) { getParkInfo(park); park.setStreetName(streetService.findById(park.getStreet_id()).getName()); } return park; } /** * 获取小区其他草稿信息 * * @param park */ private void getParkDraftInfo(Park park) { Street street = streetService.findById(park.getStreet_id()); park.setStreetName(street.getName()); if (park.getType() != null){ park.setType_desc(dictionaryItemService.findNameByTypeAndValue(Constant.DictionaryType.PARK_TYPE, park.getType().toString())); } //获取小区图片 List attachmentList = parkDraftService.findParkAttachmentDraftList(park.getId()); List fileDownList = new ArrayList<>(); for (ParkAttachmentDraft parkAtt : attachmentList) { Attachment attachment = attachmentService.findById(parkAtt.getAttachment_id()); if (attachment != null) { FileDown fileDown = new FileDown(); fileDown.setFile_id(attachment.getId()); fileDown.setDownload_uri(attachment.getDownload_uri()); fileDown.setFile_url(attachment.getFile_url()); fileDown.setFile_name(attachment.getFile_name()); fileDown.setIs_main(parkAtt.getIs_main()); fileDownList.add(fileDown); } } park.setFileDowns(fileDownList); // 获取小区配套 List supportList = new ArrayList<>(); List draftList = parkDraftService.findSupportDraftByParkId(park.getId()); for (ParkSupportDraft draft : draftList) { ParkSupport support = new ParkSupport(); Reflections.copyFieldToBean(draft, support); String attachmentType = Constant.Attachment.PARK_SUPPORT_DRAFT; List attachments = attachmentService.findListBySupport(draft.getId(), draft.getType(), attachmentType); support.setFileDowns(getFileDownList(attachments)); //获取item DictionaryItem item = dictionaryItemService. findByTypeAndValue(Constant.DictionaryType.PARK_SUPPORT, draft.getType().toString()); support.setType_desc(item.getName()); supportList.add(support); } park.setSupportList(supportList); //获取典型企业 List companyList = new ArrayList<>(); List companyDraftList = parkDraftService.findTypicalCompanyDraft(park.getId()); for (ParkTypicalCompanyDraft draft : companyDraftList) { ParkTypicalCompany company = new ParkTypicalCompany(); Reflections.copyFieldToBean(draft, company); String attachmentType = Constant.Attachment.TYPICAL_COMPANY_DRAFT; List attachments = attachmentService.findListByTypicalCompany(draft.getId(), attachmentType); company.setFileDowns(getFileDownList(attachments)); companyList.add(company); } park.setTypicalCompanyList(companyList); } /** * 获取小区信息 * * @param id * @return */ public Park frontParkDetailById(Integer id) { Park park = findById(id); if (park != null) { double sumBuildingArea = carrierStatisticService.getSumParkBuildingArea(id); park.setBuild_area(sumBuildingArea); park.setRest_area(findParkRemainArea(id)); park.setBuild_num(parkDao.findBuildingNum(id)); this.updateSelective(park); //获取其他信息 getParkInfo(park); } return park; } /** * 获取园区剩余面积 * * @param park_id */ public Double findParkRemainArea(Integer park_id) { return parkDao.findParkRemainArea(park_id); } /** * 获取小区其他信息 * * @param park */ public void getParkInfo(Park park) { Street street = streetService.findById(park.getStreet_id()); park.setStreetName(street.getName()); if (park.getType() != null){ park.setType_desc(dictionaryItemService.findNameByTypeAndValue(Constant.DictionaryType.PARK_TYPE, park.getType().toString())); } //获取小区图片 ParkAttachment parkAttachment = new ParkAttachment(); parkAttachment.setPark_id(park.getId()); List attachmentList = parkAttachmentService.findListByWhere(parkAttachment); List fileDownList = new ArrayList<>(); for (ParkAttachment parkAtt : attachmentList) { Attachment attachment = attachmentService.findById(parkAtt.getAttachment_id()); if (attachment != null) { FileDown fileDown = new FileDown(); fileDown.setFile_id(attachment.getId()); fileDown.setDownload_uri(attachment.getDownload_uri()); fileDown.setFile_url(attachment.getFile_url()); fileDown.setFile_name(attachment.getFile_name()); fileDown.setIs_main(parkAtt.getIs_main()); fileDownList.add(fileDown); } } park.setFileDowns(fileDownList); // 获取小区配套 ParkSupport support = new ParkSupport(); support.setPark_id(park.getId()); List supportList = parkSupportService.findListByWhere(support); if (supportList != null && supportList.size() > 0) { for (ParkSupport parkSupport : supportList) { String attachmentType = Constant.Attachment.PARK_SUPPORT; List attachments = attachmentService.findListBySupport(parkSupport.getId(), parkSupport.getType(), attachmentType); parkSupport.setFileDowns(getFileDownList(attachments)); //获取item DictionaryItem item = dictionaryItemService. findByTypeAndValue(Constant.DictionaryType.PARK_SUPPORT, parkSupport.getType().toString()); parkSupport.setType_desc(item.getName()); } } park.setSupportList(supportList); //获取典型企业 ParkTypicalCompany typicalCompany = new ParkTypicalCompany(); typicalCompany.setPark_id(park.getId()); List companyList = parkTypicalCompanyService.findListByWhere(typicalCompany); if (companyList != null && companyList.size() > 0) { for (ParkTypicalCompany company : companyList) { String attachmentType = Constant.Attachment.TYPICAL_COMPANY; List attachments = attachmentService.findListByTypicalCompany(company.getId(), attachmentType); company.setFileDowns(getFileDownList(attachments)); } } park.setTypicalCompanyList(companyList); } /** * 编辑小区信息 * * @param park */ public void editParkInfo(Park park) { if (park.getId() != null) { // this.updateSelective(park); if (park.getIs_draft()){ this.updateSelective(park); ParkDraft draft = parkDraftService.findById(park.getId()); if (draft != null){ parkDraftService.deleteById(draft.getId()); } parkAttachmentService.deleteByParkId(park.getId()); saveAttachment(park); //删除图片草稿 deleteDraftAttachment(park); //配套信息 parkSupportService.deleteParkSupport(park.getId()); if (park.getSupportList() != null && park.getSupportList().size() > 0) { saveParkSupport(park.getSupportList(), park.getId()); } //删除草稿中的园区配套及图片 parkDraftService.deleteDraftSupport(park.getId()); //典型企业 ParkTypicalCompany typical = new ParkTypicalCompany(); typical.setPark_id(park.getId()); List typicalCompanys = parkTypicalCompanyService.findListByWhere(typical); for (ParkTypicalCompany company : typicalCompanys) { parkTypicalCompanyService.deleteById(company.getId()); parkTypicalCompanyAttachmentService.deleteByTypicalCompanyId(company.getId()); } if (park.getTypicalCompanyList() != null && park.getTypicalCompanyList().size() > 0) { saveparkTypicalCompany(park.getTypicalCompanyList(), park.getId()); } //删除草稿中的典型企业及图片 parkDraftService.deleteDraftTypicalCompany(park.getId()); } else {//保存草稿 ParkDraft draft = parkDraftService.findById(park.getId()); if (draft != null){ Reflections.copyFieldToBean(park, draft); parkDraftService.updateSelective(draft); } else { ParkDraft insert = new ParkDraft(); Reflections.copyFieldToBean(park, insert); insert.setId(park.getId()); insert.setIs_draft(true); parkDraftService.save(insert); } deleteDraftAttachment(park); //园区图片草稿 if (park.getFileDowns() != null && park.getFileDowns().size() > 0) { saveAttachmentDraft(park.getFileDowns(), park.getId()); } parkDraftService.deleteDraftSupport(park.getId()); //配套信息 if (park.getSupportList() != null && park.getSupportList().size() > 0) { saveParkSupportDraft(park.getSupportList(), park.getId()); } parkDraftService.deleteDraftTypicalCompany(park.getId()); //典型企业 if (park.getTypicalCompanyList() != null && park.getTypicalCompanyList().size() > 0) { saveparkTypicalCompanyDraft(park.getTypicalCompanyList(), park.getId()); } } } } /** * 保存典型企业草稿 */ private void saveparkTypicalCompanyDraft(List typicalCompanyList, Integer id) { for (ParkTypicalCompany parkTypicalCompany : typicalCompanyList) { parkTypicalCompany.setPark_id(id); parkDraftService.insertTypicalCompanyDraft(parkTypicalCompany); } } /** * 保存配套信息草稿 */ private void saveParkSupportDraft(List supportList, Integer id) { for (ParkSupport parkSupport : supportList) { parkSupport.setPark_id(id); parkDraftService.insertSupportDraft(parkSupport); } } /** * 保存园区图片草稿 */ private void saveAttachmentDraft(List fileDowns, Integer park_id) { for (FileDown fileDown : fileDowns) { int fileId = fileDown.getFile_id(); ParkAttachmentDraft newAttach = new ParkAttachmentDraft(); newAttach.setPark_id(park_id); newAttach.setAttachment_id(fileId); newAttach.setIs_main(fileDown.getIs_main()); int newAttachId = parkDraftService.insertAttachmentDraft(newAttach); //图片已存在 Attachment attachment = attachmentService.findById(fileId); if (attachment != null){ attachment.setBusiness_id(newAttachId); attachment.setBusiness_type(Constant.Attachment.PARK_DRAFT); attachmentService.updateSelective(attachment); } } } /** * 保存园区典型企业 */ private void saveparkTypicalCompany(List typicalCompanyList, Integer park_id) { for (ParkTypicalCompany typicalCompany : typicalCompanyList) { typicalCompany.setPark_id(park_id); int id = parkTypicalCompanyService.insertAndGetId(typicalCompany); //删除原来图片 ParkTypicalCompanyAttachment companyAttachment = new ParkTypicalCompanyAttachment(); companyAttachment.setTypical_company_id(id); //保存新图片 List fileDowns = typicalCompany.getFileDowns(); if (fileDowns != null && fileDowns.size() > 0) { for (FileDown fileDown : fileDowns) { int fileId = fileDown.getFile_id(); companyAttachment.setAttachment_id(fileId); int typicalCompanyId = parkTypicalCompanyAttachmentService.insertAndGetId(companyAttachment); Attachment attachment = attachmentService.findById(fileDown.getFile_id()); if (attachment != null) { attachment.setBusiness_type(Constant.Attachment.TYPICAL_COMPANY); attachment.setBusiness_id(typicalCompanyId); attachmentService.updateSelective(attachment); } } } } } /** * 保存园区配套 */ private void saveParkSupport(List supportList, Integer park_id) { for (ParkSupport parkSupport : supportList) { Integer supportId = parkSupport.getId(); parkSupport.setPark_id(park_id); ParkSupportAttachment oldAttachment = new ParkSupportAttachment(); oldAttachment.setType(parkSupport.getType()); if (parkSupport.getId() != null) { parkSupportService.updateSelective(parkSupport); } else { supportId = parkSupportService.insertAndGetId(parkSupport); } //删除原来图片 oldAttachment.setSupport_id(supportId); List supportAttachments = parkSupportAttachmentService.findListByWhere(oldAttachment); if (supportAttachments != null && supportAttachments.size() > 0) { for (ParkSupportAttachment attachment : supportAttachments) { attachment.setDel_flag(true); parkSupportAttachmentService.updateSelective(attachment); } } //保存新图片 List fileDowns = parkSupport.getFileDowns(); if (fileDowns != null && fileDowns.size() > 0) { for (FileDown fileDown : fileDowns) { ParkSupportAttachment supportAttachment = new ParkSupportAttachment(); supportAttachment.setSupport_id(supportId); supportAttachment.setType(parkSupport.getType()); supportAttachment.setAttachment_id(fileDown.getFile_id()); parkSupportAttachmentService.insertAndGetId(supportAttachment); //更新配套图片信息 Attachment attachment = new Attachment(); attachment.setId(fileDown.getFile_id()); attachment.setBusiness_id(supportAttachment.getId()); attachment.setBusiness_type(Constant.Attachment.PARK_SUPPORT); attachmentService.updateSelective(attachment); } } } } /** * 删除草稿图片 */ private void deleteDraftAttachment(Park park) { parkDraftService.deleteDraftAttachment(park.getId()); } /** * 保存园区图片 */ private void saveAttachment(Park park) { if (park.getFileDowns() != null && park.getFileDowns().size() > 0) { //保存新图片 for (FileDown fileDown : park.getFileDowns()) { int fileId = fileDown.getFile_id(); ParkAttachment newAttach = new ParkAttachment(); newAttach.setPark_id(park.getId()); newAttach.setAttachment_id(fileId); newAttach.setIs_main(fileDown.getIs_main()); int newAttachId = parkAttachmentService.insertAndGetId(newAttach); //图片已存在 if (attachmentService.findById(fileId) != null) { continue; } Attachment attachment = new Attachment(); attachment.setId(fileId); attachment.setBusiness_id(newAttachId); attachment.setBusiness_type(Constant.Attachment.PARK); attachmentService.updateSelective(attachment); } } } /** * 获取fileDowns * * @param attachmentList * @return */ public List getFileDownList(List attachmentList) { List fileDowns = new ArrayList<>(); if (attachmentList != null && attachmentList.size() > 0) { for (Attachment attachment : attachmentList) { FileDown fileDown = new FileDown(); fileDown.setFile_url(attachment.getFile_url()); fileDown.setFile_id(attachment.getId()); fileDown.setDownload_uri(attachment.getDownload_uri()); fileDown.setFile_name(attachment.getFile_name()); fileDowns.add(fileDown); } } return fileDowns; } public List findParkListByStreetId(Integer streetId) { SearchCondition condition = new SearchCondition(); condition.setStreetId(streetId); return parkDao.findParkList(condition); } @Transactional(readOnly = true) public List findList() { Example example = new Example(Park.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("del_flag", 0); //倒序 //example.orderBy("create_time").desc(); example.setOrderByClause("id"); List parks = this.selectByExample(example); return parks; } @Transactional(readOnly = true) public List findParkOnUseList() { Example example = new Example(Park.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("del_flag", 0); criteria.andEqualTo("is_start", 1); criteria.andEqualTo("is_draft", 1); //倒序 //example.orderBy("create_time").desc(); example.setOrderByClause("id"); List parks = this.selectByExample(example); return parks; } public int updateNoBatch(List parks) { return parkDao.updateNoBatch(parks); } /** * 载体库园区列表 * * @param id * @return */ public List getParkStatistic(Integer id) { return parkDao.getParkStatistic(id); } public CarrierLibraryResult getParkAreaStatistic(Integer id) { return parkDao.getParkAreaStatistic(id); } public List findParkBuildingList(CustomSearchCondition customSearchCondition) { return parkDao.findParkBuildingList(customSearchCondition); } public List findByStreetId(Integer id) { return parkDao.findByStreetId(id); } public Double getParkRentAreaStatistic(Integer id) { return parkDao.getParkRentAreaStatistic(id); } /** * 更具园区名字查找园区 * * @param name * @return */ public Park findByName(String name) { Example example = new Example(Park.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("del_flag", 0); criteria.andEqualTo("name", name); List parks = this.selectByExample(example); if (parks.size() > 0){ return parks.get(0); } return null; } public PageInfo findParkNames(SearchCondition condition) { if (!condition.getIs_front()) { User user = ShiroUtils.getUserEntity(); if (user.getUser_type() != null && user.getUser_type().equals(Constant.UserType.STREET)) { condition.setStreetId(user.getStreet_id()); } if (user.getUser_type() != null && user.getUser_type().equals(Constant.UserType.BUILD)) { condition.setParkId(user.getBuild_id()); } } PageHelper.startPage(condition.getPageNum(), condition.getPageSize()); List parkList = parkDao.findParkNames(condition); return new PageInfo<>(parkList); } /** * 找到子园区 * * @param park_id * @return */ public List findParentParkById(String park_id) { return parkDao.findParentParkById(park_id); } }