package product import ( logger "eta/eta_mini_ht_api/common/component/log" "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" "eta/eta_mini_ht_api/service/user" "sync/atomic" ) 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(key string, isSignal bool) { controllers.Wrap(&p.BaseController, func() (result *controllers.WrapData, err error) { result = p.InitWrapData("搜索产品信息失败") userInfo := p.Data["user"].(user.User) var productList []productService.ProductDTO var permissionWeightMap map[int]*atomic.Int32 pageRes := page.Page{ Current: p.PageInfo.Current, PageSize: p.PageInfo.PageSize, } productIdMap, err := productService.RangeProductList() if err != nil { logger.Error("搜索产品信息失败") err = exception.NewWithException(exception.GetProductListFailed, err.Error()) return } productPage := new(page.PageResult) if isSignal { if len(productIdMap["report"]) == 0 && len(productIdMap["audio"]) == 0 && len(productIdMap["video"]) == 0 { logger.Info("没有相关的产品类型产品") productPage.Page = pageRes p.SuccessResult("搜索产品信息成功", productPage, result) return } } else { if len(productIdMap["package"]) == 0 { logger.Info("没有相关的产品类型产品") productPage.Page = pageRes p.SuccessResult("搜索产品信息成功", productList, result) return } } //搜索出相关的报告和音视频 productSearchList, searchErr := productService.SearchRelateProduct(key, productIdMap) if searchErr != nil { logger.Error("搜索报告产品失败") return } if len(productSearchList) == 0 { logger.Info("没有相关的产品类型产品") productPage.Page = pageRes p.SuccessResult("搜索产品信息成功", productList, result) return } if isSignal { pageRes.Total = int64(len(productSearchList)) pageRes.LatestId = productService.LatestId() } else { pageRes.Total, pageRes.LatestId, permissionWeightMap = productService.CountSearchPackageList(productSearchList) } 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) productList, err = productService.ProductListBySort(isSignal, productSearchList, permissionWeightMap, userInfo.Id, p.PageInfo) productPage.Data = productList productPage.Page = pageRes p.SuccessResult("搜索产品信息成功", productPage, result) return }) } // ProductList 产品列表 // @Summary 产品列表 // @Description 产品列表 // @Success 200 {object} controllers.BaseResponse // @router /ProductList [get] func (p *ProductController) ProductList(productType string, permissionIds string) { controllers.Wrap(&p.BaseController, func() (result *controllers.WrapData, err error) { result = p.InitWrapData("分页查询产品列表失败") userInfo := p.Data["user"].(user.User) if !productMap[productType] { err = exception.New(exception.ProductTypeError) p.FailedResult("分页查询产品列表失败", result) return } permissionIdList, err := p.TransPermissionIds(permissionIds) if err != nil { logger.Error("品种列表解析错误:%v", err) p.FailedResult("分页查询产品列表失败", result) err = exception.New(exception.GetProductListFailed) return } pageRes := page.Page{ Current: p.PageInfo.Current, PageSize: p.PageInfo.PageSize, } var productIds []int pageRes.Total, pageRes.LatestId, productIds = productService.GetProductListByProductType(productType, permissionIdList, userInfo.Id) 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(productIds, userInfo.Id, p.PageInfo) productList := new(page.PageResult) productList.Data = list productList.Page = pageRes p.SuccessResult("分页查询产品列表成功", productList, result) return }) }