tower_base_info.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 TowerBaseInfo struct {
  10. ID int64 `gorm:"column:ID;PRIMARY_KEY" json:"id"`
  11. SN string `gorm:"column:SN" json:"sn"`
  12. X float64 `gorm:"column:X" json:"x"`
  13. Y float64 `gorm:"column:Y" json:"y"`
  14. FrontArmLength float64 `gorm:"column:FrontArmLength" json:"front_arm_length"`
  15. BackArmLength float64 `gorm:"column:BackArmLength" json:"back_arm_length"`
  16. HatHigh float64 `gorm:"column:HatHigh" json:"hat_high"`
  17. ArmHigh float64 `gorm:"column:ArmHigh" json:"arm_high"`
  18. MaxWeight int64 `gorm:"column:MaxWeight" json:"max_weight"`
  19. MaxMoment float64 `gorm:"column:MaxMoment" json:"max_moment"`
  20. HookWeight int64 `gorm:"column:HookWeight" json:"hook_weight"`
  21. ArticulationLength float64 `gorm:"column:ArticulationLength" json:"articulation_length"`
  22. HighSensor int64 `gorm:"column:HighSensor" json:"high_sensor"`
  23. RangeSensor int64 `gorm:"column:RangeSensor" json:"range_sensor"`
  24. BackturnSensor int64 `gorm:"column:BackturnSensor" json:"backturn_sensor"`
  25. WeightSensor int64 `gorm:"column:WeightSensor" json:"weight_sensor"`
  26. WindSensor int64 `gorm:"column:WindSensor" json:"wind_sensor"`
  27. TiltSendsor int64 `gorm:"column:TiltSendsor" json:"tilt_sendsor"`
  28. ProjectID int64 `gorm:"column:ProjectID" json:"project_id"`
  29. TowerNo int `gorm:"column:TowerNo"`
  30. CreatedAt time.Time `gorm:"column:CreatedAt"`
  31. UpdatedAt time.Time `gorm:"column:UpdatedAt"`
  32. }
  33. func (TowerBaseInfo) TableName() string {
  34. return "TowerBaseInfo"
  35. }
  36. func (p *TowerBaseInfo) Insert(db *gorm.DB) error {
  37. err := db.Create(p).Error
  38. if err != nil {
  39. fields, _ := json.Marshal(p)
  40. logger.Error("mysql",
  41. zap.String("sql", "insert into TowerBaseInfo"),
  42. zap.String("fields", string(fields)),
  43. zap.String("error", err.Error()))
  44. return err
  45. }
  46. return err
  47. }
  48. func (p *TowerBaseInfo) Find(db *gorm.DB, where map[string]interface{}) error {
  49. cond, val, err := whereBuild(where)
  50. if err != nil {
  51. return err
  52. }
  53. err = db.Table(p.TableName()).Where(cond, val...).First(p).Error
  54. if err != nil {
  55. fields, _ := json.Marshal(where)
  56. logger.Error("mysql",
  57. zap.String("sql", "select from TowerBaseInfo"),
  58. zap.String("fields", string(fields)),
  59. zap.String("error", err.Error()))
  60. return err
  61. }
  62. return err
  63. }
  64. func (p *TowerBaseInfo) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  65. cond, val, err := whereBuild(where)
  66. if err != nil {
  67. return err
  68. }
  69. err = db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  70. if err != nil {
  71. fields, _ := json.Marshal(where)
  72. updateValues, _ := json.Marshal(values)
  73. logger.Error("mysql",
  74. zap.String("sql", "update TowerBaseInfo"),
  75. zap.String("fields", string(fields)),
  76. zap.String("values", string(updateValues)),
  77. zap.String("error", err.Error()))
  78. return err
  79. }
  80. return err
  81. }
  82. func (p *TowerBaseInfo) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  83. cond, val, err := whereBuildAndOr(where, or)
  84. if err != nil {
  85. return 0, err
  86. }
  87. ret := int64(0)
  88. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  89. if err != nil {
  90. fields, _ := json.Marshal(val)
  91. logger.Error("mysql",
  92. zap.String("sql", "select count(1) TowerBaseInfo"),
  93. zap.String("fields", cond+","+string(fields)),
  94. zap.String("error", err.Error()))
  95. return ret, err
  96. }
  97. return ret, err
  98. }