<?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.yijia.integral.mapper.CustomerPointsMapper">

    <resultMap type="CustomerPoints" id="CustomerPointsResult">
        <result property="id"    column="id"    />
        <result property="unionId"    column="union_id"    />
        <result property="mobilePhone"    column="mobile_phone"    />
        <result property="points"    column="points"    />
        <result property="consumptionPoints"    column="consumption_points"    />
        <result property="accumulatePoints"    column="accumulate_points"    />
        <result property="invalidPoints"    column="invalid_points"    />
        <result property="recentConsumptionDate"    column="recent_consumption_date"    />
        <result property="stationId"    column="station_id"    />
        <result property="stationName"    column="station_name"    />
        <result property="customerName"    column="customer_name"    />
    </resultMap>

    <sql id="selectCustomerPointsVo">
        select  p.id,p.union_id,p.mobile_phone,p.points,p.customer_name, p.consumption_points,
        p.accumulate_points, p.invalid_points,  p.recent_consumption_date,p.station_id,
        d.dept_name  as station_name
        from customer_points p join sys_dept d on p.station_id = d.dept_id
    </sql>

    <select id="selectCustomerPointsList" parameterType="CustomerPoints" resultMap="CustomerPointsResult">
        <include refid="selectCustomerPointsVo"/>
        <where>
            <if test="unionId != null  and unionId != ''"> and union_id = #{unionId}</if>
            <if test="mobilePhone != null  and mobilePhone != ''"> and mobile_phone like concat('%', #{mobilePhone}, '%')</if>
             and points  != 0
            <if test="consumptionPoints != null"> and consumption_points = #{consumptionPoints}</if>
            <if test="accumulatePoints != null "> and accumulate_points = #{accumulatePoints}</if>
            <if test="invalidPoints != null "> and invalid_points = #{invalidPoints}</if>
            <if test="recentConsumptionDate != null "> and recent_consumption_date = #{recentConsumptionDate}</if>
            <if test="stationId != null "> and station_id = #{stationId}</if>
            <if test="customerName != null and customerName != ''"> and customer_name like concat('%', #{customerName}, '%')</if>
            <if test="stationIdList != null ">
                and station_id in
                <foreach item="item" index="index" collection="stationIdList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by  recent_consumption_date desc
    </select>

    <select id="selectCustomerPointsById" parameterType="Long" resultMap="CustomerPointsResult">
        <include refid="selectCustomerPointsVo"/>
        where id = #{id}
    </select>

    <insert id="insertCustomerPoints" parameterType="CustomerPoints" useGeneratedKeys="true" keyProperty="id">
        insert into customer_points
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="unionId != null">union_id,</if>
            <if test="mobilePhone != null">mobile_phone,</if>
            <if test="points != null">points,</if>
            <if test="consumptionPoints != null">consumption_points,</if>
            <if test="accumulatePoints != null">accumulate_points,</if>
            <if test="invalidPoints != null">invalid_points,</if>
            <if test="recentConsumptionDate != null">recent_consumption_date,</if>
            <if test="stationId != null">station_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="unionId != null">#{unionId},</if>
            <if test="mobilePhone != null">#{mobilePhone},</if>
            <if test="points != null">#{points},</if>
            <if test="consumptionPoints != null">#{consumptionPoints},</if>
            <if test="accumulatePoints != null">#{accumulatePoints},</if>
            <if test="invalidPoints != null">#{invalidPoints},</if>
            <if test="recentConsumptionDate != null">#{recentConsumptionDate},</if>
            <if test="stationId != null">#{stationId},</if>
         </trim>
    </insert>

    <update id="updateCustomerPoints" parameterType="CustomerPoints">
        update customer_points
        <trim prefix="SET" suffixOverrides=",">
            <if test="unionId != null">union_id = #{unionId},</if>
            <if test="mobilePhone != null">mobile_phone = #{mobilePhone},</if>
            <if test="points != null">points = #{points},</if>
            <if test="consumptionPoints != null">consumption_points = #{consumptionPoints},</if>
            <if test="accumulatePoints != null">accumulate_points = #{accumulatePoints},</if>
            <if test="invalidPoints != null">invalid_points = #{invalidPoints},</if>
            <if test="recentConsumptionDate != null">recent_consumption_date = #{recentConsumptionDate},</if>
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="stationName != null">station_name = #{stationName},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCustomerPointsById" parameterType="Long">
        delete from customer_points where id = #{id}
    </delete>

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

</mapper>
