<?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.dgtly.order.mapper.BpmWxNoticeOrgMapper">
    
    <resultMap type="BpmWxNoticeOrg" id="BpmWxNoticeOrgResult">
        <result property="id"    column="id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDelete"    column="is_delete"    />
        <result property="orgName"    column="org_name"    />
        <result property="orgCode"    column="org_code"    />
        <result property="account"    column="account"    />
        <result property="userName"    column="user_name"    />
    </resultMap>

    <sql id="selectBpmWxNoticeOrgVo">
        select id, create_by, create_time, update_by, update_time, is_delete, org_name, org_code, account, user_name from bpm_wx_notice_org
    </sql>

    <select id="selectBpmWxNoticeOrgList" parameterType="BpmWxNoticeOrg" resultMap="BpmWxNoticeOrgResult">
        <include refid="selectBpmWxNoticeOrgVo"/>
        <where>  
            <if test="isDelete != null "> and is_delete = #{isDelete}</if>
            <if test="orgName != null  and orgName != ''"> and org_name like concat('%', #{orgName}, '%')</if>
            <if test="orgCode != null  and orgCode != ''"> and org_code = #{orgCode}</if>
            <if test="account != null  and account != ''"> and account = #{account}</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
        </where>
    </select>
    
    <select id="selectBpmWxNoticeOrgById" parameterType="Long" resultMap="BpmWxNoticeOrgResult">
        <include refid="selectBpmWxNoticeOrgVo"/>
        where id = #{id}
    </select>

    <select id="selectMetaDiySalesorgtreeOrgCode" resultType="java.lang.String">
        SELECT a.orgCode FROM (
            SELECT
                CASE
                WHEN Org2name = #{orgName} THEN
                    Org2code
                WHEN Org3name = #{orgName} THEN
                    Org3code
                WHEN Org4name = #{orgName} THEN
                    Org4code
                WHEN Org5name = #{orgName} THEN
                    Org5code
                WHEN Org6name = #{orgName} THEN
                    Org6code
                END orgCode
            FROM
            meta_diy_salesorgtree ) a WHERE a.orgCode IS NOT NULL LIMIT 1
    </select>
    <select id="selectBpmWxNoticeOrgByAccount" resultMap="BpmWxNoticeOrgResult">
        <include refid="selectBpmWxNoticeOrgVo"/>
        where account = #{account}
    </select>

    <insert id="insertBpmWxNoticeOrg" parameterType="BpmWxNoticeOrg" useGeneratedKeys="true" keyProperty="id">
        insert into bpm_wx_notice_org
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="createBy != null  and createBy != ''">create_by,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="updateBy != null  and updateBy != ''">update_by,</if>
            <if test="updateTime != null ">update_time,</if>
            <if test="isDelete != null ">is_delete,</if>
            <if test="orgName != null  and orgName != ''">org_name,</if>
            <if test="orgCode != null  and orgCode != ''">org_code,</if>
            <if test="account != null  and account != ''">account,</if>
            <if test="userName != null  and userName != ''">user_name,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="createBy != null  and createBy != ''">#{createBy},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null ">#{updateTime},</if>
            <if test="isDelete != null ">#{isDelete},</if>
            <if test="orgName != null  and orgName != ''">#{orgName},</if>
            <if test="orgCode != null  and orgCode != ''">#{orgCode},</if>
            <if test="account != null  and account != ''">#{account},</if>
            <if test="userName != null  and userName != ''">#{userName},</if>
         </trim>
    </insert>

    <update id="updateBpmWxNoticeOrg" parameterType="BpmWxNoticeOrg">
        update bpm_wx_notice_org
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null  and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null ">update_time = #{updateTime},</if>
            <if test="isDelete != null ">is_delete = #{isDelete},</if>
            <if test="orgName != null  and orgName != ''">org_name = #{orgName},</if>
            <if test="orgCode != null  and orgCode != ''">org_code = #{orgCode},</if>
            <if test="account != null  and account != ''">account = #{account},</if>
            <if test="userName != null  and userName != ''">user_name = #{userName},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBpmWxNoticeOrgById" parameterType="Long">
        delete from bpm_wx_notice_org where id = #{id}
    </delete>

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

    <delete id="deleteBpmWxNoticeOrgAll">
        delete from bpm_wx_notice_org
    </delete>
</mapper>