package com.xjrsoft.module.textbook.service.impl; import cn.hutool.core.util.StrUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteTable; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.github.yulichang.base.MPJBaseServiceImpl; import com.github.yulichang.wrapper.MPJLambdaWrapper; import com.xjrsoft.common.enums.*; import com.xjrsoft.common.exception.MyException; import com.xjrsoft.common.page.ConventPage; import com.xjrsoft.common.utils.VoToColumnUtil; import com.xjrsoft.common.utils.excel.ExcelFillCellMergePrevColUtil; import com.xjrsoft.common.utils.excel.ExcelMergeUtil; import com.xjrsoft.module.base.entity.*; import com.xjrsoft.module.base.mapper.BaseClassCourseMapper; import com.xjrsoft.module.base.mapper.BaseCourseSubjectMapper; import com.xjrsoft.module.base.mapper.BaseSemesterMapper; import com.xjrsoft.module.base.service.IBaseClassService; import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll; import com.xjrsoft.module.system.entity.DictionaryDetail; import com.xjrsoft.module.system.mapper.DictionarydetailMapper; import com.xjrsoft.module.system.mapper.DictionaryitemMapper; import com.xjrsoft.module.teacher.entity.XjrUser; import com.xjrsoft.module.textbook.dto.*; import com.xjrsoft.module.textbook.entity.*; import com.xjrsoft.module.textbook.mapper.*; import com.xjrsoft.module.textbook.service.ITextbookService; import com.xjrsoft.module.textbook.service.IWfExerciseBookService; import com.xjrsoft.module.textbook.vo.*; import com.xjrsoft.module.veb.util.ImportExcelUtil; import lombok.AllArgsConstructor; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.*; import java.util.stream.Collectors; import static com.xjrsoft.module.veb.util.ImportExcelUtil.*; /** * @title: 教材管理 * @Author szs * @Date: 2023-12-25 * @Version 1.0 */ @Service @AllArgsConstructor public class TextbookServiceImpl extends MPJBaseServiceImpl implements ITextbookService { private final TextbookMapper textbookTextbookMapper; private final TextbookClassRelationMapper textbookTextbookClassRelationMapper; private final TextbookSubscriptionRecordMapper textbookTextbookSubscriptionRecordMapper; private final IWfExerciseBookService wfExerciseBookService; private final IBaseClassService baseClassService; private final TextbookIssueRecordMapper textbookIssueRecordMapper; private final TextbookStudentClaimMapper textbookStudentClaimMapper; private final BaseSemesterMapper baseSemesterMapper; private final BaseClassCourseMapper baseClassCourseMapper; private final DictionaryitemMapper dictionaryitemMapper; private final DictionarydetailMapper dictionarydetailMapper; private final SubjectGroupMapper subjectGroupMapper; private final BaseCourseSubjectMapper baseCourseSubjectMapper; private final TextbookSubscriptionItemMapper textbookSubscriptionItemMapper; @Override @Transactional(rollbackFor = Exception.class) public Boolean addRubAndHand(Textbook textbook) { // 判断isbn的唯一性 if(StringUtils.isBlank(textbook.getIssn()) || ObjectUtils.isEmpty(textbook.getSubjectGroupId()) || ObjectUtils.isEmpty(textbook.getCourseSubjectId()) ){ throw new MyException("issn,学科组,课程为必填字段"); } textbook.setIsbn(textbook.getIssn()); if(ObjectUtils.isNotEmpty(textbook.getDiscount()) && ObjectUtils.isNotEmpty(textbook.getPrice())){ BigDecimal discount = BigDecimal.valueOf(textbook.getDiscount()).divide( BigDecimal.valueOf(10)); textbook.setDiscountPrice(textbook.getPrice().multiply(discount)); } // 判断导入的教材是否已经存在,根据教材的 ISSN 码和使用的学科组和课程判断 LambdaQueryWrapper textbookLambdaQueryWrapper = new LambdaQueryWrapper<>(); textbookLambdaQueryWrapper .eq(Textbook::getIssn, textbook.getIssn()) .eq(Textbook::getDeleteMark, DeleteMark.NODELETE.getCode()) ; Textbook verifyTextbook = textbookTextbookMapper.selectOne(textbookLambdaQueryWrapper); int num; if(ObjectUtils.isNotEmpty(verifyTextbook)){ textbook.setId(verifyTextbook.getId()); num = textbookTextbookMapper.updateById(textbook); }else { num = textbookTextbookMapper.insert(textbook); } return num > 0; } @Override @Transactional(rollbackFor = Exception.class) public Boolean updateRubAndHand(Textbook textbook) { Textbook old = textbookTextbookMapper.selectById(textbook.getId()); if(ObjectUtils.isEmpty(old)){ throw new MyException("教材信息已经被修改,请刷新重试。"); } // 折扣修改,定价未修改 if(ObjectUtils.isNotEmpty(textbook.getDiscount()) && ObjectUtils.isEmpty(textbook.getPrice())){ BigDecimal discount = BigDecimal.valueOf(textbook.getDiscount()).divide(BigDecimal.valueOf(10), 2, RoundingMode.DOWN); textbook.setDiscountPrice(old.getPrice().multiply(discount)); } // 折扣未修改,定价修改 if(ObjectUtils.isEmpty(textbook.getDiscount()) && ObjectUtils.isNotEmpty(textbook.getPrice())){ BigDecimal discount = BigDecimal.valueOf(old.getDiscount()).divide(BigDecimal.valueOf(10), 2, RoundingMode.DOWN); textbook.setDiscountPrice(textbook.getPrice().multiply(discount)); } // 折扣修改,定价修改 if(ObjectUtils.isNotEmpty(textbook.getDiscount()) && ObjectUtils.isNotEmpty(textbook.getPrice())){ BigDecimal discount = BigDecimal.valueOf(textbook.getDiscount()).divide(BigDecimal.valueOf(10), 2, RoundingMode.DOWN); textbook.setDiscountPrice(textbook.getPrice().multiply(discount)); } int num = textbookTextbookMapper.updateById(textbook); return num > 0; } @Override @Transactional(rollbackFor = Exception.class) public Boolean delete(List ids) { // 删除教材前应判断教材是否被班级课程使用,一旦被使用不能删除 MPJLambdaWrapper baseClassCourse = new MPJLambdaWrapper<>(); baseClassCourse .select(Textbook::getId) .select(Textbook.class, x -> VoToColumnUtil.fieldsToColumns(Textbook.class).contains(x.getProperty())) .innerJoin(BaseClassCourse.class, BaseClassCourse::getTextbookId, Textbook::getId) .in(Textbook::getId, ids) ; List baseClassCourseTextbooks = this.selectJoinList(Textbook.class, baseClassCourse); if(!baseClassCourseTextbooks.isEmpty()){ throw new MyException(baseClassCourseTextbooks.get(0).getBookName() + "教材在课程中被使用,请先从课程中移除!"); } // 删除教材前应判断教材是否存在教材征订记录,如果存在征订记录,不应该删除 MPJLambdaWrapper textbookSubscriptionItem = new MPJLambdaWrapper<>(); textbookSubscriptionItem .select(Textbook::getId) .select(Textbook.class, x -> VoToColumnUtil.fieldsToColumns(Textbook.class).contains(x.getProperty())) .innerJoin(TextbookSubscriptionItem.class, TextbookSubscriptionItem::getTextbookId, Textbook::getId) .in(Textbook::getId, ids) ; List textbookSubscriptionItemTextbooks = this.selectJoinList(Textbook.class, textbookSubscriptionItem); if(!textbookSubscriptionItemTextbooks.isEmpty()){ throw new MyException(textbookSubscriptionItemTextbooks.get(0).getBookName() + "教材在证订中被使用,请先从证订中移除!"); } textbookTextbookMapper.deleteBatchIds(ids); return true; } @Override public IPage pageRubAndHand(TextbookPageDto dto) { MPJLambdaWrapper textbookMPJLambdaWrapper = new MPJLambdaWrapper<>(); textbookMPJLambdaWrapper .select(Textbook::getId) .select(Textbook.class, x -> VoToColumnUtil.fieldsToColumns(TextbookPageVo.class).contains(x.getProperty())) .leftJoin(SubjectGroup.class, SubjectGroup::getId, Textbook::getSubjectGroupId, wrapper -> wrapper .selectAs(SubjectGroup::getGroupName, TextbookPageVo::getGroupName) .like(StringUtils.isNotBlank(dto.getGroupName()), SubjectGroup::getGroupName, dto.getGroupName()) ) .leftJoin(BaseCourseSubject.class, BaseCourseSubject::getId, Textbook::getCourseSubjectId, wrapper -> wrapper .selectAs(BaseCourseSubject::getName, TextbookPageVo::getCourseName) .like(StringUtils.isNotBlank(dto.getCourseName()), BaseCourseSubject::getName, dto.getCourseName()) ) .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, Textbook::getTextbookType, wrapper -> wrapper .selectAs(DictionaryDetail::getName, TextbookPageVo::getTextbookTypeCn) ) .eq(ObjectUtils.isNotEmpty(dto.getUseType()), Textbook::getUseType, dto.getUseType()) .like(StringUtils.isNotBlank(dto.getIssn()), Textbook::getIssn, dto.getIssn()) .like(StringUtils.isNotBlank(dto.getIsbn()), Textbook::getIsbn, dto.getIsbn()) .like(StringUtils.isNotBlank(dto.getBookName()), Textbook::getBookName, dto.getBookName()) .like(StringUtils.isNotBlank(dto.getPublishingHouse()), Textbook::getPublishingHouse, dto.getPublishingHouse()) .like(StringUtils.isNotBlank(dto.getEditorInChief()), Textbook::getEditorInChief, dto.getEditorInChief()) .eq(StringUtils.isNotBlank(dto.getVersion()), Textbook::getVersion, dto.getVersion()) .eq(ObjectUtils.isNotEmpty(dto.getCourseSubjectId()), Textbook::getCourseSubjectId, dto.getCourseSubjectId()) .eq(ObjectUtils.isNotEmpty(dto.getSubjectGroupId()), Textbook::getCourseSubjectId, dto.getCourseSubjectId()) .eq(ObjectUtils.isNotEmpty(dto.getIsTextbookPlan()), Textbook::getIsTextbookPlan, dto.getIsTextbookPlan()) .eq(StringUtils.isNotBlank(dto.getTextbookType()), Textbook::getTextbookType, dto.getTextbookType()) .eq(StringUtils.isNotBlank(dto.getSpecificationsModels()), Textbook::getSpecificationsModels, dto.getSpecificationsModels()) .eq(ObjectUtils.isNotEmpty(dto.getIsSecd()), Textbook::getIsSecd, dto.getIsSecd()) .eq(StringUtils.isNotBlank(dto.getCategory()), Textbook::getCategory, dto.getCategory()) .eq(StringUtils.isNotBlank(dto.getPlanBatch()), Textbook::getPlanBatch, dto.getPlanBatch()) .eq(ObjectUtils.isNotEmpty(dto.getWorkTotalCount()), Textbook::getWorkTotalCount, dto.getWorkTotalCount()) .eq(StringUtils.isNotBlank(dto.getTextbookCategory()), Textbook::getTextbookCategory, dto.getTextbookCategory()) .eq(ObjectUtils.isNotEmpty(dto.getPrice()), Textbook::getPrice, dto.getPrice()) .eq(ObjectUtils.isNotEmpty(dto.getDiscount()), Textbook::getDiscount, dto.getDiscount()) .orderByDesc(Textbook::getId) ; return textbookTextbookMapper.selectJoinPage(ConventPage.getPage(dto), TextbookPageVo.class, textbookMPJLambdaWrapper); } @Override public List listRubAndHand(TextbookListDto dto) { MPJLambdaWrapper textbookMPJLambdaWrapper = new MPJLambdaWrapper<>(); textbookMPJLambdaWrapper .select(Textbook::getId) .select(Textbook.class, x -> VoToColumnUtil.fieldsToColumns(TextbookListVo.class).contains(x.getProperty())) .leftJoin(SubjectGroup.class, SubjectGroup::getId, Textbook::getSubjectGroupId, wrapper -> wrapper .selectAs(SubjectGroup::getGroupName, TextbookListVo::getGroupName) .like(StringUtils.isNotBlank(dto.getGroupName()), SubjectGroup::getGroupName, dto.getGroupName()) ) .leftJoin(BaseCourseSubject.class, BaseCourseSubject::getId, Textbook::getCourseSubjectId, wrapper -> wrapper .selectAs(BaseCourseSubject::getName, TextbookListVo::getCourseName) .like(StringUtils.isNotBlank(dto.getCourseName()), BaseCourseSubject::getName, dto.getCourseName()) ) .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, Textbook::getTextbookType, wrapper -> wrapper .selectAs(DictionaryDetail::getName, TextbookListVo::getTextbookTypeCn) ) .eq(ObjectUtils.isNotEmpty(dto.getUseType()), Textbook::getUseType, dto.getUseType()) .like(StringUtils.isNotBlank(dto.getIssn()), Textbook::getIssn, dto.getIssn()) .like(StringUtils.isNotBlank(dto.getIsbn()), Textbook::getIsbn, dto.getIsbn()) .like(StringUtils.isNotBlank(dto.getBookName()), Textbook::getBookName, dto.getBookName()) .like(StringUtils.isNotBlank(dto.getPublishingHouse()), Textbook::getPublishingHouse, dto.getPublishingHouse()) .like(StringUtils.isNotBlank(dto.getEditorInChief()), Textbook::getEditorInChief, dto.getEditorInChief()) .eq(StringUtils.isNotBlank(dto.getVersion()), Textbook::getVersion, dto.getVersion()) .eq(ObjectUtils.isNotEmpty(dto.getIsTextbookPlan()), Textbook::getIsTextbookPlan, dto.getIsTextbookPlan()) .eq(StringUtils.isNotBlank(dto.getTextbookType()), Textbook::getTextbookType, dto.getTextbookType()) .eq(StringUtils.isNotBlank(dto.getSpecificationsModels()), Textbook::getSpecificationsModels, dto.getSpecificationsModels()) .eq(ObjectUtils.isNotEmpty(dto.getIsSecd()), Textbook::getIsSecd, dto.getIsSecd()) .eq(StringUtils.isNotBlank(dto.getCategory()), Textbook::getCategory, dto.getCategory()) .eq(StringUtils.isNotBlank(dto.getPlanBatch()), Textbook::getPlanBatch, dto.getPlanBatch()) .eq(ObjectUtils.isNotEmpty(dto.getWorkTotalCount()), Textbook::getWorkTotalCount, dto.getWorkTotalCount()) .eq(StringUtils.isNotBlank(dto.getTextbookCategory()), Textbook::getTextbookCategory, dto.getTextbookCategory()) .eq(ObjectUtils.isNotEmpty(dto.getPrice()), Textbook::getPrice, dto.getPrice()) .eq(ObjectUtils.isNotEmpty(dto.getDiscount()), Textbook::getDiscount, dto.getDiscount()) ; return textbookTextbookMapper.selectJoinList(TextbookListVo.class, textbookMPJLambdaWrapper); } /* @Override @Transactional public Boolean dataHandleAddTextbookNode(Long dataId) { WfTextbookSubscription wfTextbookSubscription = iWfTextbookSubscriptionService.selectById(dataId); if (ObjectUtil.isNotNull(wfTextbookSubscription) && ObjectUtil.isNotNull(wfTextbookSubscription.getWfTextbookSubscriptionItemList()) && wfTextbookSubscription.getWfTextbookSubscriptionItemList().size() > 0) { //遍历征订项 for (WfTextbookSubscriptionItem wfTextbookSubscriptionItem : wfTextbookSubscription.getWfTextbookSubscriptionItemList()) { Long semesterId = wfTextbookSubscription.getBaseSemesterId(); String issn = wfTextbookSubscriptionItem.getIssn(); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper .eq(Textbook::getIssn, issn) .eq(Textbook::getBaseSemesterId, semesterId); List textbookList = this.list(queryWrapper); //征订的教材没有在教材管理 if (ObjectUtil.isNull(textbookList) || textbookList.size() == 0) { Textbook textbook = new Textbook() {{ setIssn((ObjectUtil.isNull(wfTextbookSubscriptionItem.getIssn()) || StringUtils.isBlank(wfTextbookSubscriptionItem.getIssn())) ? "/" : wfTextbookSubscriptionItem.getIssn()); setIsbn((ObjectUtil.isNull(wfTextbookSubscriptionItem.getIsbn()) || StringUtils.isBlank(wfTextbookSubscriptionItem.getIsbn())) ? "/" : wfTextbookSubscriptionItem.getIsbn()); setBookName((ObjectUtil.isNull(wfTextbookSubscriptionItem.getBookName()) || StringUtils.isBlank(wfTextbookSubscriptionItem.getBookName())) ? "/" : wfTextbookSubscriptionItem.getBookName()); setPublishingHouse((ObjectUtil.isNull(wfTextbookSubscriptionItem.getPublishingHouse()) || StringUtils.isBlank(wfTextbookSubscriptionItem.getPublishingHouse())) ? "/" : wfTextbookSubscriptionItem.getPublishingHouse()); setEditorInChief((ObjectUtil.isNull(wfTextbookSubscriptionItem.getEditorInChief()) || StringUtils.isBlank(wfTextbookSubscriptionItem.getEditorInChief())) ? "/" : wfTextbookSubscriptionItem.getEditorInChief()); setSubjectGroupId((ObjectUtil.isNull(wfTextbookSubscription.getSubjectGroupId())) ? 0 : wfTextbookSubscription.getSubjectGroupId()); setBaseSemesterId(wfTextbookSubscription.getBaseSemesterId()); setCourseSubjectId((ObjectUtil.isNull(wfTextbookSubscriptionItem.getCourseSubjectId())) ? 0 : wfTextbookSubscriptionItem.getCourseSubjectId()); setVersion((ObjectUtil.isNull(wfTextbookSubscriptionItem.getVersion()) || StringUtils.isBlank(wfTextbookSubscriptionItem.getVersion())) ? "/" : wfTextbookSubscriptionItem.getVersion()); setIsTextbookPlan((ObjectUtil.isNull(wfTextbookSubscriptionItem.getIsTextbookPlan()) || StringUtils.isBlank(wfTextbookSubscriptionItem.getIsTextbookPlan())) ? "/" : wfTextbookSubscriptionItem.getIsTextbookPlan()); if (ObjectUtil.isNotNull(wfTextbookSubscriptionItem.getSubscriptionType()) && wfTextbookSubscriptionItem.getSubscriptionType().equals(SubscriptionTypeEnum.STextbook.getCode())) { setTextbookType(TextbookTypeEnum.TTextbook.getCode()); } if (ObjectUtil.isNotNull(wfTextbookSubscriptionItem.getSubscriptionType()) && wfTextbookSubscriptionItem.getSubscriptionType().equals(SubscriptionTypeEnum.SMaterials.getCode())) { setTextbookType(TextbookTypeEnum.TMaterials.getCode()); } setSpecificationsModels("/"); setAppraisalPrice((ObjectUtil.isNull(wfTextbookSubscriptionItem.getAppraisalPrice())) ? new BigDecimal(0) : wfTextbookSubscriptionItem.getAppraisalPrice()); setPrice(new BigDecimal(0)); setDiscount(10D); setSubtotal(new BigDecimal(0)); setStock(0); }}; //班级不为空 List classIdList = new ArrayList<>(); if (ObjectUtil.isNotNull(wfTextbookSubscriptionItem.getClassIds()) && !wfTextbookSubscriptionItem.getClassIds().equals("")) { //将班级ids转换为List String classIds = wfTextbookSubscriptionItem.getClassIds(); String[] classIdStrs = classIds.split(","); for (String classIdStr : classIdStrs) { classIdList.add(Long.parseLong(classIdStr)); } //查询年级,如果添加的班级只存在一个年级,添加年级字段值 LambdaQueryWrapper queryWrapper1 = new LambdaQueryWrapper<>(); queryWrapper1 .select(BaseClass::getGradeId) .in(BaseClass::getId, classIdList) .groupBy(BaseClass::getGradeId); List baseClassList = baseClassService.list(queryWrapper1); if (ObjectUtil.isNotNull(baseClassList) && baseClassList.size() == 1) { textbook.setGradeId(baseClassList.get(0).getGradeId()); } } //插入教材数据 textbook.setCreateDate(new Date()); textbookTextbookMapper.insert(textbook); //插入班级和教材关系 for (Long classId : classIdList) { textbookTextbookClassRelationMapper.insert(new TextbookClassRelation() {{ setCreateDate(new Date()); setTextbookId(textbook.getId()); setClassId(classId); }}); } //添加教材征订记录 textbookTextbookSubscriptionRecordMapper.insert(new TextbookSubscriptionRecord() {{ setCreateDate(new Date()); setTextbookId(textbook.getId()); setWfTextbookSubscriptionId(wfTextbookSubscription.getId()); setWfTextbookSubscriptionItemId(wfTextbookSubscriptionItem.getId());//该字段存入征订项的id,征订项中有所属征订记录 }}); } else { //征订的教材在教材管理,进行班级的和记录的管理 Textbook textbook = textbookList.get(0); //班级不为空 if (ObjectUtil.isNotNull(wfTextbookSubscriptionItem.getClassIds()) && !wfTextbookSubscriptionItem.getClassIds().equals("")) { //将班级ids转换为List String classIds = wfTextbookSubscriptionItem.getClassIds(); String[] classIdStrs = classIds.split(","); for (String classIdStr : classIdStrs) { textbookTextbookClassRelationMapper.insert(new TextbookClassRelation() {{ setCreateDate(new Date()); setTextbookId(textbook.getId()); setClassId(Long.parseLong(classIdStr)); }}); } } //添加教材征订记录 textbookTextbookSubscriptionRecordMapper.insert(new TextbookSubscriptionRecord() {{ setCreateDate(new Date()); setTextbookId(textbook.getId()); setWfTextbookSubscriptionId(wfTextbookSubscription.getId()); setWfTextbookSubscriptionItemId(wfTextbookSubscriptionItem.getId());//该字段存入正定项的id,征订项中有所属征订记录 }}); } } } return true; }*/ /** * 添加作业本规则 * * @param dataId * @return */ @Override @Transactional public Boolean dataHandleAddExerciseBookNode(Long dataId) { WfExerciseBook wfExerciseBook = wfExerciseBookService.selectById(dataId); if (ObjectUtils.isNotEmpty(wfExerciseBook) && ObjectUtils.isNotEmpty(wfExerciseBook.getWfExerciseBookItemList()) && wfExerciseBook.getWfExerciseBookItemList().size() > 0) { for (WfExerciseBookItem wfExerciseBookItem : wfExerciseBook.getWfExerciseBookItemList()) { Long semesterId = wfExerciseBook.getBaseSemesterId(); String bookName = wfExerciseBookItem.getSubscriptionType();//作业本的选择类型对应教材管理的书名 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper .eq(Textbook::getBookName, bookName) ; List textbookList = this.list(queryWrapper); //征订的作业本没有在教材管理 if (ObjectUtils.isEmpty(textbookList) || textbookList.size() == 0) { Textbook textbook = new Textbook() {{ setIssn("/"); setIsbn("/"); if (ObjectUtils.isNotEmpty(wfExerciseBookItem.getSubscriptionType()) && wfExerciseBookItem.getSubscriptionType().equals(ExerciseBookeTypeEnum.ExerciseBook1.getCode())) { setBookName(ExerciseBookeTypeEnum.ExerciseBook1.getValue()); } if (ObjectUtils.isNotEmpty(wfExerciseBookItem.getSubscriptionType()) && wfExerciseBookItem.getSubscriptionType().equals(ExerciseBookeTypeEnum.ExerciseBook2.getCode())) { setBookName(ExerciseBookeTypeEnum.ExerciseBook2.getValue()); } setPublishingHouse("/"); setEditorInChief("/"); setVersion("/"); setTextbookType(TextbookTypeEnum.TExerciseBook.getCode()); setSpecificationsModels((ObjectUtils.isEmpty(wfExerciseBookItem.getSpecificationsModels()) || StringUtils.isBlank(wfExerciseBookItem.getSpecificationsModels())) ? "/" : wfExerciseBookItem.getSpecificationsModels()); //年级id if (ObjectUtils.isNotEmpty(wfExerciseBook.getClassId()) && wfExerciseBook.getClassId() > 0) { LambdaQueryWrapper queryWrapper1 = new LambdaQueryWrapper<>(); queryWrapper1 .eq(BaseClass::getId, wfExerciseBook.getClassId()); BaseClass baseClass = baseClassService.getOne(queryWrapper1); } setPrice(new BigDecimal(0)); setDiscount(10D); }}; //插入教材数据 textbook.setCreateDate(new Date()); textbookTextbookMapper.insert(textbook); //插入班级和教材关系 if (ObjectUtils.isNotEmpty(wfExerciseBook.getClassId()) && wfExerciseBook.getClassId() > 0) { textbookTextbookClassRelationMapper.insert(new TextbookClassRelation() {{ setCreateDate(new Date()); setTextbookId(textbook.getId()); setClassId(wfExerciseBook.getClassId()); }}); } //添加教材征订记录 textbookTextbookSubscriptionRecordMapper.insert(new TextbookSubscriptionRecord() {{ setCreateDate(new Date()); setTextbookId(textbook.getId()); setWfTextbookSubscriptionId(wfExerciseBook.getId()); }}); } else { //征订的教材在教材管理,进行班级的和记录的管理 Textbook textbook = textbookList.get(0); //班级不为空 if (ObjectUtils.isNotEmpty(wfExerciseBook.getClassId()) && wfExerciseBook.getClassId() > 0) { textbookTextbookClassRelationMapper.insert(new TextbookClassRelation() {{ setCreateDate(new Date()); setTextbookId(textbook.getId()); setClassId(wfExerciseBook.getClassId()); }}); } //添加教材征订记录 textbookTextbookSubscriptionRecordMapper.insert(new TextbookSubscriptionRecord() {{ setCreateDate(new Date()); setTextbookId(textbook.getId()); setWfTextbookSubscriptionId(wfExerciseBook.getId()); }}); } } } return true; } @Override public List subscriptionList(Long id) { Textbook textbook = this.getById(id); List recordVos = new ArrayList<>(); //是作业本 if (textbook.getTextbookType().equals(TextbookTypeEnum.TExerciseBook.getCode())) { recordVos = textbookTextbookMapper.exerciseBookSubscriptionList(id); for (TextbookSubscriptionRecordVo recordVo : recordVos) { recordVo.setIssn("/"); recordVo.setPublishingHouse("/"); recordVo.setEditorInChief("/"); recordVo.setIsTextbookPlanCn("/"); recordVo.setCourseName("/"); recordVo.setGradeName("/"); recordVo.setTeacherSubscriptionNumber(0); recordVo.setTeacherReferenceNumber(0); } } //是教材或者教辅 if (textbook.getTextbookType().equals(TextbookTypeEnum.TTextbook.getCode()) || textbook.getTextbookType().equals(TextbookTypeEnum.TMaterials.getCode())) { recordVos = textbookTextbookMapper.subscriptionList(id); for (TextbookSubscriptionRecordVo recordVo : recordVos) { if (ObjectUtils.isEmpty(recordVo) || StrUtil.isEmpty(recordVo.getClassIds())) { // recordVos.remove(recordVo); // if (recordVos.isEmpty()) { // break; // } continue; } String[] split = recordVo.getClassIds().split(","); List ids = new ArrayList<>(); for (String classId : split) { ids.add(classId.trim()); } List classInfo = textbookTextbookMapper.getClassInfo(ids); String useClass = ""; for (int i = 0; i < classInfo.size(); i++) { if (i >= 1) { useClass += ","; } TextbookSubscriptionClassVo classVo = classInfo.get(i); useClass += classVo.getName(); } recordVo.setUseClass(useClass); } } return recordVos; } @Override public TextbookVo getInfoByissn(String issn) { TextbookVo infoByissn = textbookTextbookMapper.getInfoByissn(issn); if (infoByissn == null) { return null; } List classRelationList = textbookTextbookClassRelationMapper.selectList( new QueryWrapper().lambda().eq(TextbookClassRelation::getTextbookId, infoByissn.getId()) ); return infoByissn; } @Override public List warehouseList(Long id) { List result = textbookTextbookMapper.warehouseList(id); if (!result.isEmpty()) { return result; } return new ArrayList<>(); } @Override public List getClassRelation(Long id) { List classRelation = textbookTextbookMapper.getClassRelation(id); if (!classRelation.isEmpty()) { return classRelation; } return new ArrayList<>(); } @Override public List issueList(Long id) { List result = textbookTextbookMapper.issueList(id); if (!result.isEmpty()) { return result; } return new ArrayList<>(); } @Override public List claimList(Long id) { List result = new ArrayList<>(); //教材领取分为两种 //教师领取 MPJLambdaWrapper queryIssueRecord = new MPJLambdaWrapper<>(); queryIssueRecord .eq(TextbookIssueRecord::getTextbookId, id) .eq(TextbookIssueRecord::getIssueMode, "im_teacher"); List textbookIssueRecordList = textbookIssueRecordMapper.selectJoinList(TextbookIssueRecord.class, queryIssueRecord); for (TextbookIssueRecord t : textbookIssueRecordList) { MPJLambdaWrapper queryTeaClaimVo = new MPJLambdaWrapper<>(); queryTeaClaimVo .selectAs(BaseSemester::getName, WfTextbookClaimListVo::getSemesterName) .selectAs(XjrUser::getName, WfTextbookClaimListVo::getName) .selectAs(XjrUser::getUserName, WfTextbookClaimListVo::getUserName) .selectAs(TextbookIssueRecord::getCreateDate, WfTextbookClaimListVo::getClaimDate) .leftJoin(XjrUser.class, XjrUser::getId, TextbookIssueRecord::getReceiveUserId) .leftJoin(WfTextbookClaim.class, WfTextbookClaim::getId, TextbookIssueRecord::getDataId) .leftJoin(BaseSemester.class, BaseSemester::getId, WfTextbookClaim::getBaseSemesterId) .eq(TextbookIssueRecord::getId, t.getId()); WfTextbookClaimListVo wfTextbookClaimListVo = textbookIssueRecordMapper.selectJoinOne(WfTextbookClaimListVo.class, queryTeaClaimVo); for (int j = 0; j < t.getIssueNumber(); j++) { wfTextbookClaimListVo.setClaimIdentity("教师"); wfTextbookClaimListVo.setClassName("/"); result.add(wfTextbookClaimListVo); } } //学生领取 MPJLambdaWrapper queryStuClaimVo = new MPJLambdaWrapper<>(); queryStuClaimVo .select("'学生' as claimIdentity") .selectAs(BaseClass::getName, WfTextbookClaimListVo::getClassName) .selectAs(BaseSemester::getName, WfTextbookClaimListVo::getSemesterName) .selectAs(XjrUser::getName, WfTextbookClaimListVo::getName) .selectAs(XjrUser::getUserName, WfTextbookClaimListVo::getUserName) .selectAs(TextbookStudentClaim::getCreateDate, WfTextbookClaimListVo::getClaimDate) .leftJoin(XjrUser.class, XjrUser::getId, TextbookStudentClaim::getStudentUserId) .leftJoin(BaseSemester.class, BaseSemester::getId, TextbookStudentClaim::getBaseSemesterId) .leftJoin(BaseClass.class, BaseClass::getId, TextbookStudentClaim::getClassId) .eq(TextbookStudentClaim::getTextbookId, id) .eq(TextbookStudentClaim::getIsClaim, 1); List wfTextbookClaimListVoList = textbookStudentClaimMapper.selectJoinList(WfTextbookClaimListVo.class, queryStuClaimVo); result.addAll(wfTextbookClaimListVoList); return result; } @Override public List listTextbookStandingExportQuery(TextbookStandingExportQuerytDto dto) { return textbookTextbookMapper.listTextbookStandingExportQuery(dto); } @Override public ByteArrayOutputStream listTextbookSubscriptionExportQuery(TextbookSubscriptionExportQueryDto dto) { List result = textbookTextbookMapper.listTextbookSubscriptionExportQuery(dto); //将班级转换为中文 for (TextbookSubscriptionExportQueryVo to : result) { if (to != null && to.getClassIds() != null && !("").equals(to.getClassIds())) { List classIdList = new ArrayList<>(); String[] classIdStrs = to.getClassIds().split(","); for (String classIdStr : classIdStrs) { classIdList.add(Long.parseLong(classIdStr.trim())); } LambdaQueryWrapper baseClassLambdaQueryWrapper = new LambdaQueryWrapper<>(); baseClassLambdaQueryWrapper .in(BaseClass::getId, classIdList); List baseClassList = baseClassService.list(baseClassLambdaQueryWrapper); if (baseClassList.size() > 0) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < baseClassList.size(); i++) { sb.append(baseClassList.get(i).getName()); if (i != baseClassList.size() - 1) { sb.append(","); } } to.setUseClass(sb.toString()); } } } ByteArrayOutputStream bot = new ByteArrayOutputStream(); // 设置动态头 String baseSemesterCn = ""; if(dto.getBaseSemesterId() != null){ LambdaQueryWrapper baseSemesterLambdaQueryWrapper = new LambdaQueryWrapper<>(); baseSemesterLambdaQueryWrapper .eq(BaseSemester::getId, dto.getBaseSemesterId()); BaseSemester baseSemester = baseSemesterMapper.selectOne(baseSemesterLambdaQueryWrapper); if(baseSemester != null){ baseSemesterCn = baseSemester.getName(); } } String headTitle = "重庆市铜梁职业教育中心" + baseSemesterCn + "教材征订表"; List> headList = new ArrayList<>(); // List head0 = new ArrayList<>(); // head0.add("个人信息"); // head0.add("用户名"); // List head1 = new ArrayList<>(); // head1.add("个人信息"); // head1.add("年龄"); // List head2 = new ArrayList<>(); // head2.add("个人信息"); // head2.add("地址"); headList.add(new ArrayList(){{ add(headTitle); add("国际标准刊号"); }}); headList.add(new ArrayList(){{ add(headTitle); add("书名"); }}); headList.add(new ArrayList(){{ add(headTitle); add("出版社"); }}); headList.add(new ArrayList(){{ add(headTitle); add("主编"); }}); headList.add(new ArrayList(){{ add(headTitle); add("估价(元)"); }}); headList.add(new ArrayList(){{ add(headTitle); add("是否为规划教材"); }}); headList.add(new ArrayList(){{ add(headTitle); add("对应课程"); }}); headList.add(new ArrayList(){{ add(headTitle); add("使用年级"); }}); headList.add(new ArrayList(){{ add(headTitle); add("使用班级"); }}); headList.add(new ArrayList(){{ add(headTitle); add("学生用书征订数量"); }}); headList.add(new ArrayList(){{ add(headTitle); add("教师教材用书征订数量"); }}); headList.add(new ArrayList(){{ add(headTitle); add("教师教参用书征订数量"); }}); headList.add(new ArrayList(){{ add(headTitle); add("学科组名称"); }}); headList.add(new ArrayList(){{ add(headTitle); add("有无配套教学资源"); }}); headList.add(new ArrayList(){{ add(headTitle); add("备注"); }}); EasyExcel.write(bot, TextbookSubscriptionExportQueryVo.class).automaticMergeHead(true).excelType(ExcelTypeEnum.XLSX).head(headList).sheet().doWrite(result); return bot; } @Override public ByteArrayOutputStream listTextbookClaimExportQuery(TextbookClaimExportQueryDto dto) { List customerList = textbookTextbookMapper.listTextbookClaimExportQuery(dto); ByteArrayOutputStream bot = new ByteArrayOutputStream(); //内容样式 WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); //设计内容居中 contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);// 水平居中 contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中 contentWriteCellStyle.setWrapped(true); //设置自动换行; // 设置字体 WriteFont contentWriteFont = new WriteFont(); contentWriteFont.setFontHeightInPoints((short) 12);//设置字体大小 contentWriteFont.setFontName("宋体"); //设置字体名字 contentWriteCellStyle.setWriteFont(contentWriteFont);//在样式用应用设置的字体; //设置样式; contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);//设置底边框; contentWriteCellStyle.setBottomBorderColor((short) 0);//设置底边框颜色; contentWriteCellStyle.setBorderLeft(BorderStyle.THIN); //设置左边框; contentWriteCellStyle.setLeftBorderColor((short) 0);//设置左边框颜色; contentWriteCellStyle.setBorderRight(BorderStyle.THIN);//设置右边框; contentWriteCellStyle.setRightBorderColor((short) 0);//设置右边框颜色; contentWriteCellStyle.setBorderTop(BorderStyle.THIN);//设置顶边框; contentWriteCellStyle.setTopBorderColor((short) 0); ///设置顶边框颜色; //设置头部样式 WriteCellStyle headWriteCellStyle = new WriteCellStyle(); // 背景颜色 // headWriteCellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE1.getIndex()); // headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND); // 字体 WriteFont headWriteFont = new WriteFont(); headWriteFont.setFontName("宋体");//设置字体名字 headWriteFont.setFontHeightInPoints((short) 14);//设置字体大小 headWriteFont.setBold(true);//字体加粗 headWriteCellStyle.setWriteFont(headWriteFont); //在样式用应用设置的字体; // 样式 headWriteCellStyle.setBorderBottom(BorderStyle.THIN);//设置底边框; headWriteCellStyle.setBottomBorderColor((short) 0);//设置底边框颜色; headWriteCellStyle.setBorderLeft(BorderStyle.THIN); //设置左边框; headWriteCellStyle.setLeftBorderColor((short) 0);//设置左边框颜色; headWriteCellStyle.setBorderRight(BorderStyle.THIN);//设置右边框; headWriteCellStyle.setRightBorderColor((short) 0);//设置右边框颜色; headWriteCellStyle.setBorderTop(BorderStyle.THIN);//设置顶边框; headWriteCellStyle.setTopBorderColor((short) 0); //设置顶边框颜色; headWriteCellStyle.setWrapped(true); //设置自动换行; //设置头部标题居中 headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//设置水平对齐的样式为居中对齐; headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //设置垂直对齐的样式为居中对齐; headWriteCellStyle.setShrinkToFit(true);//设置文本收缩至合适 // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现 HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); try (ExcelWriter excelWriter = EasyExcel.write(bot, TextbookClaimExportQueryVo.class).registerWriteHandler(horizontalCellStyleStrategy).build()) { //已经写入的行 int rowIndex = 0; //已经写入的表 int tableIndex = 0; // 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了 WriteSheet writeSheet = EasyExcel.writerSheet("模板").needHead(Boolean.FALSE).build(); //sheet的表头 List> sheetHeadList = new ArrayList>(); sheetHeadList.add(new ArrayList() {{ add("铜梁执教中心班级教材教辅发放情况表"); }}); //sheet的表头合并策略 ExcelFillCellMergePrevColUtil sheetHeadColumn = new ExcelFillCellMergePrevColUtil(); sheetHeadColumn.add(rowIndex, 0, 12); //这是一个sheet表头的table WriteTable writeSheetHeadTable = EasyExcel.writerTable(tableIndex).needHead(Boolean.TRUE).head(sheetHeadList).registerWriteHandler(sheetHeadColumn).build(); excelWriter.write(new ArrayList<>(), writeSheet, writeSheetHeadTable); //对返回的集合进行处理 //现针对学期进行分组 Map> groupedBySemester = customerList.stream() .collect(Collectors.groupingBy(TextbookClaimExportQueryVo::getBaseSemesterId)); Iterator>> groupedBySemesterIterator = groupedBySemester.entrySet().iterator(); while (groupedBySemesterIterator.hasNext()) { Map.Entry> groupedBySemesterEntry = groupedBySemesterIterator.next(); Long key = groupedBySemesterEntry.getKey(); List value = groupedBySemesterEntry.getValue(); Map> groupedBySemesterByClass = value.stream() .collect(Collectors.groupingBy(TextbookClaimExportQueryVo::getClassId)); Iterator>> groupedBySemesterByClassIdIterator = groupedBySemesterByClass.entrySet().iterator(); while (groupedBySemesterByClassIdIterator.hasNext()) { Map.Entry> groupedBySemesterByClassIdEntry = groupedBySemesterByClassIdIterator.next(); Long k = groupedBySemesterByClassIdEntry.getKey(); List v = groupedBySemesterByClassIdEntry.getValue(); //这是一个table的表头 List> tableHeadList = new ArrayList>(); tableHeadList.add(new ArrayList() {{ add("日期:" + v.get(0).getBaseSemesterIdCn() + " 班级:" + v.get(0).getClassIdCn() + " 班主任:" + v.get(0).getHeadTeacherName() + " 教室:" + v.get(0).getClassRoomName() + " "); }}); //table的表头合并策略 ExcelFillCellMergePrevColUtil tableHeadColumn = new ExcelFillCellMergePrevColUtil(); tableHeadColumn.add(++rowIndex, 0, 9); //这是一个table表头的table WriteTable writeTableHeadTable = EasyExcel.writerTable(++tableIndex).needHead(Boolean.TRUE).head(tableHeadList).registerWriteHandler(tableHeadColumn).build(); excelWriter.write(new ArrayList<>(), writeSheet, writeTableHeadTable); Map> groupedByTextbookType = v.stream() .collect(Collectors.groupingBy(TextbookClaimExportQueryVo::getTextbookTypeCn)); Iterator>> groupedByTextbookTypeIterator = groupedByTextbookType.entrySet().iterator(); int index = 1; BigDecimal total = new BigDecimal("0"); while (groupedByTextbookTypeIterator.hasNext()) { Map.Entry> groupedByTextbookTypeEntry = groupedByTextbookTypeIterator.next(); String kk = groupedByTextbookTypeEntry.getKey(); List vv = groupedByTextbookTypeEntry.getValue(); if (index == 1) { //添加小计 ExcelFillCellMergePrevColUtil subtotalColumn = new ExcelFillCellMergePrevColUtil(); subtotalColumn.add(rowIndex + vv.size() + 2, 1, 4); BigDecimal subtotal = new BigDecimal("0"); for (TextbookClaimExportQueryVo tv : vv) { subtotal = subtotal.add(tv.getSubtotal() == null ? new BigDecimal("0") : tv.getSubtotal()); } BigDecimal finalSubtotal = subtotal; vv.add(new TextbookClaimExportQueryVo() {{ setTextbookTypeCn(vv.get(0).getTextbookTypeCn()); setBookName("小计"); setSubtotal(finalSubtotal); }}); total = total.add(finalSubtotal); // 调用合并单元格工具类,此工具类是根据工程名称相同则合并后面数据 int mergeRowIndex = rowIndex + 1; int[] mergeColumeIndex = {0}; ExcelMergeUtil excelFillCellMergeStrategy = new ExcelMergeUtil(mergeRowIndex, mergeColumeIndex); // 第一次必须指定需要头,table 会继承sheet的配置,sheet配置了不需要,table 默认也是不需要 WriteTable writeContentTable = EasyExcel.writerTable(++tableIndex).needHead(Boolean.TRUE).registerWriteHandler(subtotalColumn).registerWriteHandler(excelFillCellMergeStrategy).build(); // 第一次写入会创建头 excelWriter.write(vv, writeSheet, writeContentTable); rowIndex += (1 + vv.size()); } else { //添加小计 ExcelFillCellMergePrevColUtil subtotalColumn = new ExcelFillCellMergePrevColUtil(); subtotalColumn.add(rowIndex + vv.size() + 1, 1, 4); BigDecimal subtotal = new BigDecimal("0"); for (TextbookClaimExportQueryVo tv : vv) { subtotal = subtotal.add(tv.getSubtotal()); } BigDecimal finalSubtotal = subtotal; vv.add(new TextbookClaimExportQueryVo() {{ setTextbookTypeCn(vv.get(0).getTextbookTypeCn()); setBookName("小计"); setSubtotal(finalSubtotal); }}); total = total.add(finalSubtotal); // 调用合并单元格工具类,此工具类是根据工程名称相同则合并后面数据 int mergeRowIndex = rowIndex; int[] mergeColumeIndex = {0}; ExcelMergeUtil excelFillCellMergeStrategy = new ExcelMergeUtil(mergeRowIndex, mergeColumeIndex); // 不需要头 WriteTable writeContentTable = EasyExcel.writerTable(++tableIndex).needHead(Boolean.FALSE).registerWriteHandler(subtotalColumn).registerWriteHandler(excelFillCellMergeStrategy).build(); excelWriter.write(vv, writeSheet, writeContentTable); rowIndex += (vv.size()); } index++; } //添加合计 BigDecimal finalTotal = total; List totalList = new ArrayList<>(); totalList.add(new TextbookClaimExportQueryVo() {{ setTextbookTypeCn("合计"); setSubtotal(finalTotal); }}); //-合并策略 ExcelFillCellMergePrevColUtil totaColumn = new ExcelFillCellMergePrevColUtil(); totaColumn.add(++rowIndex, 0, 5); //这是一个合计的table WriteTable writeTotalTable = EasyExcel.writerTable(++tableIndex).needHead(Boolean.FALSE).registerWriteHandler(totaColumn).build(); excelWriter.write(totalList, writeSheet, writeTotalTable); } } } return bot; } // @Override // @Transactional // public Boolean excelImport(InputStream inputStream) { // EasyExcel.read(inputStream, TextbookImportDto.class, new PageReadListener(dataList -> { // if (dataList.isEmpty()) { // throw new MyException("导入数据为空"); // } // saveData(dataList); // })).sheet().headRowNumber(3).doRead(); // // return true; // } // @Override // public IPage getSubscriptionPageByClass(TextbookSubscriptionListDto dto) { // if(StringUtils.isBlank(dto.getClassIds())){ // throw new MyException("请选择需要征订教材的班级"); // } // String[] classIdArr = dto.getClassIds().split(","); // List classIdList = Arrays.asList(classIdArr); // if(ObjectUtils.isEmpty(classIdList)){ // throw new MyException("请选择需要征订教材的班级"); // } // // // 找到可以使用多个学期且已经征订的教材 // // 找到可以使用多个学期且已经征订的教材 // MPJLambdaWrapper subscribedTextbook = new MPJLambdaWrapper<>(); // subscribedTextbook // .distinct() // .select(BaseClassCourse::getId) // .innerJoin(TextbookSubscriptionItemClass.class, // wrapper -> wrapper // .eq(TextbookSubscriptionItemClass::getTextbookSubscriptionItemId, TextbookSubscriptionItem::getId) // .in(TextbookSubscriptionItemClass::getBaseClassId, classIdList) // ) // // .innerJoin(BaseClassAdminCourse.class, BaseClassAdminCourse::getClassId, TextbookSubscriptionItemClass::getBaseClassId) // .innerJoin(BaseClassCourse.class, // wrapper -> wrapper // .eq(BaseClassCourse::getClassId, BaseClassAdminCourse::getId) // .eq(BaseClassCourse::getTextbookId, TextbookSubscriptionItem::getTextbookId) // ) // // .innerJoin(Textbook.class, Textbook::getId, TextbookSubscriptionItem::getTextbookId) // .gt(Textbook::getUseType, 1) // ; // List baseClassCourses = textbookSubscriptionItemMapper.selectJoinList(BaseClassCourse.class, subscribedTextbook); // // List baseClassCourseIds = baseClassCourses.stream() // .map(BaseClassCourse::getId) // .collect(Collectors.toList()); // // MPJLambdaWrapper baseClassCourseMPJLambdaWrapper = new MPJLambdaWrapper<>(); // baseClassCourseMPJLambdaWrapper // .disableSubLogicDel() // .distinct() // .selectAs(BaseClassCourse::getCourseId, TextbookSubscriptionListVo::getCourseSubjectId) // .selectAs(BaseClassCourse::getTextbookId, TextbookSubscriptionListVo::getTextbookId) // .selectAs(BaseCourseSubject::getName, TextbookSubscriptionListVo::getCourseName) // .select(Textbook.class, x -> VoToColumnUtil.fieldsToColumns(TextbookSubscriptionListVo.class).contains(x.getProperty())) // .innerJoin(Textbook.class, Textbook::getId, BaseClassCourse::getTextbookId) // .leftJoin(BaseCourseSubject.class, BaseCourseSubject::getId, BaseClassCourse::getCourseId) // .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, Textbook::getTextbookType, // wrapper -> wrapper // .selectAs(DictionaryDetail::getName, TextbookSubscriptionListVo::getTextbookTypeCn) // ) // .eq(BaseClassCourse::getBaseSemesterId, dto.getBaseSemesterId()) // .in(BaseClassCourse::getClassId, classIdList) // .notIn(!baseClassCourseIds.isEmpty(), BaseClassCourse::getId, baseClassCourseIds) // .eq(BaseClassCourse::getDeleteMark, DeleteMark.NODELETE.getCode()) // .eq(Textbook::getDeleteMark, DeleteMark.NODELETE.getCode()) // ; // IPage textbookSubscriptionListVoIPage = baseClassCourseMapper.selectJoinPage(ConventPage.getPage(dto), TextbookSubscriptionListVo.class, baseClassCourseMPJLambdaWrapper); //// IPage page = this.baseMapper.getSubscriptionPageByClass(new Page<>(dto.getLimit(), dto.getSize()), dto); // // // 处理每本教材使用的人数 // if(!classIdList.isEmpty()) { // for(TextbookSubscriptionListVo t : textbookSubscriptionListVoIPage.getRecords()){ // MPJLambdaWrapper countNum = new MPJLambdaWrapper<>(); // countNum // .innerJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getClassId, BaseClassCourse::getClassId) // .eq(BaseStudentSchoolRoll::getArchivesStatus, ArchivesStatusEnum.FB2901.getCode()) // .eq(BaseClassCourse::getTextbookId, t.getTextbookId()) // .eq(BaseClassCourse::getBaseSemesterId, dto.getBaseSemesterId()) // .in(BaseClassCourse::getClassId, classIdList) // .notIn(!baseClassCourseIds.isEmpty(), BaseClassCourse::getId, baseClassCourseIds) // .eq(BaseClassCourse::getDeleteMark, DeleteMark.NODELETE.getCode()) // ; // Long num = baseClassCourseMapper.selectCount(countNum); // t.setStudentSubscriptionNumber(num.intValue()); // // t.setClassIds(dto.getClassIds()); // } // } // // return textbookSubscriptionListVoIPage; // } @Override public List getSubscriptionListByClass(TextbookSubscriptionListDto dto) { if(StringUtils.isBlank(dto.getClassIds())){ throw new MyException("请选择需要征订教材的班级"); } String[] classIdArr = dto.getClassIds().split(","); List classIdList = Arrays.asList(classIdArr); if(ObjectUtils.isEmpty(classIdList)){ throw new MyException("请选择需要征订教材的班级"); } // 找到可以使用多个学期且已经征订的教材 MPJLambdaWrapper subscribedTextbook = new MPJLambdaWrapper<>(); subscribedTextbook .distinct() .select(BaseClassCourse::getId) .innerJoin(TextbookSubscription.class, TextbookSubscription::getId, TextbookSubscriptionItem::getTextbookSubscriptionId, wrapper -> wrapper .ne(TextbookSubscription::getStatus, 0) ) .innerJoin(TextbookSubscriptionItemClass.class, wrapper -> wrapper .eq(TextbookSubscriptionItemClass::getTextbookSubscriptionItemId, TextbookSubscriptionItem::getId) .in(TextbookSubscriptionItemClass::getBaseClassId, classIdList) ) .innerJoin(BaseClassAdminCourse.class, BaseClassAdminCourse::getClassId, TextbookSubscriptionItemClass::getBaseClassId) .innerJoin(BaseClassCourse.class, wrapper -> wrapper .eq(BaseClassCourse::getClassId, BaseClassAdminCourse::getId) .eq(BaseClassCourse::getTextbookId, TextbookSubscriptionItem::getTextbookId) ) .innerJoin(Textbook.class, Textbook::getId, TextbookSubscriptionItem::getTextbookId) .ne(ObjectUtils.isNotEmpty(dto.getTextbookSubscriptionId()), TextbookSubscriptionItem::getTextbookSubscriptionId, dto.getTextbookSubscriptionId()) .gt(Textbook::getUseType, 1) ; List baseClassCourses = textbookSubscriptionItemMapper.selectJoinList(BaseClassCourse.class, subscribedTextbook); List baseClassCourseIds = baseClassCourses.stream() .map(BaseClassCourse::getId) .collect(Collectors.toList()); // 需要征订的教材 MPJLambdaWrapper baseClassCourseMPJLambdaWrapper = new MPJLambdaWrapper<>(); baseClassCourseMPJLambdaWrapper .disableSubLogicDel() .distinct() .selectAs(BaseClassCourse::getCourseId, TextbookSubscriptionListVo::getCourseSubjectId) .selectAs(BaseClassCourse::getTextbookId, TextbookSubscriptionListVo::getTextbookId) .selectAs(BaseCourseSubject::getName, TextbookSubscriptionListVo::getCourseName) .select(Textbook.class, x -> VoToColumnUtil.fieldsToColumns(TextbookSubscriptionListVo.class).contains(x.getProperty())) .innerJoin(BaseClassAdminCourse.class, BaseClassAdminCourse::getId, BaseClassCourse::getClassId) .innerJoin(Textbook.class, Textbook::getId, BaseClassCourse::getTextbookId) .leftJoin(BaseCourseSubject.class, BaseCourseSubject::getId, BaseClassCourse::getCourseId) .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, Textbook::getTextbookType, wrapper -> wrapper .selectAs(DictionaryDetail::getName, TextbookSubscriptionListVo::getTextbookTypeCn) ) .eq(BaseClassAdminCourse::getBaseSemesterId, dto.getBaseSemesterId()) .in(BaseClassAdminCourse::getClassId, classIdList) .notIn(!baseClassCourseIds.isEmpty(), BaseClassCourse::getId, baseClassCourseIds) .eq(BaseClassAdminCourse::getDeleteMark, DeleteMark.NODELETE.getCode()) .eq(Textbook::getDeleteMark, DeleteMark.NODELETE.getCode()) ; List textbookSubscriptionListVoList = baseClassCourseMapper.selectJoinList(TextbookSubscriptionListVo.class, baseClassCourseMPJLambdaWrapper); List textbookIds = textbookSubscriptionListVoList.stream() .map(TextbookSubscriptionListVo::getTextbookId) .distinct() // 去重操作 .collect(Collectors.toList()); // 处理每本教材使用的人数 if(ObjectUtils.isNotEmpty(textbookIds)){ MPJLambdaWrapper countNum = new MPJLambdaWrapper<>(); countNum .selectAs(BaseClassCourse::getTextbookId, TextbookSubscriptionListVo::getTextbookId) .selectAs(BaseClassAdminCourse::getClassId, TextbookSubscriptionListVo::getClassIds) .selectCount(BaseStudentSchoolRoll::getId, TextbookSubscriptionListVo::getStudentSubscriptionNumber) .innerJoin(BaseClassAdminCourse.class, BaseClassAdminCourse::getId, BaseClassCourse::getClassId) .innerJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getClassId, BaseClassAdminCourse::getClassId) .eq(BaseStudentSchoolRoll::getArchivesStatus, ArchivesStatusEnum.FB2901.getCode()) .in(BaseClassCourse::getTextbookId, textbookIds) .eq(BaseClassAdminCourse::getBaseSemesterId, dto.getBaseSemesterId()) .in(BaseClassAdminCourse::getClassId, classIdList) .notIn(!baseClassCourseIds.isEmpty(), BaseClassCourse::getId, baseClassCourseIds) .eq(BaseClassCourse::getDeleteMark, DeleteMark.NODELETE.getCode()) .groupBy(BaseClassCourse::getTextbookId) .groupBy(BaseClassAdminCourse::getClassId) ; List countStuNumList = baseClassCourseMapper.selectJoinList(TextbookSubscriptionListVo.class, countNum); Map> countStuNumMap = countStuNumList.stream() .collect(Collectors.groupingBy(TextbookSubscriptionListVo::getTextbookId)); if(ObjectUtils.isNotEmpty(countStuNumMap)){ StringBuilder classIds; int sum; for(TextbookSubscriptionListVo t : textbookSubscriptionListVoList){ List textbookSubscriptionListVos = countStuNumMap.get(t.getTextbookId()); if(ObjectUtils.isNotEmpty(textbookSubscriptionListVos)){ sum = 0; classIds = new StringBuilder(); for (TextbookSubscriptionListVo textbookSubscriptionListVo : textbookSubscriptionListVos){ sum += ObjectUtils.isNotEmpty(textbookSubscriptionListVo.getStudentSubscriptionNumber()) ? textbookSubscriptionListVo.getStudentSubscriptionNumber() : 0; if(ObjectUtils.isNotEmpty(textbookSubscriptionListVo.getClassIds())){ classIds.append(textbookSubscriptionListVo.getClassIds()); classIds.append(","); } } if (classIds.length() > 0) { // 确保 StringBuilder 不为空,以避免 StringIndexOutOfBoundsException classIds.deleteCharAt(classIds.length() - 1); } t.setStudentSubscriptionNumber(sum); t.setUseClassNum(textbookSubscriptionListVos.size()); t.setClassIds(classIds.toString()); } } } // for(TextbookSubscriptionListVo t : textbookSubscriptionListVoList){ // MPJLambdaWrapper countNum = new MPJLambdaWrapper<>(); // countNum // .innerJoin(BaseClassCourse.class, BaseClassCourse::getClassId, BaseClassAdminCourse::getId) // // .innerJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getClassId, BaseClassAdminCourse::getClassId) // // .eq(BaseStudentSchoolRoll::getArchivesStatus, ArchivesStatusEnum.FB2901.getCode()) // .eq(BaseClassCourse::getTextbookId, t.getTextbookId()) // .eq(BaseClassAdminCourse::getBaseSemesterId, dto.getBaseSemesterId()) // .in(BaseClassAdminCourse::getClassId, dto.getClassIdList()) // .notIn(!baseClassCourseIds.isEmpty(), BaseClassCourse::getId, baseClassCourseIds) // .eq(BaseClassCourse::getDeleteMark, DeleteMark.NODELETE.getCode()) // ; // Long num = baseClassCourseMapper.selectCount(countNum); // t.setStudentSubscriptionNumber(num.intValue()); // // t.setClassIds(dto.getClassIds()); // } return textbookSubscriptionListVoList; } // private void saveData(List dataList) { // //查询所有需要的数据 // //学科组 // List subjectGroupList = subjectGroupMapper.selectList(new LambdaQueryWrapper<>()); // Map subjectGroupNameAndIdMap = subjectGroupList.stream().collect(Collectors.toMap(SubjectGroup::getGroupName, SubjectGroup::getId, (k1, k2) -> k1)); // // //使用课程 // List baseCourseSubjectList = baseCourseSubjectMapper.selectList(new LambdaQueryWrapper<>()); // Map baseCourseSubjectNameAndIdMap = baseCourseSubjectList.stream().collect(Collectors.toMap(BaseCourseSubject::getName, BaseCourseSubject::getId, (k1, k2) -> k1)); // // //批量插入或更新数据 // for (TextbookImportDto textbookImportDto : dataList) { // TextbookCoreAttribute textbookCoreAttribute = new TextbookCoreAttribute(); // BeanUtils.copyProperties(textbookImportDto, textbookCoreAttribute); // // Textbook textbook = new Textbook(); // BeanUtils.copyProperties(textbookImportDto, textbook); // // // 处理学科组映射 // String groupName = textbookImportDto.getGroupName(); // Long subjectGroupId = Optional.ofNullable(groupName) // .map(subjectGroupNameAndIdMap::get) // .orElse(null); // textbook.setSubjectGroupId(subjectGroupId); // // // 处理课程映射 // String courseName = textbookImportDto.getCourseName(); // Long courseSubjectId = Optional.ofNullable(courseName) // .map(baseCourseSubjectNameAndIdMap::get) // .orElse(null); // textbook.setCourseSubjectId(courseSubjectId); // // // 处理是否教材计划字段 // String isTextbookPlanCn = textbookImportDto.getIsTextbookPlanCn(); // String isTextbookPlan = Optional.ofNullable(isTextbookPlanCn) // .filter("是"::equals) // .map(s -> "yes") // .orElse("no"); // textbook.setIsTextbookPlan(isTextbookPlan); // // String isSecd = textbookImportDto.getIsSecd(); // Integer isSecdI = Optional.ofNullable(isSecd) // .filter("是"::equals) // .map(s -> 1) // .orElse(0); // textbook.setIsSecd(isSecdI); // // // 处理教材类型映射 // String textbookTypeCn = textbookImportDto.getTextbookTypeCn(); // String textbookTypeCode = Optional.ofNullable(textbookTypeCn) // .map(TextbookTypeEnum::getCode) // .orElse(null); // textbook.setTextbookType(textbookTypeCode); // // //处理使用类型 // textbook.setUseType(UseSemesterTypeEnum.getCode(textbookImportDto.getUseTypeCn())); // Double discount = 1d; // if(textbook.getDiscount() != null){ // discount = textbook.getDiscount(); // } // //处理小计 // textbook.setSubtotal(textbook.getPrice().multiply(BigDecimal.valueOf(discount / 10))); // // // 判断导入的教材是否已经存在,根据教材的 ISBN 码和使用的学期判断 // LambdaQueryWrapper textbookLambdaQueryWrapper = new LambdaQueryWrapper<>(); // textbookLambdaQueryWrapper // .eq(Textbook::getIssn, textbook.getIssn()) // ; // // Textbook oldTextbook = this.getOne(textbookLambdaQueryWrapper); // // Date now = new Date(); // Long loginId = StpUtil.getLoginIdAsLong(); // // 已经存在,更新数据 // if (oldTextbook != null) { // // 更新教材数据 // textbookCoreAttribute.setId(oldTextbook.getTextbookCoreAttributeId()); // textbookCoreAttribute.setModifyDate(now); // textbookCoreAttribute.setModifyUserId(loginId); // textbookCoreAttributeService.updateById(textbookCoreAttribute); // // textbook.setId(oldTextbook.getId()); // textbook.setModifyDate(now); // textbook.setModifyUserId(loginId); // textbookTextbookMapper.updateById(textbook); // } else { // textbookCoreAttribute.setCreateDate(now); // textbookCoreAttribute.setCreateUserId(loginId); // textbookCoreAttributeService.save(textbookCoreAttribute); // // // 插入教材数据 // textbook.setCreateDate(now); // textbook.setCreateUserId(loginId); // textbook.setTextbookCoreAttributeId(textbookCoreAttribute.getId()); // textbookTextbookMapper.insert(textbook); // } // } // } @Override @Transactional(rollbackFor = Exception.class) public String textbookImport(MultipartFile file) throws IOException, IllegalAccessException { List excelDataList = EasyExcel.read(file.getInputStream()).headRowNumber(3).head(TextbookImportDto.class).sheet().doReadSync(); StringBuilder sb = new StringBuilder(); List codeList = new ArrayList<>(); codeList.add("textbook_type"); codeList.add("textbook_category"); Map dictionary = ImportExcelUtil.initDictionary(codeList, dictionaryitemMapper, dictionarydetailMapper); //查询所有需要的数据 //学科组 List subjectGroupList = subjectGroupMapper.selectList( Wrappers .lambdaQuery(SubjectGroup.class) .eq(SubjectGroup::getDeleteMark, DeleteMark.NODELETE.getCode())); Map subjectGroupNameAndIdMap = subjectGroupList.stream().collect(Collectors.toMap(SubjectGroup::getGroupName, SubjectGroup::getId, (k1, k2) -> k1)); //使用课程 List baseCourseSubjectList = baseCourseSubjectMapper.selectList( Wrappers .lambdaQuery(BaseCourseSubject.class) .eq(BaseCourseSubject::getDeleteMark, DeleteMark.NODELETE.getCode()) ); Map baseCourseSubjectNameAndIdMap = baseCourseSubjectList.stream().collect(Collectors.toMap(BaseCourseSubject::getName, BaseCourseSubject::getId, (k1, k2) -> k1)); List insertTextbooks = new ArrayList<>(); List updateTextbooks = new ArrayList<>(); for (int i = 0; i < excelDataList.size(); i++) { TextbookImportDto dto = excelDataList.get(i); if (isRequiredFieldsFilled(dto, sb, i+3)) { return sb.toString(); } Textbook textbook = new Textbook(); BeanUtils.copyProperties(dto, textbook); // 验证字段的合理性 // 字典类型 if (validateAndSetDictionaryField(dto::getTextbookTypeCn, "textbook_type", "教材分类(教材、辅材、作业本)", dictionary, textbook::setTextbookType, sb, i+3)) { return sb.toString(); } if (validateAndSetDictionaryField(dto::getTextbookCategory, "textbook_category", "教材类型(纸质教材、数字教材、纸质与数字资源结合教材)", dictionary, textbook::setTextbookCategory, sb, i+3)) { return sb.toString(); } // 关联表 if (validateAndSetString2LongField(dto::getGroupName, "学科组(必须是系统中已有的学科组名称)", subjectGroupNameAndIdMap, textbook::setSubjectGroupId, sb, i+3 )) { return sb.toString(); } if (validateAndSetString2LongField(dto::getCourseName, "对应课程(必须是系统中已有的课程名称)", baseCourseSubjectNameAndIdMap, textbook::setCourseSubjectId, sb, i+3 )) { return sb.toString(); } // 处理是否字段 if(validateAndSetBooleanField(dto::getIsTextbookPlanCn, "规划教材(是或否)", textbook::setIsTextbookPlan, sb, i + 3 )){ return sb.toString(); } if(validateAndSetBooleanField(dto::getIsSecd, "是否为校企合作教材(是或否)", textbook::setIsSecd, sb, i + 3 )){ return sb.toString(); } // 处理枚举字段 Map useTypeMap = new HashMap<>(); for (UseSemesterTypeEnum useSemesterTypeEnum : UseSemesterTypeEnum.values()) { useTypeMap.put(useSemesterTypeEnum.getValue(), useSemesterTypeEnum.getCode()); } if(validateAndSetEnumField(dto::getUseTypeCn, "使用时长(一学期~六学期)", useTypeMap, textbook::setUseType, sb, i + 3 )){ return sb.toString(); } // 判断导入的教材是否已经存在,根据教材的 ISSN 码和使用的学科组和课程判断 LambdaQueryWrapper textbookLambdaQueryWrapper = new LambdaQueryWrapper<>(); textbookLambdaQueryWrapper .eq(Textbook::getIssn, textbook.getIssn()) .eq(Textbook::getDeleteMark, DeleteMark.NODELETE.getCode()) ; Textbook verifyTextbook = this.getOne(textbookLambdaQueryWrapper); textbook.setIsbn(textbook.getIssn()); BigDecimal discount = BigDecimal.valueOf(textbook.getDiscount()).divide( BigDecimal.valueOf(10)); textbook.setDiscountPrice(textbook.getPrice().multiply(discount)); if(ObjectUtils.isNotEmpty(verifyTextbook)){ textbook.setId(verifyTextbook.getId()); updateTextbooks.add(textbook); }else { insertTextbooks.add(textbook); } } //批量新增 if (!insertTextbooks.isEmpty()) { for (Textbook textbook : insertTextbooks) { this.save(textbook); } } //批量修改 if (!updateTextbooks.isEmpty()) { for (Textbook textbook : updateTextbooks) { this.updateById(textbook); } } return sb.toString(); } }