VideosController.java 17 KB
package com.cnlive.shenhe.controller;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.bean.ErrorEnum;
import com.cnlive.shenhe.bean.ResponseBean;
import com.cnlive.shenhe.bean.SessionUser;
import com.cnlive.shenhe.entity.ShySites;
import com.cnlive.shenhe.entity.ShyUsers;
import com.cnlive.shenhe.entity.ShyVideos;
import com.cnlive.shenhe.serviceImpl.SitesServiceImpl;
import com.cnlive.shenhe.serviceImpl.UsersServiceImpl;
import com.cnlive.shenhe.serviceImpl.VideosServiceImpl;
import com.cnlive.shenhe.utils.AuditPass;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonUtils;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.List;

@Controller
@RequestMapping("/videos")
public class VideosController {
    private static Logger logger = LoggerFactory.getLogger(VideosController.class);
    @Autowired
    AuditPass Pass;
    @Autowired
    VideosServiceImpl videosService;
    @Autowired
    UsersServiceImpl usersService;
    @Autowired
    SitesServiceImpl sitesService;

    @Value("${qn_domain}")
    String qn_domain;

    @Value("${ks_domain}")
    String ks_domain;

    /**
     * 审核信息返回接口
     *
     * @param activity_id 视频id
     * @param state       状态码
     * @param attr_tags   标签
     * @param msg         备注信息
     * @return
     */
    @PostMapping("/shenheVideo")
    @ResponseBody
    public ResponseBean shenheInfo(Integer id, String activity_id, Integer state, String attr_tags, String msg, String callback, HttpSession session) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        String userId = sessionUser.getUserId();
        ShyVideos shyVideos = videosService.selectByPrimaryKey(id);
        if (shyVideos.getReceive() && CommonUtils.isNotEmpty(shyVideos.getReceive_user_id()) && userId.equals(shyVideos.getReceive_user_id())) {
            JSONObject result = null;
            try {
                result = Pass.auditPass(activity_id, state, attr_tags, msg, callback);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("视频审核失败");
                return new ResponseBean(ErrorEnum.ERROR);
            }
            if (result == null) {
                System.out.println("视频审核失败且接口错误没有返回信息");
                return new ResponseBean(ErrorEnum.ERROR);
            }
            Integer code = result.getInteger("code");
            if (code != 0) {
                System.out.println("审核失败,code==" + code);
                return new ResponseBean(code, result.getString("msg"));
            }
            state = state == 4 ? 2 : 1;
            int i = videosService.updateVideo(id, userId, state, msg);
            if (i == 0) {
                return new ResponseBean(ErrorEnum.ERROR);
            }
            return new ResponseBean(ErrorEnum.SUCCESS);
        }
        logger.error("领取逻辑不符,不能审核,Video_id:{}", shyVideos.getVideo_id());
        return new ResponseBean(ErrorEnum.ERROR.getErrorCode(), "领取逻辑不符,不能审核");
    }

    /**
     * 内容分页列表接口
     *
     * @param video_title
     * @param start_date
     * @param end_date
     * @param state
     * @param page
     * @param pageSize
     * @param orderByClause
     * @return
     */
    @GetMapping("/page")
    public Object getListVideos(Model model, String video_title, String start_date, String end_date, Integer state, Boolean receive, Integer page, Integer pageSize, String video_priority, String orderByClause, HttpSession session) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        Integer spid = sessionUser.getSpid();
        String userId = sessionUser.getUserId();
        if (CommonUtils.isNull(page)) page = 1;
        if (CommonUtils.isNull(pageSize)) pageSize = 10;
        if (spid == 0) spid = null;
        PageInfo<ShyVideos> videoPage = videosService.getListContent(video_title, start_date, end_date, state, receive, page, pageSize, video_priority, orderByClause, spid, userId);
        List<ShyVideos> list = videoPage.getList();
        if (!list.isEmpty()) {
            for (ShyVideos videos : list) {
                if (videos.getState() != CommonConst.AUDIT_INTT) {
                    String auditor = "白名单";
                    String auditor_id = videos.getAuditor_id();
                    if (CommonUtils.isNotEmpty(auditor_id)) {
                        ShyUsers user = new ShyUsers();
                        user.setUser_id(auditor_id);
                        user = usersService.select(user);
                        if (CommonUtils.isNotNull(user)) {
                            String userName = CommonUtils.isEmpty(user.getUsername()) ? "" : user.getUsername();
                            String email = CommonUtils.isEmpty(user.getEmail()) ? "" : user.getEmail();
                            auditor = userName + email;
                        }
                    }
                    videos.setAuditor(auditor);
                }
                //显示spid简称
                ShySites shySites = sitesService.findspidDescByspid(videos.getSpid());
                if (CommonUtils.isNotNull(shySites)) {
                    videos.setSpid_desc(shySites.getName());
                }
            }
        }
        /* 获取参数值给前台 用来判断是否需要把查看显示出来 state recive*/
        String state1 = state == null ? "" : state.toString();
        String receive1 = "";
        if (CommonUtils.isNotNull(receive)) {
            receive1 = receive ? "true" : "false";
        } else {
            receive1 = "";
        }
        model.addAttribute("state1", state1);
        model.addAttribute("receive1", receive1);
        model.addAttribute("videoPage", videoPage);
        return "page/video.html";
    }

    /**
     * 内容详情页接口(查看)
     *
     * @param id
     * @return
     */
    @GetMapping("/details")
    public Object getDetailsVideo(Model model, Integer id) {
        ShyVideos detailsContent = videosService.getDetailsContent(id);
        Object images = JSON.parse(detailsContent.getVideo_imgs());
        Object avsampleImgs = JSON.parse(detailsContent.getAvsample_imgs());
        String yuming = "";
        Integer plat = detailsContent.getPlat();
        if (plat == CommonConst.QINIU_PLAT) {
            yuming = qn_domain + "/";
        } else if (plat == CommonConst.KS_PLAT) {
            yuming = ks_domain + "/";
        }
        model.addAttribute("images", images);
        model.addAttribute("avsampleImgs", avsampleImgs);
        model.addAttribute("video", detailsContent);
        model.addAttribute("yuming", yuming);

        return "page/videoDetail.html";
    }

    /**
     * 认领
     *
     * @param ids     根据id认领
     * @param num     认领条数
     * @param session
     * @return
     */
    @PostMapping("/receive")
    @ResponseBody
    public ResponseBean receive(String ids, Integer num, HttpSession session) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        String userId = sessionUser.getUserId();
        Integer spid = sessionUser.getSpid();
        //领取时要检查领取的数据是否可以领
        if (CommonUtils.isNotEmpty(ids)) {
            String[] cids = ids.split(",");
            for (String cid : cids) {
                ShyVideos shyVideos = videosService.selectByPrimaryKey(Integer.parseInt(cid));
                if (CommonUtils.isNotNull(shyVideos)) {
                    if (shyVideos.getState() != CommonConst.AUDIT_INTT || shyVideos.getReceive()) {
                        return new ResponseBean(ErrorEnum.ERROR.getErrorCode(), "您领取的内容中有已被领取或者已经被审核,请检查并重试");
                    }
                } else {
                    return new ResponseBean(ErrorEnum.ERROR.getErrorCode(), "您领取的内容不存在,请检查并重试");

                }
            }
        }
        Integer n = videosService.receive(ids, num, userId, spid);
        return new ResponseBean(ErrorEnum.SUCCESS.getErrorCode(), "认领到" + n + "条内容");
    }


    @PostMapping("/retReceive")
    @ResponseBody
    public ResponseBean retReceive(String ids, HttpSession session) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        String userId = sessionUser.getUserId();
        //在退领操作前先判断是否包含已退领或者已经审核的数据,有则退回检查并重新退领
        String[] cids = ids.split(",");
        for (String cid : cids) {
            ShyVideos shyVideos = videosService.selectByPrimaryKey(Integer.parseInt(cid));
            if (shyVideos.getState() != CommonConst.AUDIT_INTT || !shyVideos.getReceive() || !shyVideos.getReceive_user_id().equals(userId)) {
                return new ResponseBean(ErrorEnum.ERROR.getErrorCode(), "退领失败,您选中的内容包含已退领的或已经审核的");
            }
        }
        return videosService.retReceive(ids, userId);
    }

    /**
     * 根据条件导出excel
     *
     * @param video_title
     * @param start_date
     * @param end_date
     * @param state
     * @param receive
     * @param video_priority
     * @param orderByClause
     * @param session
     * @param response
     */
    @GetMapping("excel")
    public void getExcel(String video_title, String start_date, String end_date, Integer state, Boolean receive, String video_priority, String orderByClause, HttpSession session, HttpServletResponse response) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(SessionUser.getSessionKey());
        Integer spid = sessionUser.getSpid();
        String userId = sessionUser.getUserId();
        if (spid == 0) spid = null;
        List<ShyVideos> shyVideosList = videosService.findByCondition(video_title, start_date, end_date, state, receive, video_priority, orderByClause, spid, userId);
        String fileName = "点播数据";
        String auditor = "";//审核员名字
        String state1 = "";//审核状态 1已审核 2审核拒绝 0未审核
        XSSFWorkbook wb = new XSSFWorkbook();
        ServletOutputStream out = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            XSSFSheet sheetlist = wb.createSheet(fileName);
            XSSFRow row = sheetlist.createRow(0);//行
            XSSFCell cell = row.createCell(0);//列
            cell.setCellValue("id");
            cell = row.createCell(1);//列
            cell.setCellValue("标题");
            cell = row.createCell(2);//列
            cell.setCellValue("视频地址");
            cell = row.createCell(3);//列
            cell.setCellValue("标题图");
            cell = row.createCell(4);//列
            cell.setCellValue("视频标签");
            cell = row.createCell(5);//列
            cell.setCellValue("审核员");
            cell = row.createCell(6);//列
            cell.setCellValue("审核意见");
            cell = row.createCell(7);//列
            cell.setCellValue("状态");
            cell = row.createCell(8);//列
            cell.setCellValue("视频关键字");
            cell = row.createCell(9);//列
            cell.setCellValue("视频简介");
            cell = row.createCell(10);//列
            cell.setCellValue("视频id");
            cell = row.createCell(11);//列
            cell.setCellValue("创建时间");
            cell = row.createCell(12);//列
            cell.setCellValue("视频上传时间");
            cell = row.createCell(13);//列
            cell.setCellValue("视频时长(秒)");
            cell = row.createCell(14);//列
            cell.setCellValue("spid");
            for (int i = 0; i < shyVideosList.size(); i++) {
                row = sheetlist.createRow((short) (i + 1));//行
                cell = row.createCell(0);//列
                cell.setCellValue(shyVideosList.get(i).getId());
                cell = row.createCell(1);//列
                cell.setCellValue(shyVideosList.get(i).getVideo_title());
                cell = row.createCell(2);//列
                cell.setCellValue(shyVideosList.get(i).getVideo_url());
                cell = row.createCell(3);//列
                cell.setCellValue(shyVideosList.get(i).getVideo_poster());
                cell = row.createCell(4);//列
                cell.setCellValue(shyVideosList.get(i).getVideo_tags());
                cell = row.createCell(5);//列
                /* 审核员需要做处理*/
                if (shyVideosList.get(i).getState() != CommonConst.AUDIT_INTT) {
                    auditor = "白名单";
                    String auditor_id = shyVideosList.get(i).getAuditor_id();
                    if (CommonUtils.isNotEmpty(auditor_id)) {
                        ShyUsers user = new ShyUsers();
                        user.setUser_id(auditor_id);
                        user = usersService.select(user);
                        if (CommonUtils.isNotNull(user)) {
                            String userName = CommonUtils.isEmpty(user.getUsername()) ? "" : user.getUsername();
                            String email = CommonUtils.isEmpty(user.getEmail()) ? "" : user.getEmail();
                            auditor = userName + email;
                        }
                    }
                }
                cell.setCellValue(auditor);
                cell = row.createCell(6);//列
                cell.setCellValue(shyVideosList.get(i).getMsg());
                cell = row.createCell(7);//列
                /*状态需要处理成用户可读*/
                if (shyVideosList.get(i).getState() == 0) {
                    state1 = "未审核";
                } else if (shyVideosList.get(i).getState() == 1) {
                    state1 = "已审核";
                } else {
                    state1 = "审核拒绝";
                }
                cell.setCellValue(state1);
                cell = row.createCell(8);//列
                cell.setCellValue(shyVideosList.get(i).getVideo_keyword());
                cell = row.createCell(9);//列
                cell.setCellValue(shyVideosList.get(i).getVideo_desc());
                cell = row.createCell(10);//列
                cell.setCellValue(shyVideosList.get(i).getVideo_id());
                cell = row.createCell(11);//列
                cell.setCellValue(CommonUtils.formatDate(shyVideosList.get(i).getCreated_at(), "yyyy-MM-dd HH:mm:ss"));
                cell = row.createCell(12);//列
                cell.setCellValue(CommonUtils.formatDate(shyVideosList.get(i).getVideo_upload_time(), "yyyy-MM-dd HH:mm:ss"));
                cell = row.createCell(13);//列
                cell.setCellValue(shyVideosList.get(i).getDuration());
                cell = row.createCell(14);//列
                cell.setCellValue(shyVideosList.get(i).getSpid());
            }

            ByteArrayOutputStream os = new ByteArrayOutputStream();
            wb.write(os);
            byte[] content = os.toByteArray();
            InputStream is = new ByteArrayInputStream(content);
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xlsx").getBytes(), "iso-8859-1"));
            out = response.getOutputStream();
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (final Exception e) {
            e.printStackTrace();
            try {
                if (bis != null)
                    bis.close();
                if (bos != null)
                    bos.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } finally {
            try {
                if (bis != null)
                    bis.close();
                if (bos != null)
                    bos.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
}