<?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="ink.wgink.module.activiti.dao.oa.IOaOnFileDao">

    <resultMap id="oaOnFilePO" type="ink.wgink.module.activiti.pojo.pos.oa.OaOnFilePO">
        <id column="id" property="id"/>
        <result column="on_file_id" property="onFileId"/>
        <result column="form_code" property="formCode"/>
        <result column="form_version" property="formVersion"/>
        <result column="process_instance_id" property="processInstanceId"/>
        <result column="report_uid" property="reportUid"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <resultMap id="oaOnFileDTO" type="ink.wgink.module.activiti.pojo.dtos.oa.OaOnFileDTO">
        <id column="on_file_id" property="onFileId"/>
        <result column="form_code" property="formCode"/>
        <result column="form_version" property="formVersion"/>
        <result column="process_instance_id" property="processInstanceId"/>
        <result column="report_uid" property="reportUid"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `oa_on_file` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `on_file_id` char(36) DEFAULT NULL,
            `form_code` varchar(255) DEFAULT NULL COMMENT '表单编码',
            `form_version` int(11) DEFAULT NULL COMMENT '表单版本',
            `process_instance_id` char(36) DEFAULT NULL COMMENT '流程实例ID',
            `report_uid` char(36) DEFAULT NULL COMMENT '上报ID',
            `creator` char(36) DEFAULT NULL COMMENT '创建人',
            `gmt_create` date DEFAULT NULL COMMENT '创建时间',
            `is_delete` int(1) DEFAULT NULL COMMENT '是否删除',
            PRIMARY KEY (`id`),
            UNIQUE KEY `on_file_id` (`on_file_id`),
            KEY `form_code` (`form_code`,`form_version`),
            KEY `process_instance_id` (`process_instance_id`),
            KEY `report_uid` (`report_uid`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='OA归档';
    </update>

    <!-- 新增 -->
    <insert id="save" parameterType="map">
        INSERT INTO oa_on_file(
            on_file_id,
            form_code,
            form_version,
            process_instance_id,
            report_uid,
            gmt_create,
            creator,
            gmt_modified,
            modifier,
            is_delete
        ) VALUES(
            #{onFileId},
            #{formCode},
            #{formVersion},
            #{processInstanceId},
            #{reportUid},
            #{gmtCreate},
            #{creator},
            #{gmtModified},
            #{modifier},
            #{isDelete}
        )
    </insert>

    <!-- 删除 -->
    <update id="remove" parameterType="map">
        UPDATE
            oa_on_file
        SET
            is_delete = 0,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            <if test="onFileIds != null and onFileIds.size > 0">
            on_file_id IN
                <foreach collection="onFileIds" index="index" open="(" separator="," close=")">
                #{onFileIds[${index}]}
                </foreach>
            </if>
    </update>

    <!-- 删除（物理） -->
    <delete id="delete" parameterType="map">
        DELETE FROM
            oa_on_file
        WHERE
            <if test="onFileIds != null and onFileIds.size > 0">
            on_file_id IN
                <foreach collection="onFileIds" index="index" open="(" separator="," close=")">
                #{onFileIds[${index}]}
                </foreach>
            </if>
    </delete>

    <!-- 详情 -->
    <select id="getPO" parameterType="map" resultMap="oaOnFilePO">
        SELECT
            on_file_id,
            form_code,
            form_version,
            process_instance_id,
            report_uid,
            gmt_create,
            creator,
            gmt_modified,
            modifier,
            is_delete
        FROM
            oa_on_file
        WHERE
            is_delete = 0
        <if test="onFileId != null and onFileId != ''">
        AND
            on_file_id = #{onFileId}
        </if>
        <if test="processInstanceId != null and processInstanceId != ''">
        AND
            process_instance_id = #{processInstanceId}
        </if>
        <if test="reportUid != null and reportUid != ''">
        AND
            report_uid = #{reportUid}
        </if>
    </select>

    <!-- 详情 -->
    <select id="get" parameterType="map" resultMap="oaOnFileDTO">
        SELECT
            on_file_id,
            form_code,
            form_version,
            process_instance_id,
            report_uid,
            gmt_create,
            creator,
            gmt_modified,
            modifier,
            is_delete
        FROM
            oa_on_file
        WHERE
            is_delete = 0
        <if test="oaFileId != null and oaFileId != ''">
        AND
            on_file_id = #{oaFileId}
        </if>
        <if test="processInstanceId != null and processInstanceId != ''">
        AND
            process_instance_id = #{processInstanceId}
        </if>
        <if test="reportUid != null and reportUid != ''">
        AND
            report_uid = #{reportUid}
        </if>
    </select>

    <!-- 列表 -->
    <select id="listPO" parameterType="map" resultMap="oaOnFilePO">
        SELECT
            on_file_id,
            form_code,
            form_version,
            process_instance_id,
            report_uid,
            gmt_create,
            creator,
            gmt_modified,
            modifier,
            is_delete
        FROM
            oa_on_file
        WHERE
            is_delete = 0
            <if test="formCode != null and formCode != ''">
        AND
            form_code = #{formCode}
            </if>
            <if test="formVersion != null">
        AND
            form_version = #{formVersion}
            </if>
            <if test="processInstanceId != null and processInstanceId != ''">
        AND
            process_instance_id = #{processInstanceId}
            </if>
            <if test="reportUid != null and reportUid != ''">
        AND
            report_uid = #{reportUid}
            </if>
            <if test="processInstanceIds != null and processInstanceIds.size > 0">
        AND
            process_instance_id IN
                <foreach collection="processInstanceIds" index="index" open="(" separator="," close=")">
                #{processInstanceIds[${index}]}
                </foreach>
            </if>
            <if test="reportUids != null and reportUids.size > 0">
        AND
            report_uid IN
                <foreach collection="reportUids" index="index" open="(" separator="," close=")">
                #{reportUids[${index}]}
                </foreach>
            </if>
        <if test="sort != null and (sort == 'gmtCreate')">
        ORDER BY
            <if test="sort == 'gmtCreate'">
                gmt_create ${order}
            </if>
        </if>
    </select>

    <!-- 列表 -->
    <select id="list" parameterType="map" resultMap="oaOnFileDTO">
        SELECT
            on_file_id,
            form_code,
            form_version,
            process_instance_id,
            report_uid
        FROM
            oa_on_file
        WHERE
            is_delete = 0
            <if test="formCode != null and formCode != ''">
        AND
            form_code = #{formCode}
            </if>
            <if test="formVersion != null">
        AND
            form_version = #{formVersion}
            </if>
            <if test="processInstanceId != null and processInstanceId != ''">
        AND
            process_instance_id = #{processInstanceId}
            </if>
            <if test="processInstanceIds != null and processInstanceIds.size > 0">
        AND
            process_instance_id IN
                <foreach collection="processInstanceIds" index="index" open="(" separator="," close=")">
                #{processInstanceIds[${index}]}
                </foreach>
            </if>
            <if test="reportUid != null and reportUid != ''">
        AND
            report_uid = #{reportUid}
            </if>
            <if test="reportUids != null and reportUids.size > 0">
        AND
            report_uid IN
                <foreach collection="reportUids" index="index" open="(" separator="," close=")">
                #{reportUids[${index}]}
                </foreach>
            </if>
        <if test="sort != null and (sort == 'gmtCreate')">
        ORDER BY
            <if test="sort == 'gmtCreate'">
                gmt_create ${order}
            </if>
        </if>
    </select>

    <!-- 未完成的流程实例列表 -->
    <select id="listUnOnFileProcessInstanceId" parameterType="map" resultType="java.lang.String">
        SELECT
            t1.PROC_INST_ID_
        FROM
            act_hi_procinst t1
        WHERE
            t1.PROC_INST_ID_ NOT IN (
                SELECT
                    st1.process_instance_id
                FROM
                    oa_on_file st1
                WHERE
                    st1.is_delete = 0
            )
            <if test="unFinished != null and unFinished == 'true'">
            AND (
                t1.END_TIME_ IS NULL
                OR
                t1.END_TIME_ = ''
            )
            </if>
            <if test="finished != null and finished == 'true'">
            AND (
                t1.END_TIME_ IS NOT NULL
                AND
                t1.END_TIME_ != ''
            )
            </if>
            <if test="modelCategories != null and modelCategories.size > 0">
            AND
                t1.PROC_DEF_ID_ IN (
                    SELECT
                        st1.ID_
                    FROM
                        act_re_procdef st1
                    INNER JOIN
                        act_re_model st2
                    ON
                        st1.DEPLOYMENT_ID_ = st2.DEPLOYMENT_ID_
                    WHERE
                        st2.CATEGORY_ IN
                        <foreach collection="modelCategories" index="index" open="(" separator="," close=")">
                            #{modelCategories[${index}]}
                        </foreach>
                )
            </if>
    </select>

    <!-- 统计 -->
    <select id="count" parameterType="map" resultType="java.lang.Integer">
        SELECT
            COUNT(*)
        FROM
            oa_on_file
        <where>
            <if test="formCode != null and formCode != ''">
            form_code = #{formCode}
            </if>
            <if test="formVersion != null">
        AND
            form_version = #{formVersion}
            </if>
        </where>
    </select>

</mapper>