<?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.ruoyi.bst.areaJoin.mapper.AreaJoinMapper">

    <resultMap type="AreaJoinVO" id="AreaJoinResult" autoMapping="true"/>

    <sql id="selectAreaJoinVo">
        select
            baj.id,
            baj.type,
            baj.area_id,
            baj.user_id,
            baj.point,
            baj.create_time,
            baj.remark,
            baj.create_id,
            mch.point as mch_point,
            su.nick_name as user_name,
            su.user_name as user_phone,
            ba.name as area_name,
            suc.nick_name as create_name
        <include refid="searchTables"/>
    </sql>

    <sql id="searchTables">
        from bst_area_join baj
            left join bst_area ba on ba.id = baj.area_id
            left join sys_user su on su.user_id = baj.user_id
            left join sys_user suc on suc.user_id = baj.create_id
            left join sys_user mch on mch.user_id = ba.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and baj.id = #{query.id}</if>
        <if test="query.type != null  and query.type != ''"> and baj.type = #{query.type}</if>
        <if test="query.areaId != null "> and baj.area_id = #{query.areaId}</if>
        <if test="query.userId != null "> and baj.user_id = #{query.userId}</if>
        <if test="query.remark != null  and query.remark != ''"> and baj.remark like concat('%', #{query.remark}, '%')</if>
        <if test="query.createId != null "> and baj.create_id = #{query.createId}</if>
        <if test="query.userName != null and query.userName != ''">and su.nick_name like concat('%', #{query.userName}, '%')</if>
        <if test="query.areaName != null and query.areaName != ''">and ba.name like concat('%', #{query.areaName}, '%')</if>
        <if test="query.userPhone != null and query.userPhone != ''">and su.user_name like concat('%', #{query.userPhone}, '%')</if>
        <if test="query.mchId != null">and ba.user_id = #{query.mchId}</if>
        <if test="query.excludeId != null">and baj.id != #{query.excludeId}</if>
        <if test="query.ids != null and query.ids.size() > 0">
            and baj.id in
            <foreach item="item" collection="query.ids" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ${@com.ruoyi.framework.util.DataScopeUtil@dataScope(
            null,
            "baj.create_id,ba.user_id,mch.agent_id",
            null,
            null,
            query.scope
        )}
        ${query.params.dataScope}
    </sql>

    <select id="selectAreaJoinList" parameterType="AreaJoinQuery" resultMap="AreaJoinResult">
        <include refid="selectAreaJoinVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectAreaJoinById" parameterType="Long" resultMap="AreaJoinResult">
        <include refid="selectAreaJoinVo"/>
        where baj.id = #{id}
    </select>

    <insert id="insertAreaJoin" parameterType="AreaJoin" useGeneratedKeys="true" keyProperty="id">
        insert into bst_area_join
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="type != null and type != ''">type,</if>
            <if test="areaId != null">area_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="point != null">point,</if>
            <if test="createTime != null">create_time,</if>
            <if test="remark != null">remark,</if>
            <if test="createId != null">create_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="type != null and type != ''">#{type},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="point != null">#{point},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createId != null">#{createId},</if>
         </trim>
    </insert>

    <update id="updateAreaJoin" parameterType="AreaJoin">
        update bst_area_join
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.type != null and data.type != ''">type = #{data.type},</if>
        <if test="data.areaId != null">area_id = #{data.areaId},</if>
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.point != null">point = #{data.point},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.remark != null">remark = #{data.remark},</if>
        <if test="data.createId != null">create_id = #{data.createId},</if>
    </sql>

    <delete id="deleteAreaJoinById" parameterType="Long">
        delete from bst_area_join where id = #{id}
    </delete>

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

    <!-- selectIdList -->

    <select id="selectIdList" parameterType="AreaJoinQuery" resultType="Long">
        select baj.id
        <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <!-- selectMaxOfPoint -->

    <select id="selectMaxOfPoint" parameterType="AreaJoinQuery" resultType="BigDecimal">
        select max(baj.point)
        <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectCount" parameterType="AreaJoinQuery" resultType="Integer">
        select count(baj.id)
        <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <!-- selectSumOfPoint --> 

    <select id="selectSumOfPoint" parameterType="AreaJoinQuery" resultType="BigDecimal">
        select sum(baj.point)
        <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>
</mapper>
