<?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.IOaCcDao">

    <resultMap id="oaCcPO" type="ink.wgink.module.activiti.pojo.pos.oa.OaCcPO">
        <id column="cc_id" property="ccId"/>
        <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="user_id" property="userId"/>
        <result column="is_read" property="isRead"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <resultMap id="oaCcDTO" type="ink.wgink.module.activiti.pojo.dtos.oa.OaCcDTO">
        <id column="cc_id" property="ccId"/>
        <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="user_id" property="userId"/>
        <result column="is_read" property="isRead"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="creator" property="creator"/>
        <result column="gmt_modified" property="gmtModified"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `oa_cc` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `cc_id` char(36) DEFAULT NULL COMMENT '主键',
            `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 '上报uid',
            `user_id` char(36) DEFAULT NULL COMMENT '抄送人ID',
            `is_read` int(1) DEFAULT '0' COMMENT '是否已读',
            `gmt_create` datetime DEFAULT NULL COMMENT '创建时间',
            `creator` char(36) DEFAULT NULL COMMENT '创建人',
            `gmt_modified` datetime DEFAULT NULL COMMENT '修改时间',
            `modifier` char(36) DEFAULT NULL COMMENT '修改人',
            PRIMARY KEY (`id`),
            UNIQUE KEY `cc_id` (`cc_id`),
            KEY `form_code` (`form_code`,`form_version`),
            KEY `process_instance_id` (`process_instance_id`),
            KEY `report_uid` (`report_uid`),
            KEY `user_id` (`user_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='表单抄送';
    </update>

    <!-- 新增 -->
    <insert id="save" parameterType="map">
        INSERT INTO oa_cc(
            cc_id,
            form_code,
            form_version,
            process_instance_id,
            report_uid,
            user_id,
            is_read,
            gmt_create,
            creator,
            gmt_modified,
            modifier
        ) VALUES(
            #{ccId},
            #{formCode},
            #{formVersion},
            #{processInstanceId},
            #{reportUid},
            #{userId},
            #{isRead},
            #{gmtCreate},
            #{creator},
            #{gmtModified},
            #{modifier}
        )
    </insert>

    <!-- 更新阅读状态 -->
    <update id="updateRead" parameterType="map">
        UPDATE
            oa_cc
        SET
            is_read = #{isRead},
            gmt_modified = #{gmtModified},
            modifier = #{modifier}
        WHERE
            cc_id = #{ccId}
    </update>

    <!-- 列表 -->
    <select id="listPO" parameterType="map" resultMap="oaCcPO">
        SELECT
            cc_id,
            form_code,
            form_version,
            process_instance_id,
            report_uid,
            user_id,
            is_read,
            gmt_create,
            gmt_modified,
            creator
        FROM
            oa_cc
        <where>
            <if test="formCode != null and formCode != ''">
            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="userId != null and userId != ''">
        AND
            user_id = #{userId}
            </if>
            <if test="isRead != null">
        AND
            is_read = #{isRead}
            </if>
        </where>
        <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="oaCcDTO">
        SELECT
            cc_id,
            form_code,
            form_version,
            process_instance_id,
            report_uid,
            user_id,
            is_read,
            gmt_create,
            gmt_modified,
            creator
        FROM
            oa_cc
        <where>
            <if test="formCode != null and formCode != ''">
            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="userId != null and userId != ''">
        AND
            user_id = #{userId}
            </if>
            <if test="isRead != null">
        AND
            is_read = #{isRead}
            </if>
        </where>
        <if test="sort != null and (sort == 'gmtCreate')">
        ORDER BY
            <if test="sort == 'gmtCreate'">
                gmt_create ${order}
            </if>
        </if>
    </select>

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

</mapper>