From 73a3f0616dc9dbc947ab09408f730ce4d24287af Mon Sep 17 00:00:00 2001 From: duyu Date: Wed, 7 Aug 2024 13:46:10 +0800 Subject: [PATCH] fix --- app/http/controllers/device/device.go | 174 ++++++++++++++++++ app/http/controllers/orders/orders.go | 4 + .../controllers/transfersys/transfersys.go | 1 + app/http/entities/device/device.go | 61 ++++++ app/http/entities/orders/orders.go | 3 + app/http/entities/transfersys/transfersys.go | 1 + app/http/routes/route.go | 11 ++ app/models/device/device.go | 119 ++++++++++++ app/models/orders/orders.go | 14 +- app/services/device/device.go | 48 +++++ app/services/orders/orders.go | 6 +- build/sql/db.sql | 12 +- 12 files changed, 449 insertions(+), 5 deletions(-) create mode 100644 app/http/controllers/device/device.go create mode 100644 app/http/entities/device/device.go create mode 100644 app/models/device/device.go create mode 100644 app/services/device/device.go diff --git a/app/http/controllers/device/device.go b/app/http/controllers/device/device.go new file mode 100644 index 0000000..2428974 --- /dev/null +++ b/app/http/controllers/device/device.go @@ -0,0 +1,174 @@ +package device + +import ( + "time" + + "com.snow.auto_monitor/app/constants/errorcode" + common "com.snow.auto_monitor/app/http/controllers" + proEnt "com.snow.auto_monitor/app/http/entities/device" + proMod "com.snow.auto_monitor/app/models/device" + proServ "com.snow.auto_monitor/app/services/device" + "github.com/gin-gonic/gin" +) + +func GetById(c *gin.Context) { + request := new(proEnt.GetListByIdReq) + err := common.GenRequest(c, request) + if err != nil { + common.Error(c, errorcode.ParamError) + return + } + + res, err := proServ.GetById(request.Id) + if err != nil { + common.Error500(c) + return + } + + var response *proEnt.GetListByIdResp = nil + + if res != nil { + + response = &proEnt.GetListByIdResp{ + Id: res.Id, + DeviceNo: res.DeviceNo, + Phone: res.Phone, + Code: res.Code, + CreatedAt: res.CreatedAt.Format(time.RFC3339), + } + } + + 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 +// } + +// 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, +// }) +// } +// } + +// total, err := proServ.CountAll( +// request.Id, +// request.Name, +// start, +// end, +// ) +// if err != nil { +// common.Error500(c) +// return +// } + +// common.SuccessWithList(c, response, total) +// } + +func Create(c *gin.Context) { + request := new(proEnt.CreateReq) + err := common.GenRequest(c, request) + if err != nil { + common.Error(c, errorcode.ParamError) + return + } + + device := &proMod.Device{ + DeviceNo: request.DeviceNo, + Phone: request.Phone, + Code: request.Code, + } + + affected, err := proServ.Create(device) + if err != nil && affected > 0 { + common.Error500(c) + return + } + + response := &proEnt.CreateResp{ + Id: device.Id, + } + + common.Success(c, response) +} + +func Update(c *gin.Context) { + request := new(proEnt.UpdateReq) + err := common.GenRequest(c, request) + if err != nil { + common.Error(c, errorcode.ParamError) + return + } + + device := &proMod.Device{ + Id: request.Id, + DeviceNo: request.DeviceNo, + Phone: request.Phone, + Code: request.Code, + } + + affected, err := proServ.Update(device) + if err != nil && affected > 0 { + common.Error500(c) + return + } + + response := &proEnt.UpdateResp{ + Id: device.Id, + } + + common.Success(c, response) +} + +func Delete(c *gin.Context) { + request := new(proEnt.DeleteReq) + err := common.GenRequest(c, request) + if err != nil { + common.Error(c, errorcode.ParamError) + return + } + + affected, err := proServ.Delete(request.Id) + if err != nil && affected > 0 { + common.Error500(c) + return + } + + response := &proEnt.DeleteResp{ + Id: request.Id, + } + + common.Success(c, response) +} diff --git a/app/http/controllers/orders/orders.go b/app/http/controllers/orders/orders.go index 6ca163e..dcc7b69 100644 --- a/app/http/controllers/orders/orders.go +++ b/app/http/controllers/orders/orders.go @@ -35,6 +35,7 @@ func GetById(c *gin.Context) { OrderNo: item.OrderNo, MerchantId: item.MerchantId, ProductId: item.ProductId, + DeviceNo: item.DeviceNo, OutTradeNo: item.OutTradeNo, RechargeAccount: item.RechargeAccount, AccountType: item.AccountType, @@ -68,6 +69,7 @@ func Search(c *gin.Context) { request.OrderNo, request.MerchantId, request.ProductId, + request.DeviceNo, request.OutTradeNo, request.RechargeAccount, request.AccountType, @@ -92,6 +94,7 @@ func Search(c *gin.Context) { OrderNo: item.OrderNo, MerchantId: item.MerchantId, ProductId: item.ProductId, + DeviceNo: item.DeviceNo, OutTradeNo: item.OutTradeNo, RechargeAccount: item.RechargeAccount, AccountType: item.AccountType, @@ -111,6 +114,7 @@ func Search(c *gin.Context) { request.OrderNo, request.MerchantId, request.ProductId, + request.DeviceNo, request.OutTradeNo, request.RechargeAccount, request.AccountType, diff --git a/app/http/controllers/transfersys/transfersys.go b/app/http/controllers/transfersys/transfersys.go index 36cc2bb..58ba04a 100644 --- a/app/http/controllers/transfersys/transfersys.go +++ b/app/http/controllers/transfersys/transfersys.go @@ -79,6 +79,7 @@ func FinishOrder(c *gin.Context) { Id: request.Id, ProductId: request.ProductId, MerchantId: request.MerchantId, + DeviceNo: request.DeviceNo, TransferStatus: int64(status), FailReason: request.FailReason, } diff --git a/app/http/entities/device/device.go b/app/http/entities/device/device.go new file mode 100644 index 0000000..d1d98c8 --- /dev/null +++ b/app/http/entities/device/device.go @@ -0,0 +1,61 @@ +package merchant + +type GetListByIdReq struct { + Id int64 `json:"id" validate:"required" example:"1"` +} + +type GetListByIdResp struct { + Id int64 `json:"id" example:"1"` + 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 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 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 CreateReq struct { + DeviceNo string `json:"device_no" example:"123456"` + Phone string `json:"phone" example:"123456"` + Code int64 `json:"code" example:"123456"` +} + +type CreateResp struct { + Id int64 `json:"id" example:"1"` +} + +type UpdateReq 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"` +} + +type UpdateResp struct { + Id int64 `json:"id" example:"1"` +} + +type DeleteReq struct { + Id int64 `json:"id" validate:"required"` +} + +type DeleteResp struct { + Id int64 `json:"id" example:"1"` +} diff --git a/app/http/entities/orders/orders.go b/app/http/entities/orders/orders.go index acdff8f..8620a32 100644 --- a/app/http/entities/orders/orders.go +++ b/app/http/entities/orders/orders.go @@ -9,6 +9,7 @@ type GetListByIdResp struct { OrderNo string `json:"order_no"` MerchantId int64 `json:"merchant_id"` ProductId int64 `json:"product_id"` + DeviceNo string `json:"device_no"` OutTradeNo string `json:"out_trade_no"` RechargeAccount string `json:"recharge_account"` AccountType int64 `json:"account_type"` @@ -26,6 +27,7 @@ type SearchReq struct { OrderNo string `json:"order_no"` MerchantId int64 `json:"merchant_id"` ProductId int64 `json:"product_id"` + DeviceNo string `json:"device_no"` OutTradeNo string `json:"out_trade_no"` RechargeAccount string `json:"recharge_account"` AccountType int64 `json:"account_type"` @@ -41,6 +43,7 @@ type SearchResp struct { OrderNo string `json:"order_no"` MerchantId int64 `json:"merchant_id"` ProductId int64 `json:"product_id"` + DeviceNo string `json:"device_no"` OutTradeNo string `json:"out_trade_no"` RechargeAccount string `json:"recharge_account"` AccountType int64 `json:"account_type"` diff --git a/app/http/entities/transfersys/transfersys.go b/app/http/entities/transfersys/transfersys.go index de0ff59..335177f 100644 --- a/app/http/entities/transfersys/transfersys.go +++ b/app/http/entities/transfersys/transfersys.go @@ -26,6 +26,7 @@ type FinishOrderReq struct { OrderNo string `json:"order_no" validate:"required"` ProductId int64 `json:"product_id" validate:"required"` MerchantId int64 `json:"merchant_id" validate:"required"` + DeviceNo string `json:"device_no"` Result string `json:"result" validate:"required"` FailReason string `json:"fail_reason"` } diff --git a/app/http/routes/route.go b/app/http/routes/route.go index 0ff2f46..bc38df4 100644 --- a/app/http/routes/route.go +++ b/app/http/routes/route.go @@ -10,6 +10,7 @@ import ( "com.snow.auto_monitor/app/utils/metric" "com.snow.auto_monitor/config" + devCon "com.snow.auto_monitor/app/http/controllers/device" merCon "com.snow.auto_monitor/app/http/controllers/merchant" ordersCon "com.snow.auto_monitor/app/http/controllers/orders" proCon "com.snow.auto_monitor/app/http/controllers/product" @@ -89,6 +90,16 @@ func RegisterRoute(router *gin.Engine) { product.POST("/delete", proCon.Delete) } + device := router.Group("/device") + // product.Use(middlewares.VerifyHtmlIp()) + { + device.POST("/get_by_id", devCon.GetById) + // device.POST("/search", devCon.Search) + device.POST("/create", devCon.Create) + device.POST("/update", devCon.Update) + device.POST("/delete", devCon.Delete) + } + whitelist := router.Group("/whitelist") whitelist.Use(middlewares.VerifyHtmlIp()) { diff --git a/app/models/device/device.go b/app/models/device/device.go new file mode 100644 index 0000000..b4d446c --- /dev/null +++ b/app/models/device/device.go @@ -0,0 +1,119 @@ +package device + +import ( + "sync" + "time" + + "github.com/qit-team/snow-core/db" +) + +var ( + once sync.Once + m *deviceModel +) + +/** + * Device + */ +type Device struct { + Id int64 `xorm:"pk autoincr"` //注:使用getOne 或者ID() 需要设置主键 + DeviceNo string + Phone string + Code int64 + CreatedAt time.Time `xorm:"created"` +} + +/** + * 表名规则 + * @wiki http://gobook.io/read/github.com/go-xorm/manual-zh-CN/chapter-02/3.tags.html + */ +func (m *Device) TableName() string { + return "device" +} + +/** + * 私有化,防止被外部new + */ +type deviceModel struct { + db.Model //组合基础Model,集成基础Model的属性和方法 +} + +// 单例模式 +func GetInstance() *deviceModel { + once.Do(func() { + m = new(deviceModel) + //m.DiName = "" //设置数据库实例连接,默认db.SingletonMain + }) + return m +} + +/** + * 查询主键ID的记录 + * @param id 主键ID + * @return has 是否有记录 + * @return err 错误信息 + * @return device 查询结果 + */ +func (m *deviceModel) GetById(id int64) (device *Device, has bool, err error) { + device = &Device{} + has, err = m.GetDb().ID(id).Get(device) + if err != nil || !has { + device = nil + } + 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) 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) Create(device *Device) (affected int64, err error) { + device.CreatedAt = time.Now() + affected, err = m.GetDb().Insert(device) + return +} + +func (m *deviceModel) Update(device *Device) (affected int64, err error) { + affected, err = m.GetDb().ID(device.Id).Update(device) + return +} + +func (m *deviceModel) Delete(id int64) (affected int64, err error) { + affected, err = m.GetDb().ID(id).Delete(&Device{}) + return +} diff --git a/app/models/orders/orders.go b/app/models/orders/orders.go index f318596..756450d 100644 --- a/app/models/orders/orders.go +++ b/app/models/orders/orders.go @@ -22,6 +22,7 @@ type Orders struct { OrderNo string MerchantId int64 ProductId int64 + DeviceNo string OutTradeNo string RechargeAccount string @@ -94,6 +95,7 @@ func (m *ordersModel) Search( order_no string, merchant_id int64, product_id int64, + device_no string, out_trade_no string, recharge_account string, account_type int64, @@ -123,6 +125,10 @@ func (m *ordersModel) Search( sql += " and product_id = ?" args = append(args, product_id) } + if device_no != "" { + sql += " and device_no = ?" + args = append(args, device_no) + } if out_trade_no != "" { sql += " and out_trade_no = ?" args = append(args, out_trade_no) @@ -156,6 +162,7 @@ func (m *ordersModel) CountAll( order_no string, merchant_id int64, product_id int64, + device_no string, out_trade_no string, recharge_account string, account_type int64, @@ -183,6 +190,10 @@ func (m *ordersModel) CountAll( sql += " and merchant_id = ?" args = append(args, product_id) } + if device_no != "" { + sql += " and device_no = ?" + args = append(args, device_no) + } if out_trade_no != "" { sql += " and out_trade_no = ?" args = append(args, out_trade_no) @@ -354,7 +365,8 @@ func (m *ordersModel) GetTimeoutOrder( sql := "1=1" var args []interface{} sql += " and transfer_status not in (1,2,4,5,6)" - sql += " and created_at <= DATE_SUB(DATE_ADD(NOW(),INTERVAL 8 HOUR), INTERVAL 5 MINUTE)" + sql += " and created_at <= DATE_SUB(NOW(), INTERVAL 5 MINUTE)" + // sql += " and created_at <= DATE_SUB(DATE_ADD(NOW(),INTERVAL 8 HOUR), INTERVAL 5 MINUTE)" err = m.GetDb().Where(sql, args...).OrderBy("created_at").Limit(limit).Find(&orders) return } diff --git a/app/services/device/device.go b/app/services/device/device.go new file mode 100644 index 0000000..cae030e --- /dev/null +++ b/app/services/device/device.go @@ -0,0 +1,48 @@ +package device + +import ( + models "com.snow.auto_monitor/app/models/device" +) + +func GetLimitStart(limit int, page int) (int, int) { + if limit <= 0 { + limit = 20 + } + if page > 0 { + page = (page - 1) * limit + } else { + page = 0 + } + return limit, page +} + +func GetById(id int64) (res *models.Device, err error) { + res, _, err = models.GetInstance().GetById(id) + 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 CountAll(id int64, name string, startTime string, endTime string) (res int64, err error) { +// res, err = models.GetInstance().CountAll(id, name, startTime, endTime) +// return +// } + +func Create(device *models.Device) (affected int64, err error) { + affected, err = models.GetInstance().Create(device) + return +} + +func Update(device *models.Device) (affected int64, err error) { + affected, err = models.GetInstance().Update(device) + return +} + +func Delete(id int64) (affected int64, err error) { + affected, err = models.GetInstance().Delete(id) + return +} diff --git a/app/services/orders/orders.go b/app/services/orders/orders.go index 553e223..b4dd2ba 100644 --- a/app/services/orders/orders.go +++ b/app/services/orders/orders.go @@ -25,6 +25,7 @@ func Search(id int64, order_no string, merchant_id int64, product_id int64, + device_no string, out_trade_no string, recharge_account string, account_type int64, @@ -36,7 +37,7 @@ func Search(id int64, limit int, page int) (res []*models.Orders, err error) { limit, page = GetLimitStart(limit, page) - res, err = models.GetInstance().Search(id, order_no, merchant_id, product_id, out_trade_no, + res, err = models.GetInstance().Search(id, order_no, merchant_id, product_id, device_no, out_trade_no, recharge_account, account_type, status, transfer_status, startTime, endTime, limit, page) return @@ -46,6 +47,7 @@ func CountAll(id int64, order_no string, merchant_id int64, product_id int64, + device_no string, out_trade_no string, recharge_account string, account_type int64, @@ -54,7 +56,7 @@ func CountAll(id int64, transfer_status int64, startTime string, endTime string) (res int64, err error) { - res, err = models.GetInstance().CountAll(id, order_no, merchant_id, product_id, out_trade_no, + res, err = models.GetInstance().CountAll(id, order_no, merchant_id, product_id,device_no, out_trade_no, recharge_account, account_type, status, transfer_status, startTime, endTime) return diff --git a/build/sql/db.sql b/build/sql/db.sql index 2c015ba..70848eb 100644 --- a/build/sql/db.sql +++ b/build/sql/db.sql @@ -40,7 +40,7 @@ create table product ( status tinyint, -- 1.上架 2.下架 created_at timestamp default current_timestamp, -- v2 - type tinyint-- 1.拼多多 2.多多进宝 3. 拼多多+多多进宝 + type tinyint-- 1.拼多多 2.多多进宝 3. 拼多多+多多进宝 4. 直接返回成功商品 extend_parameter varchar(1024), ); @@ -51,4 +51,12 @@ create table combination_product( parent_id int, child_id int, status tinyint -- 暂未用到 -) \ No newline at end of file +) + +create table device ( + id int primary key auto_increment, + device_no varchar(255), + phone varchar(255), + code int, + created_at timestamp default current_timestamp +); \ No newline at end of file