2024-07-12 18:11:21 +08:00
|
|
|
package product
|
|
|
|
|
|
|
|
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/product"
|
|
|
|
proMod "com.snow.auto_monitor/app/models/product"
|
|
|
|
proServ "com.snow.auto_monitor/app/services/product"
|
|
|
|
"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{
|
2024-07-26 17:56:54 +08:00
|
|
|
Id: res.Id,
|
|
|
|
Name: res.Name,
|
|
|
|
Price: res.Price,
|
|
|
|
ProductUrl: res.ProductUrl,
|
|
|
|
Status: res.Status,
|
|
|
|
CreatedAt: res.CreatedAt.Format(time.RFC3339),
|
|
|
|
Type: res.Type,
|
|
|
|
ExtendParameter: res.ExtendParameter,
|
2024-09-10 14:26:07 +08:00
|
|
|
QueueNo: res.QueueNo,
|
2024-07-12 18:11:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2024-07-26 17:56:54 +08:00
|
|
|
Id: item.Id,
|
|
|
|
Name: item.Name,
|
|
|
|
Price: item.Price,
|
|
|
|
ProductUrl: item.ProductUrl,
|
|
|
|
Status: item.Status,
|
|
|
|
CreatedAt: item.CreatedAt.Format(time.RFC3339),
|
|
|
|
Type: item.Type,
|
|
|
|
ExtendParameter: item.ExtendParameter,
|
2024-09-10 14:26:07 +08:00
|
|
|
QueueNo: item.QueueNo,
|
2024-07-12 18:11:21 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
product := &proMod.Product{
|
2024-07-26 17:56:54 +08:00
|
|
|
Name: request.Name,
|
|
|
|
Price: request.Price,
|
|
|
|
ProductUrl: request.ProductUrl,
|
|
|
|
Status: 1, //1.上架 2.下架
|
|
|
|
Type: request.Type,
|
|
|
|
ExtendParameter: request.ExtendParameter,
|
2024-09-10 14:26:07 +08:00
|
|
|
QueueNo: request.QueueNo,
|
2024-07-12 18:11:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := proServ.Create(product)
|
|
|
|
if err != nil && affected > 0 {
|
|
|
|
common.Error500(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response := &proEnt.CreateResp{
|
|
|
|
Id: product.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
|
|
|
|
}
|
|
|
|
|
|
|
|
product := &proMod.Product{
|
2024-07-26 17:56:54 +08:00
|
|
|
Id: request.Id,
|
|
|
|
Name: request.Name,
|
|
|
|
ProductUrl: request.ProductUrl,
|
|
|
|
Price: request.Price,
|
|
|
|
Type: request.Type,
|
|
|
|
ExtendParameter: request.ExtendParameter,
|
2024-09-10 14:26:07 +08:00
|
|
|
QueueNo: request.QueueNo,
|
2024-07-12 18:11:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := proServ.Update(product)
|
|
|
|
if err != nil && affected > 0 {
|
|
|
|
common.Error500(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response := &proEnt.UpdateResp{
|
|
|
|
Id: product.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)
|
|
|
|
}
|