list_sensor_conf.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package model
  2. import (
  3. "encoding/json"
  4. "github.com/jaryhe/gopkgs/logger"
  5. "github.com/jinzhu/gorm"
  6. "go.uber.org/zap"
  7. "time"
  8. )
  9. type LiftSensorConf struct {
  10. ID int64 `gorm:"column:ID;PRIMARY_KEY" json:"id"`
  11. SN string `gorm:"column:SN" json:"sn"`
  12. EmptyWeightAD int64 `gorm:"column:EmptyWeightAD"`
  13. EmptyWeight int64 `gorm:"column:EmptyWeight"`
  14. LoadWeightAD int64 `gorm:"column:LoadWeightAD"`
  15. LoadWeight int64 `gorm:"column:LoadWeight"`
  16. LowHighAD float64 `gorm:"column:LowHighAD"`
  17. LowHigh float64 `gorm:"column:LowHigh"`
  18. TopHighAD float64 `gorm:"column:TopHighAD"`
  19. TopHigh float64 `gorm:"column:TopHigh"`
  20. ProjectID int64 `gorm:"column:ProjectID" json:"project_id"`
  21. LiftNo int `gorm:"column:LiftNo"`
  22. LiftDirect int `gorm:"column:LiftDirect"`
  23. CreatedAt time.Time `gorm:"column:CreatedAt"`
  24. UpdatedAt time.Time `gorm:"column:UpdatedAt"`
  25. }
  26. func (LiftSensorConf) TableName() string {
  27. return "LiftSensorConf"
  28. }
  29. func (p *LiftSensorConf) Insert(db *gorm.DB) error {
  30. err := db.Create(p).Error
  31. if err != nil {
  32. fields, _ := json.Marshal(p)
  33. logger.Error("mysql",
  34. zap.String("sql", "insert into LiftSensorConf"),
  35. zap.String("fields", string(fields)),
  36. zap.String("error", err.Error()))
  37. return err
  38. }
  39. return err
  40. }
  41. func (p *LiftSensorConf) Find(db *gorm.DB, where map[string]interface{}) error {
  42. cond, val, err := whereBuild(where)
  43. if err != nil {
  44. return err
  45. }
  46. err = db.Table(p.TableName()).Where(cond, val...).First(p).Error
  47. if err != nil {
  48. fields, _ := json.Marshal(where)
  49. logger.Error("mysql",
  50. zap.String("sql", "select from LiftSensorConf"),
  51. zap.String("fields", string(fields)),
  52. zap.String("error", err.Error()))
  53. return err
  54. }
  55. return err
  56. }
  57. func (p *LiftSensorConf) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  58. cond, val, err := whereBuild(where)
  59. if err != nil {
  60. return err
  61. }
  62. err = db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  63. if err != nil {
  64. fields, _ := json.Marshal(where)
  65. updateValues, _ := json.Marshal(values)
  66. logger.Error("mysql",
  67. zap.String("sql", "update LiftSensorConf"),
  68. zap.String("fields", string(fields)),
  69. zap.String("values", string(updateValues)),
  70. zap.String("error", err.Error()))
  71. return err
  72. }
  73. return err
  74. }
  75. func (p *LiftSensorConf) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  76. cond, val, err := whereBuildAndOr(where, or)
  77. if err != nil {
  78. return 0, err
  79. }
  80. ret := int64(0)
  81. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  82. if err != nil {
  83. fields, _ := json.Marshal(val)
  84. logger.Error("mysql",
  85. zap.String("sql", "select count(1) LiftSensorConf"),
  86. zap.String("fields", cond+","+string(fields)),
  87. zap.String("error", err.Error()))
  88. return ret, err
  89. }
  90. return ret, err
  91. }