<?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.IntegralOrderMapper">

    <resultMap type="IntegralOrder" id="IntegralOrderResult">
        <result property="id"    column="id"    />
        <result property="integralOrderNo"    column="integral_order_no"    />
        <result property="waresName"    column="wares_name"    />
        <result property="waresPic"    column="wares_pic"    />
        <result property="waresDetail"    column="wares_detail"    />
        <result property="waresId"    column="wares_id"    />
        <result property="unionId"    column="union_id"    />
        <result property="customerName"    column="customer_name"    />
        <result property="exchangeNum"    column="exchange_num"    />
        <result property="exchangeTime"    column="exchange_time"    />
        <result property="status"    column="status"    />
        <result property="integral"    column="integral"    />
        <result property="stationId"    column="station_id"    />
        <result property="stationName"    column="station_name"    />
        <result property="mobilePhone"    column="mobile_phone"    />

    </resultMap>

    <sql id="selectIntegralOrderVo">
        select p.id, p.integral_order_no, w.wares_name, p.wares_id, p.union_id, p.customer_name, w.wares_pic,w.wares_detail,p.exchange_num,p.exchange_time,p.status, p.integral, p.station_id,d.dept_name as station_name,p.mobile_phone
        from integral_order p
        join  sys_dept d on p.station_id =d.dept_id
        join integral_wares w on p.wares_id = w.id
    </sql>

    <select id="selectIntegralOrderList" parameterType="IntegralOrder" resultMap="IntegralOrderResult">
        <include refid="selectIntegralOrderVo"/>
        <where>
            <if test="integralOrderNo != null  and integralOrderNo != ''"> and integral_order_no like concat('%', #{integralOrderNo}, '%')</if>
            <if test="waresName != null  and waresName != ''"> and w.wares_name like concat('%', #{waresName}, '%')</if>
            <if test="waresId != null "> and p.wares_id = #{waresId}</if>
            <if test="unionId != null  and unionId != ''"> and union_id = #{unionId}</if>
            <if test="customerName != null  and customerName != ''"> and customer_name like concat('%', #{customerName}, '%')</if>
            <if test="exchangeNum != null "> and exchange_num = #{exchangeNum}</if>
            <if test="exchangeTime != null "> and exchange_time = #{exchangeTime}</if>
            <if test="status != null  and status != ''"> and p.status = #{status}</if>
            <if test="integral != null "> and p.integral = #{integral}</if>
            <if test="stationId != null "> and p.station_id = #{stationId}</if>
            <if test="mobilePhone != null "> and p.mobile_phone = #{mobilePhone}</if>
            <if test="stationIdList != null ">
                and p.station_id in
                <foreach item="item" index="index" collection="stationIdList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by  id desc
    </select>

    <select id="selectIntegralOrderById" parameterType="Long" resultMap="IntegralOrderResult">
        <include refid="selectIntegralOrderVo"/>
        where id = #{id}
    </select>

    <insert id="insertIntegralOrder" parameterType="IntegralOrder" useGeneratedKeys="true" keyProperty="id">
        insert into integral_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="integralOrderNo != null">integral_order_no,</if>
            <if test="waresId != null">wares_id,</if>
            <if test="unionId != null">union_id,</if>
            <if test="customerName != null">customer_name,</if>
            <if test="exchangeNum != null">exchange_num,</if>
            <if test="exchangeTime != null">exchange_time,</if>
            <if test="status != null">status,</if>
            <if test="integral != null">integral,</if>
            <if test="stationId != null">station_id,</if>
            <if test="mobilePhone != null "> mobile_phone,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="integralOrderNo != null">#{integralOrderNo},</if>
            <if test="waresId != null">#{waresId},</if>
            <if test="unionId != null">#{unionId},</if>
            <if test="customerName != null">#{customerName},</if>
            <if test="exchangeNum != null">#{exchangeNum},</if>
            <if test="exchangeTime != null">#{exchangeTime},</if>
            <if test="status != null">#{status},</if>
            <if test="integral != null">#{integral},</if>
            <if test="stationId != null">#{stationId},</if>
            <if test="mobilePhone != null">#{mobilePhone},</if>
         </trim>
    </insert>

    <update id="updateIntegralOrder" parameterType="IntegralOrder">
        update integral_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="integralOrderNo != null">integral_order_no = #{integralOrderNo},</if>
            <if test="waresId != null">wares_id = #{waresId},</if>
            <if test="unionId != null">union_id = #{unionId},</if>
            <if test="customerName != null">customer_name = #{customerName},</if>
            <if test="exchangeNum != null">exchange_num = #{exchangeNum},</if>
            <if test="exchangeTime != null">exchange_time = #{exchangeTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="integral != null">integral = #{integral},</if>
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="mobilePhone != null "> mobile_phone = #{mobilePhone},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteIntegralOrderById" parameterType="Long">
        delete from integral_order where id = #{id}
    </delete>

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

</mapper>
