<?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.sundot.airport.system.mapper.approval.ApprovalCcMapper">

    <resultMap type="ApprovalCc" id="ApprovalCcResult">
        <id property="id" column="id"/>
        <result property="instanceId" column="instance_id"/>
        <result property="ccUserId" column="cc_user_id"/>
        <result property="ccUserName" column="cc_user_name"/>
        <result property="ccTime" column="cc_time"/>
        <result property="isRead" column="is_read"/>
        <result property="readTime" column="read_time"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <resultMap id="InstanceResult" type="ApprovalInstance">
        <id property="id" column="instance_id"/>
        <result property="instanceNo" column="instance_no"/>
        <result property="title" column="title"/>
        <result property="businessType" column="business_type"/>
        <result property="status" column="status"/>
        <result property="submitTime" column="submit_time"/>
        <result property="completionTime" column="completion_time"/>
    </resultMap>

    <resultMap id="ApprovalCcWithInstanceResult" type="ApprovalCc">
        <id property="id" column="id"/>
        <result property="instanceId" column="instance_id"/>
        <result property="ccUserId" column="cc_user_id"/>
        <result property="ccUserName" column="cc_user_name"/>
        <result property="ccTime" column="cc_time"/>
        <result property="isRead" column="is_read"/>
        <result property="readTime" column="read_time"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="remark" column="remark"/>
        <association property="instance" javaType="ApprovalInstance">
            <id property="id" column="ai_id"/>
            <result property="instanceNo" column="ai_instance_no"/>
            <result property="title" column="ai_title"/>
            <result property="businessId" column="ai_business_id"/>
            <result property="businessType" column="ai_business_type"/>
            <result property="status" column="ai_status"/>
            <result property="submitTime" column="ai_submit_time"/>
            <result property="completionTime" column="ai_completion_time"/>
            <result property="formData" column="ai_form_data"/>
        </association>
    </resultMap>

    <sql id="selectApprovalCcVo">
        select id,
               instance_id,
               cc_user_id,
               cc_user_name,
               cc_time,
               is_read,
               read_time,
               create_by,
               create_time,
               remark
        from approval_cc
    </sql>

    <select id="selectApprovalCcList" parameterType="ApprovalCc" resultMap="ApprovalCcResult">
        <include refid="selectApprovalCcVo"/>
        <where>
            <if test="instanceId != null ">and instance_id = #{instanceId}</if>
            <if test="ccUserId != null ">and cc_user_id = #{ccUserId}</if>
            <if test="ccUserName != null  and ccUserName != ''">and cc_user_name like concat('%', #{ccUserName}, '%')
            </if>
            <if test="isRead != null  and isRead != ''">and is_read = #{isRead}</if>
            <if test="params.beginCcTime != null and params.beginCcTime != '' and params.endCcTime != null and params.endCcTime != ''">
                and cc_time between #{params.beginCcTime} and #{params.endCcTime}
            </if>
        </where>
    </select>

    <select id="selectApprovalCcById" parameterType="Long" resultMap="ApprovalCcResult">
        <include refid="selectApprovalCcVo"/>
        where id = #{id}
    </select>

    <select id="selectApprovalCcByInstanceId" parameterType="Long" resultMap="ApprovalCcResult">
        <include refid="selectApprovalCcVo"/>
        where instance_id = #{instanceId}
        order by cc_time desc
    </select>

    <select id="selectApprovalCcByCcUser" resultMap="ApprovalCcResult">
        <include refid="selectApprovalCcVo"/>
        where cc_user_id = #{ccUserId}
        <if test="isRead != null and isRead != ''">and is_read = #{isRead}</if>
        order by cc_time desc
    </select>

    <select id="countCcMessagesByCcUser" resultType="int">
        select count(*) from approval_cc
        where cc_user_id = #{ccUserId}
        <if test="isRead != null and isRead != ''">and is_read = #{isRead}</if>
    </select>

    <insert id="insertApprovalCc" parameterType="ApprovalCc" useGeneratedKeys="true" keyProperty="id">
        insert into approval_cc
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="instanceId != null">instance_id,</if>
            <if test="ccUserId != null">cc_user_id,</if>
            <if test="ccUserName != null and ccUserName != ''">cc_user_name,</if>
            <if test="ccTime != null">cc_time,</if>
            <if test="isRead != null">is_read,</if>
            <if test="readTime != null">read_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="instanceId != null">#{instanceId},</if>
            <if test="ccUserId != null">#{ccUserId},</if>
            <if test="ccUserName != null and ccUserName != ''">#{ccUserName},</if>
            <if test="ccTime != null">#{ccTime},</if>
            <if test="isRead != null">#{isRead},</if>
            <if test="readTime != null">#{readTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateApprovalCc" parameterType="ApprovalCc">
        update approval_cc
        <trim prefix="SET" suffixOverrides=",">
            <if test="instanceId != null">instance_id = #{instanceId},</if>
            <if test="ccUserId != null">cc_user_id = #{ccUserId},</if>
            <if test="ccUserName != null and ccUserName != ''">cc_user_name = #{ccUserName},</if>
            <if test="ccTime != null">cc_time = #{ccTime},</if>
            <if test="isRead != null">is_read = #{isRead},</if>
            <if test="readTime != null">read_time = #{readTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteApprovalCcById" parameterType="Long">
        delete
        from approval_cc
        where id = #{id}
    </delete>

    <delete id="deleteApprovalCcByIds" parameterType="String">
        delete from approval_cc where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteApprovalCcByInstanceId" parameterType="Long">
        delete
        from approval_cc
        where instance_id = #{instanceId}
    </delete>

    <select id="selectCcDetailsByUserId" parameterType="Long" resultMap="ApprovalCcWithInstanceResult">
        select ac.id,
               ac.instance_id,
               ac.cc_user_id,
               ac.cc_user_name,
               ac.cc_time,
               ac.is_read,
               ac.read_time,
               ac.create_by,
               ac.create_time,
               ac.remark,
               ai.id              as ai_id,
               ai.instance_no     as ai_instance_no,
               ai.title           as ai_title,
               ai.business_id     as ai_business_id,
               ai.business_type   as ai_business_type,
               ai.status          as ai_status,
               ai.submit_time     as ai_submit_time,
               ai.completion_time as ai_completion_time,
               ai.form_data       as ai_form_data
        from approval_cc ac
                 left join approval_instance ai on ac.instance_id = ai.id
        where ac.cc_user_id = #{ccUserId}
        order by ac.cc_time desc
    </select>

    <update id="batchUpdateReadStatus" parameterType="list">
        update approval_cc
        set is_read = '1', read_time = sysdate()
        where id in
        <foreach collection="list" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>