diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CompetitionController.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CompetitionController.java index 1e3e8ac1..03fb27aa 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CompetitionController.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/CompetitionController.java @@ -1,62 +1,63 @@ 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.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.exception.CheckedException; 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.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.security.annotation.RequiresPermissions; import com.ruoyi.common.security.utils.SecurityUtils; import com.ruoyi.common.swagger.apiConstants.ApiTerminal; import com.ruoyi.system.api.domain.vo.WxAppletsCodeVo; import com.ruoyi.system.api.model.LoginUser; -import com.ruoyi.system.api.model.WxLoginUser; -import com.ruoyi.system.domain.*; -import com.ruoyi.system.domain.vo.*; +import com.ruoyi.system.domain.Competition; +import com.ruoyi.system.domain.CompetitionSharePermissions; +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.utils.LoginUserUtil; import com.ruoyi.system.utils.UtilTool; -import io.seata.core.model.Result; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.RandomStringUtils; import org.apache.poi.hssf.usermodel.*; 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.aspectj.weaver.loadtime.Aj; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.util.StringUtils; 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 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 * @@ -372,6 +373,48 @@ public class CompetitionController extends BaseController 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 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 * diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ICompetitionService.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ICompetitionService.java index d50a1ac3..9dcfd6f6 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ICompetitionService.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ICompetitionService.java @@ -87,6 +87,8 @@ public interface ICompetitionService extends IService Boolean edit(Competition entity); CompetitionExcleVo importExcleData(Long competitionId,Map maplist, Sheet sheet); + + CompetitionExcleVo importExcleDataUserAvatar(Long competitionId,Map maplist, Sheet sheet); //根据当前时间修改已经完成的赛会数据 int updateCompetitionFinish(); } diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/CompetitionServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/CompetitionServiceImpl.java index 7602e575..6f44f19c 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/CompetitionServiceImpl.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/CompetitionServiceImpl.java @@ -5,6 +5,7 @@ import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.crypto.digest.DigestUtil; import com.alibaba.fastjson.JSON; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.core.constant.CacheConstants; import com.ruoyi.common.core.constant.Constants; @@ -702,6 +703,251 @@ public class CompetitionServiceImpl extends ServiceImpl 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 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 wrapper1 = new QueryWrapper<>(); + wrapper1.lambda().eq(CompetitionMembers::getCompetitionOfTeamId,team.getId()) + .eq(CompetitionMembers::getIsDeleted,0) + .eq(CompetitionMembers::getCompetitionId,competitionId); + List dbMembersList = competitionMembersMapper.selectList(wrapper1); + //要获得属性 + List 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 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 public int updateCompetitionFinish() { return competitionMapper.updateCompetitionFinish();