company.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package model
  4. import (
  5. "github.com/jinzhu/gorm"
  6. "go.uber.org/zap"
  7. "smart-site-management/errors"
  8. "time"
  9. "github.com/jaryhe/gopkgs/logger"
  10. "encoding/json"
  11. )
  12. type TCompany struct {
  13. Id int64 `gorm:"column:id" json:"id" form:"id"`
  14. Name string `gorm:"column:name" json:"name" form:"name"`
  15. SocialCode string `gorm:"column:social_code" json:"social_code" form:"social_code"`
  16. LegalPerson string `gorm:"column:legal_person" json:"legal_person" form:"legal_person"`
  17. IdCert string `gorm:"column:id_cert" json:"id_cert" form:"id_cert"`
  18. BusinessLicense string `gorm:"column:business_license" json:"business_license" form:"business_license"`
  19. UserName string `gorm:"column:user_name" json:"user_name" form:"user_name"`
  20. Passwd string `gorm:"column:passwd" json:"passwd" form:"passwd"`
  21. BusinessContact string `gorm:"column:business_contact" json:"business_contact" form:"business_contact"`
  22. BusinessContactPhone string `gorm:"column:business_contact_phone" json:"business_contact_phone" form:"business_contact_phone"`
  23. BusinessResponsible string `gorm:"column:business_responsible" json:"business_responsible" form:"business_responsible"`
  24. BusinessResponsiblePhone string `gorm:"column:business_responsible_phone" json:"business_responsible_phone" form:"business_responsible_phone"`
  25. EmergencyContact string `gorm:"column:emergency_contact" json:"emergency_contact" form:"emergency_contact"`
  26. EmergencyContactPhone string `gorm:"column:emergency_contact_phone" json:"emergency_contact_phone" form:"emergency_contact_phone"`
  27. Province string `gorm:"column:province" json:"province" form:"province"`
  28. City string `gorm:"column:city" json:"city" form:"city"`
  29. Zone string `gorm:"column:zone" json:"zone" form:"zone"`
  30. Status int64 `gorm:"column:status" json:"status" form:"status"`
  31. CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"`
  32. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"`
  33. }
  34. var PageSize = 10
  35. func (TCompany) TableName() string {
  36. return "t_company"
  37. }
  38. func (p *TCompany) Insert(db *gorm.DB) error {
  39. return db.Create(p).Error
  40. }
  41. func (p *TCompany) Find(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) error {
  42. cond, val, err := whereBuildAndOr(where, or)
  43. if err != nil {
  44. return err
  45. }
  46. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  47. }
  48. func (p *TCompany) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  49. cond, val, err := whereBuild(where)
  50. if err != nil {
  51. return err
  52. }
  53. err = db.Table(p.TableName()).Table(p.TableName()).Where(cond, val...).Updates(values).Error
  54. if err != nil {
  55. fields, _ := json.Marshal(where)
  56. updateValues, _ := json.Marshal(values)
  57. logger.Error("mysql",
  58. zap.String("sql", "update t_company"),
  59. zap.String("fields", string(fields)),
  60. zap.String("values", string(updateValues)),
  61. zap.String("error", err.Error()))
  62. return errors.DataBaseError
  63. }
  64. return err
  65. }
  66. func (p *TCompany) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  67. cond, val, err := whereBuild(where)
  68. if err != nil {
  69. return err
  70. }
  71. ps := []TCompany{}
  72. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  73. if err != nil {
  74. fields, _ := json.Marshal(where)
  75. logger.Error("mysql",
  76. zap.String("sql", "select from t_company order by " + sort),
  77. zap.String("fields", string(fields)),
  78. zap.String("error", err.Error()))
  79. return errors.DataBaseError
  80. }
  81. if len(ps) > 0 {
  82. *p = ps[0]
  83. }
  84. return nil
  85. }
  86. func (p *TCompany) Save(db *gorm.DB) error {
  87. err := db.Save(p).Error
  88. if err != nil {
  89. fields, _ := json.Marshal(p)
  90. logger.Error("mysql",
  91. zap.String("sql", "save t_company"),
  92. zap.String("fields", string(fields)),
  93. zap.String("error", err.Error()))
  94. return errors.DataBaseError
  95. }
  96. return err
  97. }
  98. func (p *TCompany) List(db *gorm.DB, where map[string]interface{}, page int) (list []TCompany, err error) {
  99. if len(where) > 0 {
  100. cond, val, err := whereBuild(where)
  101. if err != nil {
  102. return list, err
  103. }
  104. err = db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list).Error
  105. if err != nil {
  106. fields, _ := json.Marshal(where)
  107. logger.Error("mysql",
  108. zap.String("sql", "select * from t_company "),
  109. zap.String("fields", string(fields)),
  110. zap.String("error", err.Error()))
  111. return list, errors.DataBaseError
  112. }
  113. return list, err
  114. }
  115. err = db.Table(p.TableName()).Limit(10).Offset(page).Find(&list).Error
  116. if err != nil {
  117. fields, _ := json.Marshal(where)
  118. logger.Error("mysql",
  119. zap.String("sql", "select * from t_company "),
  120. zap.String("fields", string(fields)),
  121. zap.String("error", err.Error()))
  122. return list, errors.DataBaseError
  123. }
  124. return list, err
  125. }
  126. func (p *TCompany) All(db *gorm.DB) (list []TCompany, err error) {
  127. err = db.Table(p.TableName()).Find(&list).Error
  128. if err != nil {
  129. logger.Error("mysql",
  130. zap.String("sql", "select * from t_company "),
  131. zap.String("error", err.Error()))
  132. return list, errors.DataBaseError
  133. }
  134. return list, err
  135. }
  136. func (p *TCompany) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  137. if len(where) > 0 {
  138. cond, val, err := whereBuild(where)
  139. if err != nil {
  140. return 0, err
  141. }
  142. ret := int64(0)
  143. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  144. if err != nil {
  145. fields, _ := json.Marshal(where)
  146. logger.Error("mysql",
  147. zap.String("sql", "select count(1) from t_company "),
  148. zap.String("fields", string(fields)),
  149. zap.String("error", err.Error()))
  150. return ret, errors.DataBaseError
  151. }
  152. return ret, err
  153. }
  154. ret := int64(0)
  155. err := db.Table(p.TableName()).Count(&ret).Error
  156. if err != nil {
  157. fields, _ := json.Marshal(where)
  158. logger.Error("mysql",
  159. zap.String("sql", "select count(1) from t_company "),
  160. zap.String("fields", string(fields)),
  161. zap.String("error", err.Error()))
  162. return ret, errors.DataBaseError
  163. }
  164. return ret, err
  165. }
  166. func (p *TCompany) Del(db *gorm.DB, where map[string]interface{}) error {
  167. cond, val, err := whereBuild(where)
  168. if err != nil {
  169. return err
  170. }
  171. err = db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  172. if err != nil {
  173. fields, _ := json.Marshal(where)
  174. logger.Error("mysql",
  175. zap.String("sql", "delete from t_company "),
  176. zap.String("fields", string(fields)),
  177. zap.String("error", err.Error()))
  178. return errors.DataBaseError
  179. }
  180. return err
  181. }