yujian
2019-01-22 88b54772dbcf5ecab1e2316e4e4626ac901b8908
fanli/src/main/java/com/yeshi/fanli/mapping/order/CommonOrderMapper.xml
@@ -10,6 +10,7 @@
         jdbcType="VARCHAR" />
      <result column="co_count" property="count" jdbcType="INTEGER" />
      <result column="co_state" property="state" jdbcType="INTEGER" />
      <result column="co_state_whole_order" property="stateWholeOrder" jdbcType="INTEGER" />
      <result column="co_estimate" property="estimate" jdbcType="DECIMAL" />
      <result column="co_eIncome" property="eIncome" jdbcType="DECIMAL" />
      <result column="co_payment" property="payment" jdbcType="DECIMAL" />
@@ -31,8 +32,8 @@
         <id column="co_order_goods_id" property="id" jdbcType="BIGINT" />
      </association>
   </resultMap>
   <resultMap id="BaseDetailResultMap" type="com.yeshi.fanli.entity.order.CommonOrder">
      <id column="co_id" property="id" jdbcType="BIGINT" />
      <result column="co_order_no" property="orderNo" jdbcType="VARCHAR" />
@@ -41,6 +42,7 @@
         jdbcType="VARCHAR" />
      <result column="co_count" property="count" jdbcType="INTEGER" />
      <result column="co_state" property="state" jdbcType="INTEGER" />
      <result column="co_state_whole_order" property="stateWholeOrder" jdbcType="INTEGER" />
      <result column="co_estimate" property="estimate" jdbcType="DECIMAL" />
      <result column="co_eIncome" property="eIncome" jdbcType="DECIMAL" />
      <result column="co_payment" property="payment" jdbcType="DECIMAL" />
@@ -57,8 +59,9 @@
         <id column="co_uid" property="id" jdbcType="BIGINT" />
      </association>
      <association property="commonOrderGoods" resultMap="com.yeshi.fanli.dao.mybatis.order.CommonOrderGoodsMapper.BaseResultMap">
      <association property="commonOrderGoods"
         resultMap="com.yeshi.fanli.dao.mybatis.order.CommonOrderGoodsMapper.BaseResultMap">
      </association>
   </resultMap>
@@ -70,6 +73,7 @@
         jdbcType="VARCHAR" />
      <result column="co_count" property="count" jdbcType="INTEGER" />
      <result column="co_state" property="state" jdbcType="INTEGER" />
      <result column="co_state_whole_order" property="stateWholeOrder" jdbcType="INTEGER" />
      <result column="co_estimate" property="estimate" jdbcType="DECIMAL" />
      <result column="co_eIncome" property="eIncome" jdbcType="DECIMAL" />
      <result column="co_payment" property="payment" jdbcType="DECIMAL" />
@@ -89,8 +93,18 @@
         jdbcType="TIMESTAMP" />
      <result column="totalCount" property="totalCount" jdbcType="INTEGER" />
      <result column="totalSettlement" property="totalSettlement"
         jdbcType="DECIMAL" />
      <result column="totalSettlement" property="totalSettlement" jdbcType="DECIMAL" />
      <result column="totalPayment" property="totalPayment" jdbcType="DECIMAL" />
      <result column="userId" property="userId" jdbcType="VARCHAR" />
      <result column="userName" property="userName" jdbcType="VARCHAR" />
      <result column="userPortrait" property="userPortrait" jdbcType="VARCHAR" />
      <result column="levelOneId" property="levelOneId" jdbcType="VARCHAR" />
      <result column="levelOneMoney" property="levelOneMoney" jdbcType="VARCHAR" />
      <result column="levelTwoId" property="levelTwoId" jdbcType="VARCHAR" />
      <result column="levelTwoMoney" property="levelTwoMoney" jdbcType="VARCHAR" />
      <association property="userInfo" column="co_uid"
         resultMap="com.yeshi.fanli.dao.mybatis.UserInfoMapper.BaseResultMap">
@@ -100,10 +114,10 @@
         resultMap="com.yeshi.fanli.dao.mybatis.order.CommonOrderGoodsMapper.BaseResultMap">
      </association>
   </resultMap>
   <sql id="Base_Column_List">co_id,co_uid,co_order_no,co_source_type,co_source_position,co_order_goods_id,co_count,co_state,co_estimate,co_eIncome,co_payment,co_settlement,co_third_create_time,co_settle_time,co_order_by,co_create_time,co_update_time
   <sql id="Base_Column_List">co_id,co_uid,co_order_no,co_source_type,co_source_position,co_order_goods_id,co_count,co_state,co_state_whole_order,co_estimate,co_eIncome,co_payment,co_settlement,co_third_create_time,co_settle_time,co_order_by,co_create_time,co_update_time
   </sql>
   <select id="selectByPrimaryKey" resultMap="BaseResultMap"
      parameterType="java.lang.Long">
@@ -122,6 +136,14 @@
      * from yeshi_ec_common_order where co_order_no=#{orderNo}
      and
      co_source_type=#{orderType} and co_order_by=#{orderBy}
   </select>
   <select id="countByUidAndOrderStateWithOrderBalanceTime"
      resultType="java.lang.Long">
      SELECT COUNT(*) FROM yeshi_ec_common_order co WHERE
      co.`co_state`=#{state} and co.`co_uid`=#{uid} and co.`co_settle_time`
      is not null and co.`co_settle_time`>=#{minDate} and
      #{maxDate}>co.`co_settle_time`
   </select>
@@ -146,6 +168,7 @@
         <if test="commonOrderGoods != null">co_order_goods_id,</if>
         <if test="count != null">co_count,</if>
         <if test="state != null">co_state,</if>
         <if test="stateWholeOrder != null">co_state_whole_order,</if>
         <if test="estimate != null">co_estimate,</if>
         <if test="eIncome != null">co_eIncome,</if>
         <if test="payment != null">co_payment,</if>
@@ -166,6 +189,7 @@
         <if test="commonOrderGoods != null">#{commonOrderGoods.id,jdbcType=BIGINT},</if>
         <if test="count != null">#{count,jdbcType=INTEGER},</if>
         <if test="state != null">#{state,jdbcType=INTEGER},</if>
         <if test="stateWholeOrder != null">#{stateWholeOrder,jdbcType=INTEGER},</if>
         <if test="estimate != null">#{estimate,jdbcType=DECIMAL},</if>
         <if test="eIncome != null">#{eIncome,jdbcType=DECIMAL},</if>
         <if test="payment != null">#{payment,jdbcType=DECIMAL},</if>
@@ -185,7 +209,8 @@
      #{sourcePosition,jdbcType=VARCHAR},co_order_goods_id =
      #{commonOrderGoods.id,jdbcType=BIGINT},co_count =
      #{count,jdbcType=INTEGER},co_state =
      #{state,jdbcType=INTEGER},co_estimate =
      #{state,jdbcType=INTEGER},co_state_whole_order =
      #{stateWholeOrder,jdbcType=INTEGER},co_estimate =
      #{estimate,jdbcType=DECIMAL},co_eIncome =
      #{eIncome,jdbcType=DECIMAL},co_payment =
      #{payment,jdbcType=DECIMAL},co_settlement =
@@ -208,6 +233,7 @@
         </if>
         <if test="count != null">co_count=#{count,jdbcType=INTEGER},</if>
         <if test="state != null">co_state=#{state,jdbcType=INTEGER},</if>
         <if test="stateWholeOrder != null">co_state_whole_order=#{stateWholeOrder,jdbcType=INTEGER},</if>
         <if test="estimate != null">co_estimate=#{estimate,jdbcType=DECIMAL},</if>
         <if test="eIncome != null">co_eIncome=#{eIncome,jdbcType=DECIMAL},</if>
         <if test="payment != null">co_payment=#{payment,jdbcType=DECIMAL},</if>
@@ -222,191 +248,129 @@
      where co_id = #{id,jdbcType=BIGINT}
   </update>
   <select id="listGroupOrderNoByUid" resultMap="ResultMap">
      SELECT COALESCE(SUM(th.`hb_money`),0)AS totalMoney,tc.*,th.hb_state AS
      hongBaoState, th.hb_type AS hongBaoType,
      th.hb_get_time AS
      accountTime,th.hb_get_time AS accountTime,th.hb_pre_get_time AS
      preAccountTime
      FROM yeshi_ec_common_order tc
      LEFT JOIN
      yeshi_ec_hongbao_order tr ON tr.`ho_order_id` = tc.`co_id`
      LEFT JOIN
      yeshi_ec_hongbao_v2 th ON (tr.`ho_hongbao_id` = th.`hb_id` OR
      th.`hb_pid` = tr.`ho_hongbao_id` )
      WHERE th.`hb_uid` = #{uid}
      <if test="state != null and state == 1">  <!-- 未到账 -->
         AND (th.hb_state = 1 or th.hb_state = 2)
      </if>
      <if test="state != null and state == 2"> <!-- 已到账 -->
         AND th.hb_state = 3
      </if>
      <if test="state != null and state == 3">  <!-- 红包已失效 -->
         AND th.hb_state =4
      </if>
      <if test="type != null and type == 1"> <!-- 自购订单 -->
         AND (th.hb_type =1 or th.hb_type =2)
      </if>
      <if test="type != null and type == 2"> <!-- 分享订单 -->
         AND (th.`hb_type` = 20 OR th.`hb_type` = 21 OR th.`hb_type` = 22)
      </if>
      <if test="type != null and type == 3"> <!-- 邀请订单 -->
         AND (th.`hb_type` = 5 OR th.`hb_type` = 6 OR th.`hb_type` = 7)
      </if>
      <if test="orderNo != null">
         AND tc.co_order_no = #{orderNo}
      </if>
   <sql id="SELECT_PARAM_ORDER_STATE">
      <if test="orderState != null and orderState ==  1">
         AND (tc.co_state = 1 or tc.co_state = 2) <!-- 有效订单 -->
         <!-- 有效订单: 整个订单有效 -->
         AND <![CDATA[co.`co_state` <> 3]]> AND (co.co_state_whole_order = 1 or co.co_state_whole_order = 2)
      </if>
      <if test="orderState != null and orderState ==  2">
         AND tc.co_state = 3 <!-- 维权订单 -->
         AND co.co_state = 3 <!-- 维权订单 -->
      </if>
      <if test="orderState != null and orderState ==  3">
         AND tc.co_state = 4 <!-- 失效订单 -->
         <!-- 整个订单失效 -->
         AND co.co_state_whole_order = 3
      </if>
      <if test="startTime != null">
         AND <![CDATA[tc.co_create_time >= #{startTime}]]>
   </sql>
   <sql id="SELECT_PARAM_ORDER_CREATE_TIME">
      <if test="startTime != null and startTime != '' ">
         AND <![CDATA[co.co_third_create_time >= #{startTime}]]>
      </if>
      <if test="endTime != null">
         AND <![CDATA[tc.co_create_time <= #{endTime}]]>
      <if test="endTime != null and endTime != '' ">
         AND <![CDATA[co.co_third_create_time <= #{endTime}]]>
      </if>
      <if test="day != null and day == 1"> <!-- 今天 -->
         AND TO_DAYS(tc.`co_create_time`) = TO_DAYS(NOW())
         AND TO_DAYS(co.`co_third_create_time`) = TO_DAYS(NOW())
      </if>
      <if test="day != null and day == 2">  <!-- 昨天 -->
         AND TO_DAYS(NOW()) - TO_DAYS(tc.`co_create_time`) = 1
         AND TO_DAYS(NOW()) - TO_DAYS(co.`co_third_create_time`) = 1
      </if>
      <if test="day != null and day == 3">  <!-- 本月 -->
         AND DATE_FORMAT(tc.`co_create_time`,'%Y%m') =
         AND DATE_FORMAT(co.`co_third_create_time`,'%Y%m') =
         DATE_FORMAT(CURDATE(),'%Y%m')
      </if>
      <if test="day != null and day == 4">  <!-- 上月 -->
         AND PERIOD_DIFF( DATE_FORMAT(NOW(), '%Y%m'),
         DATE_FORMAT(tc.`co_create_time`, '%Y%m')) = 1
         AND PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m'),
         DATE_FORMAT(co.`co_settle_time`, '%Y%m')) = 1
      </if>
      GROUP BY tc.`co_order_no`,tc.co_source_type
      ORDER BY tc.co_create_time
      DESC
      LIMIT ${start},${count}
   </select>
   </sql>
   
   <select id="countMoneyGroupOrderNoByUid" resultType="java.util.Map">
      SELECT COALESCE(SUM(A.moneys),0)AS totalMoney,COUNT(*) AS totalRow FROM
         (SELECT COALESCE(SUM(th.`hb_money`),0)AS moneys FROM yeshi_ec_common_order tc
         LEFT JOIN
         yeshi_ec_hongbao_order tr ON tr.`ho_order_id` = tc.`co_id`
         LEFT JOIN
         yeshi_ec_hongbao_v2 th ON (tr.`ho_hongbao_id` = th.`hb_id` OR
         th.`hb_pid` = tr.`ho_hongbao_id` )
         WHERE th.`hb_uid` = #{uid}    AND th.hb_state = 3 AND (tc.co_state = 1 or tc.co_state = 2)
         <if test="type != null and type == 1"> <!-- 自购订单 -->
            AND (th.hb_type =1 or th.hb_type =2)
         </if>
         <if test="type != null and type == 2"> <!-- 分享订单 -->
            AND (th.`hb_type` = 20 OR th.`hb_type` = 21 OR th.`hb_type` = 22)
         </if>
         <if test="type != null and type == 3"> <!-- 邀请订单 -->
            AND (th.`hb_type` = 5 OR th.`hb_type` = 6 OR th.`hb_type` = 7)
         </if>
         <if test="orderNo != null">
            AND tc.co_order_no = #{orderNo}
         </if>
         <if test="startTime != null">
            AND <![CDATA[tc.co_create_time >= #{startTime}]]>
         </if>
         <if test="endTime != null">
            AND <![CDATA[tc.co_create_time <= #{endTime}]]>
         </if>
         <if test="day != null and day == 1"> <!-- 今天 -->
            AND TO_DAYS(tc.`co_create_time`) = TO_DAYS(NOW())
         </if>
         <if test="day != null and day == 2">  <!-- 昨天 -->
            AND TO_DAYS(NOW()) - TO_DAYS(tc.`co_create_time`) = 1
         </if>
         <if test="day != null and day == 3">  <!-- 本月 -->
            AND DATE_FORMAT(tc.`co_create_time`,'%Y%m') =
            DATE_FORMAT(CURDATE(),'%Y%m')
         </if>
         <if test="day != null and day == 4">  <!-- 上月 -->
            AND PERIOD_DIFF( DATE_FORMAT(NOW(), '%Y%m'),
            DATE_FORMAT(tc.`co_create_time`, '%Y%m')) = 1
         </if>
         GROUP BY tc.`co_order_no`,tc.co_source_type)A
   </select>
   <select id="countGroupOrderNoByUid" resultType="java.lang.Long">
      SELECT IFNULL(COUNT(DISTINCT tc.`co_order_no`,tc.`co_source_type`),0)
      FROM yeshi_ec_common_order tc
      LEFT JOIN yeshi_ec_hongbao_order tr ON
      tr.`ho_order_id` = tc.`co_id`
      LEFT JOIN yeshi_ec_hongbao_v2 th ON
      (tr.`ho_hongbao_id` = th.`hb_id` OR
      th.`hb_pid` = tr.`ho_hongbao_id` )
      WHERE th.`hb_uid` = #{uid}
      <if test="state != null and state == 1">  <!-- 未到账 -->
         AND (th.hb_state = 1 or th.hb_state = 2)
      </if>
      <if test="state != null and state == 2">  <!-- 已到账 -->
         AND th.hb_state = 3
      </if>
      <if test="state != null and state == 3"> <!-- 红包已失效 -->
         AND th.hb_state =4
      </if>
   <sql id="SELECT_PARAM_HONGBAO_TYPE">
      <if test="type != null and type == 1"> <!-- 自购订单 -->
         AND (th.hb_type =1 or th.hb_type =2)
         AND (v2.hb_type =1 or v2.hb_type =2)
      </if>
      <if test="type != null and type == 2"> <!-- 分享订单 -->
         AND (th.`hb_type` = 20 OR th.`hb_type` = 21 OR th.`hb_type` = 22)
         AND v2.`hb_type` = 20
      </if>
      <if test="type != null and type == 3"> <!-- 邀请订单 -->
         AND (th.`hb_type` = 5 OR th.`hb_type` = 6 OR th.`hb_type` = 7)
         AND (v2.`hb_type` = 5 OR v2.`hb_type` = 6 OR v2.`hb_type` = 7 OR v2.`hb_type` = 21 OR v2.`hb_type` = 22)
      </if>
   </sql>
   <sql id="SELECT_PARAM_HONGBAO_STATE">
      <if test="state != null and state == 1">  <!-- 未到账 -->
         AND (hb.hb_state = 1 or hb.hb_state = 2)
      </if>
      <if test="state != null and state == 2"> <!-- 已到账 -->
         AND hb.hb_state = 3
      </if>
      <if test="state != null and state == 3">  <!-- 红包已失效、整个订单失效 -->
         AND hb.hb_state =4 AND co.co_state_whole_order = 3
      </if>
   </sql>
   <sql id="Hongbao_Column_List">
      hb_id,hb_uid,hb_urank,hb_pid,hb_type,hb_state,hb_pre_get_time,hb_get_time,hb_version,
      IF(v2.`hb_state` = 4, 0, v2.hb_money)AS hb_money
   </sql>
      <if test="orderState != null and orderState ==  1">
         AND (tc.co_state = 1 or tc.co_state = 2) <!-- 有效订单 -->
      </if>
      <if test="orderState != null and orderState == 2">
         AND tc.co_state = 3 <!-- 维权订单 -->
      </if>
      <if test="orderState != null and orderState ==  3">
         AND tc.co_state = 4 <!-- 失效订单 -->
      </if>
      <if test="orderNo != null">
         AND tc.co_order_no = #{orderNo}
      </if>
      <if test="startTime != null">
         AND <![CDATA[tc.co_create_time >= #{startTime}]]>
      </if>
      <if test="endTime != null">
         AND <![CDATA[tc.co_create_time <= #{endTime}]]>
      </if>
      <if test="day != null and day == 1"> <!-- 今天 -->
         AND TO_DAYS(tc.`co_create_time`) = TO_DAYS(NOW())
      </if>
      <if test="day != null and day == 2">  <!-- 昨天 -->
         AND TO_DAYS(NOW()) - TO_DAYS(tc.`co_create_time`) = 1
      </if>
      <if test="day != null and day == 3">  <!-- 本月 -->
         AND DATE_FORMAT(tc.`co_create_time`,'%Y%m') =
         DATE_FORMAT(CURDATE(),'%Y%m')
      </if>
      <if test="day != null and day == 4">  <!-- 上月 -->
         AND PERIOD_DIFF( DATE_FORMAT(NOW(), '%Y%m'),
         DATE_FORMAT(tc.`co_create_time`, '%Y%m')) = 1
      </if>
   <select id="listUserOrder" resultMap="ResultMap">
       <!-- 查询用户订单 -->
      SELECT COALESCE(SUM(hb.`hb_money`),0)AS totalMoney,
            hb.hb_state AS hongBaoState,
            hb.`hb_type` AS hongBaoType,
            hb.`hb_get_time` AS accountTime,
            hb.`hb_pre_get_time`AS preAccountTime,
            co.*
      FROM yeshi_ec_hongbao_order ho
      LEFT JOIN (SELECT <include refid="Hongbao_Column_List"/> FROM yeshi_ec_hongbao_v2 v2
               WHERE v2.`hb_uid`= #{uid}
                  <include refid="SELECT_PARAM_HONGBAO_TYPE"/>  <!-- 红包类型 -->
            )hb ON IF(hb.hb_pid IS NULL,hb.hb_id,hb.hb_pid)=ho.`ho_hongbao_id`
      LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
      WHERE hb.hb_id IS NOT NULL
           <include refid="SELECT_PARAM_ORDER_STATE"/>        <!-- 订单状态 -->
           <include refid="SELECT_PARAM_ORDER_CREATE_TIME"/>  <!-- 订单时间 -->
           <include refid="SELECT_PARAM_HONGBAO_STATE"/> <!-- 红包状态 -->
           <if test="orderNo != null and orderNo !='' ">
            AND co.co_order_no = #{orderNo}
           </if>
      GROUP BY co.`co_order_no`, co.`co_source_type`
      ORDER BY co.co_third_create_time DESC
      LIMIT ${start},${count}
   </select>
   <select id="listByOrderNoAndType" resultMap="ResultMap"
      parameterType="java.util.List">
      SELECT COALESCE(SUM(tc.`co_settlement`),0)AS
      totalSettlement,COALESCE(SUM(tc.`co_count`),0)AS
      totalCount,tc.`co_source_type`,tc.`co_order_no`,tg.*
   <select id="countUserOrder" resultType="java.lang.Long">
       <!-- 统计用户订单数量 -->
      SELECT IFNULL(COUNT(ho_id),0) FROM
         (SELECT ho.ho_id FROM yeshi_ec_hongbao_order ho
          LEFT JOIN (SELECT <include refid="Hongbao_Column_List"/> FROM yeshi_ec_hongbao_v2 v2
               WHERE v2.`hb_uid`= #{uid}
                  <include refid="SELECT_PARAM_HONGBAO_TYPE"/>  <!-- 红包类型 -->
            )hb ON IF(hb.hb_pid IS NULL,hb.hb_id,hb.hb_pid)=ho.`ho_hongbao_id`
           LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
          WHERE hb.hb_id IS NOT NULL
                 <include refid="SELECT_PARAM_ORDER_STATE"/>        <!-- 订单状态 -->
                <include refid="SELECT_PARAM_ORDER_CREATE_TIME"/>  <!-- 订单时间 -->
                <include refid="SELECT_PARAM_HONGBAO_STATE"/> <!-- 红包状态 -->
               <if test="orderNo != null and orderNo !='' ">
                  AND co.co_order_no = #{orderNo}
               </if>
          GROUP BY co.`co_order_no`, co.`co_source_type`
      )A
   </select>
   <select id="listOrderGoodsInfo" resultMap="ResultMap"   parameterType="java.util.List">
       <!-- 查询用户订单商品信息 -->
      SELECT COALESCE(SUM(tc.`co_payment`),0)AS totalPayment,COALESCE(SUM(tc.`co_settlement`),0)AS totalSettlement,
         COALESCE(SUM(tc.`co_count`),0)AS totalCount,tc.`co_source_type`,tc.`co_order_no`,tg.*,tc.co_id
      FROM
      yeshi_ec_common_order_goods tg
      LEFT JOIN yeshi_ec_common_order tc ON
      tc.`co_order_goods_id` = tg.`cog_id`
      LEFT JOIN yeshi_ec_common_order tc ON tc.`co_order_goods_id` = tg.`cog_id`
      WHERE
      <foreach collection="list" item="item" separator=" OR ">
         (tc.`co_source_type` = #{item.sourceType,jdbcType=INTEGER} AND
@@ -415,322 +379,329 @@
      GROUP BY tc.`co_order_no`,tc.`co_order_goods_id`
   </select>
   <select id="countOrderByHongBaoType" resultType="java.util.HashMap">
      SELECT SUM(self)AS totalSelf, SUM(shared)AS totalShared ,SUM(invite)AS
      totalInvite FROM (
      SELECT IFNULL(COUNT(DISTINCT
      tc.`co_order_no`,tc.`co_source_type`),0) AS self,0 AS shared,0 AS
      invite FROM yeshi_ec_common_order tc
      LEFT JOIN yeshi_ec_hongbao_order
      tr ON tr.`ho_order_id` = tc.`co_id`
      LEFT
      JOIN yeshi_ec_hongbao_v2 th ON
      tr.`ho_hongbao_id` = th.`hb_id`
      WHERE
      th.`hb_uid` = ${uid}
      AND
      (th.`hb_type` = 1 OR th.`hb_type` = 2)
      <if test="day != null and day == 1">
         AND TO_DAYS(tc.`co_create_time`) = TO_DAYS(NOW())
      </if>
      <if test="day != null and day == 2">
         AND TO_DAYS(NOW()) - TO_DAYS(tc.`co_create_time`) = 1
      </if>
      UNION
      SELECT 0 AS self, IFNULL(COUNT(DISTINCT
      tc.`co_order_no`,tc.`co_source_type`),0) AS shared,0 AS invite FROM
      yeshi_ec_common_order tc
      LEFT JOIN yeshi_ec_hongbao_order tr ON
      tr.`ho_order_id` = tc.`co_id`
      LEFT
      JOIN yeshi_ec_hongbao_v2 th ON
      (tr.`ho_hongbao_id` = th.`hb_id` OR
      th.`hb_pid` = tr.`ho_hongbao_id` )
      WHERE th.`hb_uid` = ${uid}
      AND (th.`hb_type` = 20 OR th.`hb_type` = 21
      OR th.`hb_type` = 22)
      <if test="day != null and day == 1">
         AND TO_DAYS(tc.`co_create_time`) = TO_DAYS(NOW())
      </if>
      <if test="day != null and day == 2">
         AND TO_DAYS(NOW()) - TO_DAYS(tc.`co_create_time`) = 1
      </if>
      UNION
      SELECT 0 AS self,0 AS shared,IFNULL(COUNT(DISTINCT
      tc.`co_order_no`,tc.`co_source_type`),0) AS invite FROM
      yeshi_ec_common_order tc
      LEFT JOIN yeshi_ec_hongbao_order tr ON
      tr.`ho_order_id` = tc.`co_id`
      LEFT JOIN yeshi_ec_hongbao_v2 th ON
      (tr.`ho_hongbao_id` = th.`hb_id` OR
      th.`hb_pid` = tr.`ho_hongbao_id` )
      WHERE th.`hb_uid` = ${uid}
      AND (th.`hb_type` = 5 OR th.`hb_type` = 6 OR
      th.`hb_type` = 7)
      <if test="day != null and day == 1">
         AND TO_DAYS(tc.`co_create_time`) = TO_DAYS(NOW())
      </if>
      <if test="day != null and day == 2">
         AND TO_DAYS(NOW()) - TO_DAYS(tc.`co_create_time`) = 1
      </if>
   <select id="countHistoryOrder" resultType="java.util.HashMap">
      <!-- 统计历史订单数量 -->
      SELECT SUM(A.self)AS totalSelf,SUM(A.shared)AS totalShared,SUM(A.invite)AS totalInvite
      FROM (
         <!-- 返利订单 -->
         SELECT IFNULL(COUNT(ho_id),0) AS self,0 AS shared,0 AS invite
         FROM  (SELECT ho.`ho_id` FROM yeshi_ec_hongbao_order ho
            LEFT JOIN (
               SELECT * FROM yeshi_ec_hongbao_v2 v2
               WHERE v2.`hb_uid`= ${uid} AND (v2.`hb_type` = 1 OR v2.`hb_type` = 2)
               ) hb  ON hb.hb_id=ho.`ho_hongbao_id` <!-- 自购订单 -->
         LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
         WHERE hb.hb_id IS NOT NULL
            <if test="day != null and day == 1"> <!-- 今天 -->
               AND TO_DAYS(co.`co_third_create_time`) = TO_DAYS(NOW())
            </if>
            <if test="day != null and day == 2">  <!-- 昨天 -->
               AND TO_DAYS(NOW()) - TO_DAYS(co.co_third_create_time) = 1
            </if>
            <if test="day != null and day == 3">  <!-- 本月 -->
               AND DATE_FORMAT(co.`co_third_create_time`,'%Y%m') =
               DATE_FORMAT(CURDATE(),'%Y%m')
            </if>
            <if test="day != null and day == 4">  <!-- 上月 -->
               AND PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m'),
               DATE_FORMAT(co.`co_settle_time`, '%Y%m')) = 1
            </if>
         GROUP BY co.`co_order_no`, co.`co_source_type`)YX
         UNION ALL
         <!-- 分享订单 -->
         SELECT 0 AS self,IFNULL(COUNT(ho_id),0) AS shared,0 AS invite
         FROM  (SELECT ho.`ho_id` FROM yeshi_ec_hongbao_order ho
               LEFT JOIN (
                  SELECT * FROM yeshi_ec_hongbao_v2 v2
                  WHERE v2.`hb_uid`= ${uid} AND v2.`hb_type` = 20
               ) hb  ON hb.hb_id=ho.`ho_hongbao_id`  <!-- 分享订单 -->
            LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
            WHERE hb.hb_id IS NOT NULL
               <if test="day != null and day == 1"> <!-- 今天 -->
                  AND TO_DAYS(co.`co_third_create_time`) = TO_DAYS(NOW())
               </if>
               <if test="day != null and day == 2">  <!-- 昨天 -->
                  AND TO_DAYS(NOW()) - TO_DAYS(co.`co_third_create_time`) = 1
               </if>
               <if test="day != null and day == 3">  <!-- 本月 -->
                  AND DATE_FORMAT(co.`co_third_create_time`,'%Y%m') =
                  DATE_FORMAT(CURDATE(),'%Y%m')
               </if>
               <if test="day != null and day == 4">  <!-- 上月 -->
                  AND PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m'),
                  DATE_FORMAT(co.`co_settle_time`, '%Y%m')) = 1
               </if>
            GROUP BY co.`co_order_no`, co.`co_source_type`)WQ
         UNION ALL
         <!-- 邀请订单 -->
         SELECT 0 AS self,0 AS shared,IFNULL(COUNT(ho_id),0) AS invite
         FROM  (SELECT ho.`ho_id` FROM yeshi_ec_hongbao_order ho
            LEFT JOIN (
               SELECT * FROM yeshi_ec_hongbao_v2 v2
               WHERE v2.`hb_uid`= ${uid}
                   AND (v2.`hb_type` = 5 OR v2.`hb_type` = 6 OR v2.`hb_type` = 7
                        OR v2.`hb_type` = 21 OR v2.`hb_type` = 22 )
               ) hbp  ON hbp.hb_pid=ho.`ho_hongbao_id`
            LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
            WHERE  hbp.hb_id IS NOT NULL
               <if test="day != null and day == 1"> <!-- 今天 -->
                  AND TO_DAYS(co.`co_third_create_time`) = TO_DAYS(NOW())
               </if>
               <if test="day != null and day == 2">  <!-- 昨天 -->
                  AND TO_DAYS(NOW()) - TO_DAYS(co.`co_third_create_time`) = 1
               </if>
               <if test="day != null and day == 3">  <!-- 本月 -->
                  AND DATE_FORMAT(co.`co_third_create_time`,'%Y%m') =
                  DATE_FORMAT(CURDATE(),'%Y%m')
               </if>
               <if test="day != null and day == 4">  <!-- 上月 -->
                  AND PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m'),
                  DATE_FORMAT(co.`co_settle_time`, '%Y%m')) = 1
               </if>
            GROUP BY co.`co_order_no`, co.`co_source_type`)SX
      )A
   </select>
   <select id="countByUidAndOrderState" resultType="java.util.HashMap">
      SELECT SUM(valid)AS totalValid, SUM(proces)AS totalProces
      ,SUM(Invalid)AS totalInvite
      FROM (SELECT IFNULL(COUNT(DISTINCT
      tc.`co_order_no`,tc.`co_source_type`),0) AS valid,0 AS proces,0 AS
      Invalid
      FROM yeshi_ec_common_order tc
      LEFT JOIN yeshi_ec_hongbao_order
      tr ON tr.`ho_order_id` = tc.`co_id`
      LEFT JOIN yeshi_ec_hongbao_v2 th ON
      (tr.`ho_hongbao_id` = th.`hb_id` OR
      th.`hb_pid` = tr.`ho_hongbao_id` )
      WHERE th.`hb_uid` = #{uid} AND
      (tc.`co_state` = 1 OR tc.`co_state` = 2)
      <if test="type != null and type == 1"> <!-- 自购订单 -->
         AND (th.hb_type =1 or th.hb_type =2)
      </if>
      <if test="type != null and type == 2"> <!-- 分享订单 -->
         AND (th.`hb_type` = 20 OR th.`hb_type` = 21 OR th.`hb_type` = 22)
      </if>
      <if test="type != null and type == 3"> <!-- 邀请订单 -->
         AND (th.`hb_type` = 5 OR th.`hb_type` = 6 OR th.`hb_type` = 7)
      </if>
      <if test="startTime != null">
         AND <![CDATA[tc.co_create_time >= #{startTime}]]>
      </if>
      <if test="endTime != null">
         AND <![CDATA[tc.co_create_time <= #{endTime}]]>
      </if>
      <if test="day != null and day == 1"> <!-- 今天 -->
         AND TO_DAYS(tc.`co_create_time`) = TO_DAYS(NOW())
      </if>
      <if test="day != null and day == 2">  <!-- 昨天 -->
         AND TO_DAYS(NOW()) - TO_DAYS(tc.`co_create_time`) = 1
      </if>
      <if test="day != null and day == 3">  <!-- 本月 -->
         AND DATE_FORMAT(tc.`co_create_time`,'%Y%m') =
         DATE_FORMAT(CURDATE(),'%Y%m')
      </if>
      <if test="day != null and day == 4">  <!-- 上月 -->
         AND PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m'),
         DATE_FORMAT(tc.`co_create_time`, '%Y%m')) = 1
      </if>
      UNION
      SELECT 0 AS valid,IFNULL(COUNT(DISTINCT
      tc.`co_order_no`,tc.`co_source_type`),0) AS proces,0 AS Invalid
      FROM
      yeshi_ec_common_order tc
      LEFT JOIN yeshi_ec_hongbao_order tr ON
      tr.`ho_order_id` = tc.`co_id`
      LEFT JOIN yeshi_ec_hongbao_v2 th ON
      (tr.`ho_hongbao_id` = th.`hb_id` OR
      th.`hb_pid` = tr.`ho_hongbao_id` )
      WHERE th.`hb_uid` = #{uid} AND
      tc.`co_state` =3
      <if test="type != null and type == 1"> <!-- 自购订单 -->
         AND (th.hb_type =1 or th.hb_type =2)
      </if>
      <if test="type != null and type == 2"> <!-- 分享订单 -->
         AND (th.`hb_type` = 20 OR th.`hb_type` = 21 OR th.`hb_type` = 22)
      </if>
      <if test="type != null and type == 3"> <!-- 邀请订单 -->
         AND (th.`hb_type` = 5 OR th.`hb_type` = 6 OR th.`hb_type` = 7)
      </if>
      <if test="startTime != null">
         AND <![CDATA[tc.co_create_time >= #{startTime}]]>
      </if>
      <if test="endTime != null">
         AND <![CDATA[tc.co_create_time <= #{endTime}]]>
      </if>
      <if test="day != null and day == 1"> <!-- 今天 -->
         AND TO_DAYS(tc.`co_create_time`) = TO_DAYS(NOW())
      </if>
      <if test="day != null and day == 2">  <!-- 昨天 -->
         AND TO_DAYS(NOW()) - TO_DAYS(tc.`co_create_time`) = 1
      </if>
      <if test="day != null and day == 3">  <!-- 本月 -->
         AND DATE_FORMAT(tc.`co_create_time`,'%Y%m') =
         DATE_FORMAT(CURDATE(),'%Y%m')
      </if>
      <if test="day != null and day == 4">  <!-- 上月 -->
         AND PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m'),
         DATE_FORMAT(tc.`co_create_time`, '%Y%m')) = 1
      </if>
      UNION
      SELECT 0 AS valid,0 AS proces,IFNULL(COUNT(DISTINCT
      tc.`co_order_no`,tc.`co_source_type`),0) AS Invalid
      FROM
      yeshi_ec_common_order tc
      LEFT JOIN yeshi_ec_hongbao_order tr ON
      tr.`ho_order_id` = tc.`co_id`
      LEFT JOIN yeshi_ec_hongbao_v2 th ON
      (tr.`ho_hongbao_id` = th.`hb_id` OR
      th.`hb_pid` = tr.`ho_hongbao_id` )
      WHERE th.`hb_uid`= #{uid} AND
      tc.`co_state` = 4
      <if test="type != null and type == 1"> <!-- 自购订单 -->
         AND (th.hb_type =1 or th.hb_type =2)
      </if>
      <if test="type != null and type == 2"> <!-- 分享订单 -->
         AND (th.`hb_type` = 20 OR th.`hb_type` = 21 OR th.`hb_type` = 22)
      </if>
      <if test="type != null and type == 3"> <!-- 邀请订单 -->
         AND (th.`hb_type` = 5 OR th.`hb_type` = 6 OR th.`hb_type` = 7)
      </if>
      <if test="startTime != null">
         AND <![CDATA[tc.co_create_time >= #{startTime}]]>
      </if>
      <if test="endTime != null">
         AND <![CDATA[tc.co_create_time <= #{endTime}]]>
      </if>
      <if test="day != null and day == 1"> <!-- 今天 -->
         AND TO_DAYS(tc.`co_create_time`) = TO_DAYS(NOW())
      </if>
      <if test="day != null and day == 2">  <!-- 昨天 -->
         AND TO_DAYS(NOW()) - TO_DAYS(tc.`co_create_time`) = 1
      </if>
      <if test="day != null and day == 3">  <!-- 本月 -->
         AND DATE_FORMAT(tc.`co_create_time`,'%Y%m') =
         DATE_FORMAT(CURDATE(),'%Y%m')
      </if>
      <if test="day != null and day == 4">  <!-- 上月 -->
         AND PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m'),
         DATE_FORMAT(tc.`co_create_time`, '%Y%m')) = 1
      </if>
      SELECT SUM(A.valid)AS totalValid, SUM(A.proces)AS totalProces,SUM(A.Invalid)AS totalInvite
      FROM (
         <!-- 有效订单 -->
         SELECT IFNULL(COUNT(ho_id),0) AS valid,0 AS proces,0 AS Invalid
         FROM  (SELECT ho.`ho_id` FROM yeshi_ec_hongbao_order ho
         LEFT JOIN (
            SELECT * FROM yeshi_ec_hongbao_v2 v2
            WHERE v2.`hb_uid`=${uid} <include refid="SELECT_PARAM_HONGBAO_TYPE" />
         ) hb  ON IF(hb.hb_pid IS NULL,hb.hb_id,hb.hb_pid)=ho.`ho_hongbao_id`
         LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
         WHERE hb.hb_id IS NOT NULL
              <![CDATA[AND co.`co_state` <> 3]]>
              AND (co.`co_state_whole_order` =1 OR co.`co_state_whole_order` = 2)
            <include refid="SELECT_PARAM_ORDER_CREATE_TIME" />
         GROUP BY co.`co_order_no`, co.`co_source_type`)YX
         UNION ALL
         <!-- 维权订单 -->
         SELECT 0 AS valid,IFNULL(COUNT(ho_id),0) AS proces,0 AS Invalid
         FROM  (SELECT ho.`ho_id` FROM yeshi_ec_hongbao_order ho
            LEFT JOIN (
               SELECT * FROM yeshi_ec_hongbao_v2 v2
               WHERE v2.`hb_uid`=${uid} <include refid="SELECT_PARAM_HONGBAO_TYPE" />
            ) hb  ON IF(hb.hb_pid IS NULL,hb.hb_id,hb.hb_pid)=ho.`ho_hongbao_id`
            LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
            WHERE hb.hb_id IS NOT NULL
                 AND co.`co_state` = 3
               <include refid="SELECT_PARAM_ORDER_CREATE_TIME" />
            GROUP BY co.`co_order_no`, co.`co_source_type`)WQ
         UNION ALL
         <!-- 失效订单 -->
         SELECT 0 AS valid,0 AS proces,IFNULL(COUNT(ho_id),0) AS Invalid
         FROM  (SELECT ho.`ho_id` FROM yeshi_ec_hongbao_order ho
            LEFT JOIN (
               SELECT * FROM yeshi_ec_hongbao_v2 v2
               WHERE v2.`hb_uid`=${uid} <include refid="SELECT_PARAM_HONGBAO_TYPE" />
            ) hb  ON IF(hb.hb_pid IS NULL,hb.hb_id,hb.hb_pid)=ho.`ho_hongbao_id`
            LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
            WHERE hb.hb_id IS NOT NULL
               AND co.`co_state_whole_order` = 3
               <include refid="SELECT_PARAM_ORDER_CREATE_TIME" />
            GROUP BY co.`co_order_no`, co.`co_source_type`)SX
      )A
   </select>
   <select id="countBonusOrder" resultType="java.lang.Long">
      SELECT COALESCE(COUNT(DISTINCT
      tc.`co_order_no`,tc.`co_source_type`),0) FROM yeshi_ec_common_order tc
      LEFT JOIN yeshi_ec_hongbao_order tr ON tr.`ho_order_id` = tc.`co_id`
      LEFT JOIN yeshi_ec_hongbao_v2 th ON (tr.`ho_hongbao_id` = th.`hb_id`
      OR th.`hb_pid` = tr.`ho_hongbao_id` )
      WHERE 1=1
      <if test="uid != null">
         AND th.`hb_uid` = ${uid}
      </if>
      <if test="type != null and type == 1"> <!-- 自购订单 -->
         AND (th.hb_type =1 or th.hb_type =2)
      </if>
      <if test="type != null and type == 2"> <!-- 分享订单 -->
         AND (th.`hb_type` = 20 OR th.`hb_type` = 21 OR th.`hb_type` = 22)
      </if>
      <if test="type != null and type == 3"> <!-- 邀请订单 -->
         AND (th.`hb_type` = 5 OR th.`hb_type` = 6 OR th.`hb_type` = 7)
      </if>
      <if test="day != null and day == 1"> <!-- 今天 -->
         AND TO_DAYS(tc.`co_create_time`) = TO_DAYS(NOW())
      </if>
      <if test="day != null and day == 2">  <!-- 昨天 -->
         AND TO_DAYS(NOW()) - TO_DAYS(tc.`co_create_time`) = 1
      </if>
      <if test="day != null and day == 3">  <!-- 本月 -->
         AND DATE_FORMAT(tc.`co_create_time`,'%Y%m') =
         DATE_FORMAT(CURDATE(),'%Y%m')
      </if>
      <if test="day != null and day == 4">  <!-- 上月 -->
         AND PERIOD_DIFF(DATE_FORMAT(NOW(), '%Y%m'),
         DATE_FORMAT(tc.`co_create_time`, '%Y%m')) = 1
      </if>
   <select id="countBonusOrderNumber" resultType="java.lang.Long">
      <!-- 奖金统计:订单数量 -->
      SELECT IFNULL(COUNT(ho_id),0)
      FROM  (SELECT ho.`ho_id` FROM yeshi_ec_hongbao_order ho
            LEFT JOIN (
               SELECT * FROM yeshi_ec_hongbao_v2 v2
               WHERE v2.`hb_uid`=${uid} <include refid="SELECT_PARAM_HONGBAO_TYPE" />
            ) hb  ON IF(hb.hb_pid IS NULL,hb.hb_id,hb.hb_pid)=ho.`ho_hongbao_id`
            LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
            WHERE hb.hb_id IS NOT NULL
               <include refid="SELECT_PARAM_ORDER_CREATE_TIME" />
            GROUP BY co.`co_order_no`, co.`co_source_type`
      )A
   </select>
   <select id="countBonusOrderMoney" resultType="java.math.BigDecimal">
      SELECT COALESCE(SUM(th.`hb_money`),0)AS totalmoney FROM
      yeshi_ec_common_order tc
      LEFT JOIN yeshi_ec_hongbao_order tr ON
      tr.`ho_order_id` = tc.`co_id`
      LEFT JOIN yeshi_ec_hongbao_v2 th ON
      (tr.`ho_hongbao_id` = th.`hb_id` OR
      th.`hb_pid` = tr.`ho_hongbao_id` )
      WHERE <![CDATA[tc.`co_state` <> 4]]>
      <if test="uid != null">
         AND th.`hb_uid` = ${uid}
      </if>
      <if test="type != null and type == 1"> <!-- 自购订单 -->
         AND (th.hb_type =1 or th.hb_type =2)
      </if>
      <if test="type != null and type == 2"> <!-- 分享订单 -->
         AND (th.`hb_type` = 20 OR th.`hb_type` = 21 OR th.`hb_type` = 22)
      </if>
      <if test="type != null and type == 3"> <!-- 邀请订单 -->
         AND (th.`hb_type` = 5 OR th.`hb_type` = 6 OR th.`hb_type` = 7)
      </if>
      <if test="day != null and day == 1"> <!-- 今天 -->
         AND TO_DAYS(tc.`co_create_time`) = TO_DAYS(NOW())
      </if>
      <if test="day != null and day == 2">  <!-- 昨天 -->
         AND TO_DAYS(NOW()) - TO_DAYS(tc.`co_create_time`) = 1
      </if>
      <if test="day != null and day == 3">  <!-- 本月 -->
         AND DATE_FORMAT(tc.`co_create_time`,'%Y%m') =
         DATE_FORMAT(CURDATE(),'%Y%m')
      </if>
      <if test="day != null and day == 4">  <!-- 上月 -->
         AND PERIOD_DIFF( DATE_FORMAT(NOW(), '%Y%m'),
         DATE_FORMAT(tc.`co_create_time`, '%Y%m')) = 1
      </if>
      <!-- 奖金统计:订单总返利金额 去掉已失效 -->
      SELECT COALESCE(SUM(hb.`hb_money`),0)AS totalmoney
      FROM yeshi_ec_hongbao_order ho
      LEFT JOIN (
         SELECT <include refid="Hongbao_Column_List"/> FROM yeshi_ec_hongbao_v2 v2
         WHERE v2.`hb_uid`=${uid} <include refid="SELECT_PARAM_HONGBAO_TYPE" />
              AND (v2.hb_state = 1 or v2.hb_state = 2 or v2.hb_state = 3)
      ) hb  ON IF(hb.hb_pid IS NULL,hb.hb_id,hb.hb_pid)=ho.`ho_hongbao_id`
      LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
      WHERE hb.hb_id IS NOT NULL
         <include refid="SELECT_PARAM_ORDER_CREATE_TIME" />
         <if test="day != null and day == 4">
            AND co.`co_state` = 2  <!-- 上月 :统计已收货 -->
         </if>
   </select>
   <select id="countBonusOrderMoneyAndNumber" resultType="java.util.HashMap">
      SELECT COUNT(*) AS totalNum, CAST(SUM(moneys) AS DECIMAL(19,2)) AS totalmoney FROM
         (SELECT CAST(SUM(hb.`hb_money`) AS DECIMAL(19,2))AS moneys
         FROM yeshi_ec_hongbao_order ho
         LEFT JOIN (
            SELECT <include refid="Hongbao_Column_List"/> FROM yeshi_ec_hongbao_v2 v2
            WHERE v2.`hb_uid`=${uid} <include refid="SELECT_PARAM_HONGBAO_TYPE" />
                 AND (v2.hb_state = 1 or v2.hb_state = 2 or v2.hb_state = 3)
         ) hb  ON IF(hb.hb_pid IS NULL,hb.hb_id,hb.hb_pid)=ho.`ho_hongbao_id`
         LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
         WHERE hb.hb_id IS NOT NULL
            <include refid="SELECT_PARAM_ORDER_CREATE_TIME" />
            <if test="day != null and day == 4">
               AND co.`co_state` = 2 <!-- 上月 :统计已收货 -->
            </if>
            GROUP BY co.`co_order_no`, co.`co_source_type`
         )A
   </select>
   <select id="listQueryByUid" resultMap="ResultMap">
       <!-- 查询用户订单 -->
      SELECT  hb.`hb_money` AS totalMoney,
            hb.hb_state AS hongBaoState,
            hb.`hb_type` AS hongBaoType,
            hb.`hb_get_time`  AS accountTime,
            hb.`hb_pre_get_time` AS preAccountTime,
            co.*,cog.*
      FROM yeshi_ec_hongbao_order ho
      LEFT JOIN (SELECT * FROM yeshi_ec_hongbao_v2 v2
               WHERE v2.`hb_uid`= #{uid}
                  <include refid="SELECT_PARAM_HONGBAO_TYPE"/>  <!-- 红包类型 -->
                  <include refid="SELECT_PARAM_HONGBAO_STATE"/> <!-- 红包状态 -->
            )hb ON IF(hb.hb_pid IS NULL,hb.hb_id,hb.hb_pid)=ho.`ho_hongbao_id`
      LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
      LEFT JOIN yeshi_ec_common_order_goods cog ON cog.`cog_id`= co.`co_order_goods_id`
      WHERE hb.hb_id IS NOT NULL
           <include refid="SELECT_PARAM_ORDER_STATE"/>        <!-- 订单状态 -->
           <include refid="SELECT_PARAM_ORDER_CREATE_TIME"/>  <!-- 订单时间 -->
           <if test="orderNo != null and orderNo !='' ">
            AND co.co_order_no = #{orderNo}
           </if>
      ORDER BY co.co_third_create_time DESC
      LIMIT ${start},${count}
   </select>
   <select id="listBonusOrder" resultMap="ResultMap">
      SELECT COALESCE(SUM(th.`hb_money`),0)AS totalMoney,tc.*,th.hb_state AS
      hongBaoState, th.hb_type AS hongBaoType,
      th.hb_get_time AS
      accountTime,th.hb_get_time AS accountTime,th.hb_pre_get_time AS
      preAccountTime
      FROM yeshi_ec_common_order tc
      LEFT JOIN
      yeshi_ec_hongbao_order tr ON tr.`ho_order_id` = tc.`co_id`
      LEFT JOIN
      yeshi_ec_hongbao_v2 th ON (tr.`ho_hongbao_id` = th.`hb_id` OR
      th.`hb_pid` = tr.`ho_hongbao_id` )
      WHERE th.`hb_uid` = #{uid}
      <if test="type != null and type == 1"> <!-- 自购订单 -->
         AND (th.hb_type =1 or th.hb_type =2)
      </if>
      <if test="type != null and type == 2"> <!-- 分享订单 -->
         AND (th.`hb_type` = 20 OR th.`hb_type` = 21 OR th.`hb_type` = 22)
      </if>
      <if test="type != null and type == 3"> <!-- 邀请订单 -->
         AND (th.`hb_type` = 5 OR th.`hb_type` = 6 OR th.`hb_type` = 7)
      </if>
      <if test="orderState != null and orderState == 1">
         AND (tc.co_state = 1 or tc.co_state = 2) <!-- 有效订单 -->
      </if>
      <if test="orderState != null and orderState == 2">
         AND tc.co_state = 3 <!-- 维权订单 -->
      </if>
      <if test="orderState != null and orderState == 3">
         AND tc.co_state = 4 <!-- 失效订单 -->
      </if>
      <if test="day != null and day == 1"> <!-- 今天 -->
         AND TO_DAYS(tc.`co_create_time`) = TO_DAYS(NOW())
      </if>
      <if test="day != null and day == 2">  <!-- 昨天 -->
         AND TO_DAYS(NOW()) - TO_DAYS(tc.`co_create_time`) = 1
      </if>
      <if test="day != null and day == 3">  <!-- 本月 -->
         AND DATE_FORMAT(tc.`co_create_time`,'%Y%m') =
         DATE_FORMAT(CURDATE(),'%Y%m')
      </if>
      <if test="day != null and day == 4">  <!-- 上月 -->
         AND PERIOD_DIFF( DATE_FORMAT(NOW(), '%Y%m'),
         DATE_FORMAT(tc.`co_create_time`, '%Y%m')) = 1
      </if>
      GROUP BY tc.`co_order_no`,tc.co_source_type
      ORDER BY tc.co_create_time
      DESC
      LIMIT ${start},${count}
   <select id="countQueryByUid" resultType="java.lang.Long">
       <!-- 统计用户订单数量 -->
      SELECT IFNULL(COUNT(ho_id),0) FROM
         (SELECT ho.ho_id FROM yeshi_ec_hongbao_order ho
          LEFT JOIN (SELECT * FROM yeshi_ec_hongbao_v2 v2
               WHERE v2.`hb_uid`= #{uid}
                  <include refid="SELECT_PARAM_HONGBAO_TYPE"/>  <!-- 红包类型 -->
                  <include refid="SELECT_PARAM_HONGBAO_STATE"/> <!-- 红包状态 -->
            )hb ON IF(hb.hb_pid IS NULL,hb.hb_id,hb.hb_pid)=ho.`ho_hongbao_id`
           LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
          WHERE hb.hb_id IS NOT NULL
                 <include refid="SELECT_PARAM_ORDER_STATE"/>        <!-- 订单状态 -->
                <include refid="SELECT_PARAM_ORDER_CREATE_TIME"/>  <!-- 订单时间 -->
               <if test="orderNo != null and orderNo !='' ">
                  AND co.co_order_no = #{orderNo}
               </if>
      )A
   </select>
   <select id="getBuFenOrderState" resultMap="ResultMap">
      SELECT co.co_state,v2.`hb_state` AS hongBaoState  FROM `yeshi_ec_common_order` co
      LEFT JOIN  yeshi_ec_hongbao_order ho  ON co.`co_id`=ho.`ho_order_id`
      LEFT JOIN  yeshi_ec_hongbao_v2 v2 ON IF(v2.hb_pid IS NULL,v2.hb_id,v2.hb_pid)=ho.`ho_hongbao_id`
      WHERE co.`co_order_no` = #{orderNo} AND co.co_state<![CDATA[<>]]> 4 AND v2.hb_uid = #{uid}
      ORDER BY co.`co_state` DESC
      LIMIT 1
   </select>
   <select id="listQuery" resultMap="ResultMap">
       SELECT A.*,u.id AS userId,u.`nick_name` AS userName,u.`portrait` AS userPortrait,
          pv2.`hb_uid` AS levelOneId,pv2.`hb_money` AS levelOneMoney,
          ppv2.`hb_uid` AS levelTwoId,ppv2.`hb_money` AS levelTwoMoney
       FROM  (SELECT  hb.hb_id,hb.hb_uid,hb.`hb_money` AS totalMoney,
            hb.hb_state AS hongBaoState,
            hb.`hb_type` AS hongBaoType,
            hb.`hb_get_time`  AS accountTime,
            hb.`hb_pre_get_time` AS preAccountTime,
            co.*,cog.*
         FROM yeshi_ec_hongbao_order ho
         LEFT JOIN (SELECT * FROM yeshi_ec_hongbao_v2 v2
                  WHERE 1=1
                     <include refid="SELECT_PARAM_HONGBAO_TYPE"/>  <!-- 红包类型 -->
                     <include refid="SELECT_PARAM_HONGBAO_STATE"/> <!-- 红包状态 -->
               )hb ON IF(hb.hb_pid IS NULL,hb.hb_id,hb.hb_pid)=ho.`ho_hongbao_id`
         LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
         LEFT JOIN yeshi_ec_common_order_goods cog ON cog.`cog_id`= co.`co_order_goods_id`
         WHERE hb.hb_id IS NOT NULL
            <include refid="SELECT_PARAM_ORDER_STATE"/>   <!-- 订单状态 -->
            <if test="startTime != null and startTime != '' ">
               AND <![CDATA[co.co_third_create_time >= #{startTime}]]>
            </if>
            <if test="endTime != null and endTime != '' ">
               AND <![CDATA[co.co_third_create_time <= #{endTime}]]>
            </if>
            <if test="key != null and key != '' ">
               <if test="keyType == 1 ">
                  AND co.co_order_no = ${key}
               </if>
               <if test="keyType == 2 ">
                  AND hb.hb_uid like = ${key}
               </if>
            </if>
         ORDER BY co.co_third_create_time DESC
         LIMIT ${start},${count}
      ) A
      LEFT JOIN  yeshi_ec_user u ON  u.id = A.hb_uid
      LEFT JOIN  yeshi_ec_hongbao_v2 pv2 ON pv2.`hb_pid` = A.hb_id
      LEFT JOIN  yeshi_ec_hongbao_v2 ppv2 ON ppv2.`hb_pid` = A.hb_id
   </select>
   <select id="countQuery" resultType="java.lang.Long">
       SELECT  count(ho.ho_id)
       FROM yeshi_ec_hongbao_order ho
      LEFT JOIN (SELECT * FROM yeshi_ec_hongbao_v2 v2
               WHERE 1=1
                  <include refid="SELECT_PARAM_HONGBAO_TYPE"/>  <!-- 红包类型 -->
                  <include refid="SELECT_PARAM_HONGBAO_STATE"/> <!-- 红包状态 -->
            )hb ON IF(hb.hb_pid IS NULL,hb.hb_id,hb.hb_pid)=ho.`ho_hongbao_id`
      LEFT JOIN yeshi_ec_common_order co ON co.`co_id`=ho.`ho_order_id`
      LEFT JOIN yeshi_ec_common_order_goods cog ON cog.`cog_id`= co.`co_order_goods_id`
      WHERE hb.hb_id IS NOT NULL
         <include refid="SELECT_PARAM_ORDER_STATE"/>   <!-- 订单状态 -->
         <if test="startTime != null and startTime != '' ">
            AND <![CDATA[co.co_third_create_time >= #{startTime}]]>
         </if>
         <if test="endTime != null and endTime != '' ">
            AND <![CDATA[co.co_third_create_time <= #{endTime}]]>
         </if>
         <if test="key != null and key != '' ">
            <if test="keyType == 1 ">
               AND co.co_order_no = ${key}
            </if>
            <if test="keyType == 2 ">
               AND hb.hb_uid like = ${key}
            </if>
         </if>
   </select>
</mapper>