yujian
2020-05-06 eb1adc13eda955e8ef0fc9fe41fb246fa89b722d
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?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.ThreeSaleMapper">
    <resultMap id="BaseResultMap" type="com.yeshi.fanli.entity.bus.user.ThreeSale">
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="state" property="state" jdbcType="BOOLEAN" />
        <result column="succeedTime" property="succeedTime" jdbcType="BIGINT" />
        <result column="createTime" property="createTime" jdbcType="BIGINT" />
        <result column="updateTime" property="updateTime" jdbcType="BIGINT" />
        <result column="expire" property="expire" jdbcType="INTEGER" />
        
        <result column="validState" property="validState" jdbcType="INTEGER" />
        
        <association property="boss" column="boss_id"
            javaType="com.yeshi.fanli.entity.bus.user.UserInfo">
            <id column="boss_id" property="id" jdbcType="BIGINT" />
        </association>
 
        <association property="worker" column="worker_id"
            javaType="com.yeshi.fanli.entity.bus.user.UserInfo">
            <id column="worker_id" property="id" jdbcType="BIGINT" />
        </association>
    </resultMap>
 
    <resultMap id="AllResultMap" type="com.yeshi.fanli.entity.bus.user.ThreeSale">
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="state" property="state" jdbcType="BOOLEAN" />
        <result column="succeedTime" property="succeedTime" jdbcType="BIGINT" />
        <result column="createTime" property="createTime" jdbcType="BIGINT" />
        <result column="updateTime" property="updateTime" jdbcType="BIGINT" />
        <result column="expire" property="expire" jdbcType="INTEGER" />
 
        <association property="boss" column="boss_id"
            select="com.yeshi.fanli.dao.mybatis.UserInfoMapper.selectByPrimaryKey" />
 
        <association property="worker" column="worker_id"
            select="com.yeshi.fanli.dao.mybatis.UserInfoMapper.selectByPrimaryKey" />
 
        <association property="threeSaleExtraInfo" column="tf_id"
            resultMap="com.yeshi.fanli.dao.mybatis.ThreeSaleExtraInfoMapper.BaseResultMap" />
 
    </resultMap>
 
    <sql id="Base_Column_List">id,boss_id,worker_id,state,expire,succeedTime,createTime,updateTime
    </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap"
        parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List" />
        from yeshi_ec_threeSale where id = #{id,jdbcType=BIGINT}
    </select>
 
 
 
    <select id="selectBoss"    resultMap="com.yeshi.fanli.dao.mybatis.UserInfoMapper.BaseResultMap" parameterType="java.lang.Long">
        select u.* from yeshi_ec_threeSale s 
        left join yeshi_ec_user u on u.id=s.boss_id 
        where s.state = 1  AND s.worker_id = #{0}
        LIMIT 1
    </select>
 
 
 
    <!-- 根据邀请者ID查询关系 -->
    <select id="listByWorkerId" resultMap="BaseResultMap"
        parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List" />
        from yeshi_ec_threeSale s where worker_id = #{0}
    </select>
 
 
    <!-- 根据邀请者ID查询关系 -->
    <select id="getByWorkerId" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select <include refid="Base_Column_List" /> from yeshi_ec_threeSale 
        where worker_id = #{0}
    </select>
    
    <select id="selectLatestByWorkerIdAndState" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from yeshi_ec_threeSale s where worker_id = #{workerId} and state=#{state}
    </select>
    
 
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">delete from
        yeshi_ec_threeSale where id = #{id,jdbcType=BIGINT}
    </delete>
    <insert id="insert" parameterType="com.yeshi.fanli.entity.bus.user.ThreeSale"
        useGeneratedKeys="true" keyProperty="id">insert into yeshi_ec_threeSale
        (id,boss_id,worker_id,state,expire,succeedTime,createTime,updateTime)
        values
        (#{id,jdbcType=BIGINT},#{boss.id,jdbcType=BIGINT},#{worker.id,jdbcType=BIGINT},#{state,jdbcType=BOOLEAN},#{expire,jdbcType=INTEGER},#{succeedTime,jdbcType=BIGINT},#{createTime,jdbcType=BIGINT},#{updateTime,jdbcType=BIGINT})
    </insert>
    <insert id="insertSelective" parameterType="com.yeshi.fanli.entity.bus.user.ThreeSale"
        useGeneratedKeys="true" keyProperty="id">
        insert into yeshi_ec_threeSale
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="boss != null">boss_id,</if>
            <if test="worker != null">worker_id,</if>
            <if test="state != null">state,</if>
            <if test="expire != null">expire,</if>
            <if test="succeedTime != null">succeedTime,</if>
            <if test="createTime != null">createTime,</if>
            <if test="updateTime != null">updateTime,</if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id,jdbcType=BIGINT},</if>
            <if test="boss != null">#{boss.id,jdbcType=BIGINT},</if>
            <if test="worker != null">#{worker.id,jdbcType=BIGINT},</if>
            <if test="state != null">#{state,jdbcType=BOOLEAN},</if>
            <if test="expire != null">#{expire,jdbcType=INTEGER},</if>
            <if test="succeedTime != null">#{succeedTime,jdbcType=BIGINT},</if>
            <if test="createTime != null">#{createTime,jdbcType=BIGINT},</if>
            <if test="updateTime != null">#{updateTime,jdbcType=BIGINT},</if>
        </trim>
    </insert>
    <update id="updateByPrimaryKey" parameterType="com.yeshi.fanli.entity.bus.user.ThreeSale">update
        yeshi_ec_threeSale set boss_id = #{boss.id,jdbcType=BIGINT},worker_id
        = #{worker.id,jdbcType=BIGINT},state =
        #{state,jdbcType=BOOLEAN},
        expire = #{expire,jdbcType=INTEGER},
        succeedTime =
        #{succeedTime,jdbcType=BIGINT},
        createTime =
        #{createTime,jdbcType=BIGINT},
        updateTime =
        #{updateTime,jdbcType=BIGINT}
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKeySelective" parameterType="com.yeshi.fanli.entity.bus.user.ThreeSale">
        update yeshi_ec_threeSale
        <set>
            <if test="boss != null">boss_id=#{boss.id,jdbcType=BIGINT},</if>
            <if test="worker != null">worker_id=#{worker.id,jdbcType=BIGINT},</if>
            <if test="state != null">state=#{state,jdbcType=BOOLEAN},</if>
            <if test="expire != null">expire=#{expire,jdbcType=INTEGER},</if>
            <if test="succeedTime != null">succeedTime=#{succeedTime,jdbcType=BIGINT},</if>
            <if test="createTime != null">createTime=#{createTime,jdbcType=BIGINT},</if>
            <if test="updateTime != null">updateTime=#{updateTime,jdbcType=BIGINT},</if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
 
    
    <update id="inviteSeparate">
        update yeshi_ec_threeSale t set t.state = 0,t.expire = 2,
            t.updateTime = <![CDATA[UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) * 1000]]>
        where t.worker_id=#{workerId} AND t.boss_id = #{bossId}
    </update>
 
 
    <select id="listFirstTeamQuery" resultMap="BaseResultMap">
        SELECT t.*,d.`tvr_type` AS validState  FROM `yeshi_ec_threesale` t
        LEFT JOIN `yeshi_ec_user_invite_valid_record` d ON (d.`tvr_uid` = t.`boss_id` AND d.`tvr_worker_id` = t.`worker_id`)
        WHERE 1=1
        <if test="uid != null">
            AND  t.`boss_id` = #{uid}
        </if>
        <if test="state != null">
            AND t.`state` =  #{state}
        </if>
        <if test="validState == 1">
            AND d.`tvr_type` = 1
        </if>
        <if test="validState == 0">
            AND d.`tvr_type` is null
        </if>
        <if test="startTime != null and startTime !='' ">
            <![CDATA[AND FROM_UNIXTIME(t.createTime/1000) >= #{startTime}]]>
        </if>
        <if test="endTime != null and  endTime !='' ">
              <![CDATA[AND FROM_UNIXTIME(t.createTime/1000) <= #{endTime}]]>
        </if>
        ORDER BY  t.createTime DESC
        LIMIT ${start},${count}
    </select>
 
    <select id="countFirstTeamQuery" resultType="java.lang.Long">
        SELECT count(t.id) FROM `yeshi_ec_threesale` t
        LEFT JOIN `yeshi_ec_user_invite_valid_record` d ON (d.`tvr_uid` = t.`boss_id` AND d.`tvr_worker_id` = t.`worker_id`)
        WHERE 1=1
        <if test="uid != null">
            AND  t.`boss_id` = #{uid}
        </if>
        <if test="state != null">
            AND t.`state` =  #{state}
        </if>
        <if test="validState == 1">
            AND d.`tvr_type` = 1
        </if>
        <if test="validState == 0">
            AND d.`tvr_type` is null
        </if>
        <if test="startTime != null and startTime !='' ">
            <![CDATA[AND FROM_UNIXTIME(t.createTime/1000) >= #{startTime}]]>
        </if>
        <if test="endTime != null and  endTime !='' ">
              <![CDATA[AND FROM_UNIXTIME(t.createTime/1000) <= #{endTime}]]>
        </if>
    </select>
 
 
    <select id="listSecondTeamQuery" resultMap="BaseResultMap">
        SELECT yet.*,d.`tvr_type` AS validState  FROM (SELECT DISTINCT(ts.`id`) AS id FROM    `yeshi_ec_threesale` ts
                          LEFT JOIN yeshi_ec_threesale tf ON ts.`boss_id`=tf.`worker_id` WHERE 1=1  AND tf.`state` = 1
        <if test="uid != null">
            AND tf.`boss_id` = #{uid}
        </if>
        <if test="state != null">
            AND ts.`state` =  #{state}
        </if>
        <if test="startTime != null and startTime !='' ">
              <![CDATA[AND FROM_UNIXTIME(ts.createTime/1000) >= #{startTime}]]>
        </if>
        <if test="endTime != null and  endTime !=''">
              <![CDATA[AND FROM_UNIXTIME(ts.createTime/1000) <= #{endTime}]]>
        </if>
        )A LEFT JOIN yeshi_ec_threesale yet ON yet.id = A.id
        LEFT JOIN `yeshi_ec_user_invite_valid_record` d ON (d.`tvr_uid` = yet.`boss_id` AND d.`tvr_worker_id` = yet.`worker_id`)
        where 1=1 
        <if test="validState == 1">
            AND d.`tvr_type` = 1
        </if>
        <if test="validState == 0">
            AND d.`tvr_type` is null
        </if>
        ORDER BY yet.`createtime` DESC
        LIMIT ${start},${count}
    </select>
 
    <select id="countSecondTeamQuery" resultType="java.lang.Long">
        SELECT COUNT(A.id) FROM (SELECT DISTINCT(ts.`id`) AS id FROM `yeshi_ec_threesale` ts
        LEFT JOIN yeshi_ec_threesale tf ON
        ts.`boss_id`=tf.`worker_id`
        WHERE 1=1 AND tf.`state` = 1
        <if test="uid != null">
            AND tf.`boss_id` = #{uid}
        </if>
        <if test="state != null">
            AND ts.`state` =  #{state}
        </if>
        <if test="startTime != null and startTime !='' ">
             <![CDATA[AND FROM_UNIXTIME(ts.createTime/1000) >= #{startTime}]]>
        </if>
        <if test="endTime != null and endTime !=''">
             <![CDATA[AND FROM_UNIXTIME(ts.createTime/1000) <= #{endTime}]]>
        </if>
        )A
        LEFT JOIN yeshi_ec_threesale yet ON yet.id = A.id
        LEFT JOIN `yeshi_ec_user_invite_valid_record` d ON (d.`tvr_uid` = yet.`boss_id` AND d.`tvr_worker_id` = yet.`worker_id`)
        where 1=1 
        <if test="validState == 0">
            AND d.`tvr_type` is null
        </if>
        <if test="validState == 1">
            AND d.`tvr_type` = 1
        </if>
    </select>
 
    <select id="listSuperiorQuery" resultMap="BaseResultMap">
        SELECT * FROM `yeshi_ec_threesale` ts
        WHERE ts.`worker_id` = #{uid}
        <if test="state == 0">
            AND ts.`state` = 0 AND (ts.expire = 0 OR ts.expire IS
            NULL)
        </if>
        <if test="state == 1">
            AND ts.`state` = 1
        </if>
        <if test="state ==2">
            AND ts.`expire` = 1  <!-- 失效 -->
        </if>
        ORDER BY ts.`createtime` DESC
        LIMIT ${start},${count}
    </select>
 
    <select id="countSuperiorQuery" resultType="java.lang.Long">
        SELECT COUNT(ts.id) FROM `yeshi_ec_threesale` ts
        WHERE ts.`worker_id` =
        #{uid}
        <if test="state == 0">
            AND ts.`state` = 0 AND (ts.expire = 0 OR ts.expire IS
            NULL)
        </if>
        <if test="state == 1">
            AND ts.`state` = 1
        </if>
        <if test="state ==2">
            AND ts.`expire` = 1  <!-- 失效 -->
        </if>
    </select>
 
 
    <select id="listFirstTeam" resultMap="AllResultMap">
        SELECT * FROM `yeshi_ec_threesale` ts
        LEFT JOIN `yeshi_ec_threesale_extra_info` ff ON (ts.`boss_id` = ff.`tf_boss_id` AND ts.`worker_id`=ff.`tf_worker_id`)
        WHERE ts.`boss_id` = #{uid}
         <if test="listId != null and listId.size > 0">
            AND ts.worker_id in
            <foreach collection="listId" item="item" open="(" separator="," close=")">#{item}</foreach>
        </if>
        ORDER BY ts.createTime DESC
        LIMIT ${start},${count}
    </select>
 
    <select id="countFirstTeam" resultType="java.lang.Long">
        SELECT IFNULL(count(id),0) FROM `yeshi_ec_threesale`
        WHERE `boss_id` = #{uid}
        <if test="listId != null and listId.size > 0">
            AND worker_id in
            <foreach collection="listId" item="item" open="(" separator="," close=")">#{item}</foreach>
        </if>
    </select>
 
    <select id="listSecondTeam" resultMap="AllResultMap">
        SELECT yet.* FROM (
            SELECT DISTINCT(ts.`id`) AS id FROM `yeshi_ec_threesale` ts
            LEFT JOIN yeshi_ec_threesale tf ON ts.`boss_id`= tf.`worker_id`
            WHERE tf.`boss_id` = #{uid})A
        LEFT JOIN yeshi_ec_threesale yet ON yet.id = A.id
        <if test="listId != null and listId.size > 0">
            WHERE yet.worker_id in
            <foreach collection="listId" item="item" open="(" separator="," close=")">#{item}</foreach>
        </if>
        ORDER BY yet.createTime DESC
        LIMIT ${start},${count}
    </select>
 
    <select id="countSecondTeam" resultType="java.lang.Long">
        SELECT COUNT(A.id) FROM (
            SELECT DISTINCT(ts.`id`) AS id FROM `yeshi_ec_threesale` ts
            LEFT JOIN yeshi_ec_threesale tf ON ts.`boss_id`=tf.`worker_id`
            WHERE tf.`boss_id` = #{uid}
            <if test="listId != null and listId.size > 0">
                AND tf.worker_id in
                <foreach collection="listId" item="item" open="(" separator="," close=")">#{item}</foreach>
            </if>
        )A
    </select>
    
    <select id="getMyBoss" resultMap="AllResultMap">
        select
        <include refid="Base_Column_List" />
        from yeshi_ec_threeSale where
        state = 1 and worker_id=#{uid}
    </select>
 
 
    <select id="listbyIdAndBossId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM yeshi_ec_threeSale
        WHERE id = #{id} AND boss_id=#{uid}
        <if test="expire != null and expire == 1">
            AND `expire` = 1  <!-- 失效 -->
        </if>
    </select>
 
    <select id="getSuccessRelationshipNum" resultType="java.lang.Integer">
        SELECT IFNULL(COUNT(*),0) FROM `yeshi_ec_threesale` t
        WHERE t.`state` = 1 AND    (t.`worker_id` =${uid} OR t.`boss_id` = ${uid})
    </select>
 
    <select id="getRelationshipByBossIdAndWorkerId" resultMap="BaseResultMap">
        SELECT * FROM `yeshi_ec_threesale` t
        WHERE t.`boss_id` = ${bossId} AND
        t.`worker_id` = ${workerId}
        AND t.`state` = 0 AND (t.`expire` = 0 OR
        t.`expire`IS NULL)
        LIMIT 1
    </select>
    
    
    <select id="getNearRelationByBossIdAndWorkerId" resultMap="BaseResultMap">
        SELECT * FROM `yeshi_ec_threesale` t
        WHERE t.`boss_id` = #{bossId} AND t.`worker_id` = #{workerId}
        ORDER BY t.`state` DESC, t.`createTime` DESC
        LIMIT 1
    </select>
 
 
    <select id="selectSuccessByWorkerId" resultMap="BaseResultMap" parameterType="java.lang.Long">
        SELECT * FROM `yeshi_ec_threesale`
        WHERE `state` = 1 AND worker_id = #{0}  order by id desc
    </select>
 
    <select id="getSuccessByDate" resultMap="AllResultMap" >
        SELECT * FROM yeshi_ec_threesale t
        WHERE t.`boss_id` = #{bossId} AND t.`state` = 1
             AND  <![CDATA[t.createTime >= #{minTime} AND t.createTime <= #{maxTime}]]>
    </select>
    
    <select id="getByWorkerIdAndTime" resultMap="BaseResultMap">
        SELECT * FROM yeshi_ec_threesale t
        WHERE t.`worker_id` = #{workerId} AND t.`state` = 1
             <![CDATA[AND t.succeedTime >= #{time}]]>
    </select>
    
    <select id="getTeamOrderNumByWorkerId" resultType="Long">
        SELECT IFNULL(COUNT(d.`id`),0)  FROM yeshi_ec_threesale d
        LEFT JOIN (SELECT t.`id`,t.`boss_id` FROM `yeshi_ec_threesale` t
                     WHERE t.`state` = 1 AND t.`worker_id` = #{workerId} LIMIT 1)t ON t.`boss_id` = d.`boss_id`
        WHERE d.`id` <![CDATA[<=]]>t.id AND d.`state` = 1 
    </select>
    
    
    <select id="getValidWorkerIdsByTime" resultMap="BaseResultMap">
        SELECT * FROM `yeshi_ec_threesale` tr 
        WHERE tr.`state` = 1 AND tr.`boss_id` = #{uid} AND tr.`succeedTime` <![CDATA[>=]]> #{limitTime}
    </select>
    
</mapper>