智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.9 KiB

  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "reflect"
  6. "xorm.io/xorm"
  7. )
  8. type FactoryIdentityDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. EnterpriseId int `json:"enterprise_id"`
  11. }
  12. func (factoryIdentityDb *FactoryIdentityDb) Set(enterpriseId int) { // set方法
  13. factoryIdentityDb.Db = Db
  14. factoryIdentityDb.EnterpriseId = enterpriseId
  15. }
  16. func (factoryIdentityDb *FactoryIdentityDb) FindFactoryIdentity(page, limit int) (list *[]model.FactoryIdentity, count int64, err error) {
  17. var m []model.FactoryIdentity
  18. if count, err = factoryIdentityDb.Db.Where("enterprise_id = ?", factoryIdentityDb.EnterpriseId).Desc("id").Limit(limit, (page-1)*limit).FindAndCount(&m); err != nil {
  19. return nil, 0, logx.Error(err)
  20. }
  21. return &m, count, nil
  22. }
  23. func (factoryIdentityDb *FactoryIdentityDb) GetFactoryIdentity(id int) (m *model.FactoryIdentity, err error) {
  24. m = new(model.FactoryIdentity)
  25. has, err := factoryIdentityDb.Db.Where("id = ?", id).Get(m)
  26. if err != nil {
  27. return nil, logx.Error(err)
  28. }
  29. if has == false {
  30. return nil, nil
  31. }
  32. return m, nil
  33. }
  34. func (factoryIdentityDb *FactoryIdentityDb) GetFactoryIdentityByName(name string) (m *model.FactoryIdentity, err error) {
  35. m = new(model.FactoryIdentity)
  36. has, err := factoryIdentityDb.Db.Where("name = ?", name).Get(m)
  37. if err != nil {
  38. return nil, logx.Error(err)
  39. }
  40. if has == false {
  41. return nil, nil
  42. }
  43. return m, nil
  44. }
  45. func (factoryIdentityDb *FactoryIdentityDb) UpdateFactoryIdentity(m *model.FactoryIdentity, columns ...string) (int64, error) {
  46. affected, err := factoryIdentityDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  47. if err != nil {
  48. return 0, err
  49. }
  50. return affected, nil
  51. }
  52. func (factoryIdentityDb *FactoryIdentityDb) FactoryIdentityDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
  53. if reflect.TypeOf(id).Kind() == reflect.Slice {
  54. return session.In("id", id).Delete(model.FactoryIdentity{})
  55. } else {
  56. return session.Where("id = ?", id).Delete(model.FactoryIdentity{})
  57. }
  58. }
  59. func (factoryIdentityDb *FactoryIdentityDb) FactoryIdentityInsert(m *model.FactoryIdentity) (int, error) {
  60. _, err := factoryIdentityDb.Db.InsertOne(m)
  61. if err != nil {
  62. return 0, err
  63. }
  64. return m.Id, nil
  65. }
  66. func (factoryIdentityDb *FactoryIdentityDb) FindUserIdentityWithFactoryIdentity(userIdentityId int) (list []*UserIdentityWithFactoryIdentity, total int64, err error) {
  67. total, err = factoryIdentityDb.Db.Where("user_identity.id =?", userIdentityId).
  68. Join("LEFT", "factory_identity_with_user_identity", "factory_identity_with_user_identity.user_identity_id = user_identity.id").
  69. Join("LEFT", "factory_identity", "factory_identity.id = factory_identity_with_user_identity.factory_identity_id").
  70. FindAndCount(&list)
  71. return
  72. }
  73. type UserIdentityWithFactoryIdentity struct {
  74. model.UserIdentity `xorm:"extends"`
  75. model.User `xorm:"extends"`
  76. model.FactoryIdentityWithUserIdentity `xorm:"extends"`
  77. model.FactoryIdentity `xorm:"extends"`
  78. }
  79. func (UserIdentityWithFactoryIdentity) TableName() string {
  80. return "user_identity"
  81. }
  82. func (factoryIdentityDb *FactoryIdentityDb) FindFactoryIdentityWithUserIdentity(factoryIdentityId int) (list []*FactoryIdentityWithUserIdentity, total int64, err error) {
  83. total, err = factoryIdentityDb.Db.Where("factory_identity.id =?", factoryIdentityId).
  84. Join("LEFT", "factory_identity_with_user_identity", "factory_identity_with_user_identity.factory_identity_id = factory_identity.id").
  85. Join("LEFT", "user_identity", "user_identity.id = factory_identity_with_user_identity.user_identity_id").
  86. FindAndCount(&list)
  87. return
  88. }
  89. type FactoryIdentityWithUserIdentity struct {
  90. model.FactoryIdentity `xorm:"extends"`
  91. model.UserIdentity `xorm:"extends"`
  92. model.FactoryIdentityWithUserIdentity `xorm:"extends"`
  93. }
  94. func (FactoryIdentityWithUserIdentity) TableName() string {
  95. return "factory_identity"
  96. }