123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package product
- import (
- "eta/eta_mini_ht_api/common/exception"
- "eta/eta_mini_ht_api/common/utils/page"
- "eta/eta_mini_ht_api/controllers"
- productService "eta/eta_mini_ht_api/service/product"
- "fmt"
- )
- var (
- productMap = map[string]bool{
- "audio": true,
- "video": true,
- "media": true,
- "report": true,
- "package": true,
- }
- )
- type ProductController struct {
- controllers.ListController
- }
- // GetProductInfo 获取商品信息
- // @Summary 获取商品信息
- // @Description 获取商品信息
- // @Success 200 {object} controllers.BaseResponse
- // @router /getProductInfo [get]
- func (p *ProductController) GetProductInfo(productId int) {
- controllers.Wrap(&p.BaseController, func() (result *controllers.WrapData, err error) {
- result = p.InitWrapData("获取商品详情失败")
- if productId <= 0 {
- err = exception.New(exception.IllegalProductId)
- p.FailedResult("获取商品详情失败", result)
- return
- }
- productInfo, err := productService.GetProductInfoById(productId)
- if err != nil {
- p.FailedResult("获取商品详情失败", result)
- return
- }
- p.SuccessResult("获取商品详情成功", productInfo, result)
- return
- })
- }
- // RelatePackage 关联套餐搜索
- // @Summary 关联套餐搜索
- // @Description 关联套餐搜索
- // @Success 200 {object} controllers.BaseResponse
- // @router /relatePackage [get]
- func (p *ProductController) RelatePackage(productId int) {
- controllers.Wrap(&p.BaseController, func() (result *controllers.WrapData, err error) {
- result = p.InitWrapData("获取关联套餐信息失败")
- if productId <= 0 {
- err = exception.New(exception.IllegalProductId)
- p.FailedResult("获取关联套餐信息失败", result)
- return
- }
- productInfo, err := productService.GetProductInfoById(productId)
- if err != nil {
- p.FailedResult("获取关联套餐信息失败", result)
- err = exception.New(exception.ProductTypeError)
- return
- }
- if productInfo.Type == "package" {
- p.FailedResult("该商品为套餐,无法获取想关联套餐商品", result)
- err = exception.New(exception.ProductTypeError)
- }
- switch productInfo.Type {
- case "audio", "video":
- productInfo.MediaId = productInfo.SourceId
- case "report":
- productInfo.ReportId = productInfo.SourceId
- }
- productList, err := productService.GetRelatePackage(productInfo)
- productList = append(productList, productInfo)
- if err != nil {
- p.FailedResult("获取关联套餐信息失败", result)
- return
- }
- p.SuccessResult("获取关联套餐信息成功", productList, result)
- return
- })
- }
- // ProductSearch 获取商品信息
- // @Summary 获取商品信息
- // @Description 获取商品信息
- // @Success 200 {object} controllers.BaseResponse
- // @router /productSearch [get]
- func (p *ProductController) ProductSearch(productType, key string) {
- controllers.Wrap(&p.BaseController, func() (result *controllers.WrapData, err error) {
- result = p.InitWrapData("搜索产品信息失败")
- if !productMap[productType] {
- p.FailedResult("搜索产品信息失败", result)
- err = exception.New(exception.ProductTypeError)
- return
- }
- //for i := 0; i < len(productMap); i++ {
- // //if productType == productMap[i] {
- // // productList, err := productService.ProductSearch(productType, key)
- // // if err != nil {
- // // p.FailedResult("搜索产品信息失败", result)
- // // return
- // // }
- // }
- //}
- fmt.Printf("productType:%v,key:%v\n", productType, key)
- if err != nil {
- p.FailedResult("获取关联套餐信息失败", result)
- err = exception.New(exception.ProductTypeError)
- return
- }
- if err != nil {
- p.FailedResult("搜索产品信息失败", result)
- return
- }
- p.SuccessResult("搜索产品信息成功", nil, result)
- return
- })
- }
- // ProductList 产品列表
- // @Summary 产品列表
- // @Description 产品列表
- // @Success 200 {object} controllers.BaseResponse
- // @router /ProductList [get]
- func (p *ProductController) ProductList(productType string) {
- controllers.Wrap(&p.BaseController, func() (result *controllers.WrapData, err error) {
- result = p.InitWrapData("分页查询产品列表失败")
- if !productMap[productType] {
- err = exception.New(exception.ProductTypeError)
- p.FailedResult("分页查询产品列表失败", result)
- return
- }
- pageRes := page.Page{
- Current: p.PageInfo.Current,
- PageSize: p.PageInfo.PageSize,
- }
- pageRes.Total, pageRes.LatestId = productService.GetTotalPageCountByProductType(productType)
- if p.PageInfo.LatestId == 0 {
- p.PageInfo.LatestId = pageRes.LatestId
- p.PageInfo.Total = pageRes.Total
- } else {
- pageRes.LatestId = p.PageInfo.LatestId
- pageRes.Total = p.PageInfo.Total
- }
- pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
- list, err := productService.ProductList(productType, p.PageInfo)
- if err != nil {
- p.FailedResult("分页查询产品列表失败", result)
- err = exception.NewWithException(exception.GetProductListInfoFailed, err.Error())
- return
- }
- productList := new(page.PageResult)
- productList.Data = list
- productList.Page = pageRes
- p.SuccessResult("分页查询产品列表成功", productList, result)
- return
- })
- }
|