package com.xjrsoft.module.textbook.controller; import cn.dev33.satoken.annotation.SaCheckPermission; import cn.hutool.core.bean.BeanUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.support.ExcelTypeEnum; import com.baomidou.mybatisplus.core.metadata.IPage; import com.github.yulichang.wrapper.MPJLambdaWrapper; import com.xjrsoft.common.enums.TextbookTypeEnum; import com.xjrsoft.common.exception.MyException; import com.xjrsoft.common.model.result.R; import com.xjrsoft.common.model.result.RT; import com.xjrsoft.common.page.ConventPage; import com.xjrsoft.common.page.PageOutput; import com.xjrsoft.common.utils.VoToColumnUtil; import com.xjrsoft.module.base.entity.BaseCourseSubject; import com.xjrsoft.module.generator.entity.ImportConfig; import com.xjrsoft.module.student.entity.BaseClassMajorSet; import com.xjrsoft.module.textbook.dto.*; import com.xjrsoft.module.textbook.entity.Textbook; import com.xjrsoft.module.textbook.entity.TextbookClassRelation; import com.xjrsoft.module.textbook.entity.TextbookStudentClaim; import com.xjrsoft.module.textbook.service.ITextbookService; import com.xjrsoft.module.textbook.service.ITextbookStudentClaimService; import com.xjrsoft.module.textbook.vo.*; import com.xjrsoft.module.veb.util.ImportExcelUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.validation.Valid; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @title: 教材管理 * @Author dzx * @Date: 2023-12-25 * @Version 1.0 */ @RestController @RequestMapping("/textbook" + "/textbook") @Api(value = "/textbook" + "/textbook",tags = "教材管理代码") @AllArgsConstructor public class TextbookController { private final ITextbookService textbookService; private final ITextbookStudentClaimService textbookStudentClaimService; @GetMapping(value = "/page") @ApiOperation(value="教材管理列表(分页)") @SaCheckPermission("textbook:detail") public RT> page(@Valid TextbookPageDto dto){ IPage page = textbookService.pageRubAndHand(dto); PageOutput pageOutput = ConventPage.getPageOutput(page, TextbookPageVo.class); return RT.ok(pageOutput); } @GetMapping(value = "/page-consumption") @ApiOperation(value="学生确认领取教材列表(分页,学生收费用)") @SaCheckPermission("textbook:detail") public RT> pageConsumption(@Valid TextbookConsumptionPageDto dto){ MPJLambdaWrapper textbookMPJLambdaWrapper = new MPJLambdaWrapper<>(); textbookMPJLambdaWrapper .select(TextbookStudentClaim::getId) .selectAs(Textbook::getId,TextbookConsumptionPageVo::getTextbookId) .selectAs(BaseCourseSubject::getName, TextbookConsumptionPageVo::getCourseSubjectIdCn) .select(Textbook.class, x -> VoToColumnUtil.fieldsToColumns(TextbookConsumptionPageVo.class).contains(x.getProperty())) .leftJoin(Textbook.class, Textbook::getId, TextbookStudentClaim::getTextbookId) .leftJoin(BaseCourseSubject.class, BaseCourseSubject::getId, Textbook::getCourseSubjectId) .eq(dto.getBaseSemesterId() != null && dto.getBaseSemesterId() > 0, TextbookStudentClaim::getBaseSemesterId, dto.getBaseSemesterId()) .eq(dto.getStudentUserId() != null && dto.getStudentUserId() > 0,TextbookStudentClaim::getStudentUserId, dto.getStudentUserId()) ; IPage page = textbookStudentClaimService.selectJoinListPage(ConventPage.getPage(dto),TextbookConsumptionPageVo.class,textbookMPJLambdaWrapper); for(TextbookConsumptionPageVo t : page.getRecords()){ if((t.getTextbookType() != null) && !t.getTextbookType().isEmpty()){ t.setTextbookTypeCn(TextbookTypeEnum.getValue(t.getTextbookType())); } } PageOutput pageOutput = ConventPage.getPageOutput(page, TextbookConsumptionPageVo.class); return RT.ok(pageOutput); } @GetMapping(value = "/List-subscription") @ApiOperation(value="教材管理列表(不分页,教材征订用)") @SaCheckPermission("textbook:detail") public RT> listSubscription(@Valid TextbookSubscriptionListDto dto){ List list = textbookService.getSubscriptionListByClass(dto); return RT.ok(list); } @GetMapping(value = "/info-detail") @ApiOperation(value="根据id查询教材管理信息") @SaCheckPermission("textbook:detail") public RT info(@RequestParam Long id){ Textbook textbook = textbookService.getById(id); if (textbook == null) { return RT.ok(new TextbookVo()); } return RT.ok(BeanUtil.toBean(textbook, TextbookVo.class)); } @GetMapping(value = "/info-byissn") @ApiOperation(value="根据isnn查询教材管理信息") @SaCheckPermission("textbook:detail") public RT infoByissn(@RequestParam String issn){ TextbookVo textbook = textbookService.getInfoByissn(issn); return RT.ok(textbook); } @GetMapping(value = "/info-subscription-list") @ApiOperation(value="教材管理-详情-征订记录") @SaCheckPermission("textbook:detail") public RT> subscriptionList(@RequestParam Long id){ List result = textbookService.subscriptionList(id); if (result == null) { return RT.ok(new ArrayList<>()); } return RT.ok(result); } @GetMapping(value = "/info-warehouse-list") @ApiOperation(value="教材管理-详情-入库记录") @SaCheckPermission("textbook:detail") public RT> warehouseList(@RequestParam Long id){ List result = textbookService.warehouseList(id); if (result == null) { return RT.ok(new ArrayList<>()); } return RT.ok(result); } @GetMapping(value = "/info-issue-list") @ApiOperation(value="教材管理-详情-出库记录") @SaCheckPermission("textbook:detail") public RT> issueList(@RequestParam Long id){ List result = textbookService.issueList(id); if (result == null) { return RT.ok(new ArrayList<>()); } return RT.ok(result); } @GetMapping(value = "/info-claim-list") @ApiOperation(value="教材管理-详情-领取记录") @SaCheckPermission("textbook:detail") public RT> claimList(@RequestParam Long id){ List result = textbookService.claimList(id); if (result == null) { return RT.ok(new ArrayList<>()); } return RT.ok(result); } @PostMapping @ApiOperation(value = "新增教材管理") @SaCheckPermission("textbook:add") public RT add(@Valid @RequestBody AddTextbookDto dto) throws ParseException { Textbook textbook = BeanUtil.toBean(dto, Textbook.class); boolean isSuccess = textbookService.addRubAndHand(textbook); return RT.ok(isSuccess); } @PutMapping @ApiOperation(value = "修改教材管理") @SaCheckPermission("textbook:edit") public RT update(@Valid @RequestBody UpdateTextbookDto dto) throws ParseException { Textbook textbook = BeanUtil.toBean(dto, Textbook.class); return RT.ok(textbookService.updateRubAndHand(textbook)); } @DeleteMapping @ApiOperation(value = "删除教材管理") @SaCheckPermission("textbook:delete") public RT delete(@Valid @RequestBody List ids){ return RT.ok(textbookService.delete(ids)); } // @GetMapping("/export") // @ApiOperation(value = "导出") // public ResponseEntity exportData(@Valid TextbookPageDto dto, @RequestParam(defaultValue = "false") Boolean isTemplate) { // List customerList = isTemplate != null && isTemplate ? new ArrayList<>() : ((PageOutput) page(dto).getData()).getList(); // ByteArrayOutputStream bot = new ByteArrayOutputStream(); // EasyExcel.write(bot, TextbookPageVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(customerList); // // return RT.fileStream(bot.toByteArray(), "Textbook" + ExcelTypeEnum.XLSX.getValue()); // } @PostMapping("/textbook-standing-export-query") @ApiOperation(value = "台账条件导出") public ResponseEntity textbookStandingExportQuery(@Valid @RequestBody TextbookStandingExportQuerytDto dto) { // @GetMapping("/textbook-standing-export-query") // @ApiOperation(value = "台账条件导出") // public ResponseEntity textbookStandingExportQuery(@Valid TextbookStandingExportQuerytDto dto) { List customerList = textbookService.listTextbookStandingExportQuery(dto); ByteArrayOutputStream bot = new ByteArrayOutputStream(); EasyExcel.write(bot, TextbookStandingExportQueryVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(customerList); return RT.fileStream(bot.toByteArray(), "TextbookStanding" + ExcelTypeEnum.XLSX.getValue()); } @PostMapping("/textbook-subscription-export-query") @ApiOperation(value = "教材征订记录条件导出") public ResponseEntity textbookSubscriptionExportQuery(@Valid @RequestBody TextbookSubscriptionExportQueryDto dto) { // @GetMapping("/textbook-subscription-export-query") // @ApiOperation(value = "教材征订记录条件导出") // public ResponseEntity textbookSubscriptionExportQuery(@Valid TextbookSubscriptionExportQueryDto dto) { ByteArrayOutputStream bot = textbookService.listTextbookSubscriptionExportQuery(dto); return RT.fileStream(bot.toByteArray(), "TextbookSubscription" + ExcelTypeEnum.XLSX.getValue()); } @PostMapping("/textbook-claim-export-query") @ApiOperation(value = "教材发放记录条件导出") public ResponseEntity textbookClaimExportQuery(@Valid @RequestBody TextbookClaimExportQueryDto dto) { // @GetMapping("/textbook-claim-export-query") // @ApiOperation(value = "教材发放记录条件导出") // public ResponseEntity textbookClaimExportQuery(@Valid TextbookClaimExportQueryDto dto) { ByteArrayOutputStream bot = textbookService.listTextbookClaimExportQuery(dto); return RT.fileStream(bot.toByteArray(), "TextbookClaim" + ExcelTypeEnum.XLSX.getValue()); } // @PostMapping("/excel-import") // @ApiOperation(value = "excel教材导入") // @SaCheckPermission("coursetable:excelimport") // public R excelImport( @RequestParam("file") MultipartFile file) throws IOException { // InputStream inputStream = file.getInputStream(); // return R.ok(textbookService.excelImport(inputStream)); // } @PostMapping("/textbook-import") @ApiOperation(value = "教材信息导入") public RT textbook(@RequestParam MultipartFile file) throws IOException, IllegalAccessException { String result = textbookService.textbook(file); if (!result.isEmpty()) { throw new MyException(result); } return RT.ok("全部成功", true); } @PostMapping("/textbook-template-download") @ApiOperation(value = "教材信息模板下载") public ResponseEntity textbookTemplate() throws IOException { // @GetMapping("/textbook-template") // @ApiOperation(value = "教材信息模板下载") // public ResponseEntity textbookTemplate() throws IOException { TextbookImportDto obj = new TextbookImportDto(); // 开始写入 Workbook workbook = new XSSFWorkbook(); // 创建一个工作表(sheet) String sheetName = "sheet1"; Sheet sheet = workbook.createSheet(sheetName); List importConfigs = ImportExcelUtil.allFields(obj); // 标题 String title = "教材信息导入模板"; ImportExcelUtil.createCautionHead(workbook, sheet, 0, title, importConfigs.size() - 1, 36); // 提示必填 String content = "说明:红底为必填项,白底为非必填项;只有教材分类为教材时才需要填写学科组和对应课程,学科组和课程必须是系统中已添加的信息,且学科组和课程已建立绑定关系;导入时请将示例数据删除,避免导入失败!"; ImportExcelUtil.createCautionHead(workbook, sheet, 1, content, importConfigs.size() - 1, 12); // 表头 ImportExcelUtil.createHead(workbook, sheet, importConfigs, 2); //写入文件 ByteArrayOutputStream bot = new ByteArrayOutputStream(); workbook.write(bot); String fileName = "教材信息导入模板"; fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8); return R.fileStream(bot.toByteArray(), fileName + ExcelTypeEnum.XLSX.getValue()); } }