package mysql import ( "context" "finclip-app-manager/domain/entity" "finclip-app-manager/domain/entity/proto/apiproto" "finclip-app-manager/domain/repository" "gorm.io/gorm" ) var _ repository.IMenuInfoRepo = new(MenuInfoByMysqlRepo) type MenuInfoByMysqlRepo struct { } type MenuInfoMysql struct { Id uint `json:"id" gorm:"primary_key;column:id;type:BIGINT(16) AUTO_INCREMENT;comment:'自增id'" sql:"auto_increment;primary_key"` TraceId string `json:"traceId" gorm:"column:trace_id;type:varchar(64);comment:唯一Id"` Name string `json:"name" gorm:"column:name;type:varchar(64);comment:名称"` InfoId string `json:"infoId" gorm:"column:info_id;type:varchar(64);comment:id"` ImageUrl string `json:"imageUrl" gorm:"column:image_url;type:varchar(256);comment:图片地址"` SortNum int `json:"sortNum" gorm:"column:sort_num;type:INT(4);comment:排序"` CreateTime int64 `json:"createTime" gorm:"column:create_time;type:BIGINT(16);comment:创建时间"` UpdateTime int64 `json:"updateTime" gorm:"column:update_time;type:BIGINT(16);comment:更新时间"` UpdateOperator string `json:"updateOperator" gorm:"column:update_operator;type:varchar(64);comment:更新人"` } func (MenuInfoMysql) TableName() string { return "menu_info" } func (b MenuInfoByMysqlRepo) GetInfoByTraceId(ctx context.Context, id string) (*entity.MenuInfo, error) { query := "trace_id = ?" args := []interface{}{id} menuInfoMysql := MenuInfoMysql{} err := DB.Model(&MenuInfoMysql{}).Where(query, args...).First(&menuInfoMysql).Error if err != nil { return nil, err } result := convertMenuInfoMysqlToMenuInfo(menuInfoMysql) return &result, err } func (b MenuInfoByMysqlRepo) GetAllMenuList(ctx context.Context, sort []string) ([]entity.MenuInfo, int, error) { query := "" args := []interface{}{} menuInfoMysqls := make([]MenuInfoMysql, 0) err := DB.Model(&MenuInfoMysql{}).Where(query, args...).Order("sort_num").Find(&menuInfoMysqls).Error if b.NotFound(err) { return []entity.MenuInfo{}, 0, nil } var count int64 err = DB.Model(&MenuInfoMysql{}).Count(&count).Error if err != nil { return []entity.MenuInfo{}, 0, nil } menuInfos := make([]entity.MenuInfo, 0) for _, menuInfoMysql := range menuInfoMysqls { menuInfos = append(menuInfos, convertMenuInfoMysqlToMenuInfo(menuInfoMysql)) } return menuInfos, int(count), err } func (b MenuInfoByMysqlRepo) Add(ctx context.Context, info *entity.MenuInfo) error { //if err := repository.Lock(ctx, 1*time.Minute); err != nil { // return err //} //defer repository.Unlock(ctx) query := "info_id = ? or name = ?" args := []interface{}{info.InfoId, info.Name} var count int64 err := DB.Model(&MenuInfoMysql{}).Where(query, args...).Count(&count).Error if err != nil { return err } if count > 0 { return entity.MenuIdOrNameExiErr } nextSortNum, err := b.GetNextSortNum(ctx) if err != nil { return err } info.SortNum = nextSortNum return DB.Model(&MenuInfoMysql{}).Create(convertMenuInfoToMenuInfoMysql(info)).Error } func (b MenuInfoByMysqlRepo) GetNextSortNum(ctx context.Context) (int, error) { query := "" args := []interface{}{} menuInfoMysql := MenuInfoMysql{} err := DB.Model(&MenuInfoMysql{}).Where(query, args...).Order("sort_num desc").First(&menuInfoMysql).Error if b.NotFound(err) { return 1, nil } return menuInfoMysql.SortNum + 1, nil } func (b MenuInfoByMysqlRepo) DelByTraceId(ctx context.Context, id string) error { query := "trace_id = ?" args := []interface{}{id} return DB.Model(&MenuInfoMysql{}).Where(query, args...).Delete(&MenuInfoMysql{}).Error } func (b MenuInfoByMysqlRepo) UpdateInfo(ctx context.Context, id string, info *entity.MenuInfo) error { //if err := repository.Lock(ctx, 1*time.Minute); err != nil { // return err //} //defer repository.Unlock(ctx) menuInfoMysqls := make([]MenuInfoMysql, 0) query := "info_id = ? or name = ?" args := []interface{}{info.InfoId, info.Name} err := DB.Model(&MenuInfoMysql{}).Where(query, args...).Find(&menuInfoMysqls).Error if err != nil && !b.NotFound(err) { return err } for _, m := range menuInfoMysqls { if m.TraceId != id { return entity.MenuIdOrNameExiErr } } query = "trace_id = ?" args = []interface{}{id} return DB.Model(&MenuInfoMysql{}).Where(query, args...).Updates(convertMenuInfoToMenuInfoMysql(info)).Error } func (b MenuInfoByMysqlRepo) AllCount(ctx context.Context) (int, error) { query := "" args := []interface{}{} var count int64 err := DB.Model(&MenuInfoMysql{}).Where(query, args...).Count(&count).Error return int(count), err } func (b MenuInfoByMysqlRepo) Sort(ctx context.Context, req *apiproto.MenuSortReq) error { //if err := repository.Lock(ctx, 1*time.Minute); err != nil { // return err //} //defer repository.Unlock(ctx) var resError error for _, v := range req.List { query := "trace_id = ?" args := []interface{}{v.TraceId} err := DB.Model(&MenuInfoMysql{}).Where(query, args...).Update("sort_num", v.SortNum).Error if err != nil { resError = err } } return resError } func (b MenuInfoByMysqlRepo) NotFound(err error) bool { return err == gorm.ErrRecordNotFound } func convertMenuInfoMysqlToMenuInfo(menuInfoMysql MenuInfoMysql) entity.MenuInfo { result := entity.MenuInfo{} result.TraceId = menuInfoMysql.TraceId result.Name = menuInfoMysql.Name result.InfoId = menuInfoMysql.InfoId result.ImageUrl = menuInfoMysql.ImageUrl result.SortNum = menuInfoMysql.SortNum result.CreateTime = menuInfoMysql.CreateTime result.UpdateTime = menuInfoMysql.UpdateTime result.UpdateOperator = menuInfoMysql.UpdateOperator return result } func convertMenuInfoToMenuInfoMysql(menuInfo *entity.MenuInfo) *MenuInfoMysql { result := MenuInfoMysql{} result.TraceId = menuInfo.TraceId result.Name = menuInfo.Name result.InfoId = menuInfo.InfoId result.ImageUrl = menuInfo.ImageUrl result.SortNum = menuInfo.SortNum result.CreateTime = menuInfo.CreateTime result.UpdateTime = menuInfo.UpdateTime result.UpdateOperator = menuInfo.UpdateOperator return &result }