This commit is contained in:
duyu 2024-08-07 13:46:10 +08:00
parent b1d3b3f8d8
commit 73a3f0616d
12 changed files with 449 additions and 5 deletions

View File

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

View File

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

View File

@ -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,
}

View File

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

View File

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

View File

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

View File

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

119
app/models/device/device.go Normal file
View File

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

View File

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

View File

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

View File

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

View File

@ -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 -- 暂未用到
)
)
create table device (
id int primary key auto_increment,
device_no varchar(255),
phone varchar(255),
code int,
created_at timestamp default current_timestamp
);