实例demo参考文章管理增加、修改、删除、查询功能
1. 列表查询
java
/**
* 查询文章列表
*/
@SaCheckLogin
@SaCheckPermission("article:list")
@ApiOperation("查询文章列表")
@GetMapping("/list")
public JSONObject listArticle(HttpServletRequest request) {
return articleService.listArticle(CommonUtil.request2Json(request));
}
/**
* 文章列表
*/
@Override
public JSONObject listArticle(JSONObject jsonObject) {
CommonUtil.fillPageParam(jsonObject);
int count = articleDao.countArticle(jsonObject);
List<JSONObject> list = articleDao.listArticle(jsonObject);
return CommonUtil.successPage(jsonObject, list, count);
}
2. 新增文章
java
/**
* 新增文章
*/
@SaCheckLogin
@SaCheckPermission("article:add")
@ApiOperation("新增文章")
@PostMapping("/add")
public JSONObject addArticle(HttpServletRequest request,HttpServletResponse response,@RequestBody EditArticleReq editArticleReq) {
String username=getCuerrtUserName(request,response);
if(null == username || "".equals(username)){
return CommonUtil.errorJson(ErrorEnum.E_20011);
}
JwAdminHeader aykYcmHeader=getAiyakuHeader(request);
if(null == aykYcmHeader){
return CommonUtil.errorJson(ErrorEnum.E_20011);
}
try {
Map<String, Object> map = MapUtil.objectToMap1(editArticleReq);
JSONObject requestJson = new JSONObject(map);
CommonUtil.hasAllRequired(requestJson, "content");
return articleService.addArticle(requestJson);
} catch (Exception e) {
// TODO Auto-generated catch block
logger.error(e.getMessage(), e);
}
return CommonUtil.errorJson(ErrorEnum.E_500);
}
/**
* 新增文章
*/
@Override
@Transactional(rollbackFor = Exception.class)
public JSONObject addArticle(JSONObject jsonObject) {
articleDao.addArticle(jsonObject);
return CommonUtil.successJson();
}
3. 修改文章
java
/**
* 修改文章
*/
@SaCheckLogin
@SaCheckPermission("article:update")
@ApiOperation("修改文章")
@PostMapping("/update")
public JSONObject updateArticle(HttpServletRequest request,HttpServletResponse response,@RequestBody EditArticleReq editArticleReq) {
String username=getCuerrtUserName(request,response);
if(null == username || "".equals(username)){
return CommonUtil.errorJson(ErrorEnum.E_20011);
}
JwAdminHeader aykYcmHeader=getAiyakuHeader(request);
if(null == aykYcmHeader){
return CommonUtil.errorJson(ErrorEnum.E_20011);
}
try {
Map<String, Object> map= MapUtil.objectToMap1(editArticleReq);
JSONObject requestJson = new JSONObject(map);
CommonUtil.hasAllRequired(requestJson, "id,content");
return articleService.updateArticle(requestJson);
} catch (Exception e) {
// TODO Auto-generated catch block
logger.error(e.getMessage(), e);
}
return CommonUtil.errorJson(ErrorEnum.E_500);
}
/**
* 更新文章
*/
@Override
@Transactional(rollbackFor = Exception.class)
public JSONObject updateArticle(JSONObject jsonObject) {
articleDao.updateArticle(jsonObject);
return CommonUtil.successJson();
}
4. 获取单个文章内容
java
/**
* 获取单个文章信息
* @param userId
* @return
*/
@SaCheckLogin
@SaCheckPermission("article:view")
@ApiOperation("获取单个文章信息")
@ApiImplicitParam(name="id",value="文章id",dataType="Long", paramType = "Query",example="1",
defaultValue="0",required=true)
@GetMapping("/info")
public JSONObject getArticleInfo(HttpServletRequest request,HttpServletResponse response,Long id) {
String username=getCuerrtUserName(request,response);
if(null == username || "".equals(username)){
return CommonUtil.errorJson(ErrorEnum.E_20011);
}
JwAdminHeader aykYcmHeader=getAiyakuHeader(request);
if(null == aykYcmHeader){
return CommonUtil.errorJson(ErrorEnum.E_20011);
}
return CommonUtil.successJson(articleService.getArticleInfo(id));
}
@Override
public JSONObject getArticleInfo(Long id) {
// TODO Auto-generated method stub
return articleDao.getArticleInfo(id);
}
5. 删除文章
java
/**
* 删除文章
*/
@SaCheckLogin
@SaCheckPermission("article:delete")
@ApiOperation("删除文章")
@PostMapping("/delete")
public JSONObject delArt(HttpServletRequest request,HttpServletResponse response,@RequestBody DelArticleReq delArticleReq) {
String username=getCuerrtUserName(request,response);
if(null == username || "".equals(username)){
return CommonUtil.errorJson(ErrorEnum.E_20011);
}
if(null == delArticleReq){
return CommonUtil.errorJson(ErrorEnum.E_90003);
}
JwAdminHeader aykYcmHeader=getAiyakuHeader(request);
if(null == aykYcmHeader){
return CommonUtil.errorJson(ErrorEnum.E_20011);
}
try {
articleService.delArt(delArticleReq.getArticleId());
return CommonUtil.successJson();
} catch (Exception e) {
// TODO Auto-generated catch block
logger.error(e.getMessage(), e);
}
return CommonUtil.successJson();
}
@Override
public int delArt(Long artId) {
// TODO Auto-generated method stub
return articleDao.delArt(artId);
}
6. 导出文章excel
java
/**
* 文章导出功能
* @param response
* @param productlistReq
*/
@SaCheckLogin
@ApiOperation("文章导出")
@RequestMapping(value = "/export", method = RequestMethod.POST)
public void exportArticle(HttpServletResponse response,HttpServletRequest request)
{
try {
//开始时间
Long startTime = System.currentTimeMillis();
// 查询导出商品列表
List<ArticleRes> list=articleService.exprotArticleList(CommonUtil.request2Json(request));
response.setContentType("APPLICATION/OCTET-STREAM;charset=UTF-8");
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
response.setHeader("Content-Disposition", "attachment; filename=Art_" + sf1.format(new Date()) + ".xls");
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
importExportExcleService.exportArticleList(list, response.getOutputStream());
//开始时间
Long endTime = System.currentTimeMillis();
long ts=(endTime-startTime)/1000;
logger.info("花费时间===》"+ts);
} catch (Exception e) {
logger.error("导出商品失败"+e.getMessage(),e);
}
}
public void exportArticleList(List<ArticleRes> list,OutputStream outputStream) throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
ExportExcelUtil eeu = new ExportExcelUtil(workbook, sheet);
int rowNO = 0;
short color = HSSFColor.BLACK.index;
List<Object> header = new ArrayList<>();
header.add("文章id");
header.add("文章内容");
header.add("创建时间");
eeu.createExcelRow(sheet, rowNO, header,-1,-1, null,color,(short) 300,null);
rowNO++;
for(ArticleRes info : list){
if(null == info){
continue;
}
List<Object> arrayList = new ArrayList<>();
if(null != info.getId()){
arrayList.add(info.getId());
}else{
arrayList.add(0);
}
if(null != info.getContent()){
arrayList.add(info.getContent());
}else{
arrayList.add("");
}
if(null != info.getCreateTime()){
arrayList.add(info.getCreateTime());
}else{
arrayList.add("");
}
eeu.createExcelRow(sheet, rowNO, arrayList,-1,-1, null,color,(short) 300,null);
rowNO++;
}
workbook.write(outputStream);
workbook.close();
outputStream.close();
}
7. 批量导入文章
java
/**
* 批量导入文章
* @param file
* @param request
* @return
* @throws Exception
*/
@SaCheckLogin
@ApiOperation("文章导入")
@PostMapping(value = "/import")
@ApiImplicitParams({@ApiImplicitParam(name = "file",value = "文件对象",dataType = "MultipartFile",required = true,
allowMultiple = false)})
@ResponseBody
public JSONObject importOrderProduct(@Param("file") MultipartFile file, HttpServletRequest request,HttpServletResponse response) throws Exception {
JSONObject resp = CommonUtil.successJson();
JwAdminHeader aykYcmHeader=getAiyakuHeader(request);
if(null == aykYcmHeader){
return CommonUtil.errorJson(ErrorEnum.E_20011);
}
String fileName = file.getOriginalFilename();
if (fileName.lastIndexOf(".xls") <= -1) {
if (fileName.lastIndexOf(".xlsx") <= -1) {
return CommonUtil.errorJson(800, "请选择上传文件");
}
}
String prefix = fileName.substring(fileName.lastIndexOf(".xls")).toLowerCase();
if (!".xls".equals(prefix) && !".xlsx".equals(prefix)) {
return CommonUtil.errorJson(800, "文件格式不对");
}
//文件数据
List<Map<String, Object>> mapList = new ArrayList<>();
List<Integer> skipList = new ArrayList<>();
InputStream inputStream = file.getInputStream();
if (prefix.equals(".xls")) {
mapList = ImportExcelUtil.readXls1(inputStream, skipList);
} else if (prefix.equals(".xlsx")) {
mapList = ImportExcelUtil.readXlsx1(inputStream, skipList);
}
if (mapList == null || mapList.size() <=1) {
return CommonUtil.errorJson(800, "请填写模板");
}
logger.info("批量修改商品 操作人:"+ getCuerrtUserName(request,response)+ ", 导入数据: " + JsonUtils.objectToJson(mapList));
ImportRes importRes=importExportExcleService.importArticleList(mapList);
Map<String, Object> responseEntity=new HashMap<String, Object>();
if(null != importRes){
responseEntity.put("isSuc" , importRes.getIsSuc());
responseEntity.put("sucCount" , importRes.getSucCount());
responseEntity.put("totalCount" , importRes.getTotalCount());
responseEntity.put("failCount" , importRes.getFailCount());
}
resp.put("data",responseEntity);
return resp;
}