12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package model
- import (
- "context"
- "fmt"
- "github.com/zeromicro/go-zero/core/stores/sqlc"
- "github.com/zeromicro/go-zero/core/stores/sqlx"
- "strings"
- )
- var _ DcWorkingValveModel = (*customDcWorkingValveModel)(nil)
- type (
- // DcWorkingValveModel is an interface to be customized, add more methods here,
- // and implement the added methods in customDcWorkingValveModel.
- DcWorkingValveModel interface {
- dcWorkingValveModel
- FindOneByCode(ctx context.Context, projectId int64, deviceCode string) (*DcWorkingValve, error)
- Search(ctx context.Context, projectId int64, deviceCode, st, et string, offset, limit int64, orderBy string) ([]DcWorkingValve, error)
- }
- customDcWorkingValveModel struct {
- *defaultDcWorkingValveModel
- }
- )
- // NewDcWorkingValveModel returns a model for the database table.
- func NewDcWorkingValveModel(conn sqlx.SqlConn) DcWorkingValveModel {
- return &customDcWorkingValveModel{
- defaultDcWorkingValveModel: newDcWorkingValveModel(conn),
- }
- }
- func (m *defaultDcWorkingValveModel) Search(ctx context.Context, projectId int64, deviceCode, st, et string, offset, limit int64, orderBy string) ([]DcWorkingValve, error) {
- var resp []DcWorkingValve
- condition := []string{"`project_id`=?", "`device_code`=?"}
- vars := []interface{}{projectId, deviceCode}
- if st != "" {
- condition = append(condition, "`c_time`>=?")
- vars = append(vars, st)
- }
- if et != "" {
- condition = append(condition, "`c_time`<?")
- vars = append(vars, et)
- }
- if orderBy == "" {
- orderBy = "id desc"
- }
- query := fmt.Sprintf("select %s from %s where %s order by %s limit %d, %d", dcWorkingValveRows, m.table, strings.Join(condition, " and "), orderBy, offset, limit)
- err := m.conn.QueryRowsCtx(ctx, &resp, query, vars...)
- switch err {
- case nil:
- return resp, nil
- case sqlc.ErrNotFound:
- return nil, ErrNotFound
- default:
- return nil, err
- }
- }
- func (m *defaultDcWorkingValveModel) FindOneByCode(ctx context.Context, projectId int64, deviceCode string) (*DcWorkingValve, error) {
- var resp DcWorkingValve
- query := fmt.Sprintf("select %s from %s where `project_id` = ? and `device_code` = ? order by c_time desc limit 1", dcWorkingValveRows, m.table)
- err := m.conn.QueryRowCtx(ctx, &resp, query, projectId, deviceCode)
- switch err {
- case nil:
- return &resp, nil
- case sqlc.ErrNotFound:
- return nil, ErrNotFound
- default:
- return nil, err
- }
- }
|