package dao import ( "enterprise/common/model" "errors" "gorm.io/gorm" "time" ) type CorpUserDao struct { } func NewCorpUserDao() *CorpUserDao { return &CorpUserDao{} } func (d *CorpUserDao) TableName() string { return "corp_user" } func (d *CorpUserDao) Create(o *model.CorpUser) (int64, error) { o.CreateTime = time.Now().Unix() res := GetDB().Table(d.TableName()).Create(o) return o.Id, res.Error } func (d *CorpUserDao) Update(o *model.CorpUser) error { o.UpdateTime = time.Now().Unix() tx := GetDB().Table(d.TableName()) res := tx.Save(o) return res.Error } func (d *CorpUserDao) Delete(id int64) error { res := GetDB().Table(d.TableName()).Delete(&model.CorpUser{}, id) return res.Error } func (d *CorpUserDao) Get(id int64) (*model.CorpUser, error) { var u model.CorpUser tx := GetDB().Table(d.TableName()) tx = tx.Where("id = ?", id) res := tx.First(&u) if errors.Is(res.Error, gorm.ErrRecordNotFound) { return nil, nil } if res.Error != nil { return nil, res.Error } return &u, nil } func (d *CorpUserDao) GetByUsername(corpId int64, username string) (*model.CorpUser, error) { var u model.CorpUser tx := GetDB().Table(d.TableName()) tx = tx.Where("corp_id = ?", corpId) tx = tx.Where("username = ?", username) res := tx.First(&u) if errors.Is(res.Error, gorm.ErrRecordNotFound) { return nil, nil } if res.Error != nil { return nil, res.Error } return &u, nil }