<?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.contract.mapper.ExtBusinessTitleMapper">

    <select id="list" resultType="cn.cordys.crm.contract.dto.response.BusinessTitleListResponse">
        SELECT
            bt.*
        FROM
            business_title bt
        WHERE
            bt.organization_id = #{orgId}
        <if test="request.keyword != null and request.keyword != ''">
            and bt.name like concat('%', #{request.keyword},'%')
        </if>

        <include refid="filters">
            <property name="filters" value="request.filters"/>
        </include>

        <include refid="combine">
            <property name="combineSearch" value="request.combineSearch"/>
        </include>
        <include refid="sort">
            <property name="sort" value="request.sort"/>
        </include>

    </select>

    <sql id="filters">
        <if test="${filters} != null and ${filters}.size() > 0">
            <trim prefix="and">
                <foreach collection="${filters}" item="condition" open="(" close=")" index="i" separator="and">
                    <include refid="condition">
                        <property name="condition" value="condition"/>
                    </include>
                </foreach>
            </trim>
        </if>
    </sql>

    <sql id="condition">
        <choose>
            <when test="condition.name == 'name'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.name"/>
                </include>
            </when>
            <when test="condition.name == 'type'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.type"/>
                </include>
            </when>
            <when test="condition.name == 'identificationNumber'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.identification_number"/>
                </include>
            </when>
            <when test="condition.name == 'openingBank'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.opening_bank"/>
                </include>
            </when>
            <when test="condition.name == 'bankAccount'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.bank_account"/>
                </include>
            </when>
            <when test="condition.name == 'registrationAddress'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.registration_address"/>
                </include>
            </when>
            <when test="condition.name == 'phoneNumber'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.phone_number"/>
                </include>
            </when>
            <when test="condition.name == 'registeredCapital'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.registered_capital"/>
                </include>
            </when>
            <when test="condition.name == 'companySize'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.company_size"/>
                </include>
            </when>
            <when test="condition.name == 'registrationNumber'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.registration_number"/>
                </include>
            </when>
            <when test="condition.name == 'approvalStatus'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.approval_status"/>
                </include>
            </when>
            <when test="condition.name == 'unapprovedReason'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.unapproved_reason"/>
                </include>
            </when>
            <when test="condition.name == 'createTime'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.create_time"/>
                </include>
            </when>
            <when test="condition.name == 'updateTime'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.update_time"/>
                </include>
            </when>
            <when test="condition.name == 'updateUser'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.update_user"/>
                </include>
            </when>
            <when test="condition.name == 'createUser'">
                <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                    <property name="condition" value="condition"/>
                    <property name="column" value="bt.create_user"/>
                </include>
            </when>
        </choose>
    </sql>

    <sql id="combine">
        <if test="${combineSearch} != null and ${combineSearch}.conditions != null and ${combineSearch}.conditions.size() > 0">
            <trim prefix="and">
                <trim prefix="(" suffix=")" suffixOverrides="and|or">
                    <foreach collection="${combineSearch}.conditions" index="i" item="condition">
                        <include refid="condition">
                            <property name="condition" value="condition"/>
                        </include>
                        <include refid="cn.cordys.common.mapper.CommonMapper.searchMode">
                            <property name="searchMode" value="${combineSearch}.searchMode"/>
                        </include>
                    </foreach>
                </trim>
            </trim>
        </if>
    </sql>

    <sql id="sort">
        <choose>
            <when test="${sort} != null and ${sort}.valid()">
                <bind name="sortType" value="${sort}.type" />
                <bind name="sortName" value="${sort}.name" />
                order by
                <choose>
                    <when test="${sort}.name in
                        {'name', 'type', 'create_time', 'update_time', 'identification_number', 'opening_bank', 'create_user', 'update_user', 'bank_account', 'registration_address',
                         'phone_number', 'registered_capital', 'company_size', 'registration_number', 'approval_status', 'unapproved_reason'
                        }">
                        bt.${sortName} ${sortType}, bt.id ASC
                    </when>
                    <otherwise>
                        order by bt.create_time desc, bt.id asc
                    </otherwise>
                </choose>
            </when>
            <otherwise>
                order by bt.create_time desc, bt.id asc
            </otherwise>
        </choose>
    </sql>

    <select id="getListByIds" resultType="cn.cordys.crm.contract.dto.response.BusinessTitleListResponse">
        SELECT
        bt.*
        FROM
        business_title bt
        where bt.organization_id = #{orgId}
        and bt.id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>


    <select id="countByName" resultType="java.lang.Integer">
        select count(id) from business_title bt
        where bt.organization_id = #{orgId}
        and bt.name = #{businessName}
        <if test="id != null and id != ''">
            AND bt.id != #{id}
        </if>
    </select>

</mapper>