导入球队的队员头像补充

pull/371/head
wuyibo 2 years ago
parent 80d5c2ac4e
commit fcc3a3e314

@ -1,62 +1,63 @@
package com.ruoyi.system.controller; package com.ruoyi.system.controller;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal;
import java.security.InvalidParameterException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.system.UserInfo;
import com.alibaba.csp.sentinel.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.constant.Constants; import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.exception.CheckedException; import com.ruoyi.common.core.exception.CheckedException;
import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.exception.UtilException; import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.utils.uuid.IdUtils; import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.redis.service.RedisService; import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils; import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.common.swagger.apiConstants.ApiTerminal; import com.ruoyi.common.swagger.apiConstants.ApiTerminal;
import com.ruoyi.system.api.domain.vo.WxAppletsCodeVo; import com.ruoyi.system.api.domain.vo.WxAppletsCodeVo;
import com.ruoyi.system.api.model.LoginUser; import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.api.model.WxLoginUser; import com.ruoyi.system.domain.Competition;
import com.ruoyi.system.domain.*; import com.ruoyi.system.domain.CompetitionSharePermissions;
import com.ruoyi.system.domain.vo.*; import com.ruoyi.system.domain.Sms;
import com.ruoyi.system.domain.UserRole;
import com.ruoyi.system.domain.vo.CompetitionExcleVo;
import com.ruoyi.system.domain.vo.CompetitionResponse;
import com.ruoyi.system.domain.vo.CompetitionVo;
import com.ruoyi.system.domain.vo.SmsResponse;
import com.ruoyi.system.service.*; import com.ruoyi.system.service.*;
import com.ruoyi.system.utils.LoginUserUtil;
import com.ruoyi.system.utils.UtilTool; import com.ruoyi.system.utils.UtilTool;
import io.seata.core.model.Result;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.*; import org.apache.poi.xssf.usermodel.*;
import org.aspectj.weaver.loadtime.Aj;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidParameterException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/** /**
* Controller * Controller
* *
@ -372,6 +373,48 @@ public class CompetitionController extends BaseController
return AjaxResult.success(excleVo); return AjaxResult.success(excleVo);
} }
@PostMapping(value = "/teamEnrollExcleImportUserPhone",produces = "application/json;charset=utf-8")
@ResponseBody
@ApiOperation(value = ApiTerminal.wxMiniProgram+"导入球队报名excel(包含图片)补充队员头像")
public AjaxResult teamEnrollExcleImportUserPhone(
@RequestParam(value = "competitionId", required = true) Long competitionId,
@RequestParam("file") MultipartFile file) throws Exception {
CompetitionExcleVo excleVo = new CompetitionExcleVo();
String fileName = file.getOriginalFilename();
// 上传文件为空
if (StringUtils.isEmpty(fileName)) {
throw new CheckedException("没有导入文件");
}
// 上传文件名格式不正确
if (fileName.lastIndexOf(".") != -1 && !".xlsx".equals(fileName.substring(fileName.lastIndexOf(".")))) {
throw new CheckedException("文件名格式不正确, 请使用后缀名为.xlsx的文件");
}
String filePath = getFilePath(file);
Sheet sheet = null;
Workbook wb = null;
//读图片--开始
Map<String, PictureData> maplist = null;
BufferedInputStream inputStream = new BufferedInputStream(file.getInputStream());
// 判断用07还是03的方法获取图片
if (filePath.endsWith("xls")) {
wb = new HSSFWorkbook(inputStream);
sheet = wb.getSheetAt(0);
maplist = getPictures03((HSSFSheet) sheet);
} else if (filePath.endsWith("xlsx")) {
wb = new XSSFWorkbook(inputStream);
sheet = wb.getSheetAt(0);
maplist = getPictures07((XSSFSheet) sheet);
} else {
throw new CheckedException("Excel文件格式不支持");
}
excleVo = competitionService.importExcleDataUserAvatar(competitionId,maplist,sheet);
//使用完成关闭
wb.close();
if (inputStream != null) {
inputStream.close();
}
return AjaxResult.success(excleVo);
}
/** /**
* 03 (xls) clerk * 03 (xls) clerk
* *

@ -87,6 +87,8 @@ public interface ICompetitionService extends IService<Competition>
Boolean edit(Competition entity); Boolean edit(Competition entity);
CompetitionExcleVo importExcleData(Long competitionId,Map<String, PictureData> maplist, Sheet sheet); CompetitionExcleVo importExcleData(Long competitionId,Map<String, PictureData> maplist, Sheet sheet);
CompetitionExcleVo importExcleDataUserAvatar(Long competitionId,Map<String, PictureData> maplist, Sheet sheet);
//根据当前时间修改已经完成的赛会数据 //根据当前时间修改已经完成的赛会数据
int updateCompetitionFinish(); int updateCompetitionFinish();
} }

@ -5,6 +5,7 @@ import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.DigestUtil; import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.CacheConstants; import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.Constants; import com.ruoyi.common.core.constant.Constants;
@ -702,6 +703,251 @@ public class CompetitionServiceImpl extends ServiceImpl<CompetitionMapper, Compe
return excleVo; return excleVo;
} }
@Override
public CompetitionExcleVo importExcleDataUserAvatar(Long competitionId, Map<String, PictureData> maplist, Sheet sheet) {
CompetitionExcleVo excleVo = new CompetitionExcleVo();
String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//System.out.println("获取到精确到日的时间格式为"+time);
String[] str = time.split("-");//根据‘-’进行拆分字符串 拆分出来的日期有,年,日,月,根据年日月创建文件夹
String datePath = "/" + str[0] + "/" + str[1] + "/" + str[2] + "/";
//读图片--结束
// printImg(maplist);
//获得数据的总行数
int totalRowNum = sheet.getPhysicalNumberOfRows();
//todo 获取赛事编码
Row row1 = sheet.getRow(1);
Cell cell1 = row1.getCell(5);
if (cell1 == null) {
throw new CheckedException("赛事编码不能为空");
}
cell1.setCellType(CellType.STRING);
String competitionCode = cell1.getStringCellValue().toString();
if (StringUtils.isEmpty(competitionCode)) {
throw new CheckedException("赛事编码不能为空");
}
log.info("开始导入-->赛事编码:" + competitionCode);
Row row5 = sheet.getRow(5);
Cell cell5_1 = row5.getCell(0);
if (cell5_1 == null) {
throw new CheckedException("球队logo不能为空");
}
cell5_1.setCellType(CellType.STRING);
//todo 获取赛事信息
Competition competition = competitionMapper.selectCompetitionById(competitionId);
//校验比赛编码是否是本
BeanUtil.copyProperties(competition, excleVo);
if (UtilTool.isNull(competition)) {
throw new CheckedException("赛会信息不存在;");
}
if (!competition.getCompetitionCode().equals(competitionCode)) {
throw new CheckedException("导入的文件中的赛事编码错误");
}
Cell cell5_2 = row5.getCell(1);
if (cell5_2 == null) {
throw new CheckedException("球队名称不能为空");
}
cell5_2.setCellType(CellType.STRING);
String teamName = cell5_2.getStringCellValue().trim();
if (StringUtils.isEmpty(teamName)) {
throw new CheckedException("球队名称不能为空");
}
//球队队长
Cell cell5_2_1 = row5.getCell(4);
String captain = "";
if (cell5_2_1 != null) {
captain = cell5_2_1.getStringCellValue();
}
Cell cell5_3 = row5.getCell(5);
if (cell5_3 == null) {
throw new CheckedException("球队领队不能为空");
}
cell5_3.setCellType(CellType.STRING);
String teamLeader = cell5_3.getStringCellValue();
if (StringUtils.isEmpty(teamLeader)) {
throw new CheckedException("球队领队不能为空");
}
Cell cell5_4 = row5.getCell(6);
if (cell5_4 == null) {
throw new CheckedException("球队领队手机号码不能为空");
}
cell5_4.setCellType(CellType.STRING);
String teamLeaderTel = cell5_4.getStringCellValue().toString();
if (StringUtils.isEmpty(teamLeaderTel)) {
throw new CheckedException("球队队长手机号码不能为空");
}
Cell cell5_5 = row5.getCell(7);
if (cell5_5 == null) {
throw new CheckedException("球队人数不能为空");
}
cell5_5.setCellType(CellType.STRING);
String teamUserNum = cell5_5.getStringCellValue();
if (StringUtils.isEmpty(teamUserNum)) {
throw new CheckedException("球队人数不能为空");
}
//todo 校验是否是整数
if (!NumberUtil.isInteger(teamUserNum)) {
throw new CheckedException("球队人数必须是正整数");
}
//todo 获取到数据后,开始保存数据
LoginUser user = SecurityUtils.getLoginUser();
String userId = null;
if (ObjectUtil.isNotNull(user) && ObjectUtil.isNotNull(user.getUserid())) {
userId = String.valueOf(user.getUserid());
}
QueryWrapper<CompetitionOfTeam> wrapper = new QueryWrapper<>();
wrapper.lambda().eq(CompetitionOfTeam::getTeamName,teamName)
.eq(CompetitionOfTeam::getIsDeleted,0)
.eq(CompetitionOfTeam::getCompetitionId,competitionId)
.last("limit 1");
CompetitionOfTeam team = competitionOfTeamMapper.selectOne(wrapper);
if(ObjectUtil.isNull(team)){
team = new CompetitionOfTeam();
}
team.setTeamName(teamName);
team.setRemark("导入方式报名");
team.setContactsTel(teamLeaderTel);
team.setContacts(teamLeader);
team.setCaptain(captain);
team.setCreatedTime(new Date());
team.setCreatedBy(userId);
team.setCreatedTime(new Date());
team.setSerialNumber(Integer.parseInt(teamUserNum));
team.setCompetitionId(competition.getId());
//保存图片
PictureData pictureData = maplist.get("5_0");
if (pictureData == null) {
throw new UtilException("球队logo不能为空");
}
byte[] data = pictureData.getData();
//得到保存的file
String osPath = linuxLocation;
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith("win")) {
osPath = winLocation;
}
String newFileName = UUID.randomUUID() + "_5.jpg";
String uploadpath = UtilTool.getFileUploadPath(osPath);
File file2 = UtilTool.bytesToFile(data, uploadpath, newFileName);
// team.setTeamLogo(domainName + datePath + newFileName);
team.setStatus(0);
System.out.println(JSON.toJSONString(team));
//todo 保存球队数据;
if (UtilTool.isNotNull(team)) {
competitionOfTeamMapper.updateCompetitionOfTeam(team);
} else {
competitionOfTeamMapper.insertCompetitionOfTeam(team);
}
excleVo.setOfTeam(team);
//todo 清空球员数据
QueryWrapper<CompetitionMembers> wrapper1 = new QueryWrapper<>();
wrapper1.lambda().eq(CompetitionMembers::getCompetitionOfTeamId,team.getId())
.eq(CompetitionMembers::getIsDeleted,0)
.eq(CompetitionMembers::getCompetitionId,competitionId);
List<CompetitionMembers> dbMembersList = competitionMembersMapper.selectList(wrapper1);
//要获得属性
List<CompetitionMembers> membersVos = new ArrayList<>();
for (int i = 8; i < totalRowNum; i++) {
CompetitionMembers membersVo = new CompetitionMembers();
membersVo.setCompetitionId(competition.getId());
membersVo.setCompetitionOfTeamId(team.getId());
membersVo.setCompetitionNature(1);
membersVo.setCreatedBy(userId);
membersVo.setCreatedTime(new Date());
//获得第i行对象
Row row = sheet.getRow(i);
//真实姓名
Cell cell = row.getCell(1);
if (cell != null) {
cell.setCellType(CellType.STRING);
}
membersVo.setRealName(cell.getStringCellValue());
if (StringUtils.isEmpty(membersVo.getRealName())) {
break;
}
Optional<CompetitionMembers> opt = dbMembersList.stream().filter(a -> a.getCompetitionId().equals(competition.getId())
&& a.getRealName().equals(membersVo.getRealName())
&& a.getCompetitionOfTeamId().equals(membersVo.getCompetitionOfTeamId())).findFirst();
if(opt.isPresent()){
membersVo.setId(opt.get().getId());
}
//球衣号码
cell = row.getCell(2);
if (cell != null) {
cell.setCellType(CellType.STRING);
membersVo.setJerseyNumber(cell.getStringCellValue());
}
//位置
cell = row.getCell(3);
if (cell != null) {
cell.setCellType(CellType.STRING);
membersVo.setTeamPosition(cell.getStringCellValue());
}
//身高
cell = row.getCell(4);
if (cell != null) {
cell.setCellType(CellType.STRING);
//todo 校验是否是整数
if (!NumberUtil.isInteger(cell.getStringCellValue())) {
throw new CheckedException(membersVo.getRealName() + " 的身高必须是正整数");
}
membersVo.setHeight(new BigDecimal(cell.getStringCellValue()));
}
//体重
cell = row.getCell(5);
if (cell != null) {
cell.setCellType(CellType.STRING);
//todo 校验是否是整数
if (!NumberUtil.isInteger(cell.getStringCellValue())) {
throw new CheckedException(membersVo.getRealName() + " 的体重必须是正整数");
}
membersVo.setWeight(new BigDecimal(cell.getStringCellValue()));
}
//证件号码
cell = row.getCell(6);
if (cell != null) {
membersVo.setIdType("身份证");
membersVo.setIdCardNo(cell.getStringCellValue());
}
//电话
cell = row.getCell(7);
if (cell != null) {
membersVo.setContactsAreaCode("86");
membersVo.setContactsTel(String.valueOf(((XSSFCell) cell).getRawValue()));
}
//电话
cell = row.getCell(8);
if (cell != null) {
membersVo.setIdCardNo(String.valueOf(((XSSFCell) cell).getRawValue()));
}
//membersVo.setAvatar(domainName+datePath+newFileName);
//保存图片
PictureData pictureData2 = maplist.get(i + "_0");
log.info(membersVo.getRealName() + " 开始导入,图片位置" + i + "_0");
if (pictureData2 == null) {
throw new CheckedException(membersVo.getRealName() + " 的头像插入方式错误,请先选中单元格然后插入图片");
}
byte[] data2 = pictureData2.getData();
String newFileName2 = UUID.randomUUID() + "_0.jpg";
String uploadpath2 = UtilTool.getFileUploadPath(osPath);
File file3 = UtilTool.bytesToFile(data2, uploadpath2, newFileName2);
membersVo.setPersonalPhoto(domainName + datePath + newFileName2);
membersVo.setCreatedTime(new Date());
//默认球员是已确认状态
membersVo.setStatus(1);
System.out.println(JSON.toJSONString((membersVo)));
membersVo.setCreatedBy(userId);
membersVos.add(membersVo);
if(ObjectUtil.isEmpty(membersVo.getId())) {
competitionMembersMapper.insertCompetitionMembers(membersVo);
}else {
competitionMembersMapper.updateCompetitionMembers(membersVo);
}
}
excleVo.setTeamMemberList(membersVos);
return excleVo;
}
@Override @Override
public int updateCompetitionFinish() { public int updateCompetitionFinish() {
return competitionMapper.updateCompetitionFinish(); return competitionMapper.updateCompetitionFinish();

Loading…
Cancel
Save