admin
2019-03-13 69bee82b81626b82b7f39f0e459e4f56b1699b51
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?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.yeshi.fanli.dao.mybatis.HongBaoV2CountMapper">
 
    <sql id="Column_DateType">
        <if test="dateType == 1">
            DATE_FORMAT(t.`hb_create_time`,'%Y-%m-%d') AS 'showDate'
        </if>
        <if test="dateType == 2">
            DATE_FORMAT(t.`hb_create_time`,'%m') AS 'showDate'
        </if>
        <if test="dateType == 3">
            DATE_FORMAT(t.`hb_create_time`,'%Y') AS 'showDate'
        </if>
    </sql>
 
    <sql id="Count_Select_DateType">
        <if test="startTime != null and startTime != '' ">
            AND DATE_FORMAT(t.`hb_create_time`,'%Y-%m-%d')<![CDATA[ >= ]]>'${startTime}'
        </if>
        <if test="endTime != null and endTime != '' ">
            AND DATE_FORMAT(t.`hb_create_time`,'%Y-%m-%d') <![CDATA[ <= ]]>'${endTime}'
        </if>
        <if test="year != null and year != '' ">
            AND DATE_FORMAT(t.`hb_create_time`,'%Y') = '${year}'
        </if>
    </sql>
 
    <sql id="Count_Group_DateType">
        <if test="dateType == 1">
            GROUP BY DATE_FORMAT(t.`hb_create_time`,'%Y-%m-%d')
        </if>
        <if test="dateType == 2">
            GROUP BY DATE_FORMAT(t.`hb_create_time`,'%Y-%m')
        </if>
        <if test="dateType == 3">
            GROUP BY DATE_FORMAT(t.`hb_create_time`,'%Y')
        </if>
    </sql>
    <sql id="Count_Select_State">
        <if test="state != null and state == 1">  <!-- 未到账 -->
            AND (t.hb_state = 1 or t.hb_state = 2)
        </if>
        <if test="state != null and state == 2"> <!-- 已到账 -->
            AND t.hb_state = 3
        </if>
        <if test="state != null and state == 3">  <!-- 红包已失效 -->
            AND t.hb_state =4
        </if>
    </sql>
    
    <sql id="Count_Select_Type">
        <if test="type != null and type == 1"> <!-- 自购订单 -->
            AND (t.hb_type =1 or t.hb_type =2)
        </if>
        <if test="type != null and type == 2"> <!-- 分享订单 -->
            AND t.`hb_type` = 20
        </if>
        <if test="type != null and type == 3"> <!-- 邀请订单 -->
            AND (t.`hb_type` = 5 OR t.`hb_type` = 6 OR t.`hb_type` = 7 OR t.`hb_type` = 21 OR t.`hb_type` = 22)
        </if>
    </sql>
    
    <select id="countHongBaoMoney" resultType="java.util.HashMap">
        SELECT CAST(SUM(t.`hb_money`)AS DECIMAL(19,2)) AS showValue,<include refid="Column_DateType"/>
        FROM yeshi_ec_hongbao_v2 t
        <if test="channel != null and  channel != 'all' ">
            LEFT JOIN (SELECT a.`lua_uid` AS uid FROM `yeshi_ec_log_user_active` a WHERE a.`lua_channel`='${channel}' GROUP BY a.`lua_uid`) a ON t.`hb_uid`=a.uid 
        </if>
        WHERE t.`hb_uid` IS NOT NULL AND (t.`hb_type`=1 OR t.`hb_type`=20) 
            <include refid="Count_Select_DateType"/> 
            <include refid="Count_Select_State"/>
            <if test="channel != null and  channel != 'all' ">
                AND a.uid is not null
            </if>
            
        <include refid="Count_Group_DateType"/>
        ORDER BY t.`hb_create_time`
    </select>
    
    <select id="countHongBaoNum" resultType="java.util.HashMap">
        SELECT COUNT(t.`hb_id`)AS showValue,<include refid="Column_DateType"/>
        FROM yeshi_ec_hongbao_v2 t 
        <if test="channel != null and  channel != 'all' ">
            LEFT JOIN (SELECT a.`lua_uid` AS uid FROM `yeshi_ec_log_user_active` a WHERE a.`lua_channel`='${channel}' GROUP BY a.`lua_uid`) a ON t.`hb_uid`=a.uid 
        </if>
        WHERE t.`hb_uid` IS NOT NULL AND (t.`hb_type`=1 OR t.`hb_type`=20) 
        
            <include refid="Count_Select_DateType"/> 
            <include refid="Count_Select_State"/>
            <if test="channel != null and  channel != 'all' ">
                AND a.uid is not null
            </if>
            
        <include refid="Count_Group_DateType"/>
        ORDER BY t.`hb_create_time`
    </select>
    
    
    <select id="countHongBaoType" resultType="java.util.HashMap">
        SELECT COUNT(t.`hb_id`)AS showValue,<include refid="Column_DateType"/>
        FROM yeshi_ec_hongbao_v2 t 
        WHERE t.`hb_uid` IS NOT NULL 
            <include refid="Count_Select_DateType"/> 
            <include refid="Count_Select_Type"/> 
        <include refid="Count_Group_DateType"/>
        ORDER BY t.`hb_create_time`
    </select>
    
    <select id="countHongBaoTotalNum" resultType="java.util.HashMap">
        SELECT COUNT(t.`hb_id`)AS showValue,<include refid="Column_DateType"/>
        FROM yeshi_ec_hongbao_v2 t  
        WHERE t.`hb_uid` IS NOT NULL AND t.`hb_type` not in(1,3,4) <include refid="Count_Select_DateType"/> 
        <include refid="Count_Group_DateType"/>
        ORDER BY t.`hb_create_time`
    </select>
    
    
    <select id="countMoneyByUidAndState" resultType="java.math.BigDecimal">
        SELECT CAST(SUM(hb.`hb_money`)AS DECIMAL(19,2)) 
        FROM `yeshi_ec_hongbao_v2` hb
        WHERE hb.`hb_uid` = #{uid}  
            <if test="state != null">
                 AND hb.`hb_state` = #{state}
            </if>    
    </select>
    
    <select id="countNumberByUidAndState" resultType="java.lang.Integer">
        SELECT COALESCE(count(hb_id),0) FROM yeshi_ec_hongbao_v2
        WHERE hb_uid = #{uid}
            <if test="state != null">
                 AND hb_state = #{state}
            </if>    
    </select>
    
    
    <select id="getTotalTiChengCount" resultType="java.lang.Integer" parameterType="java.lang.Long">
        SELECT count(*) FROM `yeshi_ec_hongbao_v2` h
        WHERE h.`hb_uid`=#{0} 
            AND (h.`hb_type`=20 OR h.`hb_type`=21 OR h.`hb_type`=22 OR h.`hb_type`=6 OR h.`hb_type`=7)
    </select>
    
    <select id="countValidNumberByUid" resultType="java.lang.Long">
        <!--统计有效订单数量 -->
        SELECT COALESCE(count(hb.hb_id),0) FROM `yeshi_ec_hongbao_v2` hb
        WHERE hb.hb_uid =#{uid} 
            AND hb.hb_type not in (3,4)
            AND <![CDATA[hb.hb_state <> 4]]>
        
        <if test="dateType != null and dateType == 1">
            AND TO_DAYS(FROM_UNIXTIME(hb.`hb_create_time`/1000)) = TO_DAYS(NOW())
        </if>
        <if test="dateType != null and dateType == 2">
            AND DATE_FORMAT( FROM_UNIXTIME(hb.`hb_create_time`/1000),'%Y%m' ) =
                DATE_FORMAT( CURDATE( ) , '%Y%m' )
        </if>
    </select>
    
    <select id="countWillGetMoneyByUid" resultType="java.math.BigDecimal">
            SELECT CAST(SUM(hb.`hb_money`)AS DECIMAL(19,2)) 
        FROM `yeshi_ec_hongbao_v2` hb
        WHERE hb.`hb_uid` = #{uid}  AND hb.`hb_state` in (1,2)
    </select>
    
    <select id="getLastHongBaoTime" resultType="java.util.Date">
        <!-- 最近一次下单时间 -->
        SELECT h.`hb_create_time` FROM yeshi_ec_hongbao_v2 h WHERE h.`hb_type` IN(1,2) <![CDATA[AND h.hb_state <>4]]>
        AND h.`hb_uid` = #{uid}
        ORDER BY h.`hb_create_time` DESC 
        LIMIT 1;
    </select>
    
    
    <select id="count24HOderByChannel" resultType="java.util.HashMap">
        SELECT <!-- COALESCE(SUM(b.money*2),0) AS 'commision', -->
        COALESCE(COUNT(b.hb_id),0) AS 'showValue',
        <!-- COALESCE(SUM(b.payMoney),0) AS 'payMoney', -->
        <if test="type == 1">
            FROM_UNIXTIME(u.`createtime`/1000,'%Y-%m-%d') AS 'showDate'
        </if>
        <if test="type == 2">
            FROM_UNIXTIME(u.`createtime`/1000,'%m') AS 'showDate'
        </if>
        <if test="type == 3">
            FROM_UNIXTIME(u.`createtime`/1000,'%Y') AS 'showDate'
        </if>
        FROM
            (
            SELECT h.* FROM yeshi_ec_hongbao_v2 h
            LEFT JOIN (SELECT a.`lua_uid` AS uid FROM `yeshi_ec_log_user_active` a
            WHERE a.`lua_channel`='${channel}' GROUP BY a.`lua_uid`) a
            ON a.uid=h.`hb_uid` WHERE a.uid IS NOT NULL AND (h.`hb_type`=1 OR h.`hb_type`=20)
            ) b
        LEFT JOIN yeshi_ec_user u ON u.`id`=b.hb_uid
        WHERE <![CDATA[ b.hb_create_time<FROM_UNIXTIME((u.`createtime`+1000*60*60*24)/1000,'%Y-%m-%d %h:%m:%s')]]>
        <if test="startTime != null and startTime != '' ">
            AND FROM_UNIXTIME(u.`createtime`/1000,'%Y-%m-%d')<![CDATA[ >= ]]>'${startTime}'
        </if>
        <if test="endTime != null and endTime != '' ">
            AND FROM_UNIXTIME(u.`createtime`/1000,'%Y-%m-%d') <![CDATA[ <= ]]>
            '${endTime}'
        </if>
        <if test="years != null and years != '' ">
            AND FROM_UNIXTIME(u.`createtime`/1000,'%Y') = '${years}'
        </if>
        <if test="type == 1">
            GROUP BY FROM_UNIXTIME(u.`createtime`/1000,'%Y-%m-%d')
        </if>
        <if test="type == 2">
            GROUP BY FROM_UNIXTIME(u.`createtime`/1000,'%Y-%m')
        </if>
        <if test="type == 3">
            GROUP BY FROM_UNIXTIME(u.`createtime`/1000,'%Y')
        </if>
        ORDER BY u.`createtime`
 
    </select>
    
</mapper>