Explorar o código

/student/studentConsumeDate/excel-import 消费记录excel导入

大数据与最优化研究所 hai 4 semanas
pai
achega
f8d5ee37fc

+ 58 - 0
src/main/java/com/xjrsoft/common/enums/ConsumeTypeEnum.java

@@ -0,0 +1,58 @@
+package com.xjrsoft.common.enums;
+
+/**
+ * @author dzx
+ * @date 2024/2/3
+ */
+public enum ConsumeTypeEnum {
+    /**
+     * 超市
+     * */
+    CONSUME_TYPE_2("consume_type_2", "超市"),
+    /**
+     * 食堂
+     * */
+    CONSUME_TYPE_1("consume_type_1", "食堂"),
+    /**
+     * 其他
+     * */
+    CONSUME_TYPE_4("consume_type_4", "其他"),
+    /**
+     * 图书馆
+     * */
+    CONSUME_TYPE_3("consume_type_3", "图书馆");
+
+    final String code;
+    final String value;
+
+    public String getCode() {
+        return this.code;
+    }
+
+    public String getValue() {
+        return this.value;
+    }
+
+    ConsumeTypeEnum(final String code, final String message) {
+        this.code = code;
+        this.value = message;
+    }
+
+    public static String getCode(String value){
+        for (ConsumeTypeEnum item : values()) {
+            if (item.getValue().equals(value)) {
+                return  item.getCode();
+            }
+        }
+        return "consume_type_4";
+    }
+
+    public static String getValue(String code) {
+        for (ConsumeTypeEnum item : values()) {
+            if (item.getCode().equals(code)) {
+                return  item.getValue();
+            }
+        }
+        return "consume_type_4";
+    }
+}

+ 24 - 0
src/main/java/com/xjrsoft/config/TransactionConfig.java

@@ -0,0 +1,24 @@
+package com.xjrsoft.config;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
+
+import javax.sql.DataSource;
+
+@Configuration
+@EnableTransactionManagement
+public class TransactionConfig {
+
+    @Autowired
+    private DataSource dataSource;
+
+    @Bean
+    public PlatformTransactionManager transactionManager() {
+        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
+        return transactionManager;
+    }
+}

+ 15 - 0
src/main/java/com/xjrsoft/module/student/controller/StudentConsumeDateController.java

@@ -6,6 +6,8 @@ import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.github.yulichang.toolkit.MPJWrappers;
+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;
@@ -29,8 +31,11 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.validation.Valid;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.List;
 
 /**
@@ -106,4 +111,14 @@ public class StudentConsumeDateController {
 
     }
 
+    @PostMapping("/excel-import")
+    @ApiOperation(value = "excel消费记录导入")
+    @SaCheckPermission("coursetable:excelimport")
+    public RT<Boolean> excelImport(@RequestParam("file") MultipartFile file) throws IOException {
+        String result = studentConsumeDateService.excelImport(file);
+        if (result.length() > 0) {
+            throw new MyException(result);
+        }
+        return RT.ok("全部成功", true);
+    }
 }

+ 36 - 0
src/main/java/com/xjrsoft/module/student/dto/StudentConsumeDateExcelImportDto.java

@@ -0,0 +1,36 @@
+package com.xjrsoft.module.student.dto;
+
+import com.alibaba.excel.annotation.ExcelProperty;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.math.BigDecimal;
+import java.util.Date;
+
+@Data
+public class StudentConsumeDateExcelImportDto {
+    /**
+     * 学生学号
+     */
+    @ExcelProperty("学号")
+    @ApiModelProperty("学号")
+    private String studentUserName;
+    /**
+     * 消费时间
+     */
+    @ExcelProperty("消费时间")
+    @ApiModelProperty("消费时间")
+    private Date consumeTime;
+    /**
+     * 消费金额
+     */
+    @ExcelProperty("消费金额(元)")
+    @ApiModelProperty("消费金额")
+    private BigDecimal consumeMoney;
+    /**
+     * 消费类型
+     */
+    @ExcelProperty("消费类型")
+    @ApiModelProperty("消费类型")
+    private String consumeType;
+}

+ 6 - 0
src/main/java/com/xjrsoft/module/student/service/IStudentConsumeDateService.java

@@ -2,6 +2,10 @@ package com.xjrsoft.module.student.service;
 
 import com.github.yulichang.base.MPJBaseService;
 import com.xjrsoft.module.student.entity.StudentConsumeDate;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+import java.io.InputStream;
 
 /**
 * @title: 学生消费数据
@@ -11,4 +15,6 @@ import com.xjrsoft.module.student.entity.StudentConsumeDate;
 */
 
 public interface IStudentConsumeDateService extends MPJBaseService<StudentConsumeDate> {
+
+    String excelImport( MultipartFile file) throws IOException;
 }

+ 160 - 5
src/main/java/com/xjrsoft/module/student/service/impl/StudentConsumeDateServiceImpl.java

@@ -1,19 +1,174 @@
 package com.xjrsoft.module.student.service.impl;
 
+import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.excel.EasyExcel;
+import com.github.yulichang.base.MPJBaseService;
 import com.github.yulichang.base.MPJBaseServiceImpl;
+import com.github.yulichang.wrapper.MPJLambdaWrapper;
+import com.xjrsoft.common.enums.ConsumeTypeEnum;
+import com.xjrsoft.config.TransactionConfig;
+import com.xjrsoft.module.base.entity.BaseClass;
+import com.xjrsoft.module.base.entity.BaseGrade;
+import com.xjrsoft.module.base.entity.BaseMajorSet;
+import com.xjrsoft.module.organization.entity.UserRoleRelation;
+import com.xjrsoft.module.student.dto.StudentConsumeDateExcelImportDto;
+import com.xjrsoft.module.student.entity.BaseMajor;
+import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
 import com.xjrsoft.module.student.entity.StudentConsumeDate;
 import com.xjrsoft.module.student.mapper.StudentConsumeDateMapper;
 import com.xjrsoft.module.student.service.IStudentConsumeDateService;
+import com.xjrsoft.module.student.vo.StuConsumeImportStuInfoVo;
+import com.xjrsoft.module.teacher.entity.XjrUser;
+import com.xjrsoft.module.teacher.mapper.XjrUserMapper;
+import com.xjrsoft.module.textbook.entity.SubjectGroup;
+import com.xjrsoft.module.textbook.service.ITextbookCoreAttributeService;
 import lombok.AllArgsConstructor;
+import org.apache.commons.lang3.ObjectUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.TransactionStatus;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.support.DefaultTransactionDefinition;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.math.BigDecimal;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
-* @title: 学生消费数据
-* @Author dzx
-* @Date: 2024-08-26
-* @Version 1.0
-*/
+ * @title: 学生消费数据
+ * @Author dzx
+ * @Date: 2024-08-26
+ * @Version 1.0
+ */
 @Service
 @AllArgsConstructor
 public class StudentConsumeDateServiceImpl extends MPJBaseServiceImpl<StudentConsumeDateMapper, StudentConsumeDate> implements IStudentConsumeDateService {
+
+    private final XjrUserMapper xjrUserMapper;
+
+    @Autowired
+    private PlatformTransactionManager transactionManager;
+
+    @Override
+    public String excelImport(MultipartFile file) throws IOException {
+        List<StudentConsumeDateExcelImportDto> excelDataList = EasyExcel.read(file.getInputStream()).headRowNumber(1).head(StudentConsumeDateExcelImportDto.class).sheet().doReadSync();
+        StringBuilder sb = new StringBuilder();
+
+        // 获取所有学生id和其他信息
+        MPJLambdaWrapper<XjrUser> xjrUserMPJLambdaWrapper = new MPJLambdaWrapper<>();
+        xjrUserMPJLambdaWrapper
+                .distinct()
+                .disableSubLogicDel()
+                .selectAs(BaseStudentSchoolRoll::getClassId, StuConsumeImportStuInfoVo::getClassId)
+                .selectAs(BaseClass::getName, StuConsumeImportStuInfoVo::getClassName)
+                .selectAs(XjrUser::getId, StuConsumeImportStuInfoVo::getStudentId)
+                .selectAs(XjrUser::getName, StuConsumeImportStuInfoVo::getStudentName)
+                .selectAs(XjrUser::getCredentialNumber, StuConsumeImportStuInfoVo::getStudentUserName)
+                .selectAs(BaseGrade::getId, StuConsumeImportStuInfoVo::getGradeId)
+                .selectAs(BaseGrade::getName, StuConsumeImportStuInfoVo::getGradeName)
+                .selectAs(BaseMajor::getId, StuConsumeImportStuInfoVo::getMajorId)
+                .selectAs(BaseMajor::getName, StuConsumeImportStuInfoVo::getMajorName)
+                .innerJoin(UserRoleRelation.class, UserRoleRelation::getUserId, XjrUser::getId)
+                .leftJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, XjrUser::getId)
+                .leftJoin(BaseClass.class, BaseClass::getId, BaseStudentSchoolRoll::getClassId)
+                .leftJoin(BaseGrade.class, BaseGrade::getId, BaseClass::getGradeId)
+                .leftJoin(BaseMajorSet.class, BaseMajorSet::getId, BaseClass::getMajorSetId)
+                .leftJoin(BaseMajor.class, BaseMajor::getId, BaseMajorSet::getMajorId)
+                .eq(UserRoleRelation::getRoleId, 3)
+        ;
+
+        List<StuConsumeImportStuInfoVo> stuConsumeImportStuInfoVos = xjrUserMapper.selectJoinList(StuConsumeImportStuInfoVo.class, xjrUserMPJLambdaWrapper);
+
+        Map<String, StuConsumeImportStuInfoVo> stringStuConsumeImportStuInfoVoMap = stuConsumeImportStuInfoVos.stream()
+                .filter(x -> ObjectUtil.isNotNull(x.getStudentUserName()))
+                .collect(Collectors.toMap(data -> data.getStudentUserName(), data -> data, (e1, e2) -> e1));
+
+        int batchSize = 1000;
+        int processedCount = 0;
+        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
+        TransactionStatus status = transactionManager.getTransaction(def);
+
+        try {
+            for (StudentConsumeDateExcelImportDto dto : excelDataList) {
+                String studentUserName = dto.getStudentUserName();
+                // 处理学生信息
+                StuConsumeImportStuInfoVo stuConsumeImportStuInfoVo = stringStuConsumeImportStuInfoVoMap.get(studentUserName);
+                if (ObjectUtils.isEmpty(stuConsumeImportStuInfoVo)) {
+                    sb.append(dto.getStudentUserName())
+                            .append("学号无效,导入失败。")
+                    ;
+                    continue;
+                }
+
+                StudentConsumeDate studentConsumeDate = BeanUtil.toBean(stuConsumeImportStuInfoVo, StudentConsumeDate.class);
+                studentConsumeDate.setConsumeTime(dto.getConsumeTime());
+
+                // 处理消费数据
+//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+//
+//            try {
+//                // 解析字符串为Date对象
+//                Date date = sdf.parse(dto.getConsumeTime());
+//                System.out.println("解析后的日期:" + date);
+//            } catch (ParseException e) {
+//                sb.append(dto.getStudentUserName())
+//                        .append("学号无效,添加失败。")
+//                ;
+//                continue;
+//            }
+
+//                try {
+//                    // 解析字符串为BigDecimal对象
+//                    if (ObjectUtils.isEmpty(dto.getConsumeMoney())) {
+//                        sb.append(dto.getStudentUserName())
+//                                .append("消费金额无效,导入失败。")
+//                        ;
+//                        continue;
+//                    }
+//                    BigDecimal consumeMoney = new BigDecimal(dto.getConsumeMoney());
+//                    studentConsumeDate.setConsumeMoney(consumeMoney);
+//                } catch (NumberFormatException e) {
+//                    sb.append(dto.getStudentUserName())
+//                            .append("消费金额无效,导入失败。")
+//                    ;
+//                    continue;
+//                }
+
+                studentConsumeDate.setConsumeMoney(dto.getConsumeMoney());
+
+                if (ObjectUtil.isNotNull(dto.getConsumeType())) {
+                    String consumeType = ConsumeTypeEnum.getCode(dto.getConsumeType());
+                    studentConsumeDate.setConsumeType(consumeType);
+                }
+                this.save(studentConsumeDate);
+
+                processedCount++;
+                if (processedCount % batchSize == 0) {
+                    // 每处理1000条记录时提交一次事务
+                    transactionManager.commit(status);
+                    status = transactionManager.getTransaction(def); // 开始新事务
+                }
+            }
+
+            // 最后处理剩余不足1000条的数据
+            if (processedCount % batchSize != 0) {
+                transactionManager.commit(status);
+            }
+
+        } catch (Exception e) {
+            // 如果发生异常,则回滚事务
+            transactionManager.rollback(status);
+            throw e;
+        }
+        return sb.toString();
+    }
 }

+ 64 - 0
src/main/java/com/xjrsoft/module/student/vo/StuConsumeImportStuInfoVo.java

@@ -0,0 +1,64 @@
+package com.xjrsoft.module.student.vo;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.math.BigDecimal;
+import java.util.Date;
+
+/**
+* @title: 学生消费数据导入学上信息出参
+* @Author szs
+* @Date: 2023-12-28
+* @Version 1.0
+*/
+@Data
+public class StuConsumeImportStuInfoVo {
+
+    /**
+     * 班级主键id
+     */
+    @ApiModelProperty("班级主键id")
+    private Long classId;
+    /**
+     * 班级名称
+     */
+    @ApiModelProperty("班级名称")
+    private String className;
+    /**
+     * 学生id
+     */
+    @ApiModelProperty("学生id")
+    private Long studentId;
+    /**
+     * 学生姓名
+     */
+    @ApiModelProperty("学生姓名")
+    private String studentName;
+    /**
+     * 学生学号
+     */
+    @ApiModelProperty("学生学号")
+    private String studentUserName;
+    /**
+     * 年级名称
+     */
+    @ApiModelProperty("年级名称")
+    private String gradeName;
+    /**
+     * 年级id
+     */
+    @ApiModelProperty("年级id")
+    private String gradeId;
+    /**
+     * 专业名称
+     */
+    @ApiModelProperty("专业名称")
+    private String majorName;
+    /**
+     * 专业id
+     */
+    @ApiModelProperty("专业id")
+    private String majorId;
+
+}