java生成sql文件

简介: java生成sql文件


java生成sql文件


场景

用于清理数据库数据,生成sql语句脚本,方便DBA执行

    @RequestMapping("/cleanQuestion")
    @ResponseBody
    public AjaxResult cleanQuestion(@ModelAttribute("common") Common common, HttpServletRequest request)
    {
        //sign校验
        AjaxResult ajaxResult = new AjaxResult();
        try{
            //获取请求参数
            String  detailPlanId = request.getParameter("detailPlanId");
            long start = System.currentTimeMillis();
            BufferedWriter out = null;
            Map objMap = new HashMap(8);
            try {
                //路径
                String filePath = "D:\\100\\export\\"+DateUtils.getFormatDate(DateUtils.YYYY_MM_DD,new Date())+"\\";
                File pathFile = new File(filePath);
                if (!pathFile.exists()) {
                    pathFile.mkdirs();
                }
                String relFilePath = filePath + "计划id-" + detailPlanId + ".sql";
                File file = new File(relFilePath);
                if (!file.exists()) {
                    file.createNewFile();
                }
                out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
                objMap.put("file",relFilePath);
                DetailPlanQuestion detailPlanQuestion = detailPlanQuestionService.selectDetailPlanQuestionByDetailPlanId(Long.parseLong(detailPlanId));
                StringBuffer detailQuesSql = new StringBuffer();
                //删除后台题信息
                detailQuesSql.append("UPDATE qs_study_detail_plan_question SET is_valid=0,update_by='手动',update_time=NOW() WHERE id=");
                detailQuesSql.append(detailPlanQuestion.getId());
                detailQuesSql.append(";");
                detailQuesSql.append("\r\n");
                //获取到当前题所在课表题信息
                UserDetailPlanQuestion udpq = new UserDetailPlanQuestion();
                udpq.setDetailPlanQuestionId(detailPlanQuestion.getId());
                List<UserDetailPlanQuestion> planQuestions = userDetailPlanQuestionService.selectUserDetailPlanQuestionList(udpq);
                if (CollectionUtils.isNotEmpty(planQuestions)) {
                    StringBuffer quesSql = new StringBuffer();
                    StringBuffer planSql = new StringBuffer();
                    StringBuffer planCompSql = new StringBuffer();
                    StringBuffer kpUpSql = new StringBuffer();
                    StringBuffer kpInSql = new StringBuffer();
                    for (UserDetailPlanQuestion planQuestion:planQuestions) {
                        Long userExtendId = planQuestion.getUserExtendId();
                        //生成删除题信息sql UPDATE qs_study_user_detail_plan_question3 SET is_valid=0 WHERE id=56321;
                        quesSql.append("UPDATE qs_study_user_detail_plan_question");
                        quesSql.append(userExtendId%32);
                        quesSql.append(" SET is_valid=0,update_by='手动',update_time=NOW() WHERE id=");
                        quesSql.append(planQuestion.getId());
                        quesSql.append(";");
                        quesSql.append("\r\n");
                        if (Constants.EXECUTE_STATUS.NOT_LEARN.getValue().equals(planQuestion.getStatus()) ||
                                Constants.EXECUTE_STATUS.LEARNING.getValue().equals(planQuestion.getStatus())) {
                            //未学习 学习中 获取课信息
                            List<UserDetailPlanListen> planListens = userDetailPlanListenService.selectUserDetailPlanListenByExtendId(userExtendId, planQuestion.getUserDetailPlanId());
                            if (CollectionUtils.isNotEmpty(planListens)) {
                                //没有题了但是有课,根据课的状态决定
                                UserDetailPlanListen planListen = planListens.get(0);
                                if (Constants.EXECUTE_STATUS.FINISHED.getValue().equals(planListen.getStatus())) {
                                    //当前课已经学完了,需要更新计划为已完成
                                    UserDetailPlan userDetailPlan = userDetailPlanService.selectUserDetailPlanById(userExtendId, planQuestion.getUserDetailPlanId());
                                    //获取计划信息
                                    if (userDetailPlan != null) {
                                        planSql.append("UPDATE qs_study_user_detail_plan");
                                        planSql.append(userExtendId%32);
                                        planSql.append(" SET status=3,update_by='手动',update_time=NOW(),finish_date='");
                                        planSql.append(DateUtils.getFormatDate(null,planListen.getFinishDate()));
                                        planSql.append("' WHERE id=");
                                        planSql.append(planQuestion.getUserDetailPlanId());
                                        planSql.append(";");
                                        planSql.append("\r\n");
                                        //插入阶段完成计划
                                        UserDaySchedule daySchedule = userDayScheduleService.selectUserDayScheduleById(userExtendId, userDetailPlan.getUserDayScheduleId());
                                        UserPath userPath = userPathService.selectUserPathById(daySchedule.getUserPathId());
                                        //INSERT INTO qs_study_user_stage_plan_complete (`user_extend_id`, `stage_id`, `subject_id`, `plan_extend_id`, `long_short_path_id`, `plan_id`, `complete_method`, `is_valid`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`, `paper_id`) VALUES ('33', '11', '1', '13', '17', '1719', '1', '1', '', '2020-08-26 15:13:06', '', NULL, '', NULL);
                                        planCompSql.append("INSERT INTO qs_study_user_stage_plan_complete (`user_extend_id`, `stage_id`, `subject_id`, `plan_extend_id`, `long_short_path_id`, `plan_id`, `complete_method`, `is_valid`, `create_by`, `create_time`) VALUES (");
                                        planCompSql.append(userExtendId);
                                        planCompSql.append(",");
                                        planCompSql.append(userPath.getStageId());
                                        planCompSql.append(",");
                                        planCompSql.append(userPath.getSubjectId());
                                        planCompSql.append(",");
                                        planCompSql.append(userPath.getPlanExtendId());
                                        planCompSql.append(",");
                                        planCompSql.append(userPath.getLongShortPathId());
                                        planCompSql.append(",");
                                        planCompSql.append(userDetailPlan.getDetailPlanId());
                                        planCompSql.append(",2,1,'手动',NOW());");
                                        planCompSql.append("\r\n");
                                        List<Long> kpIdList = detailPlanKpService.getKpIdListByPlanId(userDetailPlan.getDetailPlanId());
                                        if(CollectionUtils.isNotEmpty(kpIdList)){
                                            for (Long aLong : kpIdList) {
                                                UserStageKpComplete uskc = new UserStageKpComplete();
                                                uskc.setUserExtendId(userExtendId);
                                                uskc.setStageId(userPath.getStageId());
                                                uskc.setSubjectId(userPath.getSubjectId());
                                                uskc.setKpId(aLong);
                                                List<UserStageKpComplete> uskcList = userStageKpCompleteService.selectUserStageKpCompleteList(uskc);
                                                if(CollectionUtils.isNotEmpty(uskcList)){
                                                    uskc = uskcList.get(0);
                                                    //更新阶段完成知识点
                                                    kpUpSql.append("UPDATE qs_study_user_stage_kp_complete SET complete_method=2,update_by='手动',update_time=NOW() WHERE id=");
                                                    kpUpSql.append(uskc.getId());
                                                    kpUpSql.append(";");
                                                    kpUpSql.append("\r\n");
                                                }else {
                                                    //插入阶段完成知识点
                                                    kpInSql.append("INSERT INTO qs_study_user_stage_kp_complete (`user_extend_id`, `stage_id`, `subject_id`, `kp_id`, `complete_method`, `is_valid`, `create_by`, `create_time`) VALUES (");
                                                    kpInSql.append(userExtendId);
                                                    kpInSql.append(",");
                                                    kpInSql.append(uskc.getStageId());
                                                    kpInSql.append(",");
                                                    kpInSql.append(uskc.getSubjectId());
                                                    kpInSql.append(",");
                                                    kpInSql.append(aLong);
                                                    kpInSql.append(",2,1,'手动',NOW());");
                                                    kpInSql.append("\r\n");
                                                }
                                            }
                                        }
                                    }
                                }else if (Constants.EXECUTE_STATUS.SKIPPED.getValue().equals(planListen.getStatus())) {
                                    UserDetailPlan userDetailPlan = userDetailPlanService.selectUserDetailPlanById(userExtendId, planQuestion.getUserDetailPlanId());
                                    if (userDetailPlan != null) {
                                        planSql.append("UPDATE qs_study_user_detail_plan");
                                        planSql.append(userExtendId%32);
                                        planSql.append(" SET status=4,update_by='手动',update_time=NOW(),finish_date='");
                                        planSql.append(DateUtils.getFormatDate(null,planListen.getFinishDate()));
                                        planSql.append("' WHERE id=");
                                        planSql.append(planQuestion.getUserDetailPlanId());
                                        planSql.append(";");
                                        planSql.append("\r\n");
                                        //插入阶段完成计划
                                        UserDaySchedule daySchedule = userDayScheduleService.selectUserDayScheduleById(userExtendId, userDetailPlan.getUserDayScheduleId());
                                        UserPath userPath = userPathService.selectUserPathById(daySchedule.getUserPathId());
                                        //INSERT INTO qs_study_user_stage_plan_complete (`user_extend_id`, `stage_id`, `subject_id`, `plan_extend_id`, `long_short_path_id`, `plan_id`, `complete_method`, `is_valid`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`, `paper_id`) VALUES ('33', '11', '1', '13', '17', '1719', '1', '1', '', '2020-08-26 15:13:06', '', NULL, '', NULL);
                                        planCompSql.append("INSERT INTO qs_study_user_stage_plan_complete (`user_extend_id`, `stage_id`, `subject_id`, `plan_extend_id`, `long_short_path_id`, `plan_id`, `complete_method`, `is_valid`, `create_by`, `create_time`) VALUES (");
                                        planCompSql.append(userExtendId);
                                        planCompSql.append(",");
                                        planCompSql.append(userPath.getStageId());
                                        planCompSql.append(",");
                                        planCompSql.append(userPath.getSubjectId());
                                        planCompSql.append(",");
                                        planCompSql.append(userPath.getPlanExtendId());
                                        planCompSql.append(",");
                                        planCompSql.append(userPath.getLongShortPathId());
                                        planCompSql.append(",");
                                        planCompSql.append(userDetailPlan.getDetailPlanId());
                                        planCompSql.append(",2,1,'手动',NOW());");
                                        planCompSql.append("\r\n");
                                        List<Long> kpIdList = detailPlanKpService.getKpIdListByPlanId(userDetailPlan.getDetailPlanId());
                                        if(CollectionUtils.isNotEmpty(kpIdList)){
                                            for (Long aLong : kpIdList) {
                                                UserStageKpComplete uskc = new UserStageKpComplete();
                                                uskc.setUserExtendId(userExtendId);
                                                uskc.setStageId(userPath.getStageId());
                                                uskc.setSubjectId(userPath.getSubjectId());
                                                uskc.setKpId(aLong);
                                                List<UserStageKpComplete> uskcList = userStageKpCompleteService.selectUserStageKpCompleteList(uskc);
                                                if(CollectionUtils.isNotEmpty(uskcList)){
                                                    uskc = uskcList.get(0);
                                                    //更新阶段完成知识点
                                                    kpUpSql.append("UPDATE qs_study_user_stage_kp_complete SET complete_method=2,update_by='手动',update_time=NOW() WHERE id=");
                                                    kpUpSql.append(uskc.getId());
                                                    kpUpSql.append(";");
                                                    kpUpSql.append("\r\n");
                                                }else {
                                                    //插入阶段完成知识点
                                                    kpInSql.append("INSERT INTO qs_study_user_stage_kp_complete (`user_extend_id`, `stage_id`, `subject_id`, `kp_id`, `complete_method`, `is_valid`, `create_by`, `create_time`) VALUES (");
                                                    kpInSql.append(userExtendId);
                                                    kpInSql.append(",");
                                                    kpInSql.append(uskc.getStageId());
                                                    kpInSql.append(",");
                                                    kpInSql.append(uskc.getSubjectId());
                                                    kpInSql.append(",");
                                                    kpInSql.append(aLong);
                                                    kpInSql.append(",2,1,'手动',NOW());");
                                                    kpInSql.append("\r\n");
                                                }
                                            }
                                        }
                                    }
                                }else {
                                }
                            }else {
                                //没有题了也没有课则删除当前计划
                                planSql.append("UPDATE qs_study_user_detail_plan");
                                planSql.append(userExtendId%32);
                                planSql.append(" SET is_valid=0,update_by='手动',update_time=NOW() WHERE id=");
                                planSql.append(planQuestion.getUserDetailPlanId());
                                planSql.append(";");
                                planSql.append("\r\n");
                            }
                        }
                    }
                    out.write(detailQuesSql.toString());
                    out.newLine();
                    out.write(quesSql.toString());
                    out.newLine();
                    out.write(planSql.toString());
                    out.newLine();
                    out.write(planCompSql.toString());
                    out.newLine();
                    out.write(kpUpSql.toString());
                    out.newLine();
                    out.write(kpInSql.toString());
                }
            }catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (out != null) {
                    try {
                        out.flush();
                        out.close();
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            long end = System.currentTimeMillis();
            objMap.put("time","耗时:"+(end-start)/1000+"s");
            ajaxResult.put("code", Constants.CODE.SUCCESS.getValue());
            ajaxResult.put("msg",Constants.CODE.SUCCESS.getDescription());
            ajaxResult.put("obj",objMap);
            return ajaxResult;
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code", Constants.CODE.SYSTEM_ERROE.getValue());
            ajaxResult.put("msg", Constants.CODE.SYSTEM_ERROE.getDescription());
            ajaxResult.put("obj", "");
            return ajaxResult;
        }
    }

注:此处仅做日常记录方便后续用到时参考,不可转载


相关文章
|
1天前
|
存储 缓存 安全
在 Java 编程中,创建临时文件用于存储临时数据或进行临时操作非常常见
在 Java 编程中,创建临时文件用于存储临时数据或进行临时操作非常常见。本文介绍了使用 `File.createTempFile` 方法和自定义创建临时文件的两种方式,详细探讨了它们的使用场景和注意事项,包括数据缓存、文件上传下载和日志记录等。强调了清理临时文件、确保文件名唯一性和合理设置文件权限的重要性。
8 2
|
10天前
|
存储 安全 Java
如何保证 Java 类文件的安全性?
Java类文件的安全性可以通过多种方式保障,如使用数字签名验证类文件的完整性和来源,利用安全管理器和安全策略限制类文件的权限,以及通过加密技术保护类文件在传输过程中的安全。
|
12天前
|
存储 Java API
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
23 4
|
14天前
|
Java 数据格式 索引
使用 Java 字节码工具检查类文件完整性的原理是什么
Java字节码工具通过解析和分析类文件的字节码,检查其结构和内容是否符合Java虚拟机规范,确保类文件的完整性和合法性,防止恶意代码或损坏的类文件影响程序运行。
|
14天前
|
Java API Maven
如何使用 Java 字节码工具检查类文件的完整性
本文介绍如何利用Java字节码工具来检测类文件的完整性和有效性,确保类文件未被篡改或损坏,适用于开发和维护阶段的代码质量控制。
|
24天前
|
Java
Java开发如何实现文件的移动,但是在移动结束后才进行读取?
【10月更文挑战第13天】Java开发如何实现文件的移动,但是在移动结束后才进行读取?
45 2
|
24天前
|
Java Apache Maven
Java将word文档转换成pdf文件的方法?
【10月更文挑战第13天】Java将word文档转换成pdf文件的方法?
113 1
|
24天前
|
监控 Java
Java定时扫码一个文件夹下的文件,如何保证文件写入完成后才进行处理?
【10月更文挑战第13天】Java定时扫码一个文件夹下的文件,如何保证文件写入完成后才进行处理?
73 1
|
28天前
|
小程序 Oracle Java
JVM知识体系学习一:JVM了解基础、java编译后class文件的类结构详解,class分析工具 javap 和 jclasslib 的使用
这篇文章是关于JVM基础知识的介绍,包括JVM的跨平台和跨语言特性、Class文件格式的详细解析,以及如何使用javap和jclasslib工具来分析Class文件。
35 0
JVM知识体系学习一:JVM了解基础、java编译后class文件的类结构详解,class分析工具 javap 和 jclasslib 的使用
|
16天前
|
缓存 Java 程序员
Java|SpringBoot 项目开发时,让 FreeMarker 文件编辑后自动更新
在开发过程中,FreeMarker 文件编辑后,每次都需要重启应用才能看到效果,效率非常低下。通过一些配置后,可以让它们免重启自动更新。
22 0