|
- /* ******************************************************************************
- * 2019 - present Contributed by Apulis Technology (Shenzhen) Co. LTD
- *
- * This program and the accompanying materials are made available under the
- * terms of the MIT License, which is available at
- * https://www.opensource.org/licenses/MIT
- *
- * See the NOTICE file distributed with this work for additional
- * information regarding copyright ownership.
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- * SPDX-License-Identifier: MIT
- ******************************************************************************/
- package models
-
- import (
- "fmt"
-
- "github.com/apulis/sdk/go-utils/logging"
- )
-
- type UserProject struct {
- Uid int64 `gorm:"primaryKey" json:"uid"`
- UserName string `gorm:"userName" json:"userName"`
- ProjectId int64 `gorm:"primaryKey" json:"projectId"`
- GroupId int64 `json:"groupId"`
- Config string `json:"config"`
- CreatedAt int64 `gorm:"autoCreateTime:milli" json:"createdAt"`
- UpdatedAt int64 `gorm:"autoUpdateTime:milli" json:"-"`
- }
-
- type UserProjectOpt struct {
- }
-
- func (opt *UserProjectOpt) CreateOrUpdate(p *UserProject) error {
- db := GetDb().Model(&UserProject{})
-
- db = db.Where("project_id = ?", p.ProjectId)
- db = db.Where("uid = ?", p.Uid)
- var count int64
- db.Count(&count)
- if count > 0 {
- tmpP := &UserProject{}
- r := db.First(tmpP)
- if r.Error != nil {
- return r.Error
- }
- fmt.Println("tmpP", tmpP)
- p.CreatedAt = tmpP.CreatedAt
- db = db.Save(p)
- } else {
- db = db.Create(p)
- }
- if db.Error != nil {
- logging.Error(db.Error).Msgf("UserProjectOpt create error,%v", p)
- return fmt.Errorf("%s,UserProjectOpt create error %v", db.Error.Error(), p)
- }
-
- return nil
- }
-
- func (opt *UserProjectOpt) Delete(p *UserProject) error {
- db := GetDb().Model(&UserProject{})
-
- db = db.Where("project_id = ?", p.ProjectId)
- db = db.Where("uid = ?", p.Uid)
- var count int64
- db.Count(&count)
- if count == 0 {
- return fmt.Errorf("delete userProject error,uid:%d projectId: %d", p.Uid, p.ProjectId)
- } else {
- db = db.Delete(p)
- }
- if db.Error != nil {
- logging.Error(db.Error).Msgf("UserProjectOpt delete error,%v", p)
- return fmt.Errorf("%s,UserProjectOpt delete error %v", db.Error.Error(), p)
- }
-
- return nil
- }
-
- func (opt *UserProjectOpt) Get(p *UserProject) (*UserProject, error) {
- db := GetDb()
- result := db.First(p)
- if result.Error != nil {
- logging.Error(result.Error).Msgf("UserProjectOpt get error,%v", p)
- return nil, fmt.Errorf("%s,UserProjectOpt get error %v", result.Error.Error(), p)
- } else if result.RowsAffected == 0 {
- logging.Error(result.Error).Msgf("UserProjectOpt get error,%v,RowsAffected == 0", p)
- return nil, fmt.Errorf("%s,UserProjectOpt get error %v RowsAffected == 0", result.Error.Error(), p)
- }
- return p, nil
- }
-
- func (opt *UserProjectOpt) List(p *UserProject, baseList *BaseList) ([]UserProject, int64, error) {
- db := GetDb().Model(&UserProject{})
-
- userProjects := []UserProject{}
-
- if p.Uid >= 0 {
- db = db.Where("uid = ?", p.Uid)
- }
-
- if p.ProjectId >= 0 {
- db = db.Where("project_id = ?", p.ProjectId)
- }
-
- if p.GroupId >= 0 {
- db = db.Where("group_id = ?", p.GroupId)
- }
- var total int64
- db.Count(&total)
- db, err := PageList(db, baseList)
- if err != nil {
- return nil, 0, err
- }
-
- result := db.Find(&userProjects)
- if result.Error != nil {
- logging.Error(result.Error).Msgf("UserProjectOpt list error,%v", p)
- return userProjects, 0, fmt.Errorf("%s,UserProjectOpt list error %v", result.Error.Error(), p)
- }
- return userProjects, total, nil
- }
|