dcDeviceBindModel_gen.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Code generated by goctl. DO NOT EDIT.
  2. package model
  3. import (
  4. "context"
  5. "database/sql"
  6. "fmt"
  7. "github.com/zeromicro/go-zero/core/stores/builder"
  8. "github.com/zeromicro/go-zero/core/stores/sqlc"
  9. "github.com/zeromicro/go-zero/core/stores/sqlx"
  10. "github.com/zeromicro/go-zero/core/stringx"
  11. "metawant.greentech.com.cn/gaoyagang/gt-common/envitem"
  12. "strings"
  13. )
  14. var (
  15. dcDeviceBindFieldNames = builder.RawFieldNames(&DcDeviceBind{})
  16. dcDeviceBindRows = strings.Join(dcDeviceBindFieldNames, ",")
  17. dcDeviceBindRowsExpectAutoSet = strings.Join(stringx.Remove(dcDeviceBindFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
  18. dcDeviceBindRowsWithPlaceHolder = strings.Join(stringx.Remove(dcDeviceBindFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
  19. )
  20. type (
  21. dcDeviceBindModel interface {
  22. Insert(ctx context.Context, data *DcDeviceBind) (sql.Result, error)
  23. FindOne(ctx context.Context, id int64) (*DcDeviceBind, error)
  24. FindByProjectIdDeviceType(ctx context.Context, projectId int64, deviceType string) ([]DcDeviceBind, error)
  25. FindOneByProjectIdDeviceCode(ctx context.Context, projectId int64, deviceCode string) (*DcDeviceBind, error)
  26. Update(ctx context.Context, data *DcDeviceBind) error
  27. Delete(ctx context.Context, id int64) error
  28. }
  29. defaultDcDeviceBindModel struct {
  30. conn sqlx.SqlConn
  31. table string
  32. }
  33. DcDeviceBind struct {
  34. Id int64 `db:"id"`
  35. ProjectId int64 `db:"project_id"`
  36. Name string `db:"name"` // 关系名
  37. DeviceCode string `db:"device_code"` // 关键设备指标
  38. DeviceType string `db:"device_type"` // 设备类型
  39. Status int64 `db:"status"` // 当前状态 0: 禁用 1: 正常
  40. Items envitem.MultiEnvItem `db:"items"` // 相关点位集合
  41. TargetTable string `db:"target_table"` // 数据存入目标表名
  42. }
  43. )
  44. func newDcDeviceBindModel(conn sqlx.SqlConn) *defaultDcDeviceBindModel {
  45. return &defaultDcDeviceBindModel{
  46. conn: conn,
  47. table: "`dc_device_bind`",
  48. }
  49. }
  50. func (m *defaultDcDeviceBindModel) withSession(session sqlx.Session) *defaultDcDeviceBindModel {
  51. return &defaultDcDeviceBindModel{
  52. conn: sqlx.NewSqlConnFromSession(session),
  53. table: "`dc_device_bind`",
  54. }
  55. }
  56. func (m *defaultDcDeviceBindModel) Delete(ctx context.Context, id int64) error {
  57. query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
  58. _, err := m.conn.ExecCtx(ctx, query, id)
  59. return err
  60. }
  61. func (m *defaultDcDeviceBindModel) FindOne(ctx context.Context, id int64) (*DcDeviceBind, error) {
  62. query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", dcDeviceBindRows, m.table)
  63. var resp DcDeviceBind
  64. err := m.conn.QueryRowCtx(ctx, &resp, query, id)
  65. switch err {
  66. case nil:
  67. return &resp, nil
  68. case sqlc.ErrNotFound:
  69. return nil, ErrNotFound
  70. default:
  71. return nil, err
  72. }
  73. }
  74. func (m *defaultDcDeviceBindModel) FindByProjectIdDeviceType(ctx context.Context, projectId int64, deviceType string) ([]DcDeviceBind, error) {
  75. var resp []DcDeviceBind
  76. query := fmt.Sprintf("select %s from %s where `project_id` = ? and `device_type` = ?", dcDeviceBindRows, m.table)
  77. err := m.conn.QueryRowsCtx(ctx, &resp, query, projectId, deviceType)
  78. switch err {
  79. case nil:
  80. return resp, nil
  81. case sqlc.ErrNotFound:
  82. return nil, ErrNotFound
  83. default:
  84. return nil, err
  85. }
  86. }
  87. func (m *defaultDcDeviceBindModel) FindOneByProjectIdDeviceCode(ctx context.Context, projectId int64, deviceCode string) (*DcDeviceBind, error) {
  88. var resp DcDeviceBind
  89. query := fmt.Sprintf("select %s from %s where `project_id` = ? and `device_code` = ? limit 1", dcDeviceBindRows, m.table)
  90. err := m.conn.QueryRowCtx(ctx, &resp, query, projectId, deviceCode)
  91. switch err {
  92. case nil:
  93. return &resp, nil
  94. case sqlc.ErrNotFound:
  95. return nil, ErrNotFound
  96. default:
  97. return nil, err
  98. }
  99. }
  100. func (m *defaultDcDeviceBindModel) Insert(ctx context.Context, data *DcDeviceBind) (sql.Result, error) {
  101. query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, dcDeviceBindRowsExpectAutoSet)
  102. ret, err := m.conn.ExecCtx(ctx, query, data.ProjectId, data.Name, data.DeviceCode, data.DeviceType, data.Status, data.Items, data.TargetTable)
  103. return ret, err
  104. }
  105. func (m *defaultDcDeviceBindModel) Update(ctx context.Context, newData *DcDeviceBind) error {
  106. query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, dcDeviceBindRowsWithPlaceHolder)
  107. _, err := m.conn.ExecCtx(ctx, query, newData.ProjectId, newData.Name, newData.DeviceCode, newData.DeviceType, newData.Status, newData.Items, newData.TargetTable, newData.Id)
  108. return err
  109. }
  110. func (m *defaultDcDeviceBindModel) tableName() string {
  111. return m.table
  112. }