<?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.customer.mapper.CustomerElectronicCardMapper">

    <resultMap type="CustomerElectronicCard" id="CustomerElectronicCardResult">
        <result property="id"    column="id"    />
        <result property="unionId"    column="union_id"    />
        <result property="customerNo"    column="customer_no"    />
        <result property="customerName"    column="customer_name"    />
        <result property="mobilePhone"    column="mobile_phone"    />
        <result property="amt"    column="amt"    />
        <result property="cardOilsType"    column="card_oils_type"    />
        <result property="stationId"    column="station_id"    />
        <result property="stationName"    column="station_name"    />
        <result property="createTime"    column="create_time"    />
        <result property="recentlyTime"    column="recently_time"    />
        <result property="num"    column="num" />
        <result property="czamt"    column="czamt"    />
        <result property="xfamt"    column="xfamt"    />
        <result property="cardnum"    column="cardnum"    />
        <result property="cumamt"    column="cumamt"    />
        <result property="cumnum"    column="cumnum"    />
    </resultMap>

    <sql id="selectCustomerElectronicCardVo">
        select c.id, c.union_id,c.customer_no,c.customer_name, c.mobile_phone, c.amt, c.card_oils_type,
         c.station_id,d.dept_name as station_name, c.create_time, c.recently_time
         from customer_electronic_card c join  sys_dept d on c.station_id =d.dept_id
    </sql>

    <select id="selectCustomerElectronicCardList" parameterType="CustomerElectronicCard" resultMap="CustomerElectronicCardResult">
        <include refid="selectCustomerElectronicCardVo"/>
        <where>
            <if test="unionId != null  and unionId != ''"> and union_id = #{unionId}</if>
            <if test="customerNo != null  and customerNo != ''"> and customer_no = #{customerNo}</if>
            <if test="customerName != null  and customerName != ''"> and customer_name like concat('%', #{customerName}, '%')</if>
            <if test="mobilePhone != null  and mobilePhone != ''"> and mobile_phone = #{mobilePhone}</if>
            <if test="amt != null "> and amt = #{amt}</if>
            <if test="cardOilsType != null  and cardOilsType != ''"> and card_oils_type = #{cardOilsType}</if>
            <if test="stationId != null "> and station_id = #{stationId}</if>
            <if test="recentlyTime != null "> and recently_time = #{recentlyTime}</if>
            <if test="stationIdList != null ">
                and station_id in
                <foreach item="item" index="index" collection="stationIdList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectCustomerElectronicCardById" parameterType="Long" resultMap="CustomerElectronicCardResult">
        <include refid="selectCustomerElectronicCardVo"/>
        where id = #{id}
    </select>

    <insert id="insertCustomerElectronicCard" parameterType="CustomerElectronicCard" useGeneratedKeys="true" keyProperty="id">
        insert into customer_electronic_card
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="unionId != null">union_id,</if>
            <if test="customerNo != null">customer_no,</if>
            <if test="customerName != null">customer_name,</if>
            <if test="mobilePhone != null">mobile_phone,</if>
            <if test="amt != null">amt,</if>
            <if test="cardOilsType != null">card_oils_type,</if>
            <if test="stationId != null">station_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="recentlyTime != null">recently_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="unionId != null">#{unionId},</if>
            <if test="customerNo != null">#{customerNo},</if>
            <if test="customerName != null">#{customerName},</if>
            <if test="mobilePhone != null">#{mobilePhone},</if>
            <if test="amt != null">#{amt},</if>
            <if test="cardOilsType != null">#{cardOilsType},</if>
            <if test="stationId != null">#{stationId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="recentlyTime != null">#{recentlyTime},</if>
         </trim>
    </insert>

    <update id="updateCustomerElectronicCard" parameterType="CustomerElectronicCard">
        update customer_electronic_card
        <trim prefix="SET" suffixOverrides=",">
            <if test="unionId != null">union_id = #{unionId},</if>
            <if test="customerNo != null">customer_no = #{customerNo},</if>
            <if test="customerName != null">customer_name = #{customerName},</if>
            <if test="mobilePhone != null">mobile_phone = #{mobilePhone},</if>
            <if test="amt != null">amt = #{amt},</if>
            <if test="cardOilsType != null">card_oils_type = #{cardOilsType},</if>
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="recentlyTime != null">recently_time = #{recentlyTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCustomerElectronicCardById" parameterType="Long">
        delete from customer_electronic_card where id = #{id}
    </delete>

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

    <select id="listSum" parameterType="CustomerElectronicCard" resultMap="CustomerElectronicCardResult">
        SELECT count(DISTINCT union_id) as num,sum(amt) as amt
        from customer_electronic_card
        <where>
            <if test="unionId != null  and unionId != ''"> and union_id = #{unionId}</if>
            <if test="customerNo != null  and customerNo != ''"> and customer_no = #{customerNo}</if>
            <if test="customerName != null  and customerName != ''"> and customer_name like concat('%', #{customerName}, '%')</if>
            <if test="mobilePhone != null  and mobilePhone != ''"> and mobile_phone = #{mobilePhone}</if>
            <if test="amt != null "> and amt = #{amt}</if>
            <if test="cardOilsType != null  and cardOilsType != ''"> and card_oils_type = #{cardOilsType}</if>
            <if test="stationId != null "> and station_id = #{stationId}</if>
            <if test="recentlyTime != null "> and recently_time = #{recentlyTime}</if>
            <if test="stationIdList != null ">
                and station_id in
                <foreach item="item" index="index" collection="stationIdList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="listHYData" parameterType="CustomerElectronicCard" resultMap="CustomerElectronicCardResult">
        SELECT  sum(amt) as amt,count(DISTINCT union_id ) as num
        from customer_electronic_card
        <where>
            <if test="unionId != null  and unionId != ''"> and union_id = #{unionId}</if>
            <if test="customerNo != null  and customerNo != ''"> and customer_no = #{customerNo}</if>
            <if test="customerName != null  and customerName != ''"> and customer_name like concat('%', #{customerName}, '%')</if>
            <if test="mobilePhone != null  and mobilePhone != ''"> and mobile_phone = #{mobilePhone}</if>
            <if test="amt != null "> and amt = #{amt}</if>
            <if test="cardOilsType != null  and cardOilsType != ''"> and card_oils_type = #{cardOilsType}</if>
            <if test="stationId != null "> and station_id = #{stationId}</if>
            <if test="recentlyTime != null "> and recently_time = #{recentlyTime}</if>
            <if test="stationIdList != null ">
                and station_id in
                <foreach item="item" index="index" collection="stationIdList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="listJRHYData" parameterType="CustomerElectronicCard" resultMap="CustomerElectronicCardResult">
        SELECT count(DISTINCT union_id ) as num from customer_electronic_card
        <where>
            <if test="unionId != null  and unionId != ''"> and union_id = #{unionId}</if>
            <if test="customerNo != null  and customerNo != ''"> and customer_no = #{customerNo}</if>
            <if test="customerName != null  and customerName != ''"> and customer_name like concat('%', #{customerName}, '%')</if>
            <if test="mobilePhone != null  and mobilePhone != ''"> and mobile_phone = #{mobilePhone}</if>
            <if test="amt != null "> and amt = #{amt}</if>
            <if test="cardOilsType != null  and cardOilsType != ''"> and card_oils_type = #{cardOilsType}</if>
            <if test="stationId != null "> and station_id = #{stationId}</if>
           <if test="recentlyTime != null "> and recently_time = #{recentlyTime}</if>
            <if test="stationIdList != null ">
                and station_id in
                <foreach item="item" index="index" collection="stationIdList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="beginTime != null and beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(create_time,'%Y-%m-%d %H:%i:%s') &gt;= date_format(#{beginTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
                AND date_format(create_time,'%Y-%m-%d %H:%i:%s') &lt;= date_format(#{endTime},'%Y-%m-%d %H:%i:%s')
            </if>
        </where>
    </select>
     <!--存储过程
       CALL selectCard();
             CALL selectCard(#{pageNo,mode=IN},#{pageSetting,mode=IN});-->
    <select id="selectCard" parameterType="CustomerElectronicCard" statementType="CALLABLE" resultMap="CustomerElectronicCardResult">
            CALL selectCard(#{strSql,mode=IN},#{pageNo,mode=IN},#{pageSetting,mode=IN});
    </select>
    <!--存储过程-->
    <select id="selectCardPage" parameterType="CustomerElectronicCard" statementType="CALLABLE" resultMap="CustomerElectronicCardResult">
          CALL selectCardPage(#{strSql,mode=IN});
    </select>
</mapper>
