This commit is contained in:
duyu 2024-08-07 15:31:24 +08:00
parent 73a3f0616d
commit 22c164c780
6 changed files with 132 additions and 116 deletions

View File

@ -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)

View File

@ -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"`

View File

@ -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)

View File

@ -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()

View File

@ -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"`
}

View File

@ -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)