NewsServiceimpl
@Override
public int addWithImage(KumiaoNewsDO kumiaoNewsDO, String pcImageUrl, String mobileImageUrl) {
this.kumiaoNewsMapper.insert(kumiaoNewsDO);
if (kumiaoNewsDO.getId() <= 0L) {
return -1; // failed
}
if (pcImageUrl != null) {
KumiaoResourceDO pcKumiaoResourceDO = generateKumiaoResourceDO(pcImageUrl, "pc", kumiaoNewsDO);
this.kumiaoResourceService.add(pcKumiaoResourceDO);
}
if (mobileImageUrl != null) {
KumiaoResourceDO mobileKumiaoResourceDO = generateKumiaoResourceDO(mobileImageUrl, "mobile", kumiaoNewsDO);
this.kumiaoResourceService.add(mobileKumiaoResourceDO);
}
return 0;
}
public KumiaoResourceDO generateKumiaoResourceDO(String imageUrl, String clientType, KumiaoNewsDO kumiaoNewsDO) {
KumiaoResourceDO kumiaoResourceDO = new KumiaoResourceDO();
kumiaoResourceDO.setTitle("");
kumiaoResourceDO.setGmtCreate(kumiaoNewsDO.getGmtCreate());
kumiaoResourceDO.setGmtModified(kumiaoNewsDO.getGmtModified());
kumiaoResourceDO.setResourceType("image");
kumiaoResourceDO.setPageType("news");
kumiaoResourceDO.setIsDeleted(kumiaoNewsDO.getIsDeleted());
kumiaoResourceDO.setRemark("");
kumiaoResourceDO.setClientType(clientType);
kumiaoResourceDO.setStatus(NewsDataStatusEnum.PUBLISHED.getStoreValue()); // 图片默认是发布状态
kumiaoResourceDO.setImageUrl(imageUrl);
kumiaoResourceDO.setOwnerId(kumiaoNewsDO.getId());
kumiaoResourceDO.setOwnerType("kumiao_news");
kumiaoResourceDO.setIsTest(kumiaoNewsDO.getIsTest());
return kumiaoResourceDO;
}
NewsService
int addWithImage(KumiaoNewsDO kumiaoNewsDO, String pcImageUrl, String mobileImageUrl);
NewsServiceController
/* * 添加单条新闻 */ @RequestMapping(value = "/add", method = RequestMethod.POST) @ResponseBody public ResponseVo add(KumiaoNewsDO kumiaoNewsDO, String pcImageUrl, String mobileImageUrl) throws Exception { if (kumiaoNewsDO == null || StringUtil.isBlank(kumiaoNewsDO.getTitle())) { return ResponseUtil.error(MessageCode.PARAMS_NOT_ENOUGH); } /* * 新闻 */ Date now = new Date(); kumiaoNewsDO.setGmtCreate(now); kumiaoNewsDO.setGmtModified(now); kumiaoNewsDO.setIsDeleted(NewsDeletedStatusEnum.NOT_DELETED.getStoreValue()); kumiaoNewsDO.setRemark(""); kumiaoNewsDO.setStatus(NewsDataStatusEnum.UNPUBLISHED.getStoreValue()); int result = this.kumiaoNewsService.addWithImage(kumiaoNewsDO, pcImageUrl, mobileImageUrl); if (result == 0) { return ResponseUtil.success(MessageCode.SAVE_SUCCESS); } else { return ResponseUtil.error(MessageCode.FAILED); } } /* * 删除单条新闻 */ @RequestMapping(value = "/delete", method = RequestMethod.POST) public @ResponseBody ResponseVo delete(Long id) throws Exception { if (id == null || id <= 0L) { return ResponseUtil.error(MessageCode.PARAMS_NOT_ENOUGH); } this.kumiaoNewsService.delete(id); return ResponseUtil.success(MessageCode.DELETE_SUCCESS); } /* * 发布单条新闻 */ @RequestMapping(value = "/publish", method = RequestMethod.POST) public @ResponseBody ResponseVo publish(Long id) throws Exception { if (id == null || id <= 0L) { return ResponseUtil.error(MessageCode.PARAMS_NOT_ENOUGH); } this.kumiaoNewsService.publish(id); return ResponseUtil.success(MessageCode.PUBLISH_SUCCESS); } /* * 编辑单条新闻-查询 */ @RequestMapping(value = "/edit", method = RequestMethod.GET) public String edit(Long id, ModelMap model) throws Exception { if (id == null || id <= 0L) { return ResponseUtil.error(model, MessageCode.PARAMS_NOT_ENOUGH); } KumiaoNewsDO kumiaoNewsDO = this.kumiaoNewsService.getById(id); if (null == kumiaoNewsDO) { return ResponseUtil.error(model, MessageCode.OBJ_NOT_EXISTS); } model.put("kumiaoNewsDO", kumiaoNewsDO); KumiaoResourceDO pcImageDO = new KumiaoResourceDO(); KumiaoResourceDO mobileImageDO = new KumiaoResourceDO(); List<KumiaoResourceDO> kumiaoResourceDOList = this.kumiaoResourceService.getByNewsOwner(kumiaoNewsDO); if (kumiaoResourceDOList != null) { for (int i=0; i<kumiaoResourceDOList.size(); i++) { KumiaoResourceDO resourceDO = kumiaoResourceDOList.get(i); if (resourceDO.getClientType().equals(KumiaoResourceClientTypeEnum.PC.getStoreValue())) { pcImageDO.setId(resourceDO.getId()); pcImageDO.setImageUrl(resourceDO.getImageUrl()); }else if(resourceDO.getClientType().equals(KumiaoResourceClientTypeEnum.MOBILE.getStoreValue())) { mobileImageDO.setId(resourceDO.getId()); mobileImageDO.setImageUrl(resourceDO.getImageUrl()); } } } model.put("pcImageDO", pcImageDO); model.put("mobileImageDO", mobileImageDO); fillDefData(model); return "kumiao/news/edit"; } /* * 编辑单条新闻-更新 */ @ResponseBody @RequestMapping(value = "/edit", method = RequestMethod.POST) public ResponseVo editDo(KumiaoNewsDO kumiaoNewsDO, Long pcImageId, Long mobileImageId, String pcImageUrl, String mobileImageUrl) throws Exception { if (kumiaoNewsDO == null || kumiaoNewsDO.getId() == null) { return ResponseUtil.error(MessageCode.PARAMS_NOT_ENOUGH); } if (pcImageId != null && pcImageId > 0L) { this.updateImageByNews(pcImageId, pcImageUrl, kumiaoNewsDO); } if (mobileImageId != null && mobileImageId > 0L) { this.updateImageByNews(mobileImageId, mobileImageUrl, kumiaoNewsDO); } this.kumiaoNewsService.update(kumiaoNewsDO); return ResponseUtil.success(MessageCode.SAVE_SUCCESS); } /* * 下线单条新闻 */ @ResponseBody @RequestMapping(value = "/offline", method = RequestMethod.POST) public ResponseVo offline(Long id) throws Exception { if (id == null || id <= 0L) { return ResponseUtil.error(MessageCode.PARAMS_NOT_ENOUGH); } this.kumiaoNewsService.offlineById(id); return ResponseUtil.success(MessageCode.OFFLINE_SUCCESS); } private void fillDefData(ModelMap model) { model.put("newsDataTypeEnum", NewsDataTypeEnum.getMap()); } private void updateImageByNews(Long imageId, String imageUrl, KumiaoNewsDO kumiaoNewsDO) { KumiaoResourceDO kumiaoResourceDO = new KumiaoResourceDO(); kumiaoResourceDO.setId(imageId); kumiaoResourceDO.setImageUrl(imageUrl); kumiaoResourceDO.setIsTest(kumiaoNewsDO.getIsTest()); this.kumiaoResourceService.update(kumiaoResourceDO); }
ResourceService
List<KumiaoResourceDO> getByNewsOwner(KumiaoNewsDO kumiaoNewsDO);
ResourceServiceImpl
@Override public List<KumiaoResourceDO> getByNewsOwner(KumiaoNewsDO kumiaoNewsDO) { KumiaoResourceDOExample example = new KumiaoResourceDOExample(); example.createCriteria().andOwnerIdEqualTo(kumiaoNewsDO.getId()).andOwnerTypeEqualTo("kumiao_news").andIsDeletedEqualTo(KumiaoResourceDeletedStatusEnum.NOT_DELETED.getStoreValue()).andStatusEqualTo(KumiaoResourceStatusEnum.PUBLISHED.getStoreValue()).andIsTestEqualTo(kumiaoNewsDO.getIsTest()); List<KumiaoResourceDO> result = this.kumiaoResourceMapper.selectByExample(example); return result; }