作者 tangxvhui

更新

package command
//UpdateAdminUserPwdCommand 修改密码
type UpdateAdminUserPwdCommand struct {
Id int64 `json:"id"`
//密码
Password string `json:"password"`
}
... ...
package service
import (
"crypto/sha1"
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/command"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
... ... @@ -23,12 +27,12 @@ func (adminUserSrv AdminUserService) GetAdminUser(getAdminUserQuery *query.GetAd
if err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
// if err := transactionContext.StartTransaction(); err != nil {
// return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
// }
// defer func() {
// transactionContext.RollbackTransaction()
// }()
var (
adminuserRepository domain.AdminUserRepository
adminuser *domain.AdminUser
... ... @@ -114,10 +118,11 @@ func (adminUserSrv AdminUserService) SaveAdminUser(saveUserCmd *command.SaveAdmi
adminuser.Permission = permissionBases
} else {
defaultPwd := fmt.Sprintf("%x", sha1.Sum([]byte("123456")))
adminuser = &domain.AdminUser{
Id: saveUserCmd.Id,
Account: saveUserCmd.Account,
Password: saveUserCmd.Password,
Password: defaultPwd,
AdminName: saveUserCmd.Name,
IsUsable: saveUserCmd.IsUsable,
Permission: permissionBases,
... ... @@ -127,6 +132,7 @@ func (adminUserSrv AdminUserService) SaveAdminUser(saveUserCmd *command.SaveAdmi
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
transactionContext.CommitTransaction()
return adminuser, nil
}
... ... @@ -135,12 +141,12 @@ func (adminUserSrv AdminUserService) PageListAdminUser(listAdminUserQuery *query
if err != nil {
return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
// if err := transactionContext.StartTransaction(); err != nil {
// return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
// }
// defer func() {
// transactionContext.RollbackTransaction()
// }()
var (
adminuserRepository domain.AdminUserRepository
adminusers []domain.AdminUser
... ... @@ -167,5 +173,65 @@ func (adminUserSrv AdminUserService) PageListAdminUser(listAdminUserQuery *query
if err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
transactionContext.CommitTransaction()
return adminusers, cnt, nil
}
func (adminUserSrv AdminUserService) UpdateAdminPassword(updatecmd command.UpdateAdminUserPwdCommand) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
adminuserDao *dao.AdminUserDao
)
if v, err := factory.CreateAdminUserkDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
} else {
adminuserDao = v
}
err = adminuserDao.UpdatePassword(updatecmd.Id, updatecmd.Password)
if err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
transactionContext.CommitTransaction()
return nil
}
func (adminUserSrv AdminUserService) UpdateAdminIsUsable(uid int64, isUsable bool) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
adminuserDao *dao.AdminUserDao
)
if v, err := factory.CreateAdminUserkDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
} else {
adminuserDao = v
}
err = adminuserDao.UpdateIsUsable(uid, isUsable)
if err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
transactionContext.CommitTransaction()
return nil
}
... ...
package factory
//func CreateTaskDao(options map[string]interface{}) (*dao.TaskDao, error) {
// var transactionContext *pg.TransactionContext
// if value, ok := options["transactionContext"]; ok {
// transactionContext = value.(*pg.TransactionContext)
// }
// return dao.NewTaskDao(transactionContext)
//}
import (
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
func CreateAdminUserkDao(options map[string]interface{}) (*dao.AdminUserDao, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return dao.NewAdminUserDao(transactionContext)
}
... ...
package dao
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
type AdminUserDao struct {
transactionContext *transaction.TransactionContext
}
func NewAdminUserDao(transactionContext *transaction.TransactionContext) (*AdminUserDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &AdminUserDao{
transactionContext: transactionContext,
}, nil
}
}
//UpdatePassword ....
func (dao *AdminUserDao) UpdatePassword(id int64, pwd string) error {
tx := dao.transactionContext.PgDd
m := &models.AdminUser{}
err := tx.Model(m).Where("id=?", id).First()
if err != nil {
return err
}
_, err = tx.Model(m).Where("id=?", id).
Set("password=?", pwd).
Update()
return err
}
//UpdatePassword ....
func (dao *AdminUserDao) UpdateIsUsable(id int64, isUsable bool) error {
tx := dao.transactionContext.PgDd
m := &models.AdminUser{}
err := tx.Model(m).Where("id=?", id).First()
if err != nil {
return err
}
_, err = tx.Model(m).Where("id=?", id).
Set("is-usable=?", isUsable).
Update()
return err
}
//AdminUserAccountExist ...
func (dao *AdminUserDao) AdminUserAccountExist(account string) (bool, error) {
tx := dao.transactionContext.PgDd
m := &models.AdminUser{}
ok, err := tx.Model(m).
Where("account=?", account).
Exists()
return ok, err
}
... ...
package dao
//import (
// "fmt"
// "github.com/go-pg/pg/v10"
// pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
// "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
// "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
// "time"
//)
//
//type EmployeeDao struct {
// transactionContext *pgTransaction.TransactionContext
//}
//
//func (dao *EmployeeDao) BatchRemove(uids []int64) error {
// tx := dao.transactionContext.PgTx
// _, err := tx.QueryOne(
// pg.Scan(),
// "DELETE FROM employees WHERE uid IN (?)",
// pg.In(uids))
// return err
//}
//
//func (dao *EmployeeDao) BatchSetStatus(uids []int64, status int) error {
// tx := dao.transactionContext.PgTx
// _, err := tx.QueryOne(
// pg.Scan(),
// "UPDATE employees SET status=? WHERE uid IN (?)",
// status, pg.In(uids))
// return err
//}
//
//func (dao *EmployeeDao) ChangePrincipal(companyId int64, employeeAccount string) error {
// tx := dao.transactionContext.PgTx
// if _, err := tx.Query(
// pg.Scan(),
// "UPDATE employees SET is_principal=? WHERE company_id=?",
// false, companyId); err != nil {
// return err
// }
// if _, err := tx.QueryOne(
// pg.Scan(),
// "UPDATE employees SET is_principal=? WHERE company_id=? AND employee_account=?",
// true, companyId, employeeAccount); err != nil {
// return err
// }
// return nil
//}
//
//func (dao *EmployeeDao) TransferSuMoney(uid int64, suMoney float64) error {
// tx := dao.transactionContext.PgTx
// _, err := tx.QueryOne(
// pg.Scan(),
// "UPDATE employees SET su_money=su_money+? WHERE uid=?",
// suMoney, uid)
// return err
//}
//
//func (dao *EmployeeDao) CalculatePersonUnReadNotification(uid int64) (map[string]int, error) {
// var unReadSystemNotification int
// var unReadInteractionNotification int
// tx := dao.transactionContext.PgTx
// sentNotificationModel := new(models.SentNotification)
// if count, err := tx.Model(sentNotificationModel).Relation("Notification").
// Where(`sent_notification.receiver @> '{"uid":?}'`, uid).
// Where("notification.notification_type = ?", domain.NOTIFICATION_TYPE_SYSTEM).
// Where("sent_notification.is_read = ?", false).
// Count(); err != nil {
// return nil, err
// } else {
// unReadSystemNotification = count
// }
// if count, err := tx.Model(sentNotificationModel).Relation("Notification").
// Where(`sent_notification.receiver @> '{"uid":?}'`, uid).
// Where("notification.notification_type = ?", domain.NOTIFICATION_TYPE_INTERACTION).
// Where("sent_notification.is_read = ?", false).
// Count(); err != nil {
// return nil, err
// } else {
// unReadInteractionNotification = count
// }
// return map[string]int{
// "unReadSystemNotification": unReadSystemNotification,
// "unReadInteractionNotification": unReadInteractionNotification,
// }, nil
//}
//
//func (dao *EmployeeDao) CalculatePersonSuMoney(uid int64) (map[string]interface{}, error) {
// var incomeSuMoney float64
// var incomeSuMoneyOfYesterday float64
// tx := dao.transactionContext.PgTx
// suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
// yesterday := time.Now().AddDate(0, 0, -1)
// if err := tx.Model(suMoneyTransactionRecordModel).
// ColumnExpr("sum(su_money_transaction_record.su_money) AS income_su_money").
// Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid).
// Where(`su_money_transaction_record.record_type = ?`, 2).
// Where(`su_money_transaction_record.create_time > ?`, time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, yesterday.Location())).
// Where(`su_money_transaction_record.create_time < ?`, time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 23, 59, 59, 0, yesterday.Location())).
// Select(&incomeSuMoneyOfYesterday); err != nil {
// return nil, err
// }
// if err := tx.Model(suMoneyTransactionRecordModel).
// ColumnExpr("sum(su_money_transaction_record.su_money) AS income_su_money").
// Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid).
// Where(`su_money_transaction_record.record_type = ?`, 2).
// Select(&incomeSuMoney); err != nil {
// return nil, err
// }
// return map[string]interface{}{
// "incomeSuMoney": incomeSuMoney,
// "incomeSuMoneyOfYesterday": incomeSuMoneyOfYesterday,
// }, nil
//}
//
//func (dao *EmployeeDao) CalculateSuMoneyTransactionRecord(uid int64, transactionStartTime time.Time, transactionEndTime time.Time) (map[string]interface{}, error) {
// var incomeSuMoney float64
// var expendSuMoney float64
// tx := dao.transactionContext.PgTx
// suMoneyTransactionRecordModel := new(models.SuMoneyTransactionRecord)
// if err := tx.Model(suMoneyTransactionRecordModel).
// ColumnExpr("sum(su_money_transaction_record.su_money) AS income_su_money").
// Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid).
// Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{2, 3})).
// Where(`su_money_transaction_record.create_time > ?`, transactionStartTime).
// Where(`su_money_transaction_record.create_time < ?`, transactionEndTime).
// Select(&incomeSuMoney); err != nil {
// return nil, err
// }
// if err := tx.Model(suMoneyTransactionRecordModel).
// ColumnExpr("sum(su_money_transaction_record.su_money) AS expend_su_money").
// Where(`su_money_transaction_record.employee @> '{"uid":?}'`, uid).
// Where(`su_money_transaction_record.record_type IN (?)`, pg.In([]int{1, 4})).
// Where(`su_money_transaction_record.create_time > ?`, transactionStartTime).
// Where(`su_money_transaction_record.create_time < ?`, transactionEndTime).
// Select(&expendSuMoney); err != nil {
// return nil, err
// }
// return map[string]interface{}{
// "incomeSuMoney": incomeSuMoney,
// "expendSuMoney": expendSuMoney,
// }, nil
//}
//
//func NewEmployeeDao(transactionContext *pgTransaction.TransactionContext) (*EmployeeDao, error) {
// if transactionContext == nil {
// return nil, fmt.Errorf("transactionContext参数不能为nil")
// } else {
// return &EmployeeDao{
// transactionContext: transactionContext,
// }, nil
// }
//}
... ... @@ -5,9 +5,7 @@ import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/constant"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/log"
)
... ... @@ -23,21 +21,21 @@ func init() {
if !constant.DISABLE_SQL_GENERATE_PRINT {
DB.AddQueryHook(SqlGeneratePrintHook{})
}
if !constant.DISABLE_CREATE_TABLE {
for _, model := range []interface{}{
//(*models.Employee)(nil),
(*models.PartnerInfo)(nil),
} {
err := DB.CreateTable(model, &orm.CreateTableOptions{
Temp: false,
IfNotExists: true,
FKConstraints: true,
})
if err != nil {
panic(err)
}
}
}
// if !constant.DISABLE_CREATE_TABLE {
// for _, model := range []interface{}{
// //(*models.Employee)(nil),
// (*models.PartnerInfo)(nil),
// } {
// err := DB.CreateTable(model, &orm.CreateTableOptions{
// Temp: false,
// IfNotExists: true,
// FKConstraints: true,
// })
// if err != nil {
// panic(err)
// }
// }
// }
}
type SqlGeneratePrintHook struct{}
... ...
... ... @@ -12,6 +12,7 @@ import (
"github.com/astaxie/beego/logs"
adminPermissionquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/query"
adminPermissionService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/service"
adminuserCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/command"
adminuserquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query"
adminuserservice "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/service"
)
... ... @@ -165,3 +166,32 @@ func (c *AdminLoginController) AdminpPofile() {
}
c.ResponseData(respData)
}
func (c *AdminLoginController) PwdUpdate() {
//用与适配前端定义的数据结构
type Paramater struct {
Pwd string `json:"pwd"`
}
var (
param Paramater
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
uid := c.GetUserId()
newAdminUserService := adminuserservice.NewAdminUserService(nil)
cmd := adminuserCmd.UpdateAdminUserPwdCommand{
Id: uid,
Password: param.Pwd,
}
err = newAdminUserService.UpdateAdminPassword(cmd)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}
... ...
... ... @@ -31,7 +31,6 @@ type adminDetailParam struct {
Id int64 `json:"id"`
PermissionType []int `json:"permissionType"`
Status int `json:"status"`
Pwd string `json:"pwd"`
Account string `json:"account"`
}
... ... @@ -51,7 +50,6 @@ func (c *AdminUserController) SaveAdminUser() {
Id: param.Id,
Name: param.Account,
Account: param.Account,
Password: param.Pwd,
PermissionId: param.PermissionType,
IsUsable: false,
}
... ... @@ -169,3 +167,50 @@ func (c *AdminUserController) ListAdminUser() {
c.ResponsePageList(listData, cnt, param.PageNumber)
return
}
func (c *AdminUserController) ForbiddenAdminUser() {
//用与适配前端定义的数据结构
type Paramter struct {
Id int64 `json:"id"`
}
var (
param Paramter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
newAdminUserService := adminuserservice.NewAdminUserService(nil)
err = newAdminUserService.UpdateAdminIsUsable(param.Id, false)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}
//BeforeEditAdminUser 编辑管理员操作的前置接口
func (c *AdminUserController) BeforeEditAdminUser() {
newPermissionSrv := adminPermissionService.NewAdminPermissionService(nil)
allPermission, err := newPermissionSrv.ListAdminPermission(adminPermissionquery.ListAdminPermissionQuery{
ParentId: 0,
})
if err != nil {
logs.Error("获取权限数据失败:%s", err)
c.ResponseError(errors.New("服务异常"))
return
}
var rspData []map[string]interface{}
for i := range allPermission {
m := map[string]interface{}{
"id": allPermission[i].Id,
"permissionName": allPermission[i].Name,
}
rspData = append(rspData, m)
}
c.ResponseData(rspData)
return
}
... ...
... ... @@ -11,12 +11,14 @@ func init() {
beego.NSRouter("/login", &controllers.AdminLoginController{}, "POST:Login"),
beego.NSRouter("/captcha-init", &controllers.AdminLoginController{}, "POST:CaptchaInit"),
beego.NSRouter("/profile", &controllers.AdminLoginController{}, "POST:AdminpPofile"),
beego.NSRouter("/pwd-update", &controllers.AdminLoginController{}, "POST:PwdUpdate"),
),
beego.NSNamespace("/admin",
beego.NSRouter("/update", &controllers.AdminUserController{}, "POST:SaveAdminUser"),
beego.NSRouter("/detail", &controllers.AdminUserController{}, "POST:GetAdminUser"),
beego.NSRouter("/list", &controllers.AdminUserController{}, "POST:ListAdminUser"),
beego.NSRouter("/forbidden", &controllers.AdminUserController{}, "POST:ForbiddenAdminUser"),
beego.NSRouter("/permission", &controllers.AdminUserController{}, "POST:BeforeEditAdminUser"),
),
)
beego.AddNamespace(adminRouter)
... ...