|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- package db
-
- import (
- "applet/app/db/model"
- "applet/app/utils/logx"
- "reflect"
- "xorm.io/xorm"
- )
-
- type UserIdentityDb struct {
- Db *xorm.Engine `json:"db"`
- Uid int `json:"uid"`
- }
-
- func (userIdentityDb *UserIdentityDb) Set(uid int) { // set方法
- userIdentityDb.Db = Db
- userIdentityDb.Uid = uid
- }
-
- func (userIdentityDb *UserIdentityDb) GetUserIdentity(id int) (m *model.UserIdentity, err error) {
- m = new(model.UserIdentity)
- has, err := userIdentityDb.Db.Where("id =?", id).Get(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (userIdentityDb *UserIdentityDb) UserIdentityExist(enterpriseId int, idNo string) (m *model.UserIdentity, err error) {
- m = new(model.UserIdentity)
- has, err := userIdentityDb.Db.Where("enterprise_id =?", enterpriseId).And("id_no =?", idNo).Exist(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (userIdentityDb *UserIdentityDb) FindUserIdentity() (*[]UserIdentityWithEnterprise, error) {
- var m []UserIdentityWithEnterprise
- if err := userIdentityDb.Db.
- Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
- Where("user_identity.uid =?", userIdentityDb.Uid).Desc("user_identity.id").Find(&m); err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- }
-
- func (userIdentityDb *UserIdentityDb) UserIdentityInsert(m *model.UserIdentity) (int, error) {
- _, err := userIdentityDb.Db.InsertOne(m)
- if err != nil {
- return 0, err
- }
- return m.Id, nil
- }
-
- func (userIdentityDb *UserIdentityDb) UserIdentityInsertBySession(session *xorm.Session, m *model.UserIdentity) (int, error) {
- _, err := session.InsertOne(m)
- if err != nil {
- return 0, err
- }
- return m.Id, nil
- }
-
- func (userIdentityDb *UserIdentityDb) BatchAddUserIdentities(mm []*model.UserIdentity) (int64, error) {
- affected, err := userIdentityDb.Db.Insert(mm)
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- func (userIdentityDb *UserIdentityDb) UserIdentityDelete(id interface{}) (int64, error) {
- if reflect.TypeOf(id).Kind() == reflect.Slice {
- return Db.In("id", id).Delete(model.UserIdentity{})
- } else {
- return Db.Where("id = ?", id).Delete(model.UserIdentity{})
- }
- }
-
- func (userIdentityDb *UserIdentityDb) UserIdentityUpdate(id interface{}, m *model.UserIdentity, forceColums ...string) (int64, error) {
- var (
- affected int64
- err error
- )
- if forceColums != nil {
- affected, err = userIdentityDb.Db.Where("id=?", id).Cols(forceColums...).Update(m)
- } else {
- affected, err = userIdentityDb.Db.Where("id=?", id).Update(m)
- }
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- func (userIdentityDb *UserIdentityDb) CountUserIdentityForEnterprise(enterpriseId, identity int) (total int64, err error) {
- var m model.UserIdentity
- total, err = userIdentityDb.Db.Where("enterprise_id =? AND identity =?", enterpriseId, identity).Count(&m)
- if err != nil {
- return
- }
- return
- }
- func (userIdentityDb *UserIdentityDb) FindUserIdentityForEnterpriseByIdentity(enterpriseId, identity int) (*[]model.UserIdentity, error) {
- var m []model.UserIdentity
- err := userIdentityDb.Db.Where("enterprise_id =? AND identity =?", enterpriseId, identity).Find(&m)
- if err != nil {
- return nil, err
- }
- return &m, nil
- }
- func (userIdentityDb *UserIdentityDb) FindUserIdentityForEnterprise(enterpriseId int) (*[]model.UserIdentity, error) {
- var m []model.UserIdentity
- err := userIdentityDb.Db.Where("enterprise_id =?", enterpriseId).Find(&m)
- if err != nil {
- return nil, err
- }
- return &m, nil
- }
-
- type UserIdentityWithUser struct {
- model.UserIdentity `xorm:"extends"`
- model.User `xorm:"extends"`
- model.ClassWithUser `xorm:"extends"`
- model.Class `xorm:"extends"`
- model.Grade `xorm:"extends"`
- }
-
- func (UserIdentityWithUser) TableName() string {
- return "user_identity"
- }
-
- type UserIdentityWithEnterprise struct {
- model.UserIdentity `xorm:"extends"`
- model.Enterprise `xorm:"extends"`
- }
-
- func (UserIdentityWithEnterprise) TableName() string {
- return "user_identity"
- }
|