<?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.jsowell.pile.mapper.PileMerchantInfoMapper">
    <resultMap type="com.jsowell.pile.domain.PileMerchantInfo" id="PileMerchantInfoResult">
        <result property="id" column="id"/>
        <result property="merchantName" column="merchant_name"/>
        <result property="merchantLevel" column="merchant_level"/>
        <result property="parentId" column="parent_id"/>
        <result property="address" column="address"/>
        <result property="status" column="status"/>
        <result property="delayMode" column="delay_mode"/>
        <result property="organizationCode" column="organization_code"/>
        <result property="managerName" column="manager_name"/>
        <result property="managerPhone" column="manager_phone"/>
        <result property="servicePhone" column="service_phone"/>
        <result property="logoUrl" column="logo_url"/>
        <result property="appId" column="app_id"/>
        <result property="deptId" column="dept_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="delFlag" column="del_flag"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,
        merchant_name,
        merchant_level,
        parent_id,
        address,
        status,
        delay_mode,
        organization_code,
        manager_name,
        manager_phone,
        service_phone,
        logo_url,
        app_id,
        dept_id,
        create_by,
        create_time,
        update_by,
        update_time,
        del_flag
    </sql>

    <sql id="selectPileMerchantInfoVo">
        select
        <include refid="Base_Column_List"/>
        from pile_merchant_info t
    </sql>

    <select id="selectPileMerchantInfoList" parameterType="com.jsowell.pile.domain.PileMerchantInfo"
            resultMap="PileMerchantInfoResult">
        <include refid="selectPileMerchantInfoVo"/>
        <where>
            <if test="merchantName != null  and merchantName != ''">
                and merchant_name like concat('%', #{merchantName}, '%')
            </if>
            <if test="address != null  and address != ''">
                and address = #{address}
            </if>
            <if test="status != null  and status != ''">
                and status = #{status}
            </if>
            <if test="delayMode != null and delayMode != ''">
                and delay_mode = #{delayMode}
            </if>
            <if test="organizationCode != null  and organizationCode != ''">
                and organization_code = #{organizationCode}
            </if>
            <if test="managerName != null  and managerName != ''">
                and manager_name like concat('%', #{managerName}, '%')
            </if>
            <if test="managerPhone != null  and managerPhone != ''">
                and manager_phone = #{managerPhone}
            </if>
            <if test="servicePhone != null  and servicePhone != ''">
                and service_phone = #{servicePhone}
            </if>
            <if test="logoUrl != null  and logoUrl != ''">
                and logo_url = #{logoUrl}
            </if>
            <!-- 数据范围过滤 -->
            <if test="merchantDeptIds != null and merchantDeptIds.size() != 0">
                and dept_id in
                <foreach collection="merchantDeptIds" item="merchantDeptId" open="(" separator="," close=")">
                    #{merchantDeptId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectPileMerchantInfoById" parameterType="Long" resultMap="PileMerchantInfoResult">
        <include refid="selectPileMerchantInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertPileMerchantInfo" parameterType="com.jsowell.pile.domain.PileMerchantInfo">
        insert into pile_merchant_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="merchantName != null">
                merchant_name,
            </if>
            <if test="merchantLevel != null">
                merchant_level,
            </if>
            <if test="parentId != null">
                parent_id,
            </if>
            <if test="address != null">
                address,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="delayMode != null">
                delay_mode,
            </if>
            <if test="organizationCode != null">
                organization_code,
            </if>
            <if test="managerName != null">
                manager_name,
            </if>
            <if test="managerPhone != null">
                manager_phone,
            </if>
            <if test="servicePhone != null">
                service_phone,
            </if>
            <if test="logoUrl != null">
                logo_url,
            </if>
            <if test="appId != null">
                app_id,
            </if>
            <if test="deptId != null">
                dept_id,
            </if>
            <if test="createBy != null">
                create_by,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateBy != null">
                update_by,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="delFlag != null">
                del_flag,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="merchantName != null">
                #{merchantName},
            </if>
            <if test="merchantLevel != null">
                #{merchantLevel},
            </if>
            <if test="parentId != null">
                #{parentId},
            </if>
            <if test="address != null">
                #{address},
            </if>
            <if test="status != null">
                #{status},
            </if>
            <if test="delayMode != null">
                #{delayMode},
            </if>
            <if test="organizationCode != null">
                #{organizationCode},
            </if>
            <if test="managerName != null">
                #{managerName},
            </if>
            <if test="managerPhone != null">
                #{managerPhone},
            </if>
            <if test="servicePhone != null">
                #{servicePhone},
            </if>
            <if test="logoUrl != null">
                #{logoUrl},
            </if>
            <if test="appId != null">
                #{appId},
            </if>
            <if test="deptId != null">
                #{deptId},
            </if>
            <if test="createBy != null">
                #{createBy},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateBy != null">
                #{updateBy},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="delFlag != null">
                #{delFlag},
            </if>
        </trim>
    </insert>

    <update id="updatePileMerchantInfo" parameterType="com.jsowell.pile.domain.PileMerchantInfo">
        update pile_merchant_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="merchantName != null">
                merchant_name = #{merchantName},
            </if>
            <if test="merchantLevel != null">
                merchant_level = #{merchantLevel},
            </if>
            <if test="parentId != null">
                parent_id = #{parentId},
            </if>
            <if test="address != null">
                address = #{address},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="delayMode != null">
                delay_mode = #{delayMode},
            </if>
            <if test="organizationCode != null">
                organization_code = #{organizationCode},
            </if>
            <if test="managerName != null">
                manager_name = #{managerName},
            </if>
            <if test="managerPhone != null">
                manager_phone = #{managerPhone},
            </if>
            <if test="servicePhone != null">
                service_phone = #{servicePhone},
            </if>
            <if test="logoUrl != null">
                logo_url = #{logoUrl},
            </if>
            <if test="appId != null">
                app_id = #{appId},
            </if>
            <if test="createBy != null">
                create_by = #{createBy},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePileMerchantInfoById" parameterType="Long">
        delete
        from pile_merchant_info
        where id = #{id}
    </delete>

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

    <select id="selectPileMerchantInfoByAppId" resultMap="PileMerchantInfoResult">
        select
        <include refid="Base_Column_List"/>
        from pile_merchant_info
        where
        app_id = #{appId,jdbcType=VARCHAR}
    </select>

    <select id="queryByMerchantDeptIds" resultType="java.lang.String">
        SELECT
            t2.id
        FROM
            pile_merchant_info t1
                LEFT JOIN pile_station_info t2 ON t2.merchant_id = t1.id and t2.del_flag = '0'
        WHERE
            t1.del_flag = '0'
        <if test="merchantDeptIds != null and merchantDeptIds.size() != 0">
          AND t1.dept_id IN
        <foreach collection="merchantDeptIds" item="item" open="(" separator="," close=")">
            #{item,jdbcType=VARCHAR}
        </foreach>
        </if>
    </select>

    <select id="queryInfoByDeptId" resultMap="PileMerchantInfoResult">
        select
        <include refid="Base_Column_List"/>
        from pile_merchant_info
        where
        dept_id = #{deptId,jdbcType=VARCHAR}
    </select>

    <select id="queryInfoListByIds" resultMap="PileMerchantInfoResult">
        select
            <include refid="Base_Column_List"/>
        from
            pile_merchant_info
        where del_flag = '0'
        <if test="deptIds != null and deptIds.size() != 0">
            and dept_id in
            <foreach collection="deptIds" item="deptId" open="(" separator="," close=")">
                #{deptId,jdbcType=VARCHAR}
            </foreach>
        </if>
    </select>

    <select id="queryFirstLevelMerchantList" resultMap="PileMerchantInfoResult">
        select
        <include refid="Base_Column_List"/>
        from
        pile_merchant_info
        where del_flag = '0'
        and merchant_level = '1'
    </select>

    <select id="getFirstLevelMerchantByAppId" resultMap="PileMerchantInfoResult">
        select
            <include refid="Base_Column_List"/>
        from
            pile_merchant_info
        where del_flag = '0'
        and merchant_level = '1'
        and app_id = #{appId,jdbcType=VARCHAR}
    </select>

    <select id="getDeptIdsByAppId" resultType="java.lang.String">
        select
            dept_id as deptId
        from
            pile_merchant_info
        where 1 = 1
        <if test="appId != null and appId != ''">
            and app_id = #{appId,jdbcType=VARCHAR}
        </if>
    </select>
</mapper>