<?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">
<!-- 命名，每一个映射对象不一样
	namespace:必须与对应的接口全类名一致
 -->
<mapper namespace="com.iamberry.wechat.service.mapper.AgentPurchaseOrderMapper">

    <sql id="agentOrderItem">
        item_id itemId,
        agent_orderid agentOrderid,
        product_id productId,
        color_id colorId,
        product_type productType,
        product_rent_type productRentType,
        item_num itemNum,
        item_total itemTotal,
        item_create_date itemCreateDate,
        item_product_name itemProductName,
        item_color_name itemColorName,
        item_product_discount itemProductDiscount,
        item_product_price itemProductPrice,
        item_product_pic  itemProductPic
    </sql>

    <insert id="addAgentOrder" parameterType="AgentOrder">
		insert into tb_iamberry_agent_order (`agent_orderid` ,`agent_id` ,`agent_yet_amount` ,`agent_amount` ,`agent_create_date`,`agent_status` ,`agent_address_info` ,`agent_address_postnum` ,`agent_address_tel` ,`agent_address_name` ,`agent_admin_remark` )
		values (#{agentOrderid},#{agentId},#{agentYetAmount},#{agentAmount},#{agentCreateDate},#{agentStatus},#{agentAddressInfo},#{agentAddressPostnum},#{agentAddressTel},#{agentAddressName},#{agentAdminRemark})
	</insert>

    <insert id="addAgentOrderItemList" parameterType="AgentOrderItem" useGeneratedKeys="true">
        insert into tb_iamberry_agent_order_item (`agent_orderid` ,`product_id` ,`color_id` ,`product_type`
        ,`product_rent_type` ,`item_num` ,`item_total` ,`item_product_name` ,`item_color_name` ,`item_product_discount`
        ,`item_product_price` ,`item_product_pic` )
        values
        <foreach collection="list" item="item" index="index" separator="," open="" close="">
            (#{item.agentOrderid},#{item.productId},#{item.colorId},#{item.productType},#{item.productRentType},#{item.itemNum},#{item.itemTotal},#{item.itemProductName},#{item.itemColorName},#{item.itemProductDiscount},#{item.itemProductPrice},#{item.itemProductPic})
        </foreach>
    </insert>


    <select id="listAgentOrderItem" parameterType="AgentOrderItem" resultType="AgentOrderItem">
        SELECT
            <include refid="agentOrderItem"></include>
        FROM
            tb_iamberry_agent_order_item
        WHERE
            agent_orderid = #{agentOrderid}
        ORDER BY item_id DESC
    </select>

    <update id="updateAgentPurchaseOrder" parameterType="AgentOrder" >
    update
        tb_iamberry_agent_order
    <set>
        <if test="agentPostNum !=null and agentPostNum !=''">
            agent_post_num = #{agentPostNum},
        </if>
        <if test="agentPostFirm !=null and agentPostFirm !=''">
            agent_post_firm = #{agentPostFirm},
        </if>
        <if test="agentStatus !=null and agentStatus !=''">
            agent_status = #{agentStatus},
        </if>
        <if test="agentSendDate !=null and agentSendDate !=''">
            agent_send_date = #{agentSendDate},
        </if>
        <if test="agentAddressInfo !=null and agentAddressInfo !=''">
            agent_address_info = #{agentAddressInfo},
        </if>
        <if test="agentAddressPostnum !=null and agentAddressPostnum !=''">
            agent_address_postnum = #{agentAddressPostnum},
        </if>
        <if test="agentAddressTel !=null and agentAddressTel !=''">
            agent_address_tel = #{agentAddressTel},
        </if>
        <if test="agentAddressName !=null and agentAddressName !=''">
            agent_address_name = #{agentAddressName},
        </if>
        <if test="agentAdminRemark !=null and agentAdminRemark !=''">
            agent_admin_remark = #{agentAdminRemark},
        </if>
        <if test="agentTransactionId !=null and agentTransactionId !=''">
            agent_transaction_id = #{agentTransactionId},
        </if>
        <if test="agentTransactionDate !=null and agentTransactionDate !=''">
            agent_transaction_date = #{agentTransactionDate},
        </if>
    </set>
    where
       agent_orderid = #{agentOrderid}
  </update>

    <!-- 修改备注信息 -->
    <update id="updateRemark" parameterType="AgentOrder">
        update
          tb_iamberry_agent_order
        SET
          agent_admin_remark = #{agentAdminRemark}
        WHERE
          agent_orderid = #{agentOrderid}
    </update>

    <!-- 查询代理商订单列表 不分页 -->
    <select id="listSelectAgentOrder" parameterType="AgentOrder" resultType="AgentOrder">
      select
            o.agent_orderid agentOrderid,
            o.agent_id agentId,
            o.agent_yet_amount agentYetAmount,
            o.agent_amount agentAmount,
            o.agent_create_date agentCreateDate,
            o.agent_post_num agentPostNum,
            o.agent_post_firm agentPostFirm,
            o.agent_status agentStatus,
            o.agent_send_date agentSendDate,
            o.agent_address_info agentAddressInfo,
            o.agent_address_postnum agentAddressPostnum,
            o.agent_address_tel agentAddressTel,
            o.agent_address_name agentAddressName,
            o.agent_admin_remark agentAdminRemark,
            o.agent_transaction_id agentTransactionId,
            o.agent_transaction_date agentTransactionDate,
            a.agent_name agentName,
            a.agent_tel agentTel,
            a.agent_openid agentOpenId
        from
          tb_iamberry_agent_order o
        LEFT JOIN tb_iamberry_agent_agentinfo a ON o.agent_id = a.agent_id
        <where>
            <if test="agentOrderid != null and agentOrderid != ''">
                o.agent_orderid = #{agentOrderid}
            </if>
        </where>
    </select>

    <!-- 查询代理商订单信息,可分页 -->
    <select id="listAgentOrder" parameterType="AgentOrder" resultMap="agentOrderMap">
        select
            o.agent_orderid,
            o.agent_id,
            o.agent_yet_amount,
            o.agent_amount,
            o.agent_create_date,
            o.agent_post_num,
            o.agent_post_firm,
            o.agent_status,
            o.agent_send_date,
            o.agent_address_info,
            o.agent_address_postnum,
            o.agent_address_tel,
            o.agent_address_name,
            o.agent_admin_remark,
            o.agent_transaction_id,
            o.agent_transaction_date,
            a.agent_name agentName,
            a.agent_tel agentTel
        from
          tb_iamberry_agent_order o
        LEFT JOIN tb_iamberry_agent_agentinfo a ON o.agent_id = a.agent_id
      <where>
          <if test="agentOrderid != null and agentOrderid != ''">
              o.agent_orderid = #{agentOrderid}
          </if>
          <if test="agentId != null and agentId != ''">
              and a.agent_id = #{agentId}
          </if>
          <if test="agentName != null and agentName != ''">
              and a.agent_name like CONCAT('%',#{agentName},'%')
          </if>
          <if test="agentTel != null and agentTel != ''">
              and a.agent_tel like CONCAT('%',#{agentTel},'%')
          </if>
          <if test="agentAddressName != null and agentAddressName != ''">
              and o.agent_address_name like CONCAT('%',#{agentAddressName},'%')
          </if>
          <if test="agentAddressTel != null and agentAddressTel != ''">
              and o.agent_address_tel like CONCAT('%',#{agentAddressTel},'%')
          </if>
          <if test="agentStatus != null and agentStatus != -1">
              and o.agent_status = #{agentStatus}
          </if>
          <if test="beginDate != null">
              AND DATE_FORMAT(o.agent_create_date, '%Y-%m-%d') <![CDATA[ >= ]]> DATE_FORMAT(#{beginDate},'%Y-%m-%d')
          </if>
          <if test="endDate != null">
              AND DATE_FORMAT(o.agent_create_date, '%Y-%m-%d') <![CDATA[ <= ]]> DATE_FORMAT(#{endDate},'%Y-%m-%d')
          </if>
      </where>
        ORDER BY agent_create_date DESC
        <if test="page!=null and page.pageSize>0 ">
            LIMIT ${page.recordBegin},${page.pageSize}
        </if>
    </select>

    <resultMap type="com.iamberry.wechat.core.entity.agentInfo.AgentOrder" id="agentOrderMap">
        <id column="agent_orderid" property="agentOrderid"/>
        <result column="agent_id" property="agentId"/>
        <result column="agent_yet_amount" property="agentYetAmount"/>
        <result column="agent_amount" property="agentAmount"/>
        <result column="agent_create_date" property="agentCreateDate"/>
        <result column="agent_post_num" property="agentPostNum"/>
        <result column="agent_post_firm" property="agentPostFirm"/>
        <result column="agent_status" property="agentStatus"/>
        <result column="agent_send_date" property="agentSendDate"/>
        <result column="agent_address_info" property="agentAddressInfo"/>
        <result column="agent_address_postnum" property="agentAddressPostnum"/>
        <result column="agent_address_tel" property="agentAddressTel"/>
        <result column="agent_address_name" property="agentAddressName"/>
        <result column="agent_admin_remark" property="agentAdminRemark"/>
        <result column="agent_transaction_id" property="agentTransactionId"/>
        <result column="agent_transaction_date" property="agentTransactionDate"/>
        <collection property="itemList" column="agent_orderid" ofType="com.iamberry.wechat.core.entity.agentInfo.AgentOrderItem" select="listAgentItem"/>
    </resultMap>

    <select id="listAgentItem" parameterType="String" resultType="AgentOrderItem">
        SELECT
            i.item_id itemId,
            i.agent_orderid agentOrderid,
            i.product_id productId,
            i.color_id colorId,
            i.product_type productType,
            i.product_rent_type productRentType,
            i.item_num itemNum,
            i.item_total itemTotal,
            i.item_create_date itemCreateDate,
            i.item_product_name itemProductName,
            i.item_color_name itemColorName,
            i.item_product_discount itemProductDiscount,
            i.item_product_price itemProductPrice,
            i.item_product_pic  itemProductPic,
            c.color_69code itemProductSKU
        FROM
            tb_iamberry_agent_order_item i
        LEFT JOIN tb_iamberry_product_color c ON i.color_id = c.color_id
        WHERE
          agent_orderid = #{agentOrderid}
        ORDER BY item_id DESC
    </select>

    <select id="listAgentOrderCount" parameterType="AgentOrder" resultType="Integer">
        select
          count(o.agent_orderid)
        from
          tb_iamberry_agent_order o
        LEFT JOIN tb_iamberry_agent_agentinfo a ON o.agent_id = a.agent_id
        <where>
            <if test="agentOrderid != null and agentOrderid != ''">
                o.agent_orderid = #{agentOrderid}
            </if>
            <if test="agentId != null and agentId != ''">
                and a.agent_id = #{agentId}
            </if>
            <if test="agentName != null and agentName != ''">
                and a.agent_name like CONCAT('%',#{agentName},'%')
            </if>
            <if test="agentTel != null and agentTel != ''">
                and a.agent_tel like CONCAT('%',#{agentTel},'%')
            </if>
            <if test="agentAddressName != null and agentAddressName != ''">
                and o.agent_address_name like CONCAT('%',#{agentAddressName},'%')
            </if>
            <if test="agentAddressTel != null and agentAddressTel != ''">
                and o.agent_address_tel like CONCAT('%',#{agentAddressTel},'%')
            </if>
            <if test="agentStatus != null and agentStatus != -1">
                and o.agent_status = #{agentStatus}
            </if>
            <if test="beginDate != null">
                AND DATE_FORMAT(o.agent_create_date, '%Y-%m-%d') <![CDATA[ >= ]]> DATE_FORMAT(#{beginDate},'%Y-%m-%d')
            </if>
            <if test="endDate != null">
                AND DATE_FORMAT(o.agent_create_date, '%Y-%m-%d') <![CDATA[ <= ]]> DATE_FORMAT(#{endDate},'%Y-%m-%d')
            </if>
        </where>
    </select>

    <!-- 批量查询需要更换刷头的订单 -->
    <select id="listOrderHint" parameterType="OrderHint" resultType="OrderHint">
      select
        *
      from
        tb_iamberry_order_hint
      <where>
          <if test="hintTime != null and hintTime != ''">
              date_format(hint_time, '%Y-%m-%d') <![CDATA[ <= ]]> date_format(#{hintTime}, '%Y-%m-%d')
          </if>
          <if test="hintStatus != null and hintStatus != ''">
              and hint_status = #{hintStatus}
          </if>
          <if test="orderId != null and orderId != ''">
              and order_id = #{orderId}
          </if>
          <if test="userOpenId != null and userOpenId != ''">
              and user_open_id = #{userOpenId}
          </if>
          <if test="hintId != null and hintId != ''">
              and hint_id = #{hintId}
          </if>
      </where>
    </select>

    <!-- 批量修改更换刷头信息 -->
    <update id="updateOrderHintList" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";" open="" close="">
          update
            tb_iamberry_order_hint
          <set>
              <if test="item.hintTime != null and item.hintTime != ''">
                  hint_time = #{item.hintTime},
              </if>
              <if test="item.hintStatus != null and item.hintStatus != ''">
                  hint_status = #{item.hintStatus}
              </if>
              <if test="item.hintNum != null and item.hintNum != ''">
                  hint_num = (hint_num + 1)
              </if>
          </set>
          <where>
              hint_id = #{item.hintId}
          </where>
        </foreach>
    </update>

    <!-- 添加更换刷头信息 -->
    <insert id="addOrderHint" parameterType="OrderHint"
            useGeneratedKeys="true" keyProperty="hintId">
      insert into tb_iamberry_order_hint
      (
        order_id,user_open_id,hint_num,hint_time,
        hint_status,hint_create_time
      )
      values
      (
        #{orderId},#{userOpenId},#{hintNum},#{hintTime},
        #{hintStatus},NOW()
      )
    </insert>
</mapper>
