package com.lyms.talkonlineweb.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyms.talkonlineweb.domain.ArticleInfo;
import com.lyms.talkonlineweb.domain.LymsArticle;
import com.lyms.talkonlineweb.domain.LymsDoctor;
import com.lyms.talkonlineweb.domain.LymsPushedart;
import com.lyms.talkonlineweb.result.BaseResponse;
import com.lyms.talkonlineweb.service.ArticleInfoService;
import com.lyms.talkonlineweb.service.LymsArticleService;
import com.lyms.talkonlineweb.service.LymsPushedartService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.codec.multipart.Part;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
@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;//推送记录
/**
* 上传文件
* @return
*/
@PostMapping("upFile")
public String upFile(MultipartFile imgFile){
String furl="";
if(Objects.nonNull(imgFile)){
try {
File file=new File(uploadPath+File.separator+System.currentTimeMillis()+imgFile.getOriginalFilename());
imgFile.transferTo(file);
furl=file.getAbsolutePath();
log.info("上传文件:"+furl);
furl=imgUrlPre+file.getName();
}catch (Exception e){
log.error(e.getMessage());
e.printStackTrace();
}
}
return furl;
}
/**
* 更新或插入文章文章
* @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
* @return
*/
@GetMapping("sltArticleLst")
public BaseResponse sltArticleLst(ArticleInfo article, int current, int size){
BaseResponse baseResponse=new BaseResponse();
Page<ArticleInfo> page=new Page<>(current,size);
Page<ArticleInfo> articlePagePage=articleInfoService.page(page, Wrappers.query(article).orderByDesc("updated_time","createdtime"));
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;
}
/**
* 根据患者获取推送的文章
*/
@GetMapping("getPushArt")
public BaseResponse getPushArt(LymsPushedart pushedart){
BaseResponse baseResponse=new BaseResponse();
List<LymsPushedart> pLst=lymsPushedartService.list(Wrappers.query(pushedart));
List idLst=new ArrayList();
pLst.forEach(e->{
idLst.add(e.getAid());
});
if(idLst.size()>0){
List<LymsArticle> aLst=lymsArticleService.listByIds(idLst);
baseResponse.setObject(aLst);
}
return baseResponse;
}
}