From 22c164c780a97430b031390326e7a614629f4a1d Mon Sep 17 00:00:00 2001 From: duyu Date: Wed, 7 Aug 2024 15:31:24 +0800 Subject: [PATCH] fix --- app/http/controllers/device/device.go | 103 +++++++++++++------------- app/http/entities/device/device.go | 33 ++++----- app/http/routes/route.go | 2 +- app/models/device/device.go | 90 +++++++++++++--------- app/models/orders/orders.go | 2 +- app/services/device/device.go | 18 ++--- 6 files changed, 132 insertions(+), 116 deletions(-) diff --git a/app/http/controllers/device/device.go b/app/http/controllers/device/device.go index 2428974..34a8a89 100644 --- a/app/http/controllers/device/device.go +++ b/app/http/controllers/device/device.go @@ -41,61 +41,62 @@ func GetById(c *gin.Context) { common.Success(c, response) } -// func Search(c *gin.Context) { -// request := new(proEnt.SearcgReq) -// err := common.GenRequest(c, request) -// if err != nil { -// common.Error(c, errorcode.ParamError) -// return -// } -// start, end := "", "" -// if len(request.CreatedAt) > 1 { -// start = request.CreatedAt[0] -// end = request.CreatedAt[1] -// } -// res, err := proServ.Search( -// request.Id, -// request.Name, -// start, -// end, -// request.PageSize, -// request.PageNum, -// ) -// if err != nil { -// common.Error500(c) -// return -// } +func Search(c *gin.Context) { + request := new(proEnt.SearcgReq) + err := common.GenRequest(c, request) + if err != nil { + common.Error(c, errorcode.ParamError) + return + } + start, end := "", "" + if len(request.CreatedAt) > 1 { + start = request.CreatedAt[0] + end = request.CreatedAt[1] + } + res, err := proServ.Search( + request.Id, + request.DeviceNo, + request.Phone, + request.Code, + start, + end, + request.PageSize, + request.PageNum, + ) + if err != nil { + common.Error500(c) + return + } -// var response []*proEnt.SearchResp = nil + var response []*proEnt.SearchResp = nil -// if len(res) > 0 { -// for _, item := range res { -// response = append(response, &proEnt.SearchResp{ -// Id: item.Id, -// Name: item.Name, -// Price: item.Price, -// DeviceUrl: item.DeviceUrl, -// Status: item.Status, -// CreatedAt: item.CreatedAt.Format(time.RFC3339), -// Type: item.Type, -// ExtendParameter: item.ExtendParameter, -// }) -// } -// } + if len(res) > 0 { + for _, item := range res { + response = append(response, &proEnt.SearchResp{ + Id: item.Id, + DeviceNo: item.DeviceNo, + Phone: item.Phone, + Code: item.Code, + CreatedAt: item.CreatedAt.Format(time.RFC3339), + }) + } + } -// total, err := proServ.CountAll( -// request.Id, -// request.Name, -// start, -// end, -// ) -// if err != nil { -// common.Error500(c) -// return -// } + total, err := proServ.CountAll( + request.Id, + request.DeviceNo, + request.Phone, + request.Code, + start, + end, + ) + if err != nil { + common.Error500(c) + return + } -// common.SuccessWithList(c, response, total) -// } + common.SuccessWithList(c, response, total) +} func Create(c *gin.Context) { request := new(proEnt.CreateReq) diff --git a/app/http/entities/device/device.go b/app/http/entities/device/device.go index d1d98c8..2cd3b08 100644 --- a/app/http/entities/device/device.go +++ b/app/http/entities/device/device.go @@ -12,24 +12,23 @@ type GetListByIdResp struct { CreatedAt string `json:"created_at" example:"2020-01-01 00:00:00"` } -// type SearcgReq struct { -// Id int64 `json:"id"` -// Name string `json:"name"` -// CreatedAt []string `json:"created_at" form:"created_at"` -// PageNum int `json:"page_num" form:"page" validate:"required"` -// PageSize int `json:"page_size" form:"page_size" validate:"required"` -// } +type SearcgReq struct { + Id int64 `json:"id"` + DeviceNo string `json:"device_no" form:"device_no"` + Phone string `json:"phone" form:"phone"` + Code int64 `json:"code" form:"code"` + CreatedAt []string `json:"created_at" form:"created_at"` + PageNum int `json:"page_num" form:"page" validate:"required"` + PageSize int `json:"page_size" form:"page_size" validate:"required"` +} -// type SearchResp struct { -// Id int64 `json:"id" example:"1"` -// Name string `json:"name" example:"snow"` -// Price int64 `json:"price" example:"100"` -// ProductUrl string `json:"product_url" example:"http://www.baidu.com"` -// Status int64 `json:"status" example:"1"` -// CreatedAt string `json:"created_at" example:"2020-01-01 00:00:00"` -// Type int64 `json:"type" example:"1"` -// ExtendParameter string `json:"extend_parameter" example:"{}"` -// } +type SearchResp struct { + Id int64 `json:"id" validate:"required"` + DeviceNo string `json:"device_no" example:"123456"` + Phone string `json:"phone" example:"123456"` + Code int64 `json:"code" example:"123456"` + CreatedAt string `json:"created_at" example:"2020-01-01 00:00:00"` +} type CreateReq struct { DeviceNo string `json:"device_no" example:"123456"` diff --git a/app/http/routes/route.go b/app/http/routes/route.go index bc38df4..eeffde6 100644 --- a/app/http/routes/route.go +++ b/app/http/routes/route.go @@ -94,7 +94,7 @@ func RegisterRoute(router *gin.Engine) { // product.Use(middlewares.VerifyHtmlIp()) { device.POST("/get_by_id", devCon.GetById) - // device.POST("/search", devCon.Search) + device.POST("/search", devCon.Search) device.POST("/create", devCon.Create) device.POST("/update", devCon.Update) device.POST("/delete", devCon.Delete) diff --git a/app/models/device/device.go b/app/models/device/device.go index b4d446c..36c8167 100644 --- a/app/models/device/device.go +++ b/app/models/device/device.go @@ -63,44 +63,60 @@ func (m *deviceModel) GetById(id int64) (device *Device, has bool, err error) { return } -// func (m *deviceModel) Search(id int64, name string, startTime string, endTime string, limit int, page int) (device []*Device, err error) { -// device = make([]*Device, 0) -// sql := "1=1" -// var args []interface{} -// if id != 0 { -// sql += " and id = ?" -// args = append(args, id) -// } -// if name != "" { -// sql += " and name = ?" -// args = append(args, name) -// } -// if startTime != "" && endTime != "" { -// sql += " and created_at >= ? and created_at <= ?" -// args = append(args, startTime, endTime) -// } -// err = m.GetDb().Where(sql, args...).OrderBy("created_at desc").Limit(limit, page).Find(&device) -// return -// } +func (m *deviceModel) Search(id int64, device_no string, phone string, code int64, startTime string, endTime string, limit int, page int) (device []*Device, err error) { + device = make([]*Device, 0) + sql := "1=1" + var args []interface{} + if id != 0 { + sql += " and id = ?" + args = append(args, id) + } + if device_no != "" { + sql += " and device_no = ?" + args = append(args, device_no) + } + if phone != "" { + sql += " and phone = ?" + args = append(args, phone) + } + if code != 0 { + sql += " and code = ?" + args = append(args, code) + } + if startTime != "" && endTime != "" { + sql += " and created_at >= ? and created_at <= ?" + args = append(args, startTime, endTime) + } + err = m.GetDb().Where(sql, args...).OrderBy("created_at desc").Limit(limit, page).Find(&device) + return +} -// func (m *deviceModel) CountAll(id int64, name string, startTime string, endTime string) (res int64, err error) { -// sql := "1=1" -// var args []interface{} -// if id != 0 { -// sql += " and id = ?" -// args = append(args, id) -// } -// if name != "" { -// sql += " and name = ?" -// args = append(args, name) -// } -// if startTime != "" && endTime != "" { -// sql += " and created_at >= ? and created_at <= ?" -// args = append(args, startTime, endTime) -// } -// res, err = m.GetDb().Table("device").Where(sql, args...).Count() -// return -// } +func (m *deviceModel) CountAll(id int64, device_no string, phone string, code int64, startTime string, endTime string) (res int64, err error) { + sql := "1=1" + var args []interface{} + if id != 0 { + sql += " and id = ?" + args = append(args, id) + } + if device_no != "" { + sql += " and device_no = ?" + args = append(args, device_no) + } + if phone != "" { + sql += " and phone = ?" + args = append(args, phone) + } + if code != 0 { + sql += " and code = ?" + args = append(args, code) + } + if startTime != "" && endTime != "" { + sql += " and created_at >= ? and created_at <= ?" + args = append(args, startTime, endTime) + } + res, err = m.GetDb().Table("device").Where(sql, args...).Count() + return +} func (m *deviceModel) Create(device *Device) (affected int64, err error) { device.CreatedAt = time.Now() diff --git a/app/models/orders/orders.go b/app/models/orders/orders.go index 756450d..d77dceb 100644 --- a/app/models/orders/orders.go +++ b/app/models/orders/orders.go @@ -22,7 +22,6 @@ type Orders struct { OrderNo string MerchantId int64 ProductId int64 - DeviceNo string OutTradeNo string RechargeAccount string @@ -34,6 +33,7 @@ type Orders struct { Status int64 TransferStatus int64 FailReason string + DeviceNo string CreatedAt time.Time `xorm:"created"` } diff --git a/app/services/device/device.go b/app/services/device/device.go index cae030e..93a95a9 100644 --- a/app/services/device/device.go +++ b/app/services/device/device.go @@ -21,16 +21,16 @@ func GetById(id int64) (res *models.Device, err error) { return } -// func Search(id int64, name string, startTime string, endTime string, limit int, page int) (res []*models.Device, err error) { -// limit, page = GetLimitStart(limit, page) -// res, err = models.GetInstance().Search(id, name, startTime, endTime, limit, page) -// return -// } +func Search(id int64, device_no string,phone string,code int64, startTime string, endTime string, limit int, page int) (res []*models.Device, err error) { + limit, page = GetLimitStart(limit, page) + res, err = models.GetInstance().Search(id, device_no,phone,code, startTime, endTime, limit, page) + return +} -// func CountAll(id int64, name string, startTime string, endTime string) (res int64, err error) { -// res, err = models.GetInstance().CountAll(id, name, startTime, endTime) -// return -// } +func CountAll(id int64, device_no string,phone string,code int64, startTime string, endTime string) (res int64, err error) { + res, err = models.GetInstance().CountAll(id, device_no,phone,code, startTime, endTime) + return +} func Create(device *models.Device) (affected int64, err error) { affected, err = models.GetInstance().Create(device)