package model import ( "project_management/app/e" "project_management/global" "project_management/unity" "project_management/utils" "strings" ) type Capabilities struct { utils.BaseModel CapName string `gorm:"type:varchar(255);index:idx_name,unique" json:"cap_name"` // 应用名 CpaType string `gorm:"type:varchar(50);" json:"cap_type"` // 应用类型 Image string `gorm:"type:varchar(255);" json:"image"` // 应用图片 ICON string `gorm:"type:varchar(255);" json:"icon"` // 应用图标 CapId string `gorm:"type:varchar(50);unique" json:"cap_id"` // 应用id CapDescription string `gorm:"type:varchar(255);" json:"cap_description"` // 应用描述 State int `gorm:"type:int;" json:"state"` // 状态 1启用 2禁用 CapUrl string `gorm:"type:varchar(255);" json:"cap_url"` // 应用地址 Price float64 `gorm:"type:decimal(10,5);" json:"price"` //价格 Count int64 `gorm:"type:int;" json:"count"` // 使用数量 IsAdd int `gorm:"type:int;" json:"is_add"` // 是否添加 1已添加 } type CapabilitiesApply struct { utils.BaseModel CapId string `gorm:"type:varchar(50);" json:"cap_id"` // 功能id AppId string `gorm:"type:varchar(50);" json:"app_id"` // 应用id } type CapabilitiesDto struct { Capabilities Count int64 `json:"count"` // 使用数量 } func (c Capabilities) TableName() string { return "capabilities" } // AddCapabilities 添加应用 func (c Capabilities) AddCapabilities(capabilities Capabilities) e.Rescode { //TODO implement me capabilities.State = 1 tx := global.DBLink.Table(c.TableName()).Create(&capabilities) if tx.Error != nil { if strings.Contains(tx.Error.Error(), "Duplicate entry") { return e.TheParametersAreRepeated } return e.INSERTFAIL } if tx.RowsAffected > 0 { return e.SUCCESS } return e.INSERTFAIL } // GetCapabilitiesList 获取应用列表 func (c Capabilities) GetCapabilitiesList(params unity.CapQueryPageParams, capabilities Capabilities, queryCond interface{}) (result []Capabilities, total int64, err error) { //TODO implement me query := global.DBLink.Table(capabilities.TableName()) if params.Query != "%%" { query = query.Where(queryCond, params.Query) } if params.State != "" { query = query.Where("state=?", params.State) } if err = query.Where("state=?", 1).Count(&total).Error; err != nil { return nil, 0, err } offset := (params.Page - 1) * params.Size if err = query.Where("state=?", 1).Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil { return nil, 0, err } tablename := "appcap_" + params.AppId var appcap []ApplyCap tx := global.DBLink.Table(tablename).Select("cap_id").Find(&appcap) if tx.Error != nil { if strings.Contains(tx.Error.Error(), "Error 1146 (42S02)") { return result, total, nil } return nil, 0, tx.Error } capIdsMap := make(map[string]bool) // 假设cap_id类型为uint for _, app := range appcap { capIdsMap[app.CapId] = true } for i, _ := range result { if _, exists := capIdsMap[result[i].CapId]; exists { result[i].IsAdd = 1 } } return result, total, nil } // AdminGetCapabilitiesList 管理员获取应用列表 func (c Capabilities) AdminGetCapabilitiesList(params unity.QueryPageParams, capabilities Capabilities, queryCond interface{}) (result []CapabilitiesDto, total int64, err error) { //TODO implement me query := global.DBLink.Table(capabilities.TableName()) if params.Query != "%%" { query = query.Where(queryCond, params.Query) } if params.State != "" { query = query.Where("state=?", params.State) } if err = query.Count(&total).Error; err != nil { return nil, 0, err } offset := (params.Page - 1) * params.Size if err = query.Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil { return nil, 0, err } return result, total, nil } // GetCapabilitiesById 根据id获取应用信息 func (c Capabilities) GetCapabilitiesById(id string) (Capabilities, e.Rescode) { //TODO implement me tx := global.DBLink.Table(c.TableName()).Select("cap_name", "image", "cap_id", "cap_description", "state", "cap_url", "price").Where("cap_id = ?", id).First(&c) if tx.Error != nil { return c, e.FINDFAIL } if tx.RowsAffected > 0 { return c, e.SUCCESS } return c, e.FINDFAIL } // UpdateCapabilities 更新应用信息 func (c Capabilities) UpdateCapabilities(capabilities Capabilities) e.Rescode { //TODO implement me tx := global.DBLink.Table(c.TableName()).Where("cap_id = ?", capabilities.CapId).Updates(&capabilities) if tx.Error != nil { return e.UPDATEFAIL } if tx.RowsAffected > 0 { return e.SUCCESS } return e.UPDATEFAIL } func (c Capabilities) DeleteCapabilities(capid string) e.Rescode { //TODO implement me tx := global.DBLink.Table(c.TableName()).Where("cap_id = ?", capid).Delete(&Capabilities{}) if tx.Error != nil { return e.DELETEFAIL } if tx.RowsAffected > 0 { return e.SUCCESS } return e.DELETEFAIL } // CapIdISRepeat 检查CapId是否重复 func CapIdISRepeat(id string) bool { tx := global.DBLink.Where("cap_id = ?", id).First(&Capabilities{}) if tx.RowsAffected > 0 { return true } return false }