<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.jarvis.mapper.WeComShareLinkLogisticsJobMapper">

    <resultMap id="WeComShareLinkLogisticsJobResult" type="WeComShareLinkLogisticsJob">
        <id property="id" column="id"/>
        <result property="jobKey" column="job_key"/>
        <result property="fromUserName" column="from_user_name"/>
        <result property="trackingUrl" column="tracking_url"/>
        <result property="userRemark" column="remark"/>
        <result property="touserPush" column="touser_push"/>
        <result property="status" column="status"/>
        <result property="waybillNo" column="waybill_no"/>
        <result property="scanAttempts" column="scan_attempts"/>
        <result property="lastNote" column="last_note"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectVo">
        select id, job_key, from_user_name, tracking_url, remark, touser_push, status, waybill_no,
               scan_attempts, last_note, create_time, update_time
        from wecom_share_link_logistics_job
    </sql>

    <insert id="insertWeComShareLinkLogisticsJob" parameterType="WeComShareLinkLogisticsJob" useGeneratedKeys="true" keyProperty="id">
        insert into wecom_share_link_logistics_job (
            job_key, from_user_name, tracking_url, remark, touser_push, status, scan_attempts, last_note
            <if test="createTime != null">, create_time, update_time</if>
        ) values (
            #{jobKey}, #{fromUserName}, #{trackingUrl}, #{userRemark}, #{touserPush}, #{status},
            #{scanAttempts}, #{lastNote}
            <if test="createTime != null">, #{createTime}, #{createTime}</if>
        )
    </insert>

    <update id="updateByJobKey">
        update wecom_share_link_logistics_job
        <set>
            <if test="status != null">status = #{status},</if>
            <if test="lastNote != null">last_note = #{lastNote},</if>
            <if test="scanAttempts != null">scan_attempts = #{scanAttempts},</if>
            <if test="waybillNo != null">waybill_no = #{waybillNo},</if>
            update_time = now()
        </set>
        where job_key = #{jobKey}
    </update>

    <select id="selectByJobKey" resultMap="WeComShareLinkLogisticsJobResult">
        <include refid="selectVo"/>
        where job_key = #{jobKey}
    </select>

    <select id="selectWeComShareLinkLogisticsJobList" parameterType="WeComShareLinkLogisticsJob" resultMap="WeComShareLinkLogisticsJobResult">
        <include refid="selectVo"/>
        <where>
            <if test="jobKey != null and jobKey != ''">and job_key = #{jobKey}</if>
            <if test="fromUserName != null and fromUserName != ''">and from_user_name like concat('%', #{fromUserName}, '%')</if>
            <if test="status != null and status != ''">and status = #{status}</if>
            <if test="trackingUrl != null and trackingUrl != ''">and tracking_url like concat('%', #{trackingUrl}, '%')</if>
            <if test="userRemark != null and userRemark != ''">and remark like concat('%', #{userRemark}, '%')</if>
            <if test="waybillNo != null and waybillNo != ''">and waybill_no like concat('%', #{waybillNo}, '%')</if>
            <if test="params.beginTime != null and params.beginTime != ''">
                and create_time &gt;= #{params.beginTime}
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                and create_time &lt;= concat(#{params.endTime}, ' 23:59:59')
            </if>
        </where>
        order by id desc
    </select>

    <select id="selectJobsNeedingQueueReconcile" resultMap="WeComShareLinkLogisticsJobResult">
        <include refid="selectVo"/>
        where status in ('PENDING', 'WAITING', 'IMPORTED', 'ABANDONED')
          and tracking_url is not null and trim(tracking_url) != ''
          and create_time >= date_sub(now(), interval 1 month)
        order by id asc
        limit #{limit}
    </select>

    <delete id="deleteByJobKey">
        delete from wecom_share_link_logistics_job where job_key = #{jobKey}
    </delete>
</mapper>
