package com.stylefeng.guns.modular.system.service.impl; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.stylefeng.guns.core.util.ToolUtil; import com.stylefeng.guns.modular.system.dao.UserRedPacketRecordMapper; import com.stylefeng.guns.modular.system.model.UserRedPacketRecord; import com.stylefeng.guns.modular.system.service.ITCompanyService; import com.stylefeng.guns.modular.system.service.IUserRedPacketRecordService; import org.apache.poi.hssf.usermodel.*; //import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.util.CellRangeAddress; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class UserRedPacketRecordServiceImpl extends ServiceImpl implements IUserRedPacketRecordService { @Resource private UserRedPacketRecordMapper userRedPacketRecordMapper; @Autowired private ITCompanyService companyService; /** * 获取红包统计数据 * @param name * @param time * @param companyId * @param offset * @param limit * @return * @throws Exception */ @Override public Map queryRedEnvelopes(String name, String time, Integer companyId, Integer offset, Integer limit) throws Exception { String start = null; String end = null; if(ToolUtil.isNotEmpty(time)){ start = time.split(" - ")[0]; end = time.split(" - ")[1]; } List> list = userRedPacketRecordMapper.queryRedEnvelopes(name, start, end, companyId, offset, limit); int i = userRedPacketRecordMapper.queryRedEnvelopesCount(name, start, end, companyId); Map map = new HashMap<>(); map.put("rows", list); map.put("total", i); return map; } /** * 下载红包统计数据 * @param name * @param time * @param companyId * @return * @throws Exception */ @Override public HSSFWorkbook downloadRedEnvelopes(String name, String time, Integer companyId) throws Exception { String start = null; String end = null; if(ToolUtil.isNotEmpty(time)){ start = time.split(" - ")[0]; end = time.split(" - ")[1]; } List> maps = userRedPacketRecordMapper.queryRedEnvelopes(name, start, end, companyId, null, null); List> lists = new ArrayList<>(); List list = new ArrayList<>(); list.add("活动名称:"); list.add(name); list.add(""); list.add("起止时间:"); list.add(time); list.add(""); list.add("运营商:"); list.add(null != companyId ? companyService.selectById(companyId).getName() : ""); lists.add(list); list = new ArrayList<>(); list.add("时间"); list.add("领取情况"); list.add(""); list.add(""); list.add(""); list.add("使用情况"); list.add(""); list.add(""); lists.add(list); list = new ArrayList<>(); list.add(""); list.add("注册领取人数"); list.add("系统派发人数"); list.add("合计"); list.add("领取金额"); list.add("使用人数"); list.add("使用张数"); list.add("使用金额"); lists.add(list); List>> lists1 = new ArrayList<>(); List> lists2 = new ArrayList<>(); for(Map map : maps){ List list2 = new ArrayList<>(); list2.add(null != map.get("time") ? map.get("time").toString() : ""); list2.add(null != map.get("receivePeople") ? map.get("receivePeople").toString() : ""); list2.add(null != map.get("receivePeople") ? map.get("receivePeople").toString() : ""); list2.add(null != map.get("receive") ? map.get("receive").toString() : ""); list2.add(null != map.get("receiveMoney") ? map.get("receiveMoney").toString() : ""); list2.add(null != map.get("usePeople") ? map.get("usePeople").toString() : ""); list2.add(null != map.get("use") ? map.get("use").toString() : ""); list2.add(null != map.get("useMoney") ? map.get("useMoney").toString() : ""); lists2.add(list2); } lists1.add(lists2); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(); HSSFSheet hssfSheet = hssfWorkbook.createSheet(); hssfSheet.setColumnWidth(0, 6 * 256); hssfSheet.setDefaultRowHeightInPoints(20f); for(int i = 0; i < lists.size(); i++){ HSSFRow hssfRow = hssfSheet.createRow(i);//设置第一行数据(标题) HSSFCellStyle style = hssfWorkbook.createCellStyle(); HSSFFont font = hssfWorkbook.createFont(); // font.setBold(true); style.setFont(font); // style.setAlignment(HorizontalAlignment.CENTER); for (int l = 0; l < lists.get(i).size(); l++) { HSSFCell hssfCell = hssfRow.createCell(l); // hssfCell.setCellType(CellType.STRING);//设置表格类型 hssfCell.setCellValue(lists.get(i).get(l)); hssfCell.setCellStyle(style); if(l > 0) { hssfSheet.setColumnWidth(l , 20 * 256); } } } //这个就是合并单元格 //参数说明:1:开始行 2:结束行 3:开始列 4:结束列 //比如我要合并 第二行到第四行的 第六列到第八列 sheet.addMergedRegion(new CellRangeAddress(1,3,5,7)); hssfSheet.addMergedRegion(new CellRangeAddress(0,0,1,2)); hssfSheet.addMergedRegion(new CellRangeAddress(0,0,4,5)); hssfSheet.addMergedRegion(new CellRangeAddress(1,2,0,0)); hssfSheet.addMergedRegion(new CellRangeAddress(1,1,1,4)); hssfSheet.addMergedRegion(new CellRangeAddress(1,1,5,7)); for(int i = 0; i < lists1.size(); i++){ //将数据添加到表格中 List data = null; for (int l = 0; l < lists1.get(i).size(); l++) { HSSFRow row = hssfSheet.createRow(l + 3); data = lists1.get(i).get(l); for (int j = 0; j < data.size(); j++) { HSSFCell hssfCell = row.createCell(j); // hssfCell.setCellType(CellType.STRING);//设置表格类型 hssfCell.setCellValue(data.get(j)); } } } return hssfWorkbook; } /** * 获取红包统计明细 * @param name * @param time * @param companyId * @param offset * @param limit * @return * @throws Exception */ @Override public Map queryRedEnvelopesInfo(String name, String time, Integer companyId, Integer offset, Integer limit) throws Exception { String start = null; String end = null; if(ToolUtil.isNotEmpty(time)){ start = time.split(" - ")[0]; end = time.split(" - ")[1]; } List> list = userRedPacketRecordMapper.queryRedEnvelopesInfo(name, start, end, companyId, offset, limit); int i = userRedPacketRecordMapper.queryRedEnvelopesInfoCount(name, start, end, companyId); Map map = new HashMap<>(); map.put("rows", list); map.put("total", i); return map; } /** * 下载红包统计明细 * @param name * @param time * @param companyId * @return * @throws Exception */ @Override public HSSFWorkbook downloadRedEnvelopesInfo(String name, String time, Integer companyId) throws Exception { String start = null; String end = null; if(ToolUtil.isNotEmpty(time)){ start = time.split(" - ")[0]; end = time.split(" - ")[1]; } List> maps = userRedPacketRecordMapper.queryRedEnvelopesInfo(name, start, end, companyId, null, null); List> lists = new ArrayList<>(); List list = new ArrayList<>(); list.add("活动名称:"); list.add(name); list.add(""); list.add("起止时间:"); list.add(time); list.add("运营商:"); list.add(null != companyId ? companyService.selectById(companyId).getName() : ""); lists.add(list); list = new ArrayList<>(); list.add("领取时间"); list.add("领取人"); list.add("领取人电话"); list.add("领取金额"); list.add("到期时间"); list.add("状态"); list.add("使用时间"); lists.add(list); List>> lists1 = new ArrayList<>(); List> lists2 = new ArrayList<>(); for(Map map : maps){ List list2 = new ArrayList<>(); list2.add(null != map.get("time") ? map.get("time").toString() : ""); list2.add(null != map.get("name") ? map.get("name").toString() : ""); list2.add(null != map.get("phone") ? map.get("phone").toString() : ""); list2.add(null != map.get("money") ? map.get("money").toString() : ""); list2.add(null != map.get("expirationTime") ? map.get("expirationTime").toString() : ""); list2.add(null != map.get("state") ? map.get("state").toString() : ""); list2.add(null != map.get("endTime") ? map.get("endTime").toString() : ""); lists2.add(list2); } lists1.add(lists2); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(); HSSFSheet hssfSheet = hssfWorkbook.createSheet(); hssfSheet.setColumnWidth(0, 6 * 256); hssfSheet.setDefaultRowHeightInPoints(20f); for(int i = 0; i < lists.size(); i++){ HSSFRow hssfRow = hssfSheet.createRow(i);//设置第一行数据(标题) HSSFCellStyle style = hssfWorkbook.createCellStyle(); HSSFFont font = hssfWorkbook.createFont(); // font.setBold(true); style.setFont(font); // style.setAlignment(HorizontalAlignment.CENTER); for (int l = 0; l < lists.get(i).size(); l++) { HSSFCell hssfCell = hssfRow.createCell(l); // hssfCell.setCellType(CellType.STRING);//设置表格类型 hssfCell.setCellValue(lists.get(i).get(l)); hssfCell.setCellStyle(style); if(l > 0) { hssfSheet.setColumnWidth(l , 20 * 256); } } } //这个就是合并单元格 //参数说明:1:开始行 2:结束行 3:开始列 4:结束列 //比如我要合并 第二行到第四行的 第六列到第八列 sheet.addMergedRegion(new CellRangeAddress(1,3,5,7)); hssfSheet.addMergedRegion(new CellRangeAddress(0,0,1,2)); for(int i = 0; i < lists1.size(); i++){ //将数据添加到表格中 List data = null; for (int l = 0; l < lists1.get(i).size(); l++) { HSSFRow row = hssfSheet.createRow(l + 2); data = lists1.get(i).get(l); for (int j = 0; j < data.size(); j++) { HSSFCell hssfCell = row.createCell(j); // hssfCell.setCellType(CellType.STRING);//设置表格类型 hssfCell.setCellValue(data.get(j)); } } } return hssfWorkbook; } }