package com.lyms.talkonlineweb.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyms.talkonlineweb.domain.*;
import com.lyms.talkonlineweb.result.BaseResponse;
import com.lyms.talkonlineweb.service.ArticleInfoService;
import com.lyms.talkonlineweb.service.GetpushartInfoService;
import com.lyms.talkonlineweb.service.LymsArticleService;
import com.lyms.talkonlineweb.service.LymsPushedartService;
import com.lyms.talkonlineweb.service.impl.PushedartlogsInfoServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.codec.multipart.Part;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
@RestController
@RequestMapping("art")
@Log4j2
public class ArticleController {
@Value("${uploadPath}")
private String uploadPath;//上传路径
@Value("${imgUrlPre}")
private String imgUrlPre;//图片查看路径
@Autowired
private LymsArticleService lymsArticleService;
@Autowired
private ArticleInfoService articleInfoService;
@Autowired
private LymsPushedartService lymsPushedartService;//推送记录
@Autowired
private PushedartlogsInfoServiceImpl pushedartlogsInfoService;
@Autowired
private GetpushartInfoService getpushartInfoService;//小程序用户端-根据患者获取推送的文章-视图
/**
* 上传文件
*
* @return
*/
@PostMapping("upFile")
public String upFile(MultipartFile imgFile) {
String furl = "";
if (Objects.nonNull(imgFile)) {
try {
String[] orginaF=imgFile.getOriginalFilename().split("\\.");
File file = new File(uploadPath + File.separator + System.currentTimeMillis() +"."+ orginaF[orginaF.length-1]);
imgFile.transferTo(file);
furl = file.getAbsolutePath();
log.info("上传文件:" + furl);
furl = imgUrlPre + file.getName();
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
}
}
return furl;
}
/**
* 显示图片
* @return
*/
@GetMapping("showPic/{fileName}")
public void showPic(@PathVariable("fileName") String fileName, HttpServletResponse response) {
try(InputStream in=new FileInputStream(new File(uploadPath + File.separator +fileName));
OutputStream out=response.getOutputStream();
){
IOUtils.copy(in,out);
out.flush();
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 更新或插入文章文章
*
* @param article
* @return
*/
@PostMapping("saveArticle")
public BaseResponse saveArticle(LymsArticle article) {
BaseResponse baseResponse = new BaseResponse();
if (article.getAid() == null) {
article.setCreatedtime(new Date());
} else {
article.setUpdatedTime(new Date());
}
boolean f = lymsArticleService.saveOrUpdate(article);
return baseResponse;
}
/**
* 获取文章列表
*
* @param article
* @param current
* @param size
* @param sort
* @return
*/
@GetMapping("sltArticleLst")
public BaseResponse sltArticleLst(ArticleInfo article, int current, int size, int sort) {
BaseResponse baseResponse = new BaseResponse();
Page<ArticleInfo> page = new Page<>(current, size);
Page<ArticleInfo> articlePagePage = new Page<>();
QueryWrapper query=new QueryWrapper();
if(StringUtils.isEmpty(article.getTitle())){
article.setTitle(null);
}else{
query.like("title",article.getTitle());
}
if (sort == 1) {
query.orderByDesc("createdtime");
}
if (sort == 2) {
query.orderByAsc("createdtime");
}
articlePagePage = articleInfoService.page(page, query);
baseResponse.setObject(articlePagePage);
return baseResponse;
}
/**
* 删除文章
*
* @param aid
* @return
*/
@GetMapping("delArticle")
public BaseResponse delArticle(int aid) {
BaseResponse baseResponse = new BaseResponse();
boolean f = lymsArticleService.removeById(aid);
baseResponse.setErrorcode(f == true ? 0 : 1);
return baseResponse;
}
/**
* 根据患者获取推送的文章
*/
@PostMapping("getPushArt")
// public BaseResponse getPushArt(LymsPushedart pushedart) {
// BaseResponse baseResponse = new BaseResponse();
// List<LymsPushedart> pLst = lymsPushedartService.list(Wrappers.query(pushedart).orderByDesc("createdtime"));
// List idLst = new ArrayList();
// pLst.forEach(e -> {
// idLst.add(e.getAid());
// });
// if (idLst.size() > 0) {
// List<LymsArticle> aLst = lymsArticleService.listByIds(idLst);
//
// //赋值已读未读
// pLst.forEach(p->{
// aLst.forEach(a->{
// if(p.getAid()==a.getAid()){
// a.setStat(p.getIsread());
// }
// });
// });
//
// baseResponse.setObject(aLst);
// }
// return baseResponse;
// }
public BaseResponse getPushArt(@RequestBody GetpushartInfo getpushartInfo) {
BaseResponse baseResponse = new BaseResponse();
List<GetpushartInfo> getpushartInfoList= getpushartInfoService.list(Wrappers.query(getpushartInfo).orderByDesc("createdtime"));
baseResponse.setObject(getpushartInfoList);
return baseResponse;
}
/**
* 统计文章
*
* @return
*/
@GetMapping("getArtStat")
public BaseResponse getArtStat() {
BaseResponse baseResponse = new BaseResponse();
List<Map<String, Object>> aLst = lymsArticleService.getArtStat();
baseResponse.setObject(aLst);
return baseResponse;
}
/**
* 专家组推荐统计
* @return
*/
@GetMapping("getArtPushStat")
public BaseResponse getArtPushStat(){
BaseResponse baseResponse=new BaseResponse();
Map<String, Object> pMap = lymsArticleService.getArtPushStat();
baseResponse.setObject(pMap);
return baseResponse;
}
/**
* 专家组推荐-推荐记录
*
* @return
*/
@GetMapping("getPushedartlogsInfo")
public BaseResponse getPushedartlogsInfo(PushedartlogsInfo pushedartlogsInfo, Integer current, Integer size){
BaseResponse baseResponse=new BaseResponse();
try {
Page<PushedartlogsInfo> page=new Page<>(current,size);
Page<PushedartlogsInfo> pushedartlogsInfoPage=pushedartlogsInfoService.page(page,Wrappers.query(pushedartlogsInfo).orderByDesc("createdtime"));
baseResponse.setObject(pushedartlogsInfoPage);
baseResponse.setErrormsg("成功");
} catch (Exception e) {
baseResponse.setErrormsg("失败");
e.printStackTrace();
}
return baseResponse;
}
/**
* 根据患者id,问诊id,状态类型1为已读更新推送文章记录
*/
@PostMapping("updatePushedartlogs")
public BaseResponse updatePushedartlogs(@RequestBody LymsPushedart pushedart) {
BaseResponse baseResponse=new BaseResponse();
try {
UpdateWrapper<LymsPushedart> updateWrapper = new UpdateWrapper<>();
pushedart.setReadtime(new Date());
updateWrapper.eq("pid",pushedart.getPid()).eq("aid", pushedart.getAid());
boolean f=lymsPushedartService.update(pushedart,updateWrapper);
baseResponse.setErrorcode(f==true?0:1);
baseResponse.setErrormsg(f==true?"成功":"失败");
} catch (Exception e) {
baseResponse.setErrorcode(1);
baseResponse.setErrormsg("失败");
e.printStackTrace();
}
return baseResponse;
}
}