if not exists (select 1 from wcs_job_dispatch with (updlock, holdlock) where job_id = #{jobId}) begin insert into wcs_job_dispatch (job_id, dispatch_status, auto_retry_count, manual_retry_count, version_no, create_time, update_time) values (#{jobId}, 'PENDING', 0, 0, 0, getdate(), getdate()) end update wcs_job_dispatch set dispatch_status = 'SENDING', auto_retry_count = auto_retry_count + case when #{trigger} = 'AUTO_RETRY' then 1 else 0 end, manual_retry_count = manual_retry_count + case when #{trigger} = 'MANUAL' then 1 else 0 end, last_attempt_time = #{attemptTime}, next_retry_time = null, update_time = #{attemptTime}, version_no = version_no + 1 where job_id = #{jobId} and version_no = #{versionNo} and dispatch_status in ('PENDING', 'BLOCKED', 'RETRY_WAIT', 'EXHAUSTED', 'SENDING') and (dispatch_status != 'SENDING' or last_attempt_time <= dateadd(minute, -5, #{attemptTime})) and (#{trigger} != 'AUTO_RETRY' or auto_retry_count < 5) update wcs_job_dispatch set dispatch_status = 'BLOCKED', last_error = #{lastError}, next_retry_time = #{nextRetryTime}, update_time = #{updateTime}, version_no = version_no + 1 where job_id = #{jobId} and version_no = #{versionNo} and dispatch_status != 'SUCCESS' update wcs_job_dispatch set dispatch_status = 'RETRY_WAIT', last_error = #{lastError}, next_retry_time = #{nextRetryTime}, update_time = #{updateTime}, version_no = version_no + 1 where job_id = #{jobId} and version_no = #{versionNo} and dispatch_status = 'SENDING' update wcs_job_dispatch set dispatch_status = 'SUCCESS', last_error = null, next_retry_time = null, success_time = #{successTime}, update_time = #{successTime}, version_no = version_no + 1 where job_id = #{jobId} and version_no = #{versionNo} and dispatch_status = 'SENDING' update wcs_job_dispatch set dispatch_status = 'EXHAUSTED', last_error = #{lastError}, next_retry_time = null, update_time = #{updateTime}, version_no = version_no + 1 where job_id = #{jobId} and version_no = #{versionNo} and dispatch_status in ('SENDING', 'BLOCKED', 'RETRY_WAIT')