|
@@ -0,0 +1,103 @@
|
|
|
+package thyyxxk.webserver.controller.singlepage;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import thyyxxk.webserver.config.auth.PassToken;
|
|
|
+import thyyxxk.webserver.config.exception.ExceptionEnum;
|
|
|
+import thyyxxk.webserver.dao.his.singlepage.LotteryDao;
|
|
|
+import thyyxxk.webserver.entity.ResultVo;
|
|
|
+import thyyxxk.webserver.entity.singlepage.lottery.LotteryResult;
|
|
|
+import thyyxxk.webserver.entity.singlepage.lottery.LotteryUser;
|
|
|
+import thyyxxk.webserver.entity.singlepage.lottery.StartLotteryRequest;
|
|
|
+import thyyxxk.webserver.utils.ResultVoUtil;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@RequestMapping("/lottery")
|
|
|
+public class LotteryController {
|
|
|
+ private final LotteryDao dao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public LotteryController(LotteryDao dao) {
|
|
|
+ this.dao = dao;
|
|
|
+ }
|
|
|
+
|
|
|
+ @PassToken
|
|
|
+ @PostMapping("/uploadLotteryUsers")
|
|
|
+ public ResultVo<List<LotteryUser>> uploadLotteryUsers(@RequestBody MultipartFile file) throws IOException {
|
|
|
+
|
|
|
+ List<LotteryUser> userList = new ArrayList<>();
|
|
|
+ dao.truncateTable();
|
|
|
+ Workbook workbook;
|
|
|
+ InputStream is = file.getInputStream();
|
|
|
+ workbook = WorkbookFactory.create(is);
|
|
|
+ // 获取第一个活页数据
|
|
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
|
+ // 第几行开始解析
|
|
|
+ int rowStart = 2;
|
|
|
+ for (int i = rowStart; i <= sheet.getLastRowNum(); i++) {
|
|
|
+ Row row = sheet.getRow(i);
|
|
|
+ if(null == row.getCell(0)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 获取行所有单元格数据
|
|
|
+ LotteryUser user = new LotteryUser();
|
|
|
+ user.setCodeRs(getCellValue(row.getCell(0)));
|
|
|
+ user.setName(getCellValue(row.getCell(1)));
|
|
|
+ userList.add(user);
|
|
|
+ if (userList.size() == 100) {
|
|
|
+ dao.insertUsers(userList);
|
|
|
+ userList.clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ workbook.close();
|
|
|
+ is.close();
|
|
|
+
|
|
|
+ if (!userList.isEmpty()) {
|
|
|
+ dao.insertUsers(userList);
|
|
|
+ }
|
|
|
+
|
|
|
+ return selectLotteryUsers();
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getCellValue(Cell cell) {
|
|
|
+ if (cell.getCellType() == CellType.NUMERIC) {
|
|
|
+ return String.valueOf((int) cell.getNumericCellValue());
|
|
|
+ }
|
|
|
+ return cell.getRichStringCellValue().getString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @PassToken
|
|
|
+ @GetMapping("/selectLotteryUsers")
|
|
|
+ public ResultVo<List<LotteryUser>> selectLotteryUsers() {
|
|
|
+ return ResultVoUtil.success(dao.selectLotteryUsers());
|
|
|
+ }
|
|
|
+
|
|
|
+ @PassToken
|
|
|
+ @PostMapping("/chooseWinner")
|
|
|
+ public ResultVo<LotteryUser> chooseWinner(@RequestBody StartLotteryRequest request) {
|
|
|
+ String designated = request.getCode() + "-" + request.getRound();
|
|
|
+ LotteryUser user = dao.selectDesignatedLotteryUser(designated);
|
|
|
+ if (null == user) {
|
|
|
+ user = dao.selectRandomLotteryUser();
|
|
|
+ if (null == user) {
|
|
|
+ return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "所以人都已中奖,无法进行下一轮抽奖。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ResultVoUtil.success(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PassToken
|
|
|
+ @PostMapping("/recordLotteryResult")
|
|
|
+ public ResultVo<Integer> recordLotteryResult(@RequestBody LotteryResult result) {
|
|
|
+ return ResultVoUtil.success(dao.updateLotteryResult(result));
|
|
|
+ }
|
|
|
+}
|