<?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.foreverwin.mesnac.meapi.mapper.WorkCenterMemberMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.foreverwin.mesnac.meapi.model.WorkCenterMember">
        <result column="HANDLE" property="handle" />
        <result column="WORK_CENTER_BO" property="workCenterBo" />
        <result column="WORK_CENTER_OR_RESOURCE_GBO" property="workCenterOrResourceGbo" />
        <result column="PRIMARY_WORK_CENTER" property="primaryWorkCenter" />
        <result column="SEQUENCE" property="sequence" />
        <result column="DEFAULT_MEMBER" property="defaultMember" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        HANDLE, WORK_CENTER_BO, WORK_CENTER_OR_RESOURCE_GBO, PRIMARY_WORK_CENTER, SEQUENCE, DEFAULT_MEMBER
    </sql>

    <!-- BaseMapper标准查询/修改/删除 -->

    <select id="selectByMap" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include>
        FROM WORK_CENTER_MEMBER
        <if test="cm!=null and !cm.isEmpty">
            <where>
                <foreach collection="cm.keys" item="k" separator="AND">
                    <if test="cm[k] != null">
                        ${k} = #{cm[${k}]}
                    </if>
                </foreach>
            </where>
        </if>
    </select>


    <select id="selectOne" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include> FROM WORK_CENTER_MEMBER
        <where>
            <if test="ew.entity.handle!=null">
              HANDLE=#{ew.handle}
            </if>
            <if test="ew.entity.workCenterBo!=null"> AND WORK_CENTER_BO=#{ew.entity.workCenterBo}</if>
            <if test="ew.entity.workCenterOrResourceGbo!=null"> AND WORK_CENTER_OR_RESOURCE_GBO=#{ew.entity.workCenterOrResourceGbo}</if>
            <if test="ew.entity.primaryWorkCenter!=null"> AND PRIMARY_WORK_CENTER=#{ew.entity.primaryWorkCenter}</if>
            <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
            <if test="ew.entity.defaultMember!=null"> AND DEFAULT_MEMBER=#{ew.entity.defaultMember}</if>
        </where>
    </select>

    <select id="selectCount" resultType="Integer">
        SELECT COUNT(1) FROM WORK_CENTER_MEMBER
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                <if test="ew.entity.handle!=null">
                    HANDLE=#{ew.entity.handle}
                </if>
                <if test="ew.entity.workCenterBo!=null"> AND WORK_CENTER_BO=#{ew.entity.workCenterBo}</if>
                <if test="ew.entity.workCenterOrResourceGbo!=null"> AND WORK_CENTER_OR_RESOURCE_GBO=#{ew.entity.workCenterOrResourceGbo}</if>
                <if test="ew.entity.primaryWorkCenter!=null"> AND PRIMARY_WORK_CENTER=#{ew.entity.primaryWorkCenter}</if>
                <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                <if test="ew.entity.defaultMember!=null"> AND DEFAULT_MEMBER=#{ew.entity.defaultMember}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectList" resultMap="BaseResultMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM WORK_CENTER_MEMBER
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.workCenterBo!=null"> AND WORK_CENTER_BO=#{ew.entity.workCenterBo}</if>
                    <if test="ew.entity.workCenterOrResourceGbo!=null"> AND WORK_CENTER_OR_RESOURCE_GBO=#{ew.entity.workCenterOrResourceGbo}</if>
                    <if test="ew.entity.primaryWorkCenter!=null"> AND PRIMARY_WORK_CENTER=#{ew.entity.primaryWorkCenter}</if>
                    <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                    <if test="ew.entity.defaultMember!=null"> AND DEFAULT_MEMBER=#{ew.entity.defaultMember}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectMaps" resultType="HashMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM WORK_CENTER_MEMBER
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.workCenterBo!=null"> AND WORK_CENTER_BO=#{ew.entity.workCenterBo}</if>
                    <if test="ew.entity.workCenterOrResourceGbo!=null"> AND WORK_CENTER_OR_RESOURCE_GBO=#{ew.entity.workCenterOrResourceGbo}</if>
                    <if test="ew.entity.primaryWorkCenter!=null"> AND PRIMARY_WORK_CENTER=#{ew.entity.primaryWorkCenter}</if>
                    <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                    <if test="ew.entity.defaultMember!=null"> AND DEFAULT_MEMBER=#{ew.entity.defaultMember}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectObjs" resultType="Object">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM WORK_CENTER_MEMBER
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.workCenterBo!=null"> AND WORK_CENTER_BO=#{ew.entity.workCenterBo}</if>
                    <if test="ew.entity.workCenterOrResourceGbo!=null"> AND WORK_CENTER_OR_RESOURCE_GBO=#{ew.entity.workCenterOrResourceGbo}</if>
                    <if test="ew.entity.primaryWorkCenter!=null"> AND PRIMARY_WORK_CENTER=#{ew.entity.primaryWorkCenter}</if>
                    <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                    <if test="ew.entity.defaultMember!=null"> AND DEFAULT_MEMBER=#{ew.entity.defaultMember}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectPage" resultMap="BaseResultMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM WORK_CENTER_MEMBER
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.workCenterBo!=null"> AND WORK_CENTER_BO=#{ew.entity.workCenterBo}</if>
                    <if test="ew.entity.workCenterOrResourceGbo!=null"> AND WORK_CENTER_OR_RESOURCE_GBO=#{ew.entity.workCenterOrResourceGbo}</if>
                    <if test="ew.entity.primaryWorkCenter!=null"> AND PRIMARY_WORK_CENTER=#{ew.entity.primaryWorkCenter}</if>
                    <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                    <if test="ew.entity.defaultMember!=null"> AND DEFAULT_MEMBER=#{ew.entity.defaultMember}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectMapsPage" resultType="HashMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM WORK_CENTER_MEMBER
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.workCenterBo!=null"> AND WORK_CENTER_BO=#{ew.entity.workCenterBo}</if>
                    <if test="ew.entity.workCenterOrResourceGbo!=null"> AND WORK_CENTER_OR_RESOURCE_GBO=#{ew.entity.workCenterOrResourceGbo}</if>
                    <if test="ew.entity.primaryWorkCenter!=null"> AND PRIMARY_WORK_CENTER=#{ew.entity.primaryWorkCenter}</if>
                    <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                    <if test="ew.entity.defaultMember!=null"> AND DEFAULT_MEMBER=#{ew.entity.defaultMember}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <insert id="insert" parameterType="com.foreverwin.mesnac.meapi.model.WorkCenterMember">
        INSERT INTO WORK_CENTER_MEMBER
        <trim prefix="(" suffix=")" suffixOverrides=",">
            HANDLE,
            <if test="workCenterBo!=null">WORK_CENTER_BO,</if>
            <if test="workCenterOrResourceGbo!=null">WORK_CENTER_OR_RESOURCE_GBO,</if>
            <if test="primaryWorkCenter!=null">PRIMARY_WORK_CENTER,</if>
            <if test="sequence!=null">SEQUENCE,</if>
            <if test="defaultMember!=null">DEFAULT_MEMBER,</if>
        </trim> VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            #{handle},
            <if test="workCenterBo!=null">#{workCenterBo},</if>
            <if test="workCenterOrResourceGbo!=null">#{workCenterOrResourceGbo},</if>
            <if test="primaryWorkCenter!=null">#{primaryWorkCenter},</if>
            <if test="sequence!=null">#{sequence},</if>
            <if test="defaultMember!=null">#{defaultMember},</if>
        </trim>
    </insert>

    <insert id="insertAllColumn" parameterType="com.foreverwin.mesnac.meapi.model.WorkCenterMember">
        INSERT INTO WORK_CENTER_MEMBER
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <include refid="Base_Column_List"></include>
        </trim> VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            #{handle},
            #{workCenterBo},
            #{workCenterOrResourceGbo},
            #{primaryWorkCenter},
            #{sequence},
            #{defaultMember},
        </trim>
    </insert>






    <update id="update">
        UPDATE WORK_CENTER_MEMBER <trim prefix="SET" suffixOverrides=",">
        <if test="et.handle!=null">HANDLE=#{et.handle},</if>
        <if test="et.workCenterBo!=null">WORK_CENTER_BO=#{et.workCenterBo},</if>
        <if test="et.workCenterOrResourceGbo!=null">WORK_CENTER_OR_RESOURCE_GBO=#{et.workCenterOrResourceGbo},</if>
        <if test="et.primaryWorkCenter!=null">PRIMARY_WORK_CENTER=#{et.primaryWorkCenter},</if>
        <if test="et.sequence!=null">SEQUENCE=#{et.sequence},</if>
        <if test="et.defaultMember!=null">DEFAULT_MEMBER=#{et.defaultMember},</if>
    </trim>
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    HANDLE=#{ew.entity.handle}
                    <if test="ew.entity.workCenterBo!=null"> AND WORK_CENTER_BO=#{ew.entity.workCenterBo}</if>
                    <if test="ew.entity.workCenterOrResourceGbo!=null"> AND WORK_CENTER_OR_RESOURCE_GBO=#{ew.entity.workCenterOrResourceGbo}</if>
                    <if test="ew.entity.primaryWorkCenter!=null"> AND PRIMARY_WORK_CENTER=#{ew.entity.primaryWorkCenter}</if>
                    <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                    <if test="ew.entity.defaultMember!=null"> AND DEFAULT_MEMBER=#{ew.entity.defaultMember}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </update>


    <delete id="deleteByMap">
        DELETE FROM WORK_CENTER_MEMBER
        <if test="cm!=null and !cm.isEmpty">
            <where>
                <foreach collection="cm.keys" item="k" separator="AND">
                    <if test="cm[k] != null">
                        ${k} = #{cm[${k}]}
                    </if>
                </foreach>
            </where>
        </if>
    </delete>

    <delete id="delete">
        DELETE FROM WORK_CENTER_MEMBER
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.workCenterBo!=null"> AND WORK_CENTER_BO=#{ew.entity.workCenterBo}</if>
                    <if test="ew.entity.workCenterOrResourceGbo!=null"> AND WORK_CENTER_OR_RESOURCE_GBO=#{ew.entity.workCenterOrResourceGbo}</if>
                    <if test="ew.entity.primaryWorkCenter!=null"> AND PRIMARY_WORK_CENTER=#{ew.entity.primaryWorkCenter}</if>
                    <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                    <if test="ew.entity.defaultMember!=null"> AND DEFAULT_MEMBER=#{ew.entity.defaultMember}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </delete>

    <!-- BaseMapper标准查询/修改/删除 -->

    <select id="selectMaxSeq" resultType="Integer">
        SELECT MAX("SEQUENCE")
        FROM WORK_CENTER_MEMBER
        WHERE WORK_CENTER_BO = #{workCenterBo}
    </select>

    <select id="listWorkCenterLink" resultType="com.foreverwin.mesnac.meapi.dto.WorkCenterLinkDTO">
        SELECT WC.HANDLE, WC.SITE, WC.WORK_CENTER, WC.ROUTER_BO, WC.CAN_BE_RELEASED_TO, WC.WC_CATEGORY, WC.STATUS_BO, WC.WC_TYPE, WC.ASSIGNMENT_ENFORCEMENT, WCM.WORK_CENTER_BO AS PARENT_BO, WCP.WORK_CENTER AS PARENT
        FROM WORK_CENTER WC
        LEFT JOIN WORK_CENTER_MEMBER WCM ON WCM.WORK_CENTER_OR_RESOURCE_GBO = WC.HANDLE AND WCM.PRIMARY_WORK_CENTER = 'true'
        LEFT JOIN WORK_CENTER WCP
          ON WCP.HANDLE = WCM.WORK_CENTER_BO
        WHERE WC.SITE = #{site}
    </select>
</mapper>
