Files
kfpa-cx-dash-back/src/main/resources/mybatis/mapper/DataMapper.xml

230 lines
8.7 KiB
XML

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kri.kfpa.dash.mapper.DataMapper">
<resultMap id="DivCsiDataResultMap" type="com.kri.kfpa.dash.entity.CsiDataEntity">
<result property="div" column="div"/>
<result property="sampleCount" column="sample_count"/>
<result property="csi" column="csi"/>
<result property="direct1" column="direct1"/>
<result property="direct2" column="direct2"/>
<result property="indirect1" column="indirect1"/>
<result property="indirect2" column="indirect2"/>
</resultMap>
<resultMap id="DivObjectTypeCsiRankResultMap" type="com.kri.kfpa.dash.entity.ObjectTypeCsiRankEntity">
<result property="div" column="div"/>
<result property="objectType" column="object_type"/>
<result property="csi" column="csi"/>
<result property="divRank" column="div_rank"/>
</resultMap>
<resultMap id="DivCommentResultMap" type="com.kri.kfpa.dash.entity.DivCommentEntity">
<result property="performance25DirectExperience" column="performance25_direct_experience"/>
<result property="performance25IndirectExperience" column="performance25_indirect_experience"/>
<result property="performance25Capability" column="performance25_capability"/>
<result property="plan26DirectExperience" column="plan26_direct_experience"/>
<result property="plan26IndirectExperience" column="plan26_indirect_experience"/>
<result property="plan26Capability" column="plan26_capability"/>
<result property="performance26DirectExperience" column="performance26_direct_experience"/>
<result property="performance26IndirectExperience" column="performance26_indirect_experience"/>
<result property="performance26Capability" column="performance26_capability"/>
</resultMap>
<sql id="CsiScoreExpression">
q1 * #{weights.q1} + q2 * #{weights.q2} + q4 * #{weights.q4} + q5 * #{weights.q5}
</sql>
<sql id="BaseDataSelect">
SELECT
DA.IDKEY
<foreach collection="qids" item="qid">
, TRY_CAST(MAX(CASE WHEN DV.[KEY] = #{qid.realQid} THEN DV.[VALUE] END) AS DECIMAL(18, 4)) AS ${qid.queryQid}
</foreach>
<choose>
<when test="pkey == '2601009_A'">
, MAX(CASE WHEN DV.[KEY] = 'Q14' THEN DV.[VALUE] END) AS object_type_raw
, MAX(CASE WHEN DV.[KEY] = 'Q16' THEN DV.[VALUE] END) AS div
</when>
<otherwise>
, '0' AS object_type_raw
, '0' AS div
</otherwise>
</choose>
FROM Kon.dbo.DATA DA
CROSS APPLY OPENJSON(DA.DATJSON) DJ
CROSS APPLY OPENJSON(DJ.VALUE) DV
WHERE SURVEY_TYPE != 'TEST'
AND Status = 10
AND PKEY = #{pkey}
AND dbo.FN_LAST_TIME(survey_date) &lt;= #{dt}
AND (
DV.[KEY] IN
<foreach collection="qids" item="qid" open="(" separator="," close=")">
#{qid.realQid}
</foreach>
<if test="pkey == '2601009_A'">
OR DV.[KEY] IN ('Q14', 'Q16')
</if>
)
<if test="pkey == '2601009_A'">
AND JSON_VALUE(DATJSON, '$.Q16.Q16') != '99'
</if>
GROUP BY DA.IDKEY
</sql>
<select id="selectDivCsiData" resultMap="DivCsiDataResultMap">
WITH Base AS (
<include refid="BaseDataSelect"/>
),
Filtered AS (
SELECT
div
, q1
, q2
, q4
, q5
FROM Base
WHERE object_type_raw IS NOT NULL
AND div IS NOT NULL
AND object_type_raw != '99'
AND div != '99'
)
SELECT
div
, AVG(<include refid="CsiScoreExpression"/>) AS csi
, COUNT(1) AS sample_count
, AVG(q1) AS direct1
, AVG(q2) AS direct2
, AVG(q4) AS indirect1
, AVG(q5) AS indirect2
, 1 AS sort_order
FROM Filtered
GROUP BY div
UNION ALL
SELECT
'-1' AS div
, AVG(<include refid="CsiScoreExpression"/>) AS csi
, COUNT(1) AS sample_count
, AVG(q1) AS direct1
, AVG(q2) AS direct2
, AVG(q4) AS indirect1
, AVG(q5) AS indirect2
, 0 AS sort_order
FROM Filtered
ORDER BY sort_order, div
</select>
<select id="selectDivObjectTypeCsiRank" resultMap="DivObjectTypeCsiRankResultMap">
WITH Base AS (
<include refid="BaseDataSelect"/>
),
Recode AS (
SELECT
IDKEY
, q1
, q2
, q4
, q5
, div
, object_type_raw
, CASE
WHEN object_type_raw IN ('4', '12') THEN object_type_raw
ELSE '90'
END AS object_type
FROM Base
),
CsiAgg AS (
SELECT
div
, object_type
, AVG(<include refid="CsiScoreExpression"/>) AS csi
FROM Recode
WHERE div IS NOT NULL
AND div != '99'
AND object_type_raw IS NOT NULL
AND object_type_raw != '99'
GROUP BY div, object_type
),
TotalCsiAgg AS (
SELECT
div
, AVG(<include refid="CsiScoreExpression"/>) AS csi
FROM Recode
WHERE div IS NOT NULL
AND div != '99'
AND object_type_raw IS NOT NULL
AND object_type_raw != '99'
GROUP BY div
),
Ranked AS (
SELECT
div
, object_type
, csi
, DENSE_RANK() OVER (
PARTITION BY object_type
ORDER BY csi DESC
) AS div_rank
FROM CsiAgg
UNION ALL
SELECT
div
, '-1' AS object_type
, csi
, DENSE_RANK() OVER (
ORDER BY csi DESC
) AS div_rank
FROM TotalCsiAgg
)
SELECT
div
, object_type
, csi
, div_rank
FROM Ranked
ORDER BY div, object_type, div_rank
</select>
<select id="selectDivComment" resultMap="DivCommentResultMap">
SELECT
ISNULL(JSON_VALUE(DATJSON, '$.Q1.Q1_1_1'), '') AS performance25_direct_experience
, ISNULL(JSON_VALUE(DATJSON, '$.Q1.Q1_2_1'), '') AS performance25_indirect_experience
, ISNULL(JSON_VALUE(DATJSON, '$.Q1.Q1_3_1'), '') AS performance25_capability
, ISNULL(JSON_VALUE(DATJSON, '$.Q1.Q1_1_2'), '') AS plan26_direct_experience
, ISNULL(JSON_VALUE(DATJSON, '$.Q1.Q1_2_2'), '') AS plan26_indirect_experience
, ISNULL(JSON_VALUE(DATJSON, '$.Q1.Q1_3_2'), '') AS plan26_capability
, ISNULL(JSON_VALUE(DATJSON, '$.Q1.Q1_1_3'), '') AS performance26_direct_experience
, ISNULL(JSON_VALUE(DATJSON, '$.Q1.Q1_2_3'), '') AS performance26_indirect_experience
, ISNULL(JSON_VALUE(DATJSON, '$.Q1.Q1_3_3'), '') AS performance26_capability
FROM Kon.dbo.DATA
WHERE PKEY = #{pkey}
AND IDKEY = #{idkey}
</select>
<update id="updateDivComment">
UPDATE A
SET DATJSON = JSON_MODIFY(
DATJSON,
'$.Q1',
JSON_QUERY((
SELECT
#{commentValues.performance25.directExperience} AS [Q1_1_1]
, #{commentValues.performance25.indirectExperience} AS [Q1_2_1]
, #{commentValues.performance25.capability} AS [Q1_3_1]
, #{commentValues.plan26.directExperience} AS [Q1_1_2]
, #{commentValues.plan26.indirectExperience} AS [Q1_2_2]
, #{commentValues.plan26.capability} AS [Q1_3_2]
, #{commentValues.performance26.directExperience} AS [Q1_1_3]
, #{commentValues.performance26.indirectExperience} AS [Q1_2_3]
, #{commentValues.performance26.capability} AS [Q1_3_3]
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
))
),
survey_date = JSON_MODIFY(survey_date, '$.Q1', CONVERT(varchar(19), GETDATE(), 120))
FROM Kon.dbo.DATA A
WHERE PKEY = #{pkey}
AND IDKEY = #{idkey}
</update>
</mapper>