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{ 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, QueueNo: res.QueueNo, } } 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, ProductUrl: item.ProductUrl, Status: item.Status, CreatedAt: item.CreatedAt.Format(time.RFC3339), Type: item.Type, ExtendParameter: item.ExtendParameter, QueueNo: item.QueueNo, }) } } 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{ Name: request.Name, Price: request.Price, ProductUrl: request.ProductUrl, Status: 1, //1.上架 2.下架 Type: request.Type, ExtendParameter: request.ExtendParameter, QueueNo: request.QueueNo, } 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{ Id: request.Id, Name: request.Name, ProductUrl: request.ProductUrl, Price: request.Price, Type: request.Type, ExtendParameter: request.ExtendParameter, QueueNo: request.QueueNo, } 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) }