UserMessengeController.java 6.99 KB
package zzsy.controller;
/**
 * Created by 周伟鹏 on 2018/10/16.
 */

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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zzsy.dao.UserMessengeRepository;
import zzsy.entity.UserMessenge;
import zzsy.utils.ErrorEnum;
import zzsy.utils.ResponseBean;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Auther: Administrator
 * @Date: 2018/10/16 16:19
 * @Description:
 */
@RestController
@RequestMapping(value = "/userApi")
public class UserMessengeController {
    @Autowired
    UserMessengeRepository userMessengeService;

    @PostMapping("save")
    public ResponseBean save(@RequestParam(name = "name") String name, @RequestParam(name = "phone")String phone, @RequestParam(name = "company")String company,@RequestParam(name = "videoId")Integer videoId,
                             @RequestParam(name = "videoTitle") String videoTitle, @RequestParam(name = "videoUrl") String videoUrl){
        ResponseBean responseBean = new ResponseBean();
        try {
            String REGEX_PHONE = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
            boolean isPhone = Pattern.matches(REGEX_PHONE, phone);
            if(isPhone){
                UserMessenge userMessenge = new UserMessenge();
                userMessenge.setPhone(phone);
                userMessenge.setVideoId(videoId);
                Example<UserMessenge> example = Example.of(userMessenge);
                List<UserMessenge> userMessengeList = userMessengeService.findAll(example);
                if(userMessengeList.isEmpty()){
                    userMessenge.setName(name);
                    userMessenge.setCompany(company);
                    userMessenge.setVideoTitle(videoTitle);
                    userMessenge.setVideoUrl(videoUrl);
                    userMessengeService.save(userMessenge);
                }else {
                    userMessenge.setId(userMessengeList.get(0).getId());
                    userMessenge.setName(name);
                    userMessenge.setCompany(company);
                    userMessenge.setVideoTitle(videoTitle);
                    userMessenge.setVideoUrl(videoUrl);
                    userMessengeService.saveAndFlush(userMessenge);
                }
            }else{
                responseBean = new ResponseBean(ErrorEnum.PHONE_ERROR.getErrorCode(),ErrorEnum.PHONE_ERROR.getErrorMessage());
            }

        }catch (DataIntegrityViolationException e){
            responseBean = new ResponseBean(ErrorEnum.PHONE_REPEAT.getErrorCode(),ErrorEnum.PHONE_REPEAT.getErrorMessage());
        }
        return responseBean;
    }


    @RequestMapping("messenge")
    public ResponseBean findAll() {
        ResponseBean responseBean = new ResponseBean();
        responseBean.setData(userMessengeService.findAll());
        return responseBean;
    }

    @RequestMapping("excel")
    public void getExcel(HttpServletResponse response) {
        List<UserMessenge> userMessengeList = userMessengeService.findAll();
        String fileName = "正在上演用户信息表";
        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("填写时间");
            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("视频id");
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
            for (int i = 0; i < userMessengeList.size(); i++) {
                row = sheetlist.createRow((short) (i + 1));//行
                cell = row.createCell(0);//列
                cell.setCellValue(dateFormat.format(userMessengeList.get(i).getInsertDate()));
                cell = row.createCell(1);//列
                cell.setCellValue(userMessengeList.get(i).getVideoTitle());
                cell = row.createCell(2);//列
                cell.setCellValue(userMessengeList.get(i).getVideoUrl());
                cell = row.createCell(3);//列
                cell.setCellValue(userMessengeList.get(i).getName());
                cell = row.createCell(4);//列
                cell.setCellValue(userMessengeList.get(i).getPhone());
                cell = row.createCell(5);//列
                cell.setCellValue(userMessengeList.get(i).getCompany());
                cell = row.createCell(6);//列
                cell.setCellValue(userMessengeList.get(i).getVideoId());
            }

            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();
            }
        }
    }

}