| package com.doumee.service.business.impl; | 
|   | 
| import com.dingtalk.api.request.OapiMaterialNewsListRequest; | 
| import com.doumee.biz.system.SystemDictDataBiz; | 
| import com.doumee.core.constants.ResponseStatus; | 
| import com.doumee.core.exception.BusinessException; | 
| import com.doumee.core.model.LoginUserInfo; | 
| import com.doumee.core.model.PageData; | 
| import com.doumee.core.model.PageWrap; | 
| import com.doumee.core.utils.Constants; | 
| import com.doumee.core.utils.DateUtil; | 
| import com.doumee.core.utils.Utils; | 
| import com.doumee.dao.business.ActionsMapper; | 
| import com.doumee.dao.business.MultifileMapper; | 
| import com.doumee.dao.business.NewsMapper; | 
| import com.doumee.dao.business.model.Actions; | 
| import com.doumee.dao.business.model.BjParam; | 
| import com.doumee.dao.business.model.Multifile; | 
| import com.doumee.dao.business.model.News; | 
| import com.doumee.dao.system.model.SystemUser; | 
| import com.doumee.dao.web.response.DailyUpdatesResponse; | 
| import com.doumee.service.business.NewsService; | 
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
| import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
| import com.baomidou.mybatisplus.core.metadata.IPage; | 
| import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
| import com.github.yulichang.wrapper.MPJLambdaWrapper; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.apache.shiro.SecurityUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
| import org.springframework.util.CollectionUtils; | 
|   | 
| import javax.swing.*; | 
| import java.util.*; | 
| import java.util.stream.Collectors; | 
|   | 
| /** | 
|  * 资讯和定制服务信息表Service实现 | 
|  * @author 江蹄蹄 | 
|  * @date 2024/07/04 14:40 | 
|  */ | 
| @Service | 
| public class NewsServiceImpl implements NewsService { | 
|   | 
|     @Autowired | 
|     private NewsMapper newsMapper; | 
|     @Autowired | 
|     private MultifileMapper multifileMapper; | 
|     @Autowired | 
|     private SystemDictDataBiz systemDictDataBiz; | 
|     @Autowired | 
|     private ActionsMapper actionsMapper; | 
|   | 
|     @Override | 
|     @Transactional | 
|     public Long create(News param) { | 
|         LoginUserInfo userInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|         param.setIsdeleted(Constants.ZERO); | 
|         param.setEditor(userInfo.getId()); | 
|         param.setCreator(userInfo.getId()); | 
|         param.setStatus(Constants.formatIntegerNum(param.getStatus())); | 
|         param.setCreateDate(new Date()); | 
|         param.setEditDate(param.getCreateDate()); | 
|         param.setPublishUserid(param.getCreator()); | 
|         param.setLookNum(Constants.ZERO); | 
|         param.setDonwloadNum(Constants.ZERO); | 
|         param.setFileType(Constants.formatIntegerNum(param.getFileType())); | 
|         newsMapper.insert(param); | 
|         if(param.getFileList()!=null && param.getFileList().size()>0){ | 
|             List<Multifile> multifiles = new ArrayList<>(); | 
|             int index =1; | 
|             for(Multifile f : param.getFileList()){ | 
|                 if(StringUtils.isNotBlank(f.getFileurl())){ | 
|                     f.setId(null); | 
|                     f.setIsdeleted(Constants.ZERO); | 
|                     f.setObjId(param.getId()); | 
|                     f.setType(StringUtils.endsWith(f.getFileurl(),".mp4")?Constants.ONE:Constants.ZERO); | 
|                     f.setObjType(Constants.MultiFile.NEWS_FILE.getKey()); | 
|                     f.setCreator(param.getCreator()); | 
|                     f.setCreateDate(param.getCreateDate()); | 
|                     f.setSortnum(index++); | 
|                     multifiles.add(f); | 
|                 } | 
|             } | 
|             if(multifiles.size()>0){ | 
|                 multifileMapper.insert(multifiles); | 
|             } | 
|         } | 
|         return param.getId(); | 
|     } | 
|   | 
|     @Override | 
|     public void deleteById(Long id) { | 
|         LoginUserInfo userInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|         newsMapper.update(null,new UpdateWrapper<News>().lambda() | 
|                 .set(News::getIsdeleted,Constants.ONE ) | 
|                 .set(News::getEditor,userInfo.getId() ) | 
|                 .set(News::getEditDate,new Date() ) | 
|                 .eq(News::getId,id)); | 
|         multifileMapper.update(null,new UpdateWrapper<Multifile>().lambda() | 
|                 .set(Multifile::getIsdeleted,Constants.ONE ) | 
|                 .eq(Multifile::getIsdeleted,Constants.ZERO ) | 
|                 .eq(Multifile::getObjType,Constants.MultiFile.NEWS_FILE.getKey()) | 
|                 .eq(Multifile::getObjId,id)); | 
|     } | 
|   | 
|     @Override | 
|     public void delete(News news) { | 
|         UpdateWrapper<News> deleteWrapper = new UpdateWrapper<>(news); | 
|         newsMapper.delete(deleteWrapper); | 
|     } | 
|   | 
|     @Override | 
|     public void deleteByIdInBatch(List<Long> ids) { | 
|         if (CollectionUtils.isEmpty(ids)) { | 
|             return; | 
|         } | 
|         for(Long id : ids){ | 
|             deleteById(id); | 
|         } | 
|     } | 
|   | 
|     @Override | 
|     @Transactional | 
|     public void updateById(News news) { | 
|         LoginUserInfo userInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|         news.setEditor(userInfo.getId()); | 
|         news.setEditDate(new Date()); | 
|         news.setPublishUserid(news.getEditor()); | 
|         newsMapper.updateById(news); | 
|         if(news.getFileList()!=null && news.getFileList().size()>0){ | 
|             List<Multifile> multifiles = new ArrayList<>(); | 
|             int index =1; | 
|             for(Multifile f : news.getFileList()){ | 
|                 if(StringUtils.isNotBlank(f.getFileurl())){ | 
|                     f.setId(null); | 
|                     f.setIsdeleted(Constants.ZERO); | 
|                     f.setType(StringUtils.endsWith(f.getFileurl(),".mp4")?Constants.ONE:Constants.ZERO); | 
|                     f.setObjId(news.getId()); | 
|                     f.setObjType(Constants.MultiFile.NEWS_FILE.getKey()); | 
|                     f.setCreator(news.getCreator()); | 
|                     f.setCreateDate(news.getCreateDate()); | 
|                     f.setSortnum(index++); | 
|                     multifiles.add(f); | 
|                 } | 
|             } | 
|             if(multifiles.size()>0){ | 
|                 multifileMapper.update(null,new UpdateWrapper<Multifile>().lambda() | 
|                         .set(Multifile::getIsdeleted,Constants.ONE ) | 
|                         .eq(Multifile::getIsdeleted,Constants.ZERO ) | 
|                         .eq(Multifile::getObjType,Constants.MultiFile.NEWS_FILE.getKey()) | 
|                         .eq(Multifile::getObjId,news.getId() )); | 
|                 multifileMapper.insert(multifiles); | 
|             } | 
|         } | 
|     } | 
|   | 
|     @Override | 
|     public void updateByIdInBatch(List<News> newss) { | 
|         if (CollectionUtils.isEmpty(newss)) { | 
|             return; | 
|         } | 
|         for (News news: newss) { | 
|             this.updateById(news); | 
|         } | 
|     } | 
|   | 
|     @Override | 
|     public News findById(Long id) { | 
|         return newsMapper.selectById(id); | 
|     } | 
|   | 
|     @Override | 
|     public News findOne(News news) { | 
|         QueryWrapper<News> wrapper = new QueryWrapper<>(news); | 
|         return newsMapper.selectOne(wrapper); | 
|     } | 
|   | 
|     @Override | 
|     public List<News> findList(News news) { | 
|         QueryWrapper<News> wrapper = new QueryWrapper<>(news); | 
|         return newsMapper.selectList(wrapper); | 
|     } | 
|    | 
|     @Override | 
|     public PageData<News> findPage(PageWrap<News> pageWrap) { | 
|         IPage<News> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); | 
|         MPJLambdaWrapper<News> queryWrapper = new MPJLambdaWrapper<>(); | 
|         Utils.MP.blankToNull(pageWrap.getModel()); | 
|         if(Objects.isNull(pageWrap.getModel())){ | 
|             News news = new News(); | 
|             pageWrap.setModel(news); | 
|         } | 
|         pageWrap.getModel().setIsdeleted(Constants.ZERO); | 
|         queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,News::getEditor) | 
|                 .selectAll(News.class) | 
|                 .selectAs(SystemUser::getRealname,News::getEditorName); | 
|         if (pageWrap.getModel().getId() != null) { | 
|             queryWrapper.eq(News::getId, pageWrap.getModel().getId()); | 
|         } | 
|         if(pageWrap.getModel().getIsPublish()!=null && Constants.equalsInteger(pageWrap.getModel().getIsPublish(),Constants.ONE)){ | 
|             queryWrapper.apply(" now() >= t.PUBLISH_DATE "); | 
|         } | 
|         if (pageWrap.getModel().getCreator() != null) { | 
|             queryWrapper.eq(News::getCreator, pageWrap.getModel().getCreator()); | 
|         } | 
|         if (pageWrap.getModel().getCreateDate() != null) { | 
|             queryWrapper.ge(News::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate())); | 
|             queryWrapper.le(News::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate())); | 
|         } | 
|         if (pageWrap.getModel().getEditor() != null) { | 
|             queryWrapper.eq(News::getEditor, pageWrap.getModel().getEditor()); | 
|         } | 
|         if (pageWrap.getModel().getEditDate() != null) { | 
|             queryWrapper.ge(News::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate())); | 
|             queryWrapper.le(News::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate())); | 
|         } | 
|         if (pageWrap.getModel().getIsdeleted() != null) { | 
|             queryWrapper.eq(News::getIsdeleted, pageWrap.getModel().getIsdeleted()); | 
|         } | 
|         if (pageWrap.getModel().getRemark() != null) { | 
|             queryWrapper.eq(News::getRemark, pageWrap.getModel().getRemark()); | 
|         } | 
|         if (pageWrap.getModel().getTitle() != null) { | 
|             queryWrapper.like(News::getTitle, pageWrap.getModel().getTitle()); | 
|         } | 
|         if (pageWrap.getModel().getSubTitle() != null) { | 
|             queryWrapper.eq(News::getSubTitle, pageWrap.getModel().getSubTitle()); | 
|         } | 
|         if (pageWrap.getModel().getSortnum() != null) { | 
|             queryWrapper.eq(News::getSortnum, pageWrap.getModel().getSortnum()); | 
|         } | 
|         if (pageWrap.getModel().getContent() != null) { | 
|             queryWrapper.eq(News::getContent, pageWrap.getModel().getContent()); | 
|         } | 
|         if (pageWrap.getModel().getLinkType() != null) { | 
|             queryWrapper.eq(News::getLinkType, pageWrap.getModel().getLinkType()); | 
|         } | 
|         if (pageWrap.getModel().getStatus() != null) { | 
|             queryWrapper.eq(News::getStatus, pageWrap.getModel().getStatus()); | 
|         } | 
|         if (pageWrap.getModel().getLookNum() != null) { | 
|             queryWrapper.eq(News::getLookNum, pageWrap.getModel().getLookNum()); | 
|         } | 
|         if (pageWrap.getModel().getDonwloadNum() != null) { | 
|             queryWrapper.eq(News::getDonwloadNum, pageWrap.getModel().getDonwloadNum()); | 
|         } | 
|         if (pageWrap.getModel().getPublishDate() != null) { | 
|             queryWrapper.ge(News::getPublishDate, Utils.Date.getStart(pageWrap.getModel().getPublishDate())); | 
|             queryWrapper.le(News::getPublishDate, Utils.Date.getEnd(pageWrap.getModel().getPublishDate())); | 
|         } | 
|         if (pageWrap.getModel().getPublishUserid() != null) { | 
|             queryWrapper.eq(News::getPublishUserid, pageWrap.getModel().getPublishUserid()); | 
|         } | 
|         if (pageWrap.getModel().getPublishInfo() != null) { | 
|             queryWrapper.eq(News::getPublishInfo, pageWrap.getModel().getPublishInfo()); | 
|         } | 
|         if (pageWrap.getModel().getType() != null) { | 
|             queryWrapper.eq(News::getType, pageWrap.getModel().getType()); | 
|         } | 
|         queryWrapper.orderByDesc(News::getCreateDate); | 
|   | 
|         PageData<News> pageData =  PageData.from(newsMapper.selectJoinPage(page, News.class,queryWrapper)); | 
|         if(pageData!=null && pageData.getRecords()!=null && pageData.getRecords().size()>0){ | 
|             List<Long> idList = new ArrayList<>(); | 
|             for(News model : pageData.getRecords()){ | 
|                 idList.add(model.getId()); | 
|             } | 
|             List<Multifile> files = dealMultifileList(idList); | 
|             for(News model : pageData.getRecords()){ | 
|                 setFilelistById(model,files); | 
|             } | 
|         } | 
|         return pageData; | 
|     } | 
|   | 
|     private void setFilelistById(News model, List<Multifile> multifiles) { | 
|         if(multifiles!=null && multifiles.size()>0){ | 
|             for(Multifile f : multifiles){ | 
|                 if(Constants.equalsLong(f.getObjId(),model.getId())){ | 
|                     if(Constants.equalsInteger(model.getType(),Constants.ZERO) &&Constants.equalsInteger(model.getFileType(),Constants.ONE) && Constants.equalsInteger(f.getType(),Constants.ONE)){ | 
|                         //如果是视频 | 
|                         if(model.getFileList() == null){ | 
|                             model.setFileList(new ArrayList<>()); | 
|                         } | 
|                         model.getFileList().add(f); | 
|                         break; | 
|                     } | 
|                     if(Constants.equalsInteger(model.getType(),Constants.ZERO) && Constants.equalsInteger(model.getFileType(),Constants.ZERO) && Constants.equalsInteger(f.getType(),Constants.ZERO)){ | 
|                         //如果是视频 | 
|                         if(model.getFileList() == null){ | 
|                             model.setFileList(new ArrayList<>()); | 
|                         } | 
|                         model.getFileList().add(f); | 
|                     } | 
|   | 
|                     if(Constants.equalsInteger(model.getType(),Constants.ONE)){ | 
|                         //如果是视频 | 
|                         if(model.getFileList() == null){ | 
|                             model.setFileList(new ArrayList<>()); | 
|                         } | 
|                         model.getFileList().add(f); | 
|                         break; | 
|                     } | 
|                 } | 
|             } | 
|         } | 
|     } | 
|   | 
|     private List<Multifile> dealMultifileList(List<Long> idList) { | 
|         List<Integer> types = new ArrayList<>(); | 
|         types.add(Constants.MultiFile.NEWS_FILE.getKey()); | 
|         List<Multifile> multifiles = multifileMapper.selectList(new QueryWrapper<Multifile>().lambda() | 
|                 .eq(Multifile::getIsdeleted,Constants.ZERO ) | 
|                 .in(Multifile::getObjType,types) | 
|                 .in(Multifile::getObjId,idList ) | 
|                 .orderByAsc(Multifile::getSortnum) | 
|         ); | 
|         if(multifiles!=null && multifiles.size()>0){ | 
|             String path = systemDictDataBiz.queryByCode(Constants.OBJCET_STORAGE,Constants.RESOURCE_PATH ).getCode() + | 
|                     systemDictDataBiz.queryByCode(Constants.OBJCET_STORAGE,Constants.NEWS_FILE ).getCode(); | 
|             for(Multifile f : multifiles){ | 
|                 if(StringUtils.isNotBlank(f.getFileurl())){ | 
|                     f.setFileurlFull(path + f.getFileurl()); | 
|                 } | 
|             } | 
|         } | 
|         return multifiles; | 
|     } | 
|   | 
|     @Override | 
|     public long count(News news) { | 
|         QueryWrapper<News> wrapper = new QueryWrapper<>(news); | 
|         return newsMapper.selectCount(wrapper); | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 查询定制服务列表 | 
|      * @return | 
|      */ | 
|     @Override | 
|     public List<News> getCustomizedNewsList(Integer type,Integer num,Integer fileType){ | 
|         List<News> list = newsMapper.selectList(new QueryWrapper<News>().lambda() | 
|                 .eq(News::getIsdeleted,Constants.ZERO) | 
|                 .eq(News::getType,type) | 
|                 .eq(News::getStatus,Constants.ZERO) | 
|                 .eq(Objects.nonNull(fileType),News::getFileType,fileType) | 
|                 .last(Objects.nonNull(num),"limit " + num) | 
|                 .orderByAsc(News::getSortnum) | 
|         ); | 
|         if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(list)){ | 
|             List<Long> idList =  list.stream().map(i->i.getId()).collect(Collectors.toList()); | 
|             List<Multifile> files = dealMultifileList(idList); | 
|             for(News model : list){ | 
|                 setFilelistById(model,files); | 
|             } | 
|         } | 
|         return list; | 
|     } | 
|   | 
|     @Override | 
|     public News getCustomizedNewsDetail(Long id){ | 
|         News news =  newsMapper.selectById(id); | 
|         if(Objects.isNull(news)){ | 
|             throw new BusinessException(ResponseStatus.DATA_EMPTY); | 
|         } | 
|         List<Long> idList =  new ArrayList<>(); | 
|         idList.add(id); | 
|         List<Multifile> files = dealMultifileList(idList); | 
|         setFilelistById(news,files); | 
|         //增加浏览量 | 
|         newsMapper.update(new UpdateWrapper<News>().lambda() | 
|                 .setSql( " LOOK_NUM = ( LOOK_NUM + 1 ) ").eq(News::getId,id)); | 
|         return news; | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 每日上新 | 
|      * @return | 
|      */ | 
|     @Override | 
|     public DailyUpdatesResponse getDailyUpdatesResponse(){ | 
|         DailyUpdatesResponse dailyUpdatesResponse = new DailyUpdatesResponse(); | 
|         dailyUpdatesResponse.setDailyUpdateNum( | 
|                 newsMapper.selectCount(new QueryWrapper<News>().lambda() | 
|                 .eq(News::getIsdeleted,Constants.ZERO) | 
|                 .like(News::getCreateDate, DateUtil.getDateLong(new Date())) | 
|                 .eq(News::getType,Constants.ZERO) | 
|         )); | 
|         dailyUpdatesResponse.setShareNum( | 
|                 actionsMapper.selectCount(new QueryWrapper<Actions>()) | 
|         ); | 
|         List<News> list = newsMapper.selectList(new QueryWrapper<News>().lambda() | 
|                 .eq(News::getIsdeleted,Constants.ZERO) | 
|                 .eq(News::getType,Constants.ZERO) | 
|                 .eq(News::getStatus,Constants.ZERO) | 
|                 .eq(News::getFileType,Constants.ZERO) | 
|                 .apply(" now() >= t.PUBLISH_DATE ") | 
|                 .last( "limit 3"  ) | 
|                 .orderByDesc(News::getCreateDate) | 
|         ); | 
|         if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(list)){ | 
|             List<Long> idList =  list.stream().map(i->i.getId()).collect(Collectors.toList()); | 
|             List<Multifile> files = dealMultifileList(idList); | 
|             for(News model : list){ | 
|                 setFilelistById(model,files); | 
|             } | 
|         } | 
|         dailyUpdatesResponse.setNewsList(list); | 
|         return dailyUpdatesResponse; | 
|     } | 
|   | 
|     @Transactional(rollbackFor = {Exception.class,BusinessException.class}) | 
|     @Override | 
|     public void saveShareRecord(Long id,Long userId){ | 
|         News news = newsMapper.selectById(id); | 
|         if(Objects.isNull(news)){ | 
|             throw new BusinessException(ResponseStatus.DATA_EMPTY); | 
|         } | 
|         Actions actions = new Actions(); | 
|         actions.setCreateDate(new Date()); | 
|         actions.setIsDeleted(Constants.ZERO); | 
|         actions.setType(Constants.ZERO); | 
|         actions.setObjId(id); | 
|         actions.setObjType(Constants.ZERO); | 
|         actions.setMemberId(userId); | 
|         actions.setTitle(news.getTitle()); | 
|         actions.setContent(news.getContent()); | 
|         actions.setSubTitle(news.getSubTitle()); | 
|         actionsMapper.insert(actions); | 
|         //增加分享量 | 
|         newsMapper.update(new UpdateWrapper<News>().lambda() | 
|                 .setSql( " DONWLOAD_NUM = ( DONWLOAD_NUM + 1 ) ").eq(News::getId,id)); | 
|   | 
|     } | 
|   | 
|   | 
|   | 
|   | 
| } |