<?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="cn.cordys.crm.system.mapper.ExtOrganizationConfigDetailMapper">

    <select id="getOrganizationConfigDetail" parameterType="cn.cordys.crm.system.domain.OrganizationConfigDetail" resultType="cn.cordys.crm.system.domain.OrganizationConfigDetail">
        SELECT * FROM sys_organization_config_detail WHERE config_id = #{configId} limit 1;
    </select>

    <select id="getOrganizationConfigDetails" parameterType="cn.cordys.crm.system.domain.OrganizationConfigDetail" resultType="cn.cordys.crm.system.domain.OrganizationConfigDetail">
        SELECT * FROM sys_organization_config_detail WHERE config_id = #{configId}
        <if test="name != null">
            and name like concat('%', #{name},'%')
        </if>
    </select>

    <select id="getOrganizationConfigDetailCount" parameterType="cn.cordys.crm.system.domain.OrganizationConfigDetail" resultType="java.lang.Integer">
        SELECT count(id) FROM sys_organization_config_detail WHERE config_id = #{configId}
        <if test="name != null">
            and name like concat('%', #{name},'%')
        </if>
        <if test="type != null">
            and type = #{type}
        </if>
    </select>

    <select id="getOrgConfigDetailByType" parameterType="cn.cordys.crm.system.domain.OrganizationConfigDetail" resultType="cn.cordys.crm.system.domain.OrganizationConfigDetail">
        SELECT * FROM sys_organization_config_detail WHERE config_id = #{configId}
        <if test="name != null">
            and name like concat('%', #{name},'%')
        </if>
        and type in
        <foreach collection="types" item="type" open="(" close=")" separator=",">
            #{type}
        </foreach>
    </select>

    <select id="getRepeatDetails" parameterType="cn.cordys.crm.system.domain.OrganizationConfigDetail" resultType="java.lang.Integer">
        SELECT count(id)
        FROM sys_organization_config_detail
        WHERE sys_organization_config_detail.name = #{name}
        <if test="id != null">
            and id != #{id}
        </if>
    </select>

    <update id="updateStatus" parameterType="cn.cordys.crm.system.domain.OrganizationConfigDetail">
        UPDATE sys_organization_config_detail SET enable = #{enable} WHERE type = #{type}
        AND config_id = #{configId}
    </update>

    <select id="getEnableOrganizationConfigDetails" parameterType="cn.cordys.crm.system.domain.OrganizationConfigDetail" resultType="cn.cordys.crm.system.domain.OrganizationConfigDetail">
        SELECT * FROM sys_organization_config_detail WHERE config_id = #{configId} and enable = true
        <if test="types != null and types.size() > 0">
            and type in
            <foreach collection="types" item="type" open="(" close=")" separator=",">
                #{type}
            </foreach>
        </if>

    </select>
    <select id="getOrganizationConfigDetailList"
            parameterType="cn.cordys.crm.system.dto.request.AuthSourceRequest"
            resultType="cn.cordys.crm.system.domain.OrganizationConfigDetail">
        SELECT * FROM sys_organization_config_detail WHERE config_id = #{request.configId}
        <if test="request.keyword != null">
            and name like concat('%', #{request.keyword},'%')
        </if>
        <include refid="cn.cordys.common.mapper.CommonMapper.sort">
            <property name="sort" value="request.sort"/>
            <property name="prefix" value="sys_organization_config_detail"/>
            <property name="defaultSort" value="create_time desc"/>
        </include>
    </select>
    <select id="getOrgConfigDetailByIdAndType"
            resultType="cn.cordys.crm.system.domain.OrganizationConfigDetail">
        SELECT * FROM sys_organization_config_detail WHERE config_id = #{configId} and type = #{type} and enable = true
    </select>

</mapper>