package com.qxgmat.controller.admin; import com.alibaba.fastjson.JSONObject; import com.nuliji.tools.Response; import com.nuliji.tools.ResponseHelp; import com.nuliji.tools.Transform; import com.qxgmat.data.constants.enums.SettingKey; import com.qxgmat.data.dao.entity.Rank; import com.qxgmat.data.dao.entity.Setting; import com.qxgmat.dto.admin.request.RankDto; import com.qxgmat.service.inline.RankService; import com.qxgmat.service.inline.SettingService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; @RestController("AdminSettingController") @RequestMapping("/admin/setting") @Api(tags = "配置信息接口", description = "全局独立配置设置", produces = MediaType.APPLICATION_JSON_VALUE) public class SettingController { private static final Logger logger = LoggerFactory.getLogger(SettingController.class); @Autowired private SettingService settingService; @Autowired private RankService rankService; @RequestMapping(value = "/index", method = RequestMethod.PUT) @ApiOperation(value = "修改首页配置", httpMethod = "PUT") private Response editIndex(@RequestBody @Validated JSONObject dto){ Setting entity = settingService.getByKey(SettingKey.INDEX); entity.setValue(dto); settingService.edit(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/index", method = RequestMethod.GET) @ApiOperation(value = "获取首页配置", httpMethod = "GET") private Response getIndex(){ Setting entity = settingService.getByKey(SettingKey.INDEX); logger.debug("{}", entity); return ResponseHelp.success(entity.getValue()); } @RequestMapping(value = "/place", method = RequestMethod.PUT) @ApiOperation(value = "修改考点设置", httpMethod = "PUT") private Response editPlace(@RequestBody @Validated JSONObject dto){ Setting entity = settingService.getByKey(SettingKey.PLACE); entity.setValue(dto); settingService.edit(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/place", method = RequestMethod.GET) @ApiOperation(value = "获取考点配置", httpMethod = "GET") private Response getPlace(){ Setting entity = settingService.getByKey(SettingKey.PLACE); return ResponseHelp.success(entity.getValue()); } @RequestMapping(value = "/sentence", method = RequestMethod.PUT) @ApiOperation(value = "修改长难句设置", httpMethod = "PUT") private Response editSentence(@RequestBody @Validated JSONObject dto){ Setting entity = settingService.getByKey(SettingKey.SENTENCE); entity.setValue(dto); settingService.edit(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/sentence", method = RequestMethod.GET) @ApiOperation(value = "获取长难句配置", httpMethod = "GET") private Response getSentence(){ Setting entity = settingService.getByKey(SettingKey.SENTENCE); return ResponseHelp.success(entity.getValue()); } @RequestMapping(value = "/exercise_time", method = RequestMethod.PUT) @ApiOperation(value = "修改做题时间设置", httpMethod = "PUT") private Response editExerciseTime(@RequestBody @Validated JSONObject dto){ Setting entity = settingService.getByKey(SettingKey.EXERCISE_TIME); entity.setValue(dto); settingService.edit(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/exercise_time", method = RequestMethod.GET) @ApiOperation(value = "获取做题时间配置", httpMethod = "GET") private Response getExerciseTime(){ Setting entity = settingService.getByKey(SettingKey.EXERCISE_TIME); return ResponseHelp.success(entity.getValue()); } @RequestMapping(value = "/examination_time", method = RequestMethod.PUT) @ApiOperation(value = "修改做题时间设置", httpMethod = "PUT") private Response editExaminationTime(@RequestBody @Validated JSONObject dto){ Setting entity = settingService.getByKey(SettingKey.EXAMINATION_TIME); entity.setValue(dto); settingService.edit(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/examination_time", method = RequestMethod.GET) @ApiOperation(value = "获取做题时间配置", httpMethod = "GET") private Response getExaminationTime(){ Setting entity = settingService.getByKey(SettingKey.EXAMINATION_TIME); return ResponseHelp.success(entity.getValue()); } @RequestMapping(value = "/filter_time", method = RequestMethod.PUT) @ApiOperation(value = "修改剔除时间设置", httpMethod = "PUT") private Response editFilterTime(@RequestBody @Validated JSONObject dto){ Setting entity = settingService.getByKey(SettingKey.FILTER_TIME); entity.setValue(dto); settingService.edit(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/filter_time", method = RequestMethod.GET) @ApiOperation(value = "获取剔除时间配置", httpMethod = "GET") private Response getFilterTime(){ Setting entity = settingService.getByKey(SettingKey.FILTER_TIME); return ResponseHelp.success(entity.getValue()); } @RequestMapping(value = "/sentence_time", method = RequestMethod.PUT) @ApiOperation(value = "修改长难句时间设置", httpMethod = "PUT") private Response editSentenceTime(@RequestBody @Validated JSONObject dto){ Setting entity = settingService.getByKey(SettingKey.SENTENCE_TIME); entity.setValue(dto); settingService.edit(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/sentence_time", method = RequestMethod.GET) @ApiOperation(value = "获取长难句时间配置", httpMethod = "GET") private Response getSentenceTime(){ Setting entity = settingService.getByKey(SettingKey.SENTENCE_TIME); return ResponseHelp.success(entity.getValue()); } @RequestMapping(value = "/textbook_time", method = RequestMethod.PUT) @ApiOperation(value = "修改机经时间设置", httpMethod = "PUT") private Response editTextbookTime(@RequestBody @Validated JSONObject dto){ Setting entity = settingService.getByKey(SettingKey.TEXTBOOK_TIME); entity.setValue(dto); settingService.edit(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/textbook_time", method = RequestMethod.GET) @ApiOperation(value = "获取机经时间配置", httpMethod = "GET") private Response getTextbookTime(){ Setting entity = settingService.getByKey(SettingKey.TEXTBOOK_TIME); return ResponseHelp.success(entity.getValue()); } @RequestMapping(value = "/score_switch", method = RequestMethod.PUT) @ApiOperation(value = "修改分数开关", httpMethod = "PUT") private Response editScoreSwitch(@RequestBody @Validated JSONObject dto){ Setting entity = settingService.getByKey(SettingKey.SCORE_SWITCH); entity.setValue(dto); settingService.edit(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/score_switch", method = RequestMethod.GET) @ApiOperation(value = "获取分数开关", httpMethod = "GET") private Response getScoreSwitch(){ Setting entity = settingService.getByKey(SettingKey.SCORE_SWITCH); return ResponseHelp.success(entity.getValue()); } @RequestMapping(value = "/service_vip", method = RequestMethod.PUT) @ApiOperation(value = "修改Vip服务", httpMethod = "PUT") private Response editServiceVip(@RequestBody @Validated JSONObject dto){ Setting entity = settingService.getByKey(SettingKey.SERVICE_VIP); entity.setValue(dto); settingService.edit(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/service_vip", method = RequestMethod.GET) @ApiOperation(value = "获取Vip服务", httpMethod = "GET") private Response getServiceVip(){ Setting entity = settingService.getByKey(SettingKey.SERVICE_VIP); return ResponseHelp.success(entity.getValue()); } @RequestMapping(value = "/service_textbook", method = RequestMethod.PUT) @ApiOperation(value = "修改千行CAT服务", httpMethod = "PUT") private Response editServiceTextbook(@RequestBody @Validated JSONObject dto){ Setting entity = settingService.getByKey(SettingKey.SERVICE_TEXTBOOK); entity.setValue(dto); settingService.edit(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/service_textbook", method = RequestMethod.GET) @ApiOperation(value = "获取千行CAT服务", httpMethod = "GET") private Response getServiceTextbook(){ Setting entity = settingService.getByKey(SettingKey.SERVICE_TEXTBOOK); return ResponseHelp.success(entity.getValue()); } @RequestMapping(value = "/service_qx_cat", method = RequestMethod.PUT) @ApiOperation(value = "修改千行CAT服务", httpMethod = "PUT") private Response editServiceQXCat(@RequestBody @Validated JSONObject dto){ Setting entity = settingService.getByKey(SettingKey.SCORE_SWITCH); entity.setValue(dto); settingService.edit(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/service_qx_cat", method = RequestMethod.GET) @ApiOperation(value = "获取千行CAT服务", httpMethod = "GET") private Response getServiceQXCat(){ Setting entity = settingService.getByKey(SettingKey.SCORE_SWITCH); return ResponseHelp.success(entity.getValue()); } @RequestMapping(value = "/tips", method = RequestMethod.PUT) @ApiOperation(value = "修改结构说明", httpMethod = "PUT") private Response editTips(@RequestBody @Validated JSONObject dto){ Setting entity = settingService.getByKey(SettingKey.TIPS); entity.setValue(dto); settingService.edit(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/tips", method = RequestMethod.GET) @ApiOperation(value = "获取结构说明", httpMethod = "GET") private Response getTips(){ Setting entity = settingService.getByKey(SettingKey.TIPS); return ResponseHelp.success(entity.getValue()); } @RequestMapping(value = "/rank/add", method = RequestMethod.POST) @ApiOperation(value = "添加排行", httpMethod = "POST") private Response addRank(@RequestBody @Validated RankDto dto){ Rank entity = Transform.dtoToEntity(dto); rankService.add(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/rank/edit", method = RequestMethod.PUT) @ApiOperation(value = "修改排行", httpMethod = "PUT") private Response editRank(@RequestBody @Validated RankDto dto){ Rank entity = Transform.dtoToEntity(dto); rankService.edit(entity); return ResponseHelp.success(true); } @RequestMapping(value = "/rank/delete", method = RequestMethod.DELETE) @ApiOperation(value = "删除排行", httpMethod = "DELETE") private Response deleteRank(@RequestParam int id){ rankService.delete(id); return ResponseHelp.success(true); } @RequestMapping(value = "/rank/list", method = RequestMethod.GET) @ApiOperation(value = "获取排行设置", httpMethod = "GET") private Response> getRank(){ List rankList = rankService.all(); return ResponseHelp.success(rankList); } @RequestMapping(value = "/rank/import", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE, method = RequestMethod.POST) @ApiOperation(value = "导入排行数据", httpMethod = "POST") private Response importRank(@RequestParam("file") MultipartFile multipartFile) throws IOException { // 删除所有排行数据, 并导入excel数据 // 读取文件 InputStream is = new FileInputStream("002.xls"); // 将文件流解析成 POI 文档 POIFSFileSystem fs = new POIFSFileSystem(is); // 再将 POI 文档解析成 Excel 工作簿 HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFRow row = null; HSSFCell cell = null; // 得到第 1 个工作簿 HSSFSheet sheet = wb.getSheetAt(0); // 得到这一行一共有多少列 int totalColumns = sheet.getRow(0).getPhysicalNumberOfCells(); // 得到最后一行的坐标 Integer lastRowNum = sheet.getLastRowNum(); System.out.println("lastRowNum => " + lastRowNum); List rankList = new ArrayList<>(); Rank rank = null; String cellValue = null; // 从第 2 行开始读 for(int i=1;i<=lastRowNum;i++){ row = sheet.getRow(i); rank = new Rank(); for(int j=0;j