初始
This commit is contained in:
99
test/user_project_tans_test.go
Normal file
99
test/user_project_tans_test.go
Normal file
@ -0,0 +1,99 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/samber/lo"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model/entity"
|
||||
|
||||
wxDao "github.com/tiger1103/gfast/v3/internal/app/wxApplet/dao"
|
||||
wxApplet "github.com/tiger1103/gfast/v3/internal/app/wxApplet/model/do"
|
||||
)
|
||||
|
||||
// 获取所有的 sys_user 的用户,如果该用户在 bus_construction_user 中的手机号相同
|
||||
// 则将该用户的在 bus_construction_user 中的项目绑定到 App 中
|
||||
func TestUserProjectTans(t *testing.T) {
|
||||
// 获取所有的 sys_user 的用户
|
||||
sysUsers := GetSysUsers()
|
||||
|
||||
// 提取出所有用户的手机号
|
||||
userMobiles := lo.FilterMap(sysUsers, func(user entity.SysUser, _ int) (string, bool) {
|
||||
if user.Mobile == "" {
|
||||
return "", false
|
||||
}
|
||||
return user.Mobile, true
|
||||
})
|
||||
|
||||
// 获取所有的 bus_construction_user 的用户
|
||||
busUsers := GetBusUsers(userMobiles)
|
||||
|
||||
// 1. 获取所有的 sys_user 的用户
|
||||
// 2. 获取所有的 bus_construction_user 的用户
|
||||
// 3. 如果该用户在 bus_construction_user 中的手机号相同
|
||||
// 4. 则将该用户的在 bus_construction_user 中的项目绑定到PC中
|
||||
|
||||
// 批量插入列表
|
||||
var insertList []wxApplet.SysUserProjectRelevancy
|
||||
|
||||
for _, sysUser := range sysUsers {
|
||||
if busUser, ok := busUsers[sysUser.Mobile]; ok {
|
||||
// 如果该数据在 sys_user_project_relevancy 中不存在,则插入
|
||||
|
||||
exist, err := wxDao.SysUserProjectRelevancy.Ctx(context.Background()).
|
||||
Where(wxDao.SysUserProjectRelevancy.Columns().UserId, sysUser.Id).
|
||||
Where(wxDao.SysUserProjectRelevancy.Columns().ProjectId, busUser.ProjectId).
|
||||
Where("source", 0).
|
||||
Count()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if exist == 0 {
|
||||
insertList = append(insertList, wxApplet.SysUserProjectRelevancy{
|
||||
UserId: sysUser.Id,
|
||||
ProjectId: busUser.ProjectId,
|
||||
Source: 0,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(insertList) > 0 {
|
||||
if _, err := wxDao.SysUserProjectRelevancy.Ctx(context.Background()).Insert(insertList); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取所有的 sys_user 的用户
|
||||
func GetSysUsers() []entity.SysUser {
|
||||
List := []entity.SysUser{}
|
||||
|
||||
ctx := context.Background()
|
||||
// 获取所有的 sys_user 的用户
|
||||
if err := dao.SysUser.Ctx(ctx).Scan(&List); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return List
|
||||
}
|
||||
|
||||
// 获取所有的 bus_construction_user 的用户
|
||||
func GetBusUsers(mobiles []string) map[string]entity.BusConstructionUser {
|
||||
ctx := context.Background()
|
||||
|
||||
List := []entity.BusConstructionUser{}
|
||||
// 获取所有的 bus_construction_user 的用户
|
||||
if err := dao.BusConstructionUser.Ctx(ctx).
|
||||
WhereIn(dao.BusConstructionUser.Columns().Phone, mobiles).
|
||||
WhereNotNull(dao.BusConstructionUser.Columns().ProjectId).
|
||||
Scan(&List); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return lo.Associate(List, func(item entity.BusConstructionUser) (string, entity.BusConstructionUser) {
|
||||
return item.Phone, item
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user