<?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.usky.oa.mapper.OaDocumentMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.usky.oa.domain.OaDocument">
        <id column="id" property="id"/>
        <result column="form_id" property="formId"/>
        <result column="type" property="type"/>
        <result column="doc_no" property="docNo"/>
        <result column="proposer" property="proposer"/>
        <result column="doc_status" property="docStatus"/>
        <result column="create_by" property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="dept_id" property="deptId"/>
        <result column="tenant_id" property="tenantId"/>
    </resultMap>

    <!-- 动态创建表-->
    <update id="createTable">
        create table if not exists ${tableName} (
        <foreach collection="columns" item="column" separator=",">
            ${column.name} ${column.type}
        </foreach>
        , PRIMARY KEY (${primaryKey})
        )
    </update>

    <!-- 动态更新数据 -->
    <update id="updateData" parameterType="map">
        update ${(tableName)}
        <set>
            <if test="map != null and !map.isEmpty()">
                <foreach collection="map" item="entry" index="key" separator=",">
                    #{key} = #{entry.value}
                </foreach>
            </if>
        </set>
        where id = #{id}
    </update>

    <!-- 动态查询数据 -->
    <select id="selectData" parameterType="map" resultType="map">
        select *
        from ${tableName}
        <where>
            <foreach collection="conditions" item="condition" separator="and">
                ${condition.column} ${condition.operator} #{condition.value}
            </foreach>
        </where>
        <if test="limit != null and offset != null">
            limit #{limit} offset #{offset}
        </if>
    </select>

    <!-- 动态插入数据 -->
    <insert id="insertData">
        insert into ${tableName} (
        <foreach collection="columns" item="column" separator=",">
            ${column}
        </foreach>
        ) values (
        <foreach collection="values" item="value" separator=",">
            #{value}
        </foreach>
        )
    </insert>

    <!-- 动态删除数据 -->
    <delete id="deleteData" parameterType="map">
        delete from ${tableName}
        <where>
            <foreach collection="conditions" item="condition" separator="and">
                ${condition.column} ${condition.operator} #{condition.value}
            </foreach>
        </where>
    </delete>

</mapper>
