Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
push-message
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liucheng
push-message
Commits
ecec3911
Commit
ecec3911
authored
Dec 21, 2021
by
renandong
🇨🇳
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
标签中间表做逻辑删除,并加上时间
parent
2e792609
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
142 additions
and
28 deletions
+142
-28
MenuService.java
src/main/java/com/weface/component/MenuService.java
+6
-1
MetaObjectHandlerConfig.java
src/main/java/com/weface/config/MetaObjectHandlerConfig.java
+30
-0
MybatisPlusConfig.java
src/main/java/com/weface/config/MybatisPlusConfig.java
+7
-0
UserMenusDao.java
src/main/java/com/weface/dao/UserMenusDao.java
+2
-2
TUserTagEntity.java
src/main/java/com/weface/entity/TUserTagEntity.java
+5
-1
UserMenusEntity.java
src/main/java/com/weface/entity/UserMenusEntity.java
+16
-2
UserMenusServiceImpl.java
...ain/java/com/weface/serviceimpl/UserMenusServiceImpl.java
+8
-4
application.yml
src/main/resources/application.yml
+2
-2
TUserTagDao.xml
src/main/resources/mapper/TUserTagDao.xml
+5
-8
UserMenusDao.xml
src/main/resources/mapper/UserMenusDao.xml
+8
-6
PushMessageApplicationTests.java
src/test/java/com/weface/PushMessageApplicationTests.java
+53
-2
No files found.
src/main/java/com/weface/component/MenuService.java
View file @
ecec3911
...
...
@@ -7,6 +7,7 @@ import cn.hutool.core.util.ObjectUtil;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
com.weface.entity.MenuTagsEntity
;
import
com.weface.entity.TUserTagEntity
;
import
com.weface.entity.UserMenusEntity
;
...
...
@@ -102,7 +103,8 @@ public class MenuService {
//收集中间表数据
List
<
UserMenusEntity
>
userMenusList
=
new
ArrayList
<>();
//获取所有用户信息
List
<
TUserTagEntity
>
tUserTagEntities
=
tUserTagService
.
list
();
List
<
TUserTagEntity
>
tUserTagEntities
=
tUserTagService
.
list
(
new
QueryWrapper
<
TUserTagEntity
>()
.
lambda
().
orderByDesc
(
TUserTagEntity:
:
getSaveDate
));
//遍历所有返回用户数据
for
(
Map
<
String
,
Object
>
item
:
userTag
)
{
//拿到当前用户giUid
...
...
@@ -119,11 +121,14 @@ public class MenuService {
//收集标签id
List
<
Long
>
collect
=
list
.
stream
().
map
(
MenuTagsEntity:
:
getId
).
collect
(
Collectors
.
toList
());
//遍历标签id
Date
date
=
new
Date
();
for
(
Long
aLong
:
collect
)
{
//填充数据
UserMenusEntity
userMenusEntity
=
new
UserMenusEntity
();
userMenusEntity
.
setUserId
(
uid
);
userMenusEntity
.
setTagsId
(
aLong
);
userMenusEntity
.
setIsValid
(
1
);
userMenusEntity
.
setCreateTime
(
date
);
//添加数据到集合内
userMenusList
.
add
(
userMenusEntity
);
}
...
...
src/main/java/com/weface/config/MetaObjectHandlerConfig.java
0 → 100644
View file @
ecec3911
package
com
.
weface
.
config
;
import
com.baomidou.mybatisplus.core.handlers.MetaObjectHandler
;
import
org.apache.ibatis.reflection.MetaObject
;
import
java.util.Date
;
/**
* @CreateUser: Administrator
* @CreateTime: 2021/11/16
* 自动填充属性
*/
public
class
MetaObjectHandlerConfig
implements
MetaObjectHandler
{
/**
* 在添加时为公共字段填充属性
* @param metaObject
*/
@Override
public
void
insertFill
(
MetaObject
metaObject
)
{
setFieldValByName
(
"createTime"
,
new
Date
(),
metaObject
);
setFieldValByName
(
"isValid"
,
1
,
metaObject
);
}
@Override
public
void
updateFill
(
MetaObject
metaObject
)
{
}
}
src/main/java/com/weface/config/MybatisPlusConfig.java
View file @
ecec3911
...
...
@@ -28,4 +28,11 @@ public class MybatisPlusConfig {
return
new
PaginationInterceptor
();
}
/**
* 自动填充属性
*/
@Bean
public
MetaObjectHandlerConfig
metaObjectHandlerConfig
(){
return
new
MetaObjectHandlerConfig
();
}
}
src/main/java/com/weface/dao/UserMenusDao.java
View file @
ecec3911
...
...
@@ -19,7 +19,7 @@ public interface UserMenusDao extends BaseMapper<UserMenusEntity> {
//批量插入
int
batchInsert
(
List
<
UserMenusEntity
>
list
);
//
全表删除
int
deleteAll
(
List
<
String
>
list
);
//
根据uid查询标签id
List
<
Long
>
findIdByUserId
(
List
<
String
>
list
);
}
src/main/java/com/weface/entity/TUserTagEntity.java
View file @
ecec3911
...
...
@@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
import
lombok.Data
;
import
java.io.Serializable
;
import
java.util.Date
;
import
java.util.List
;
/**
...
...
@@ -43,7 +44,10 @@ public class TUserTagEntity implements Serializable {
*/
@Excel
(
name
=
"用户Gid"
,
width
=
40
)
private
String
gid
;
/**
* 更新时间
*/
private
Date
saveDate
;
/**
* 标签数据
*/
...
...
src/main/java/com/weface/entity/UserMenusEntity.java
View file @
ecec3911
package
com
.
weface
.
entity
;
import
com.baomidou.mybatisplus.annotation.TableId
;
import
com.baomidou.mybatisplus.annotation.TableName
;
import
com.baomidou.mybatisplus.annotation.*
;
import
lombok.Data
;
import
java.io.Serializable
;
import
java.util.Date
;
/**
* 用户和标签中间表
...
...
@@ -31,5 +31,19 @@ public class UserMenusEntity implements Serializable {
* 标签ID
*/
private
Long
tagsId
;
/**
* 是否删除 1 未删除 0 删除
*/
/**
* 是否有效 1:有效 0 无效
*/
@TableLogic
(
value
=
"1"
,
delval
=
"0"
)
@TableField
(
fill
=
FieldFill
.
INSERT
)
private
Integer
isValid
;
/**
* 创建时间
*/
@TableField
(
fill
=
FieldFill
.
INSERT
)
private
Date
createTime
;
}
src/main/java/com/weface/serviceimpl/UserMenusServiceImpl.java
View file @
ecec3911
package
com
.
weface
.
serviceimpl
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.weface.dao.UserMenusDao
;
import
com.weface.entity.UserMenusEntity
;
...
...
@@ -17,7 +18,6 @@ import java.util.stream.Collectors;
@Transactional
public
class
UserMenusServiceImpl
extends
ServiceImpl
<
UserMenusDao
,
UserMenusEntity
>
implements
UserMenusService
{
/**
* 批量插入
*
...
...
@@ -28,7 +28,11 @@ public class UserMenusServiceImpl extends ServiceImpl<UserMenusDao, UserMenusEnt
if
(
CollectionUtils
.
isNotEmpty
(
userMenusList
))
{
List
<
String
>
deleteUid
=
getDeleteUid
(
userMenusList
);
//批量插入前全删
this
.
baseMapper
.
deleteAll
(
deleteUid
);
List
<
Long
>
ids
=
this
.
baseMapper
.
findIdByUserId
(
deleteUid
);
this
.
removeByIds
(
ids
);
// this.remove(new QueryWrapper<UserMenusEntity>().lambda()
// .eq(UserMenusEntity::getIsValid, 1)
// .in(UserMenusEntity::getUserId, deleteUid));
//获取集合长度
int
count
=
userMenusList
.
size
();
//每次批量插入个数
...
...
@@ -46,7 +50,6 @@ public class UserMenusServiceImpl extends ServiceImpl<UserMenusDao, UserMenusEnt
}
//批量插入每次一万条
sum
+=
this
.
baseMapper
.
batchInsert
(
userMenusEntities
);
}
if
(
sum
>
0
)
{
log
.
warn
(
"新增成功记录=="
+
sum
+
"==条"
);
...
...
@@ -55,7 +58,8 @@ public class UserMenusServiceImpl extends ServiceImpl<UserMenusDao, UserMenusEnt
}
}
}
private
List
<
String
>
getDeleteUid
(
List
<
UserMenusEntity
>
userMenusList
){
private
List
<
String
>
getDeleteUid
(
List
<
UserMenusEntity
>
userMenusList
)
{
return
userMenusList
.
stream
().
map
(
UserMenusEntity:
:
getUserId
).
distinct
().
collect
(
Collectors
.
toList
());
}
...
...
src/main/resources/application.yml
View file @
ecec3911
...
...
@@ -44,8 +44,8 @@ mybatis-plus:
db-config
:
#主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID", ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
id-type
:
AUTO
logic-delete-value
:
-1
logic-not-delete-value
:
0
logic-delete-value
:
0
logic-not-delete-value
:
1
banner
:
false
#原生配置
configuration
:
...
...
src/main/resources/mapper/TUserTagDao.xml
View file @
ecec3911
...
...
@@ -28,12 +28,9 @@
</select>
<!-- 查询当天更新用户gid-->
<select
id=
"findTodayGid"
resultType=
"string"
>
SELECT
gid
FROM
t_user_tag
WHERE
gid IS NOT NULL
SELECT DISTINCT gid
FROM t_user_tag
WHERE gid IS NOT NULL
AND TO_DAYS( save_date ) = TO_DAYS( NOW() )
</select>
<!-- 将公共片段抽取:只针对个别业务-->
...
...
@@ -50,7 +47,7 @@
FROM t_user_tag ut
LEFT JOIN tb_user_menus um ON ut.uid = um.user_id
LEFT JOIN tb_menu_tags mt ON um.tags_id = mt.id
WHERE mt.level_first = "Android"
WHERE mt.level_first = "Android"
AND um.is_valid = 1
</sql>
<!-- 查询用户和标签信息-->
<select
id=
"getUserAndTags"
resultMap=
"tUserTagMap"
>
...
...
@@ -81,7 +78,7 @@
LEFT JOIN tb_user_menus um ON ut.uid = um.user_id
LEFT JOIN tb_menu_tags mt ON um.tags_id = mt.id
WHERE
mt.level_first = "Android"
mt.level_first = "Android"
AND um.is_valid = 1
AND mt.level_third IN
<foreach
item=
"item"
index=
"index"
collection=
"list"
open=
"("
separator=
","
close=
")"
>
#{item}
...
...
src/main/resources/mapper/UserMenusDao.xml
View file @
ecec3911
...
...
@@ -8,21 +8,23 @@
<result
property=
"id"
column=
"id"
/>
<result
property=
"userId"
column=
"user_id"
/>
<result
property=
"tagsId"
column=
"tags_id"
/>
<result
property=
"isValid"
column=
"is_valid"
/>
<result
property=
"createTime"
column=
"create_time"
/>
</resultMap>
<!--批量添加-->
<insert
id=
"batchInsert"
parameterType=
"java.util.List"
>
insert into tb_user_menus(id,user_id,tags_id)
insert into tb_user_menus(id,user_id,tags_id
,is_valid,create_time
)
values
<foreach
collection=
"list"
item=
"item"
index=
"index"
separator=
","
>
(#{item.id},#{item.userId},#{item.tagsId})
(#{item.id},#{item.userId},#{item.tagsId}
,#{item.isValid},#{item.createTime}
)
</foreach>
</insert>
<!--
批量删除
-->
<
delete
id=
"deleteAll
"
>
DELETE FROM tb_user_menus where
user_id IN
<!--
根据uid查询标签id
-->
<
select
id=
"findIdByUserId"
resultType=
"long
"
>
SELECT id FROM tb_user_menus WHERE is_valid = 1 AND
user_id IN
<foreach
collection=
"list"
item=
"item"
index=
"index"
separator=
","
open=
"("
close=
")"
>
#{item}
</foreach>
</
delete
>
</
select
>
</mapper>
\ No newline at end of file
src/test/java/com/weface/PushMessageApplicationTests.java
View file @
ecec3911
package
com
.
weface
;
import
com.weface.component.MenuService
;
import
com.weface.entity.UserMenusEntity
;
import
com.weface.service.TUserTagService
;
import
com.weface.service.UserMenusService
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.concurrent.CountDownLatch
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
@SpringBootTest
class
PushMessageApplicationTests
{
@Autowired
private
TUserTagService
tUserTagService
;
@Autowired
private
MenuService
menuService
;
@Autowired
private
UserMenusService
userMenusService
;
@Test
void
contextLoads
()
{
}
@Test
public
void
test
(){
try
{
List
<
String
>
todayGid
=
tUserTagService
.
findTodayGid
();
int
size
=
todayGid
.
size
();
int
limit
=
1000
;
List
<
UserMenusEntity
>
userMenusList
=
new
ArrayList
<>();
int
totalPage
=
(
size
%
limit
==
0
)
?
(
size
/
limit
)
:
(
size
/
limit
+
1
);
CountDownLatch
latch
=
new
CountDownLatch
(
totalPage
);
ExecutorService
pool
=
Executors
.
newFixedThreadPool
(
totalPage
);
//收集中间表数据
for
(
int
i
=
0
;
i
<
totalPage
;
i
++)
{
int
finalI
=
i
;
Runnable
runnable
=
()
->
{
List
<
String
>
list
=
new
ArrayList
<>();
if
(
finalI
==
totalPage
-
1
)
{
list
=
todayGid
.
subList
(
finalI
*
limit
,
size
);
}
else
{
list
=
todayGid
.
subList
(
finalI
*
limit
,
(
finalI
+
1
)
*
limit
);
}
if
(
CollectionUtils
.
isNotEmpty
(
list
))
{
String
[]
strings
=
list
.
toArray
(
new
String
[
0
]);
Map
<
String
,
Object
>
android
=
menuService
.
getUserTags
(
strings
);
List
<
UserMenusEntity
>
tagUser
=
menuService
.
getTagUser
(
android
);
if
(
CollectionUtils
.
isNotEmpty
(
tagUser
))
{
userMenusList
.
addAll
(
tagUser
);
}
}
latch
.
countDown
();
};
pool
.
execute
(
runnable
);
}
latch
.
await
();
userMenusService
.
batchInsert
(
userMenusList
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment