Commit cf0ee72336cdcac6e15bf161a6266f2534ab44f4

Authored by liwei2917
1 parent 9b0b8040

评论审核增加excel导出功能

src/main/java/com/cnlive/shenhe/controller/CommentsController.java
@@ -17,6 +17,11 @@ import com.cnlive.shenhe.utils.AuditPass; @@ -17,6 +17,11 @@ import com.cnlive.shenhe.utils.AuditPass;
17 import com.cnlive.shenhe.utils.CommonConst; 17 import com.cnlive.shenhe.utils.CommonConst;
18 import com.cnlive.shenhe.utils.CommonUtils; 18 import com.cnlive.shenhe.utils.CommonUtils;
19 import com.github.pagehelper.PageInfo; 19 import com.github.pagehelper.PageInfo;
  20 +
  21 +import org.apache.poi.xssf.usermodel.XSSFCell;
  22 +import org.apache.poi.xssf.usermodel.XSSFRow;
  23 +import org.apache.poi.xssf.usermodel.XSSFSheet;
  24 +import org.apache.poi.xssf.usermodel.XSSFWorkbook;
20 import org.slf4j.Logger; 25 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory; 26 import org.slf4j.LoggerFactory;
22 import org.springframework.beans.factory.annotation.Autowired; 27 import org.springframework.beans.factory.annotation.Autowired;
@@ -25,7 +30,16 @@ import org.springframework.stereotype.Controller; @@ -25,7 +30,16 @@ import org.springframework.stereotype.Controller;
25 import org.springframework.ui.Model; 30 import org.springframework.ui.Model;
26 import org.springframework.web.bind.annotation.*; 31 import org.springframework.web.bind.annotation.*;
27 32
  33 +import javax.servlet.ServletOutputStream;
  34 +import javax.servlet.http.HttpServletResponse;
28 import javax.servlet.http.HttpSession; 35 import javax.servlet.http.HttpSession;
  36 +
  37 +import java.awt.color.CMMException;
  38 +import java.io.BufferedInputStream;
  39 +import java.io.BufferedOutputStream;
  40 +import java.io.ByteArrayInputStream;
  41 +import java.io.ByteArrayOutputStream;
  42 +import java.io.InputStream;
29 import java.util.Date; 43 import java.util.Date;
30 import java.util.List; 44 import java.util.List;
31 45
@@ -289,4 +303,189 @@ public class CommentsController extends BaseController { @@ -289,4 +303,189 @@ public class CommentsController extends BaseController {
289 } 303 }
290 return responseBean; 304 return responseBean;
291 } 305 }
  306 +
  307 +
  308 + /**
  309 + * 根据条件导出excel
  310 + */
  311 + @GetMapping("excel")
  312 + public void getExcel( String activity_id, String content, Integer is_hot,
  313 + String start_date, String end_date, Integer state, Integer receive,Integer type,
  314 + Integer page, Integer pageSize, String orderByClause, HttpSession session,HttpServletResponse response) {
  315 + SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
  316 + Integer spid = sessionUser.getSpid();
  317 + String userId = sessionUser.getUserId();
  318 + if (spid == 0) spid = null;
  319 + if (CommonUtils.isNull(page)) page = 1;
  320 + if (CommonUtils.isNull(pageSize)) pageSize = 10;
  321 + Date sDate = null;
  322 + Date eDate = null;
  323 +
  324 + if (CommonUtils.isNotEmpty(start_date)) {
  325 + try {
  326 + sDate = CommonUtils.parseDate(start_date, "yyyy-MM-dd");
  327 + } catch (Exception e) {
  328 + }
  329 + }
  330 + if (CommonUtils.isNotEmpty(end_date)) {
  331 + try {
  332 + eDate = CommonUtils.parseDate(end_date, "yyyy-MM-dd");
  333 + } catch (Exception e) {
  334 + }
  335 + }
  336 + PageInfo<ShyComments> commentPage = commentsService.getPageExcel(activity_id, content, is_hot, sDate, eDate, state, receive, null, page, pageSize, orderByClause, spid, userId,type);
  337 + List<ShyComments> list = commentPage.getList();
  338 + String fileName = "评论数据";
  339 +
  340 + XSSFWorkbook wb = new XSSFWorkbook();
  341 + ServletOutputStream out = null;
  342 + BufferedInputStream bis = null;
  343 + BufferedOutputStream bos = null;
  344 + try {
  345 + XSSFSheet sheetlist = wb.createSheet(fileName);
  346 + XSSFRow row = sheetlist.createRow(0);//行
  347 + XSSFCell cell = row.createCell(0);//列
  348 + cell.setCellValue("id");
  349 + cell = row.createCell(1);//列
  350 + cell.setCellValue("sp昵称");
  351 + cell = row.createCell(2);//列
  352 + cell.setCellValue("评论用户id");
  353 + cell = row.createCell(3);//列
  354 + cell.setCellValue("评论用户昵称");
  355 + cell = row.createCell(4);//列
  356 + cell.setCellValue("评论内容");
  357 + cell = row.createCell(5);//列
  358 + cell.setCellValue("评论标题");
  359 + cell = row.createCell(6);//列
  360 + cell.setCellValue("评论类型");
  361 + cell = row.createCell(7);//列
  362 + cell.setCellValue("评论层级");
  363 + cell = row.createCell(8);//列
  364 + cell.setCellValue("评论来源");
  365 + cell = row.createCell(9);//列
  366 + cell.setCellValue("评论送审时间");
  367 + cell = row.createCell(10);//列
  368 + cell.setCellValue("审核状态");
  369 + cell = row.createCell(11);//列
  370 + cell.setCellValue("审核人");
  371 + cell = row.createCell(12);//列
  372 + cell.setCellValue("审核意见");
  373 +
  374 + for (int i = 0; i < list.size(); i++) {
  375 + row = sheetlist.createRow((short) (i + 1));//行
  376 + cell = row.createCell(0);//列
  377 + cell.setCellValue(list.get(i).getId());
  378 + cell = row.createCell(1);//列
  379 + cell.setCellValue(list.get(i).getSpid()==null?"":sitesService.findspidDescByspid(list.get(i).getSpid()).getName());
  380 + cell = row.createCell(2);//列
  381 + cell.setCellValue(list.get(i).getComment_user_id());
  382 + cell = row.createCell(3);//列
  383 + cell.setCellValue(list.get(i).getComment_user_name());
  384 +
  385 + cell = row.createCell(4);//列
  386 + cell.setCellValue(list.get(i).getComment_content());
  387 +
  388 + cell = row.createCell(5);//列
  389 + cell.setCellValue(list.get(i).getComment_title());
  390 +
  391 +
  392 +
  393 + /*状态需要处理成用户可读*/
  394 + String common_type = "";//评论类型,topic:话题
  395 + if (list.get(i).getType().equals(CommonConst.CATEGORY_TOPIC) ) {
  396 + common_type = "话题";
  397 + } else {
  398 + common_type = "其他";
  399 + }
  400 + cell = row.createCell(6);//列
  401 + cell.setCellValue(common_type);
  402 + cell = row.createCell(7);//列
  403 + cell.setCellValue(list.get(i).getLevel());
  404 + /*状态需要处理成用户可读*/
  405 + String platform = "";//评论所属的平台
  406 + if (list.get(i).getPlatform().equals("a")) {
  407 + platform = "安卓";
  408 + } else if (list.get(i).getPlatform().equals("i")) {
  409 + platform = "ios";
  410 + } else {
  411 + platform = "未知";
  412 + }
  413 + cell = row.createCell(8);//列
  414 + cell.setCellValue(platform);
  415 +
  416 +
  417 +
  418 + cell = row.createCell(9);//列
  419 + cell.setCellValue(list.get(i).getCreated_at()==null?"":CommonUtils.formatDate(list.get(i).getCreated_at(), "yyyy-MM-dd HH:mm:ss"));
  420 +
  421 + /*状态需要处理成用户可读*/
  422 + String shenhe_Status = "";//状态 0.未审核,1:已审核,2审核失败
  423 + if (list.get(i).getState() == 1) {
  424 + shenhe_Status = "已审核";
  425 + } else if (list.get(i).getState() == 2) {
  426 + shenhe_Status = "已拒绝";
  427 + } else {
  428 + shenhe_Status = "未审核";
  429 + }
  430 + cell = row.createCell(10);//列
  431 + cell.setCellValue(shenhe_Status);
  432 +
  433 + /* 审核员需要做处理*/
  434 + String updater = "";//审核员名字
  435 + if (list.get(i).getState() != CommonConst.AUDIT_INTT) {
  436 + updater = CommonUtils.isEmpty(list.get(i).getUpdater()) || "null".equals(list.get(i).getUpdater())? "自动通过":list.get(i).getUpdater();
  437 + String auditor_id = list.get(i).getAuditor_id();
  438 + if (CommonUtils.isNotEmpty(auditor_id)) {
  439 + ShyUsers user = new ShyUsers();
  440 + user.setUser_id(auditor_id);
  441 + user = usersService.select(user);
  442 + if (CommonUtils.isNotNull(user)) {
  443 + String email = CommonUtils.isEmpty(user.getEmail()) || "null".equals(user.getEmail()) ? "" : user.getEmail();
  444 + String userName = CommonUtils.isEmpty(user.getUsername()) ? email : user.getUsername();
  445 + updater = userName ;
  446 + }
  447 + }
  448 + }
  449 + cell = row.createCell(11);//列
  450 + cell.setCellValue(updater);
  451 + cell = row.createCell(12);//列
  452 + cell.setCellValue(list.get(i).getMsg());
  453 +
  454 + }
  455 +
  456 + ByteArrayOutputStream os = new ByteArrayOutputStream();
  457 + wb.write(os);
  458 + byte[] contents = os.toByteArray();
  459 + InputStream is = new ByteArrayInputStream(contents);
  460 + response.setContentType("application/vnd.ms-excel;charset=utf-8");
  461 + response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xlsx").getBytes(), "iso-8859-1"));
  462 + out = response.getOutputStream();
  463 + bis = new BufferedInputStream(is);
  464 + bos = new BufferedOutputStream(out);
  465 + byte[] buff = new byte[2048];
  466 + int bytesRead;
  467 + while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
  468 + bos.write(buff, 0, bytesRead);
  469 + }
  470 + } catch (final Exception e) {
  471 + e.printStackTrace();
  472 + try {
  473 + if (bis != null)
  474 + bis.close();
  475 + if (bos != null)
  476 + bos.close();
  477 + } catch (Exception e1) {
  478 + e1.printStackTrace();
  479 + }
  480 + } finally {
  481 + try {
  482 + if (bis != null)
  483 + bis.close();
  484 + if (bos != null)
  485 + bos.close();
  486 + } catch (Exception e2) {
  487 + e2.printStackTrace();
  488 + }
  489 + }
  490 + }
292 } 491 }
src/main/java/com/cnlive/shenhe/serviceImpl/CommentsServiceImpl.java
@@ -140,6 +140,67 @@ public class CommentsServiceImpl { @@ -140,6 +140,67 @@ public class CommentsServiceImpl {
140 PageInfo<ShyComments> pageInfo = new PageInfo<>(commentsList); 140 PageInfo<ShyComments> pageInfo = new PageInfo<>(commentsList);
141 return pageInfo; 141 return pageInfo;
142 } 142 }
  143 + public PageInfo<ShyComments> getPageExcel(String activity_id, String content, Integer is_hot, Date start_date, Date end_date, Integer states, Integer receive, String updater, Integer page, Integer pageSize, String orderByClause, Integer spid, String userId,Integer type) {
  144 + Example example = new Example(ShyComments.class);
  145 + Example.Criteria criteria = example.createCriteria();
  146 + if (CommonUtils.isNotEmpty(orderByClause)) {
  147 + example.setOrderByClause(orderByClause);
  148 + } else {
  149 + example.setOrderByClause("comment_time desc");
  150 + }
  151 + if (CommonUtils.isNotNull(start_date) && CommonUtils.isNotNull(end_date)) {
  152 + criteria.andGreaterThanOrEqualTo("comment_time", start_date);
  153 + criteria.andLessThanOrEqualTo("comment_time", end_date);
  154 + }
  155 + if (CommonUtils.isNotEmpty(content)) {
  156 + criteria.andLike("comment_content", "%" + content + "%");
  157 + }
  158 + if (CommonUtils.isNotEmpty(updater)) {
  159 + criteria.andEqualTo("updater", updater);
  160 + }
  161 + if (CommonUtils.isNotNull(spid)) {
  162 + criteria.andEqualTo("spid", spid);
  163 + }
  164 + if (CommonUtils.isNotNull(is_hot)) {
  165 + criteria.andEqualTo("is_hot", is_hot);
  166 + }
  167 + if (CommonUtils.isNotNull(type)&&type==0) {
  168 + criteria.andEqualTo("type", CommonConst.CATEGORY_TOPIC);
  169 + }else if(CommonUtils.isNotNull(type)&&type==1){
  170 + criteria.andIsNull("type");
  171 + }
  172 + if (CommonUtils.isNotEmpty(activity_id)) {
  173 + criteria.andLike("activity_id", "%" + activity_id + "%");
  174 + }
  175 + if (CommonUtils.isNotNull(receive)) {
  176 + criteria.andEqualTo("receive", receive);
  177 + if (receive==CommonConst.RECEIVE_AFTER) {
  178 + criteria.andEqualTo("receive_user_id", userId);
  179 + }
  180 + }
  181 +
  182 +// if (CommonUtils.isNotEmpty(states)) {
  183 +// Example.Criteria criteria1 = example.createCriteria();
  184 +// String[] stas = states.split(",");
  185 +// for (String s : stas) {
  186 +// criteria1.orEqualTo("state", Integer.parseInt(s));
  187 +// }
  188 +// example.and(criteria1);
  189 +// }
  190 +
  191 + if (CommonUtils.isNotNull(states) && states != 3) {
  192 + criteria.andEqualTo("state", states);
  193 + } else if (CommonUtils.isNotNull(states) && states == 3) {
  194 + Example.Criteria criteria1 = example.createCriteria();
  195 + criteria1.orEqualTo("state", 1);
  196 + criteria1.orEqualTo("state", 2);
  197 + example.and(criteria1);
  198 + }
  199 +
  200 + List<ShyComments> commentsList = shyCommentsMapper.selectByExample(example);
  201 + PageInfo<ShyComments> pageInfo = new PageInfo<>(commentsList);
  202 + return pageInfo;
  203 + }
143 204
144 public ShyComments getDetailsComment(Integer id) { 205 public ShyComments getDetailsComment(Integer id) {
145 ShyComments comment = shyCommentsMapper.selectByPrimaryKey(id); 206 ShyComments comment = shyCommentsMapper.selectByPrimaryKey(id);
src/main/resources/templates/page/comment.html
@@ -60,6 +60,9 @@ @@ -60,6 +60,9 @@
60 60
61 <div class="btn-group margin-bottom pull-right"> 61 <div class="btn-group margin-bottom pull-right">
62 <div class="btn-group margin-bottom"> 62 <div class="btn-group margin-bottom">
  63 + <button type="button" class="btn btn-success btn-sm margin-r-5 filter "
  64 + th:onclick="'javascript:daochu()'" value="" id="excel">导出excel
  65 + </button>
63 <button type="button" id="quanbu" 66 <button type="button" id="quanbu"
64 class="btn btn-sm margin-r-5 action_bottom" 67 class="btn btn-sm margin-r-5 action_bottom"
65 th:onclick="'javascript:quanbu()'">全部 68 th:onclick="'javascript:quanbu()'">全部
@@ -834,6 +837,12 @@ @@ -834,6 +837,12 @@
834 } 837 }
835 }) 838 })
836 } 839 }
  840 +
  841 + //导出excel
  842 + function daochu() {
  843 + var param = location.search;
  844 + window.location.href = "/comments/excel" + param;
  845 + }
837 </script> 846 </script>
838 <script> 847 <script>
839 /*时间插件*/ 848 /*时间插件*/