QuestionnaireMapper.xml 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="cn.iocoder.byzs.module.bjdx.dal.mysql.questionnaire.QuestionnaireMapper">
  4. <select id="selectConfigQuestionList"
  5. parameterType="cn.iocoder.byzs.module.bjdx.controller.admin.questionnaire.vo.ConfigQuestionRespVO"
  6. resultType="cn.iocoder.byzs.module.bjdx.controller.admin.questionnaire.vo.ConfigQuestionRespVO">
  7. SELECT
  8. cq.id,qc.tenant_id as tenantId,
  9. q.id as questionnaireId,
  10. cq.cq_quest_type as cqQuestType,
  11. cq.cq_question as cqQuestion,
  12. cq.cq_quest_analysis as cqQuestAnalysis,
  13. cq.cq_quest_answer_id as cqQuestAnswerId,
  14. qc.bq_order as bqOrder,
  15. cq.cq_quest_score as cqQuestScore
  16. FROM bjdx_questionnaire_config qc
  17. LEFT JOIN bjdx_questionnaire q ON q.id = qc.bqc_questionnaire_id
  18. LEFT JOIN bjdx_course_question cq ON qc.bqc_question_id = cq.id
  19. WHERE
  20. qc.deleted = 0 AND q.deleted = 0 AND cq.deleted = 0
  21. <if test="questionnaireId != null">
  22. AND q.id = #{questionnaireId}
  23. </if>
  24. <if test="cqQuestType != null">
  25. AND cq.cq_quest_type = #{cqQuestType}
  26. </if>
  27. <if test="cqQuestion != null">
  28. AND cq.cq_question LIKE CONCAT('%', #{cqQuestion}, '%')
  29. </if>
  30. order by qc.bq_order
  31. <if test="pageSize != null and pageSize != -1">
  32. limit #{pageNo}, #{pageSize}
  33. </if>
  34. </select>
  35. <select id="selectConfigQuestionCount" resultType="java.lang.Integer">
  36. SELECT count(*)
  37. FROM bjdx_questionnaire_config qc
  38. LEFT JOIN bjdx_questionnaire q ON q.id = qc.bqc_questionnaire_id
  39. LEFT JOIN bjdx_course_question cq ON qc.bqc_question_id = cq.id
  40. WHERE
  41. <if test="questionnaireId != null">
  42. q.id = #{questionnaireId}
  43. </if>
  44. <if test="cqQuestType != null">
  45. AND cq.cq_quest_type = #{cqQuestType}
  46. </if>
  47. <if test="cqQuestion != null">
  48. AND cq.cq_question LIKE CONCAT('%', #{cqQuestion}, '%')
  49. </if>
  50. order by qc.bq_order
  51. </select>
  52. <select id="selectConfigQuestionNoList"
  53. parameterType="cn.iocoder.byzs.module.bjdx.controller.admin.questionnaire.vo.ConfigQuestionRespVO"
  54. resultType="cn.iocoder.byzs.module.bjdx.controller.admin.questionnaire.vo.ConfigQuestionRespVO">
  55. SELECT
  56. cq.id,
  57. cq.cq_quest_type as cqQuestType,
  58. cq.cq_question as cqQuestion,
  59. cq.cq_quest_analysis as cqQuestAnalysis,
  60. cq.cq_quest_answer_id as cqQuestAnswerId
  61. FROM bjdx_course_question cq
  62. <!-- 左连接子查询结果,用于判断是否存在关联 -->
  63. LEFT JOIN (
  64. SELECT qc.bqc_question_id id
  65. FROM bjdx_questionnaire q
  66. LEFT JOIN bjdx_questionnaire_config qc ON qc.bqc_questionnaire_id = q.id
  67. WHERE
  68. <if test="questionnaireId != null">
  69. q.id = #{questionnaireId}
  70. </if>
  71. ) t ON cq.id = t.id
  72. WHERE
  73. <!-- 当子查询无匹配时,t.id为NULL -->
  74. t.id IS NULL
  75. <if test="cqQuestType != null">
  76. AND cq.cq_quest_type = #{cqQuestType}
  77. </if>
  78. <if test="cqQuestion != null">
  79. AND cq.cq_question LIKE CONCAT('%', #{cqQuestion}, '%')
  80. </if>
  81. </select>
  82. <insert id="addConfigQuest" parameterType="cn.iocoder.byzs.module.bjdx.controller.admin.questionnaire.vo.ConfigQuestionRespVO">
  83. INSERT INTO bjdx_questionnaire_config (bqc_questionnaire_id, bqc_question_id, bq_order, tenant_id)
  84. SELECT
  85. #{questionnaireId},
  86. q.id,
  87. @max_order := @max_order + 1,
  88. #{tenantId}
  89. FROM (
  90. <!-- 使用foreach直接生成所有ID行,无需单独处理第一个元素 -->
  91. <foreach collection="questionIds" item="id" index="index" open="" separator="UNION ALL " close="">
  92. SELECT #{questionnaireId} AS qn_id, #{id} AS id
  93. </foreach>
  94. ) AS q
  95. CROSS JOIN (SELECT @max_order := COALESCE((
  96. SELECT MAX(bq_order)
  97. FROM bjdx_questionnaire_config
  98. WHERE bqc_questionnaire_id = #{questionnaireId}
  99. ), 0)) AS init;
  100. </insert>
  101. <delete id="delConfigQuest" parameterType="cn.iocoder.byzs.module.bjdx.controller.admin.questionnaire.vo.ConfigQuestionRespVO">
  102. DELETE FROM bjdx_questionnaire_config WHERE bqc_questionnaire_id = #{questionnaireId} AND bqc_question_id IN
  103. <foreach collection="questionIds" item="id" open="(" separator="," close=")">
  104. #{id}
  105. </foreach>
  106. </delete>
  107. </mapper>