<?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="org.xyzh.workcase.mapper.TbWorkcaseProcessMapper">

    <resultMap id="BaseResultMap" type="org.xyzh.api.workcase.dto.TbWorkcaseProcessDTO">
        <id column="process_id" property="processId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="workcase_id" property="workcaseId" jdbcType="VARCHAR"/>
        <result column="action" property="action" jdbcType="VARCHAR"/>
        <result column="message" property="message" jdbcType="VARCHAR"/>
        <result column="files" property="files" jdbcType="ARRAY" typeHandler="org.xyzh.common.jdbc.handler.StringArrayTypeHandler"/>
        <result column="processor" property="processor" jdbcType="VARCHAR"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="VOResultMap" type="org.xyzh.api.workcase.vo.WorkcaseProcessVO">
        <id column="process_id" property="processId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="workcase_id" property="workcaseId" jdbcType="VARCHAR"/>
        <result column="action" property="action" jdbcType="VARCHAR"/>
        <result column="message" property="message" jdbcType="VARCHAR"/>
        <result column="files" property="files" jdbcType="ARRAY" typeHandler="org.xyzh.common.jdbc.handler.StringArrayTypeHandler"/>
        <result column="processor" property="processor" jdbcType="VARCHAR"/>
        <result column="processor_name" property="processorName" jdbcType="VARCHAR"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="creator_name" property="creatorName" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        process_id, optsn, workcase_id, action, message, files, processor, remark, creator, create_time
    </sql>

    <insert id="insertWorkcaseProcess" parameterType="org.xyzh.api.workcase.dto.TbWorkcaseProcessDTO">
        INSERT INTO workcase.tb_workcase_process (
            optsn, workcase_id, process_id, action, creator
            <if test="message != null">, message</if>
            <if test="files != null">, files</if>
            <if test="processor != null">, processor</if>
            <if test="remark != null">, remark</if>
        ) VALUES (
            #{optsn}, #{workcaseId}, #{processId}, #{action}, #{creator}
            <if test="message != null">, #{message}</if>
            <if test="files != null">, #{files, typeHandler=org.xyzh.common.jdbc.handler.StringArrayTypeHandler}</if>
            <if test="processor != null">, #{processor}</if>
            <if test="remark != null">, #{remark}</if>
        )
    </insert>

    <update id="updateWorkcaseProcess" parameterType="org.xyzh.api.workcase.dto.TbWorkcaseProcessDTO">
        UPDATE workcase.tb_workcase_process
        <set>
            <if test="action != null and action != ''">action = #{action},</if>
            <if test="message != null">message = #{message},</if>
            <if test="files != null">files = #{files, typeHandler=org.xyzh.common.jdbc.handler.StringArrayTypeHandler},</if>
            <if test="processor != null">processor = #{processor},</if>
            <if test="remark != null">remark = #{remark},</if>
        </set>
        WHERE process_id = #{processId}
    </update>

    <delete id="deleteWorkcaseProcess">
        DELETE FROM workcase.tb_workcase_process
        WHERE process_id = #{processId}
    </delete>

    <select id="selectWorkcaseProcessById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_workcase_process
        WHERE process_id = #{processId}
    </select>

    <select id="selectWorkcaseProcessList" resultMap="VOResultMap">
        SELECT p.process_id, p.optsn, p.workcase_id, p.action, p.message, p.files, 
               p.processor, p.remark, p.creator, p.create_time,
               COALESCE(u1.username, g1.name) as creator_name,
               COALESCE(u2.username, g2.name) as processor_name
        FROM workcase.tb_workcase_process p
        LEFT JOIN sys.tb_sys_user_info u1 ON p.creator = u1.user_id
        LEFT JOIN sys.tb_guest g1 ON p.creator = g1.user_id
        LEFT JOIN sys.tb_sys_user_info u2 ON p.processor = u2.user_id
        LEFT JOIN sys.tb_guest g2 ON p.processor = g2.user_id
        <where>
            <if test="filter.processId != null and filter.processId != ''">
                AND p.process_id = #{filter.processId}
            </if>
            <if test="filter.workcaseId != null and filter.workcaseId != ''">
                AND p.workcase_id = #{filter.workcaseId}
            </if>
            <if test="filter.action != null and filter.action != ''">
                AND p.action = #{filter.action}
            </if>
            <if test="filter.processor != null and filter.processor != ''">
                AND p.processor = #{filter.processor}
            </if>
            <if test="filter.creator != null and filter.creator != ''">
                AND p.creator = #{filter.creator}
            </if>
        </where>
        ORDER BY p.create_time ASC
    </select>

    <select id="selectWorkcaseProcessPage" resultMap="VOResultMap">
        SELECT p.process_id, p.optsn, p.workcase_id, p.action, p.message, p.files, 
               p.processor, p.remark, p.creator, p.create_time,
               COALESCE(u1.username, g1.name) as creator_name,
               COALESCE(u2.username, g2.name) as processor_name
        FROM workcase.tb_workcase_process p
        LEFT JOIN sys.tb_sys_user_info u1 ON p.creator = u1.user_id
        LEFT JOIN sys.tb_guest g1 ON p.creator = g1.user_id
        LEFT JOIN sys.tb_sys_user_info u2 ON p.processor = u2.user_id
        LEFT JOIN sys.tb_guest g2 ON p.processor = g2.user_id
        <where>
            <if test="filter.processId != null and filter.processId != ''">
                AND p.process_id = #{filter.processId}
            </if>
            <if test="filter.workcaseId != null and filter.workcaseId != ''">
                AND p.workcase_id = #{filter.workcaseId}
            </if>
            <if test="filter.action != null and filter.action != ''">
                AND p.action = #{filter.action}
            </if>
            <if test="filter.processor != null and filter.processor != ''">
                AND p.processor = #{filter.processor}
            </if>
            <if test="filter.creator != null and filter.creator != ''">
                AND p.creator = #{filter.creator}
            </if>
        </where>
        ORDER BY p.create_time ASC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countWorkcaseProcesses" resultType="long">
        SELECT COUNT(*)
        FROM workcase.tb_workcase_process
        <where>
            <if test="filter.processId != null and filter.processId != ''">
                AND process_id = #{filter.processId}
            </if>
            <if test="filter.workcaseId != null and filter.workcaseId != ''">
                AND workcase_id = #{filter.workcaseId}
            </if>
            <if test="filter.action != null and filter.action != ''">
                AND action = #{filter.action}
            </if>
            <if test="filter.processor != null and filter.processor != ''">
                AND processor = #{filter.processor}
            </if>
            <if test="filter.creator != null and filter.creator != ''">
                AND creator = #{filter.creator}
            </if>
        </where>
    </select>

</mapper>
