<?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.willalp.flow.mapper.HsFlowTaskMapper">

    <resultMap type="com.willalp.flow.domain.HsFlowTask" id="HsFlowTaskResult">
        <result property="id" column="id"/>
        <result property="taskName" column="task_name"/>
        <result property="examplesId" column="examples_id"/>
        <result property="baseGroupId" column="base_group_id"/>
        <result property="baseId" column="base_id"/>
        <result property="baseName" column="base_name"/>
        <result property="isCandidate" column="is_candidate"/>
        <result property="operator" column="operator"/>
        <result property="initiator" column="initiator"/>
        <result property="isEnd" column="is_end"/>
        <result property="businessId" column="business_id"/>
        <result property="msgIsRead" column="msg_is_read"/>
        <result property="isDelete" column="is_delete"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectHsFlowTaskVo">
        select hft.id,
               task_name,
               examples_id,
               base_group_id,
               hft.base_id,
               base_name,
               is_candidate,
               initiator,
               hft.operator,
               is_end,
               business_id,
               msg_is_read,
               hft.is_delete,
               hft.create_by,
               hft.create_time,
               hft.update_by,
               hft.update_time,
               hft.remark
        from hs_flow_task hft
    </sql>

    <select id="selectHsFlowTaskList" parameterType="HsFlowTask" resultMap="HsFlowTaskResult">
        <include refid="selectHsFlowTaskVo"/>
        <where>
            is_delete = 0
            <if test="taskName != null  and taskName != ''">
                and task_name like concat('%', #{taskName}, '%')
            </if>
            <if test="examplesId != null  and examplesId != ''">
                and examples_id = #{examplesId}
            </if>
            <if test="baseId != null  and baseId != ''">
                and base_id = #{baseId}
            </if>
            <if test="initiator != null  and initiator != ''">
                and initiator = #{initiator}
            </if>
            <if test="businessId != null  and businessId != ''">
                and business_id = #{businessId}
            </if>
        </where>
    </select>

    <select id="selectAllByOperator" resultType="com.willalp.flow.domain.HsFlowTask">
        <include refid="selectHsFlowTaskVo"/>
        <where>
            is_delete = 0
            AND is_end = '0'
            AND operator = #{userCode}
        </where>
        group by create_time desc
    </select>

    <select id="selectAllByInitiator" resultType="com.willalp.flow.domain.HsFlowTask">
        SELECT
        hft.id,
        task_name,
        examples_id,
        base_group_id,
        hft.base_id,
        base_name,
        is_candidate,
        initiator,
        is_end,
        business_id,
        (SELECT msg_is_read FROM hs_app_user_msg where remark = hft.id AND msg_is_read = '0') msg_is_read,
        hft.create_time
        FROM
        hs_flow_task hft
        <where>
            hft.is_delete = 0
            AND initiator = #{userCode}
        </where>
        order by hft.create_time desc
    </select>

    <select id="getDone" resultType="com.willalp.flow.domain.HsFlowTask">
        <include refid="selectHsFlowTaskVo"/>
        left join hs_flow_running_record hfrr on hft.id = hfrr.task_id
        <where>
            hft.is_delete = 0
            AND hfrr.operator = #{userCode}
        </where>
        group by hft.create_time desc
    </select>
</mapper>
