Browse Source

fix:修改beego默认主库

zqbao 10 months ago
parent
commit
c5042ebc8f

+ 1 - 1
models/crm_config.go

@@ -9,7 +9,7 @@ type CrmConfig struct {
 }
 
 func GetConfigDetailByCode(configCode string) (item CrmConfig, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := ` SELECT config_value FROM crm_config WHERE config_code=? `
 	err = o.Raw(sql, configCode).QueryRow(&item)
 	return

+ 5 - 5
models/db.go

@@ -16,12 +16,12 @@ func init() {
 	report_db, _ := orm.GetDB("rddp")
 	report_db.SetConnMaxLifetime(10 * time.Minute)
 
-	_ = orm.RegisterDataBase("master", "mysql", utils.MYSQL_URL_MASTER)
-	orm.SetMaxIdleConns("master", 50)
-	orm.SetMaxOpenConns("master", 100)
+	_ = orm.RegisterDataBase("default", "mysql", utils.MYSQL_URL_MASTER)
+	orm.SetMaxIdleConns("default", 50)
+	orm.SetMaxOpenConns("default", 100)
 
-	master_db, _ := orm.GetDB("master")
-	master_db.SetConnMaxLifetime(10 * time.Minute)
+	db, _ := orm.GetDB("default")
+	db.SetConnMaxLifetime(10 * time.Minute)
 
 	orm.Debug = true
 	orm.DebugLog = orm.NewLog(utils.BinLog)

+ 13 - 13
models/sys_department.go

@@ -17,13 +17,13 @@ type SysDepartment struct {
 }
 
 func (s *SysDepartment) Add() (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.Insert(s)
 	return
 }
 
 func (s *SysDepartment) Update(cols []string) (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.Update(s, cols...)
 	return
 }
@@ -37,7 +37,7 @@ type SysDepartmentList struct {
 }
 
 func DeleteSysDepartmentById(sysDepartmentIds []string, level int) (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
 		sql := `DELETE FROM sys_department WHERE sys_department_id IN (?) `
 		_, e := txOrm.QueryTable(&SysDepartment{}).
@@ -72,7 +72,7 @@ func DeleteSysDepartmentById(sysDepartmentIds []string, level int) (err error) {
 }
 
 func DeleteSysDepartmentByIdV2(sysDepartmentIds []string, level int) (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
 		// sql := `DELETE FROM sys_department WHERE sys_department_id IN (?) `
 		_, e := txOrm.QueryTable(&SysDepartment{}).
@@ -99,7 +99,7 @@ func DeleteSysDepartmentByIdV2(sysDepartmentIds []string, level int) (err error)
 }
 
 func UpdateDepartmentSortByIds(sysDepartmentIds []int) (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
 		sql := `UPDATE sys_department SET sort=? WHERE sys_department_id=?`
 		for i, depId := range sysDepartmentIds {
@@ -114,56 +114,56 @@ func UpdateDepartmentSortByIds(sysDepartmentIds []int) (err error) {
 }
 
 func GetSysDepartmentById(sysDepartmentId int) (item *SysDepartment, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT * FROM sys_department WHERE sys_department_id=? `
 	err = o.Raw(sql, sysDepartmentId).QueryRow(&item)
 	return
 }
 
 func GetSysDepartmentCountById(sysDepartmentId int) (count int, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT COUNT(1) AS count FROM sys_department WHERE sys_department_id=?`
 	err = o.Raw(sql, sysDepartmentId).QueryRow(&count)
 	return
 }
 
 func GetSysDepartmentCountByParentId(parentId int, sysDepartmentName string) (count int, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT COUNT(1) AS count FROM sys_department WHERE parent_id=? AND sys_department_name=?`
 	err = o.Raw(sql, parentId, sysDepartmentName).QueryRow(&count)
 	return
 }
 
 func GetSysDepartmentCountByName(sysDepartmentName string) (count int, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT COUNT(1) AS count FROM sys_department WHERE sys_department_name=?`
 	err = o.Raw(sql, sysDepartmentName).QueryRow(&count)
 	return
 }
 
 func GetSysDepartmentListByLevel(level int) (items []*SysDepartmentList, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT * FROM sys_department WHERE level=? ORDER BY sort`
 	_, err = o.Raw(sql, level).QueryRows(&items)
 	return
 }
 
 func GetSysDepartmentListByParentId(parentId int) (item SysDepartmentList, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT * FROM sys_department WHERE sys_department_id=? `
 	err = o.Raw(sql, parentId).QueryRow(&item)
 	return
 }
 
 func GetChildSysDepartmentListById(sysDepartmentId int) (items []*SysDepartmentList, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT * FROM sys_department WHERE parent_id=? `
 	_, err = o.Raw(sql, sysDepartmentId).QueryRows(&items)
 	return
 }
 
 func GetChildSysDepartmentListByIds(sysDepartmentIds string) (items []*SysDepartmentList, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT * FROM sys_department WHERE parent_id in (?) `
 	_, err = o.Raw(sql, sysDepartmentIds).QueryRows(&items)
 	return

+ 3 - 3
models/sys_menu.go

@@ -54,14 +54,14 @@ func GetMenuButtonsByRoleId(roleId int) (items []*SysMenu, err error) {
 			ORDER BY
 				r.sort ASC,
 				r.create_time DESC`
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.Raw(sql, roleId).QueryRows(&items)
 	return
 }
 
 // GetMenuListByRoleIds 根据管理员角色id查询菜单
 func GetMenuListByRoleIds(roleId int) (items []*SysMenuItem, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT  a.* 
 			FROM sys_menu AS a
 			LEFT JOIN sys_role_menu_mapping AS b ON a.sys_menu_id=b.sys_menu_id 
@@ -73,7 +73,7 @@ func GetMenuListByRoleIds(roleId int) (items []*SysMenuItem, err error) {
 
 // GetSysMenuItemsByCondition 获取菜单列表
 func GetSysMenuItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SysMenu, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	fields := strings.Join(fieldArr, ",")
 	if len(fieldArr) == 0 {
 		fields = `*`

+ 6 - 6
models/sys_message_report.go

@@ -19,33 +19,33 @@ type SysMessageReport struct {
 }
 
 func (s *SysMessageReport) Update(cols []string) (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.Update(s, cols...)
 	return
 }
 
 func GetSysMessageReportCountBySysUserId(sysUserId int) (count int, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT COUNT(*) AS count FROM sys_message_report WHERE receive_sys_user_id=?`
 	err = o.Raw(sql, sysUserId).QueryRow(&count)
 	return
 }
 
 func GetSysMessageReportBySysUserId(sysUserId, startSize, pageSize int) (item []*SysMessageReport, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT * FROM sys_message_report WHERE receive_sys_user_id=? LIMIT ?,?`
 	_, err = o.Raw(sql, sysUserId, startSize, pageSize).QueryRows(&item)
 	return
 }
 
 func InsertMultiSysMessageReport(sysMessageReportList []SysMessageReport) (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.InsertMulti(500, sysMessageReportList)
 	return
 }
 
 func GetSysMessageReportListById(sysMessageReportIds []int) (items []*SysMessageReport, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.QueryTable(&SysMessageReport{}).
 		Filter("sys_message_report_id__in", sysMessageReportIds).
 		All(&items)
@@ -53,7 +53,7 @@ func GetSysMessageReportListById(sysMessageReportIds []int) (items []*SysMessage
 }
 
 func GetSysMessageReportCount(userId int, messageType int) (count int, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT COUNT(*) AS count FROM sys_message_report WHERE user_id=? AND message_type=? `
 	err = o.Raw(sql, userId, messageType).QueryRow(&count)
 	return

+ 9 - 9
models/sys_role.go

@@ -15,27 +15,27 @@ type SysRole struct {
 }
 
 func (s *SysRole) Add() (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.Insert(s)
 	return
 }
 
 func DeleteSysRoleById(roleId int) (err error) {
 	sql := `DELETE FROM sys_role WHERE sys_role_id=? `
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.Raw(sql, roleId).Exec()
 	return
 }
 
 func GetSysRoleCountByRoleName(roleName string) (count int, err error) {
 	sql := `SELECT COUNT(1) AS count FROM sys_role WHERE sys_role_name=? `
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.Raw(sql, roleName).QueryRow(&count)
 	return
 }
 
 func UpdateSysUserRoleByRoleId(roleId int, roleName string) (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
 		role := &SysRole{}
 		role.SysRoleId = roleId
@@ -53,21 +53,21 @@ func UpdateSysUserRoleByRoleId(roleId int, roleName string) (err error) {
 }
 
 func GetSysRoleById(id int) (item *SysRole, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT * FROM sys_role WHERE sys_role_id=?`
 	err = o.Raw(sql, id).QueryRow(&item)
 	return
 }
 
 func GetSysRoleListCount() (count int, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT COUNT(1) AS count FROM sys_role `
 	err = o.Raw(sql).QueryRow(&count)
 	return
 }
 
 func GetSysRoleList(startSize, pageSize int) (items []*SysRole, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT * FROM sys_role ORDER BY modify_time DESC LIMIT ?,?`
 	_, err = o.Raw(sql, startSize, pageSize).QueryRows(&items)
 	return
@@ -78,7 +78,7 @@ func CreateMultiSysRoleMenu(roleId int, items []*SysRoleMenuMapping) (err error)
 	if roleId == 0 {
 		return
 	}
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
 		sql := `DELETE FROM sys_role_menu_mapping WHERE sys_role_id = ?`
 		_, e := txOrm.Raw(sql, roleId).Exec()
@@ -99,7 +99,7 @@ func CreateMultiSysRoleMenu(roleId int, items []*SysRoleMenuMapping) (err error)
 
 // GetSysRoleMenuByRoleId 获取角色关联菜单
 func GetSysRoleMenuByRoleId(roleId int) (items []*SysRoleMenuMapping, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT * FROM sys_role_menu_mapping WHERE sys_role_id = ?`
 	_, err = o.Raw(sql, roleId).QueryRows(&items)
 	return

+ 3 - 3
models/sys_session.go

@@ -17,7 +17,7 @@ type SysSession struct {
 }
 
 func (s *SysSession) AddSysSession() (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	insertId, err := o.Insert(s)
 	s.SysSessionId = int(insertId)
 	return
@@ -25,7 +25,7 @@ func (s *SysSession) AddSysSession() (err error) {
 
 func GetSysSessionByToken(token string) (item *SysSession, err error) {
 	sql := `SELECT * FROM sys_session WHERE access_token=? AND expired_time> NOW() ORDER BY expired_time DESC LIMIT 1 `
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.Raw(sql, token).QueryRow(&item)
 	return
 }
@@ -33,7 +33,7 @@ func GetSysSessionByToken(token string) (item *SysSession, err error) {
 // ExpiredSysSessionBySysUserId 过期掉用户token
 func ExpiredSysSessionBySysUserId(sysUserId int) (err error) {
 	sql := `UPDATE sys_session SET expired_time = NOW()  WHERE sys_user_id=? AND expired_time > NOW()`
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.Raw(sql, sysUserId).Exec()
 	return
 }

+ 23 - 12
models/sys_user.go

@@ -68,19 +68,19 @@ type SysUserMapping struct {
 }
 
 func (s *SysUser) Save() (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.InsertOrUpdate(s, "sys_user_id")
 	return
 }
 
 func (s *SysUser) Update(cols []string) (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.Update(s, cols...)
 	return
 }
 
 func SaveSysUser(sysUser *SysUser, sysDepartmendPathIds []int) (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sysUser.SysDepartmentId1 = sysDepartmendPathIds[0]
 	sysUser.SysDepartmentId2 = sysDepartmendPathIds[1]
 	sysUser.SysDepartmentId3 = sysDepartmendPathIds[2]
@@ -90,35 +90,35 @@ func SaveSysUser(sysUser *SysUser, sysDepartmendPathIds []int) (err error) {
 
 func GetSysUserBySysUserName(sysUserName string) (item *SysUser, err error) {
 	sql := `SELECT * FROM sys_user WHERE sys_user_name=?`
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.Raw(sql, sysUserName).QueryRow(&item)
 	return
 }
 
 func GetSysUserByDepartmentId(sysDepartmentId int, enabled bool) (items []*SysUser, err error) {
 	sql := `SELECT * FROM sys_user WHERE sys_department_id=? AND is_enabled=?`
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.Raw(sql, sysDepartmentId, enabled).QueryRows(&items)
 	return
 }
 
 func GetSysUserById(sysUserId int) (item *SysUser, err error) {
 	sql := `SELECT * FROM sys_user WHERE sys_user_id=?`
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.Raw(sql, sysUserId).QueryRow(&item)
 	return
 }
 
 func GetSysUserCountById(sysUserId int) (count int, err error) {
 	sql := `SELECT COUNT(1) AS count FROM sys_user WHERE sys_user_id=?`
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.Raw(sql, sysUserId).QueryRow(&count)
 	return
 }
 
 func GetSysUserCountBySysUserName(sysUserName string) (count int, err error) {
 	sql := `SELECT COUNT(1) AS count FROM sys_user WHERE sys_user_name=?`
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.Raw(sql, sysUserName).QueryRow(&count)
 	return
 }
@@ -128,14 +128,14 @@ func GetSysUserCount(condition string, pars []interface{}) (count int, err error
 	if condition != "" {
 		sql += condition
 	}
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.Raw(sql, pars...).QueryRow(&count)
 	return
 }
 
 func GetSysUserCountByRoleId(roleId int) (count int, err error) {
 	sql := `SELECT COUNT(1) AS count FROM sys_user WHERE sys_role_id=?`
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.Raw(sql, roleId).QueryRow(&count)
 	return
 }
@@ -154,14 +154,25 @@ func GetSysUserList(condition string, pars []interface{}, startSize, pageSize in
 		sql += condition
 	}
 	sql += `ORDER BY u.is_enabled, u.modify_time DESC LIMIT ?,?`
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
+	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
+	return
+}
+
+func GetSysUserListV2(condition string, pars []interface{}, startSize, pageSize int) (items []*SysUser, err error) {
+	sql := `SELECT * FROM sys_user u WHERE 1=1 `
+	if condition != "" {
+		sql += condition
+	}
+	sql += `ORDER BY u.is_enabled, u.modify_time DESC LIMIT ?,?`
+	o := orm.NewOrm()
 	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
 	return
 }
 
 func GetSysUserIdList() (items []int, err error) {
 	sql := ` SELECT sys_user_id FROM sys_user WHERE 1=1 `
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.Raw(sql).QueryRows(&items)
 	return
 }

+ 10 - 10
models/user.go

@@ -45,19 +45,19 @@ type UserView struct {
 }
 
 func (u *User) Save() (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.InsertOrUpdate(u)
 	return
 }
 
 func (u *User) Update(cols []string) (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.Update(u, cols...)
 	return
 }
 
 func UpdateUserStatus(condition string, pars []interface{}) (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := ` UPDATE user SET status=0 WHERE 1=1 `
 	if condition != "" {
 		sql += condition
@@ -67,7 +67,7 @@ func UpdateUserStatus(condition string, pars []interface{}) (err error) {
 }
 
 func SaveUser(user *User, chartPermissionIds []int) (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
 		insertId, er := txOrm.InsertOrUpdate(user)
 		if er != nil {
@@ -100,21 +100,21 @@ func SaveUser(user *User, chartPermissionIds []int) (err error) {
 }
 
 func GetUserByPhone(phone, areaCode string) (item *User, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT * FROM user WHERE phone=? AND area_code=?`
 	err = o.Raw(sql, phone, areaCode).QueryRow(&item)
 	return
 }
 
 func GetUserByEmail(email string) (item *User, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT * FROM user WHERE email=? `
 	err = o.Raw(sql, email).QueryRow(&item)
 	return
 }
 
 func GetUserById(userId int) (item *User, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT * FROM user WHERE user_id=? `
 	err = o.Raw(sql, userId).QueryRow(&item)
 	return
@@ -129,7 +129,7 @@ func GetUserList(condition string, pars []interface{}, startSize, pageSize int)
 		sql += condition
 	}
 	sql += ` ORDER BY modify_time DESC LIMIT ?,? `
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
 	return
 }
@@ -139,13 +139,13 @@ func GetUserCount(condition string, pars []interface{}) (count int, err error) {
 	if condition != "" {
 		sql += condition
 	}
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.Raw(sql, pars...).QueryRow(&count)
 	return
 }
 
 func DeleteUserById(userId int) (err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
 		sql := `DELETE FROM user WHERE user_id=?`
 		_, e := txOrm.Raw(sql, userId).Exec()

+ 3 - 3
models/user_change_record.go

@@ -16,21 +16,21 @@ type UserChangeRecord struct {
 
 func (u *UserChangeRecord) Insert() (err error) {
 	u.CreateTime = time.Now()
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.Insert(u)
 	return
 }
 
 func GetUserChangeRecordListById(userId int) (items []*UserChangeRecord, err error) {
 	sql := `SELECT * FROM user_change_record WHERE user_id=? ORDER BY create_time DESC `
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	_, err = o.Raw(sql, userId).QueryRows(&items)
 	return
 }
 
 func GetUserChangeRecordCount() (count int, err error) {
 	sql := `SELECT COUNT(*) AS count FROM user_change_record `
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	err = o.Raw(sql).QueryRow(&count)
 	return
 }

+ 1 - 1
models/user_chart_permission_mapping.go

@@ -9,7 +9,7 @@ type UserChartPermissionMapping struct {
 }
 
 func GetChartPermissionIdByUserId(UserId int) (items []int, err error) {
-	o := orm.NewOrmUsingDB("master")
+	o := orm.NewOrm()
 	sql := `SELECT chart_permission_id FROM user_chart_permission_mapping WHERE user_id=?`
 	_, err = o.Raw(sql, UserId).QueryRows(&items)
 	return

+ 15 - 27
scheduler/task.go

@@ -1,62 +1,50 @@
 package scheduler
 
 import (
+	"context"
 	"eta/eta_mini_crm/models"
 	"eta/eta_mini_crm/utils"
 	"fmt"
 	"time"
 
+	"github.com/beego/beego/v2/task"
 	"github.com/rdlucklib/rdluck_tools/paging"
-	"github.com/robfig/cron/v3"
 )
 
 func InitJob() {
 	fmt.Println("消息推送任务开启。。。")
-	c := cron.New(cron.WithSeconds())
 
 	// 每天凌晨1点检测, 发送消息
-	_, err := c.AddFunc("0 0 1 * * *", CheckUserTime)
-	if err != nil {
-		utils.ApiLog.Info("cron CheckUserTime task err: %s", err.Error())
-	}
+	tk1 := task.NewTask("CheckUserPermission", "0 0 1 * * *", CheckUserTime)
+	task.AddTask("CheckUserPermission", tk1)
 	// 每天凌晨1点检测, 修改用户状态
-	_, err = c.AddFunc("0 0 1 * * *", ModifyUser)
-	if err != nil {
-		utils.ApiLog.Info("cron ModifyUser task err: %s", err.Error())
-	}
-	c.Start()
+	tk2 := task.NewTask("ModifyUserStatus", "0 0 1 * * *", ModifyUser)
+	task.AddTask("ModifyUserStatus", tk2)
+	task.StartTask()
+	defer task.StopTask()
+
 }
 
-func CheckUserTime() {
-	retryCount := 10
+func CheckUserTime(ctx context.Context) error {
 	var err error
 	defer func() {
 		if err != nil {
 			utils.ApiLog.Warn("userPermission 定时任务出错,Err: %s", err.Error())
 		}
 	}()
-	for i := 0; i <= retryCount; i++ {
-		err = CheckUserPermissionTime()
-		if err == nil {
-			break
-		}
-	}
+	err = CheckUserPermissionTime()
+	return err
 }
 
-func ModifyUser() {
-	retryCount := 10
+func ModifyUser(ctx context.Context) error {
 	var err error
 	defer func() {
 		if err != nil {
 			utils.ApiLog.Warn("userPermission 定时任务出错,Err: %s", err.Error())
 		}
 	}()
-	for i := 0; i <= retryCount; i++ {
-		err = ModifyUserStatus()
-		if err == nil {
-			break
-		}
-	}
+	err = ModifyUserStatus()
+	return err
 }
 
 func ModifyUserStatus() (err error) {