From e39d8e5ba5c48b7a978062de796b767037aa9cf9 Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期二, 20 十月 2026 16:00:33 +0800
Subject: [PATCH] 【丽水】service_subtask 新增 '医生处理意见' doctor_opinion

---
 smartor/src/main/resources/mapper/smartor/ServiceSubtaskMapper.xml |  178 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 165 insertions(+), 13 deletions(-)

diff --git a/smartor/src/main/resources/mapper/smartor/ServiceSubtaskMapper.xml b/smartor/src/main/resources/mapper/smartor/ServiceSubtaskMapper.xml
index 3ff5f5a..1948988 100644
--- a/smartor/src/main/resources/mapper/smartor/ServiceSubtaskMapper.xml
+++ b/smartor/src/main/resources/mapper/smartor/ServiceSubtaskMapper.xml
@@ -23,6 +23,7 @@
         <result property="senduuid" column="senduuid"/>
         <result property="result" column="result"/>
         <result property="finishtime" column="finishtime"/>
+        <result property="finishState" column="finish_state"/>
         <result property="recordid" column="recordid"/>
         <result property="exrecallcount" column="exrecallcount"/>
         <result property="pulltime" column="pulltime"/>
@@ -72,6 +73,7 @@
         <result property="leavehospitaldistrictname" column="leavehospitaldistrictname"/>
         <result property="excep" column="excep"/>
         <result property="remark" column="remark"/>
+        <result property="doctorOpinion" column="doctor_opinion"/>
         <result property="suggest" column="suggest"/>
         <result property="sendstate" column="sendstate"/>
         <result property="inhospid" column="inhospid"/>
@@ -182,6 +184,7 @@
                long_send_time,
                openid,
                remark,
+               doctor_opinion,
                service_type,
                libtemplateid,
                libtemplatename,
@@ -202,6 +205,7 @@
                senduuid,
                result,
                finishtime,
+               finish_state,
                recordid,
                exrecallcount,
                pulltime,
@@ -251,6 +255,53 @@
         from service_subtask
     </sql>
 
+    <!-- 瀹屾垚鏃剁姸鎬�(finish_state)璁$畻锛氫粎褰撴湰娆℃洿鏂版妸 sendstate 缃负 6锛堥殢璁垮畬鎴愶級鏃朵娇鐢ㄣ��
+         鍩哄噯鎴鏃堕棿 base = endtime + send_day(缂虹渷1) + (危 service_subtask_preachform.compensate_time + 1) 澶╋紝
+         涓� RyTask/RedisMqReceiver 鐨� isSubtaskDeadline 鍙e緞淇濇寔涓�鑷达紱
+         delay = COALESCE(service_task.delay_finish_days, 0)锛�
+         finish_time = COALESCE(鍏ュ弬 finishtime, 搴撲腑 finishtime, NOW())锛�
+           finish_time <= base                                    -> 1 姝e父瀹屾垚
+           delay > 0 涓� finish_time <= base + delay               -> 2 寤舵湡瀹屾垚
+           鍏朵綑锛堝惈 delay 涓� 0 鎴� NULL銆佹垨宸茶秴鍑哄欢鏈熺獥鍙o級          -> 3 瓒呮椂瀹屾垚
+         finishtime 涓虹┖鏃剁敤 NOW() 鍏滃簳锛屼繚璇佸畬鎴愭椂涓�瀹氬啓鍏ョ姸鎬侊紱
+         浠呭綋 endtime 涓虹┖瀵艰嚧鏃犳硶璁$畻鏃舵墠淇濇寔鍘熷�间笉瑕嗙洊銆� -->
+    <sql id="finishStateExpr">
+        CASE
+        WHEN service_subtask.endtime IS NULL THEN finish_state
+        WHEN COALESCE(#{finishtime,jdbcType=TIMESTAMP}, service_subtask.finishtime, NOW())
+        &lt;= DATE_ADD(
+        DATE_ADD(
+        service_subtask.endtime,
+        INTERVAL COALESCE((SELECT st.send_day FROM service_task st
+        WHERE st.taskid = service_subtask.taskid AND st.del_flag = '0' LIMIT 1), 1) DAY
+        ),
+        INTERVAL (COALESCE((SELECT SUM(CAST(COALESCE(NULLIF(TRIM(pf.compensate_time), ''), '0') AS SIGNED))
+        FROM service_subtask_preachform pf
+        WHERE pf.subid = service_subtask.id AND pf.del_flag = 0), 0) + 1) DAY
+        )
+        THEN 1
+        WHEN COALESCE((SELECT st.delay_finish_days FROM service_task st
+        WHERE st.taskid = service_subtask.taskid AND st.del_flag = '0' LIMIT 1), 0) &gt; 0
+        AND COALESCE(#{finishtime,jdbcType=TIMESTAMP}, service_subtask.finishtime, NOW())
+        &lt;= DATE_ADD(
+        DATE_ADD(
+        DATE_ADD(
+        service_subtask.endtime,
+        INTERVAL COALESCE((SELECT st.send_day FROM service_task st
+        WHERE st.taskid = service_subtask.taskid AND st.del_flag = '0' LIMIT 1), 1) DAY
+        ),
+        INTERVAL (COALESCE((SELECT SUM(CAST(COALESCE(NULLIF(TRIM(pf.compensate_time), ''), '0') AS SIGNED))
+        FROM service_subtask_preachform pf
+        WHERE pf.subid = service_subtask.id AND pf.del_flag = 0), 0) + 1) DAY
+        ),
+        INTERVAL COALESCE((SELECT st.delay_finish_days FROM service_task st
+        WHERE st.taskid = service_subtask.taskid AND st.del_flag = '0' LIMIT 1), 0) DAY
+        )
+        THEN 2
+        ELSE 3
+        END
+    </sql>
+
     <select id="selectServiceSubtaskBySendstate" parameterType="com.smartor.domain.entity.ServiceSubtaskEntity"
             resultMap="ServiceSubtaskResult">
         <include refid="selectServiceSubtaskVo"/>
@@ -291,6 +342,36 @@
         <if test="continueTimeNext != null ">and continue_time_next = #{continueTimeNext}</if>
     </select>
 
+    <!-- 鍗冲皢瓒呮椂杩囨护锛氭槸鍚﹀嵆灏嗚秴鏃�(soonTimeout=1) + 鍗冲皢瓒呮椂鏃堕棿(soonTimeoutHours锛屽崟浣嶅皬鏃�)銆�
+         鎴鏃堕棿鍙e緞涓� RyTask/RedisMqReceiver 鐨� isSubtaskDeadline 淇濇寔涓�鑷达細
+         service_subtask.endtime + service_task.send_day(缂虹渷1) + (危 service_subtask_preachform.compensate_time + 1) 澶╋紱
+         浠呭綋瀛樺湪鏈夋晥琛ュ伩璁板綍鏃舵墠璁$畻鎴鏃堕棿锛堟棤琛ュ伩璁板綍涓嶅弬涓庡嵆灏嗚秴鏃讹級锛�
+         浠呯粺璁♀�滃皻鏈秴鏃躲�佷笖鍦� N 灏忔椂鍐呭皢瑕佽秴鏃垛�濈殑鏁版嵁銆� -->
+    <sql id="soonTimeoutDeadlineExpr">
+        (
+        SELECT DATE_ADD(
+        DATE_ADD(ss.endtime, INTERVAL COALESCE(st.send_day, 1) DAY),
+        INTERVAL (COALESCE(pf.compensate_days, 0) + 1) DAY
+        )
+        FROM service_subtask ss
+        LEFT JOIN service_task st ON st.taskid = ss.taskid AND st.del_flag = '0'
+        INNER JOIN (
+        SELECT subid,
+        SUM(CAST(COALESCE(NULLIF(TRIM(compensate_time), ''), '0') AS SIGNED)) AS compensate_days
+        FROM service_subtask_preachform
+        WHERE del_flag = 0
+        GROUP BY subid
+        ) pf ON pf.subid = ss.id
+        WHERE ss.id = service_subtask.id
+        )
+    </sql>
+
+    <sql id="soonTimeoutFilter">
+        <if test="soonTimeout != null and soonTimeout == 1">
+            AND <include refid="soonTimeoutDeadlineExpr"/> &gt; NOW()
+            AND <include refid="soonTimeoutDeadlineExpr"/> &lt;= DATE_ADD(NOW(), INTERVAL COALESCE(#{soonTimeoutHours}, 0) HOUR)
+        </if>
+    </sql>
 
     <select id="selectServiceSubtaskList" parameterType="com.smartor.domain.entity.ServiceSubtaskEntity"
             resultMap="ServiceSubtaskResult">
@@ -515,6 +596,7 @@
             AND visit_count > 1
         </if>
         <if test="remark != null">and remark = #{remark}</if>
+        <if test="doctorOpinion != null and doctorOpinion != ''">and doctor_opinion like concat('%', #{doctorOpinion}, '%')</if>
         <if test="suggest != null">and suggest = #{suggest}</if>
         <if test="createBy != null">and create_by = #{createBy}</if>
         <if test="taskGuid != null">and task_guid = #{taskGuid}</if>
@@ -529,6 +611,7 @@
                 #{visitDeptCode}
             </foreach>
         </if>
+        <include refid="soonTimeoutFilter"/>
         <if test="sort != null  and sort==0">order by endtime asc</if>
         <if test="sort != null  and sort==1">order by endtime desc</if>
         <if test="sort != null  and sort==2">order by visit_time asc</if>
@@ -538,7 +621,9 @@
         <if test="sort != null  and sort==7">order by visit_time asc</if>
         <if test="sort != null  and sort==8">order by visit_time desc</if>
         <if test="sort != null  and sort==9">order by sendname asc</if>
-        <if test="pageSize != null  and pageNum != null">limit ${pageSize} OFFSET ${pageNum}</if>
+        <if test="pageSize != null  and pageNum != null">
+            LIMIT ${pageSize} OFFSET ${pageNum}
+        </if>
     </select>
 
     <select id="selectSatisfactionSubtaskList" parameterType="com.smartor.domain.VO.PatSatisfactionReqVO"
@@ -696,6 +781,7 @@
         <if test="continueTimeNow != null ">and continue_time_now = #{continueTimeNow,jdbcType=TIMESTAMP}</if>
         <if test="continueCount != null ">and continue_count = #{continueCount}</if>
         <if test="continueTimeNext != null ">and continue_time_next = #{continueTimeNext}</if>
+        <include refid="soonTimeoutFilter"/>
     </select>
 
     <select id="selectSendstateByCondition" parameterType="com.smartor.domain.ServiceSubtask" resultType="Integer">
@@ -916,6 +1002,7 @@
             <if test="senduuid != null">senduuid,</if>
             <if test="result != null">result,</if>
             <if test="finishtime != null">finishtime,</if>
+            <if test="finishState != null">finish_state,</if>
             <if test="recordid != null">recordid,</if>
             <if test="exrecallcount != null">exrecallcount,</if>
             <if test="pulltime != null">pulltime,</if>
@@ -969,6 +1056,7 @@
             <if test="nurseName != null ">nurse_name,</if>
             <if test="nurseId != null ">nurse_id,</if>
             <if test="remark != null ">remark,</if>
+            <if test="doctorOpinion != null ">doctor_opinion,</if>
             <if test="suggest != null ">suggest,</if>
             <if test="sendstate != null ">sendstate,</if>
             <if test="inhospid != null ">inhospid,</if>
@@ -1014,6 +1102,7 @@
             <if test="senduuid != null">#{senduuid},</if>
             <if test="result != null">#{result},</if>
             <if test="finishtime != null">#{finishtime},</if>
+            <if test="finishState != null">#{finishState},</if>
             <if test="recordid != null">#{recordid},</if>
             <if test="exrecallcount != null">#{exrecallcount},</if>
             <if test="pulltime != null">#{pulltime},</if>
@@ -1067,6 +1156,7 @@
             <if test="nurseName != null ">#{nurseName},</if>
             <if test="nurseId != null ">#{nurseId},</if>
             <if test="remark != null ">#{remark},</if>
+            <if test="doctorOpinion != null ">#{doctorOpinion},</if>
             <if test="suggest != null ">#{suggest},</if>
             <if test="sendstate != null ">#{sendstate},</if>
             <if test="inhospid != null ">#{inhospid},</if>
@@ -1097,6 +1187,15 @@
             <if test="isManuallyCalled != null ">#{isManuallyCalled},</if>
         </trim>
     </insert>
+
+    <!-- 浜屾闅忚骞傜瓑锛氫粎褰� is_visit_again 涓嶆槸鐩爣鍊兼椂鏇存柊锛岃繑鍥炲彈褰卞搷琛屾暟 -->
+    <update id="updateIsVisitAgainById">
+        update service_subtask
+        set is_visit_again = #{isVisitAgain},
+            update_time = now()
+        where id = #{id}
+          and (is_visit_again is null or is_visit_again &lt;&gt; #{isVisitAgain})
+    </update>
 
     <update id="updateServiceSubtask" parameterType="com.smartor.domain.ServiceSubtask">
         update service_subtask
@@ -1154,6 +1253,7 @@
             <if test="openid != null ">openid = #{openid},</if>
             <if test="longSendTime != null ">long_send_time = #{longSendTime},</if>
             <if test="sendstate != null ">sendstate = #{sendstate},</if>
+            <if test="sendstate != null and sendstate == 6">finish_state = <include refid="finishStateExpr"/>,</if>
             <if test="serviceType != null ">service_type = #{serviceType},</if>
             <if test="score != null ">score = #{score},</if>
             <if test="submit != null ">submit = #{submit},</if>
@@ -1171,6 +1271,7 @@
             <if test="nurseName != null ">nurse_name=#{nurseName},</if>
             <if test="nurseId != null ">nurse_id=#{nurseId},</if>
             <if test="remark != null ">remark=#{remark},</if>
+            <if test="doctorOpinion != null ">doctor_opinion=#{doctorOpinion},</if>
             <if test="suggest != null ">suggest=#{suggest},</if>
             <if test="inhospid != null ">inhospid=#{inhospid},</if>
             <if test="patfrom != null ">patfrom=#{patfrom},</if>
@@ -1258,6 +1359,7 @@
             <if test="openid != null ">openid = #{openid},</if>
             <if test="longSendTime != null ">long_send_time = #{longSendTime},</if>
             <if test="sendstate != null ">sendstate = #{sendstate},</if>
+            <if test="sendstate != null and sendstate == 6">finish_state = <include refid="finishStateExpr"/>,</if>
             <if test="serviceType != null ">service_type = #{serviceType},</if>
             <if test="score != null ">score = #{score},</if>
             <if test="visitCount != null ">visit_count = #{visitCount},</if>
@@ -1276,6 +1378,7 @@
             <if test="nurseName != null ">nurse_name=#{nurseName},</if>
             <if test="nurseId != null ">nurse_id=#{nurseId},</if>
             <if test="remark != null ">remark=#{remark},</if>
+            <if test="doctorOpinion != null ">doctor_opinion=#{doctorOpinion},</if>
             <if test="suggest != null ">suggest=#{suggest},</if>
             <if test="inhospid != null ">inhospid=#{inhospid},</if>
             <if test="patfrom != null ">patfrom=#{patfrom},</if>
@@ -1313,6 +1416,7 @@
             <if test="sex != null">sex = #{sex},</if>
             <if test="delFlag != null">del_flag = #{delFlag},</if>
             <if test="sendstate != null ">sendstate = #{sendstate},</if>
+            <if test="sendstate != null and sendstate == 6">finish_state = <include refid="finishStateExpr"/>,</if>
             <if test="age != null">age = #{age},</if>
             <if test="sfzh != null">sfzh = #{sfzh},</if>
             <if test="addr != null">addr = #{addr},</if>
@@ -1380,6 +1484,7 @@
             <if test="nurseName != null ">nurse_name=#{nurseName},</if>
             <if test="nurseId != null ">nurse_id=#{nurseId},</if>
             <if test="remark != null ">remark=#{remark},</if>
+            <if test="doctorOpinion != null ">doctor_opinion=#{doctorOpinion},</if>
             <if test="suggest != null ">suggest=#{suggest},</if>
             <if test="inhospid != null ">inhospid=#{inhospid},</if>
             <if test="patfrom != null ">patfrom=#{patfrom},</if>
@@ -1424,6 +1529,7 @@
             <if test="phone != null">phone = #{phone},</if>
             <if test="sex != null">sex = #{sex},</if>
             <if test="sendstate != null ">sendstate = #{sendstate},</if>
+            <if test="sendstate != null and sendstate == 6">finish_state = <include refid="finishStateExpr"/>,</if>
             <if test="age != null">age = #{age},</if>
             <if test="sfzh != null">sfzh = #{sfzh},</if>
             <if test="addr != null">addr = #{addr},</if>
@@ -1491,6 +1597,7 @@
             <if test="nurseName != null ">nurse_name=#{nurseName},</if>
             <if test="nurseId != null ">nurse_id=#{nurseId},</if>
             <if test="remark != null ">remark=#{remark},</if>
+            <if test="doctorOpinion != null ">doctor_opinion=#{doctorOpinion},</if>
             <if test="suggest != null ">suggest=#{suggest},</if>
             <if test="inhospid != null ">inhospid=#{inhospid},</if>
             <if test="patfrom != null ">patfrom=#{patfrom},</if>
@@ -1573,7 +1680,8 @@
         continue_content,
         continue_count,
         continue_time_next,
-        task_situation
+        task_situation,
+        is_manually_called
         from service_subtask
         where 1=1
         and del_flag = 0
@@ -1860,7 +1968,9 @@
         continue_count,
         continue_time_next,
         task_situation,
-        remark
+        remark,
+        doctor_opinion,
+        is_manually_called
         from service_subtask
         where 1=1
         and del_flag = 0
@@ -1894,6 +2004,10 @@
         <if test="startTime != null and endTime!=null">
             AND visit_time >= DATE(#{startTime})
             AND visit_time &lt; DATE_ADD(DATE(#{endTime}), INTERVAL 1 DAY)
+        </if>
+        <if test="hospitalStartTime != null and hospitalEndTime!=null">
+            AND endtime >= DATE(#{hospitalStartTime})
+            AND endtime &lt; DATE_ADD(DATE(#{hospitalEndTime}), INTERVAL 1 DAY)
         </if>
         <if test="visitDeptCode != null">and visit_dept_code = #{visitDeptCode}</if>
         <if test="visitDeptName != null">and visit_dept_name = #{visitDeptName}</if>
@@ -1946,6 +2060,9 @@
         </if>
         <if test="deptcode != null and deptcode != ''">
             AND deptcode = #{deptcode}
+        </if>
+        <if test="isManuallyCalled != null">
+            AND is_manually_called = #{isManuallyCalled}
         </if>
         order by endtime desc
         <if test="pageSize != null  and pageNum != null">
@@ -2357,6 +2474,7 @@
                nurse_id,
                nurse_name,
                remark,
+               doctor_opinion,
                inhospid,
                patfrom,
                task_guid,
@@ -2841,22 +2959,54 @@
         COUNT(CASE WHEN sendstate = 5 THEN 1 END) AS failedVisitCount,
         SUM(CASE WHEN excep IS NOT NULL AND excep = '1' THEN 1 ELSE 0 END) AS abnormalVisitCount,
         SUM(CASE WHEN excep IS NOT NULL AND excep = '2' THEN 1 ELSE 0 END) AS warnningVisitCount,
-        COUNT(*) AS allVisitCount
-        FROM service_subtask
-        WHERE del_flag = '0'
-        AND service_type = '2'
-        AND visit_count = 1
+        COUNT(*) AS allVisitCount,
+        <choose>
+            <when test="soonTimeoutHours != null and soonTimeoutHours > 0">
+                COUNT(CASE WHEN t.soon_deadline &gt; NOW()
+                AND t.soon_deadline &lt;= DATE_ADD(NOW(), INTERVAL #{soonTimeoutHours} HOUR)
+                THEN 1 END) AS soonTimeoutCount
+            </when>
+            <otherwise>
+                0 AS soonTimeoutCount
+            </otherwise>
+        </choose>
+        FROM (
+        SELECT ss.*,
+        <!-- 鍗冲皢瓒呮椂鎴鏃堕棿 = 鍑洪櫌鏃堕棿(endtime) + 鍙戦�佹棩(send_day, 缂虹渷1) + 琛ュ伩澶╂暟(compensate_time 姹傚拰鍚庡啀 +1 澶�)锛�
+             涓� RyTask/RedisMqReceiver 鐨� isSubtaskDeadline 鍙e緞淇濇寔涓�鑷达紱
+             鏃犳湁鏁堣ˉ鍋胯褰�(cp.subid 涓虹┖)鏃舵埅姝㈡椂闂翠负 NULL锛屼笉鍙備笌鍗冲皢瓒呮椂缁熻 -->
+        CASE
+        WHEN cp.subid IS NULL THEN NULL
+        ELSE DATE_ADD(
+        DATE_ADD(ss.endtime, INTERVAL COALESCE(st.send_day, 1) DAY),
+        INTERVAL (COALESCE(cp.compensate_days, 0) + 1) DAY
+        )
+        END AS soon_deadline
+        FROM service_subtask ss
+        LEFT JOIN service_task st ON st.taskid = ss.taskid AND st.del_flag = '0'
+        <!-- 琛ュ伩鏃堕棿锛氬厛鎸夊瓙浠诲姟鑱氬悎鎴愪竴琛屽啀鍏宠仈锛岄伩鍏嶄竴瀵瑰鏀惧ぇ璁℃暟锛�
+             compensate_time 鏄� varchar 涓斿彲鑳藉瓨鎴愮┖涓诧紝闇�鍏堢疆绌哄啀杞暟瀛� -->
+        LEFT JOIN (
+        SELECT subid,
+        SUM(CAST(COALESCE(NULLIF(TRIM(compensate_time), ''), '0') AS SIGNED)) AS compensate_days
+        FROM service_subtask_preachform
+        WHERE del_flag = 0
+        GROUP BY subid
+        ) cp ON cp.subid = ss.id
+        WHERE ss.del_flag = '0'
+        AND ss.service_type = '2'
+        AND ss.visit_count = 1
         <if test="orgid != null and orgid != ''">
-            AND orgid = #{orgid}
+            AND ss.orgid = #{orgid}
         </if>
         <if test="campusid != null and campusid != ''">
-            and campusid = #{campusid}
+            and ss.campusid = #{campusid}
         </if>
         <if test="(leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0)
                 or (leaveldeptcodes != null and leaveldeptcodes.size() > 0)">
             AND (
             <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
-                leavehospitaldistrictcode IN
+                ss.leavehospitaldistrictcode IN
                 <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="("
                          separator=","
                          close=")">
@@ -2868,13 +3018,14 @@
                 OR
             </if>
             <if test=" leaveldeptcodes != null and leaveldeptcodes.size()>0">
-                deptcode IN
+                ss.deptcode IN
                 <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
                          close=")">
                     #{leaveldeptcode}
                 </foreach>
             </if>)
         </if>
+        ) t
     </select>
 
     <select id="querySendCount" parameterType="com.smartor.domain.entity.ServiceSubtaskSatisfactionEntity"
@@ -3217,7 +3368,8 @@
         continue_count,
         continue_time_next,
         task_situation,
-        remark
+        remark,
+        doctor_opinion
         from service_subtask
         where 1=1
         and del_flag = 0

--
Gitblit v1.9.3