package services import ( "nearle/models" "nearle/repositories" "time" ) type ProductService interface { GetProductSubCategory(categoryID, tenantID int) ([]models.ProductSubCategory, error) GetProductCount(tenantID, categoryID, subcategoryID int, approve string) ([]models.Productcount, error) GetProductCategory() ([]models.ProductCategory, error) GetProductVariants(tenantID, subcategoryID int) ([]models.Productvariant, error) GetCatalougeProducts(tenantID, locationID, subcategoryID, pageno, pagesize int, keyword string) ([]models.Products, error) GetProductStocks(tenantID, locationID string) ([]models.Productstocks, error) UpdateProductStatus(productIDs []int, status string) error CreateProductStock(stocks []models.Productstock) error CreateProduct(product models.Products) error UpdateProduct(product models.Products) error DeleteProduct(productID int) error GetStockStatement(tenantID, locationID, subcategoryID, pageno, pagesize int, keyword string) ([]models.Productstockstatement, error) GetLocationProducts(tenantID, locationID, subcategoryID, pageno, pagesize int, keyword string) ([]models.Locationproducts, error) GetLocationProductSummary(tenantID, locationID int) ([]models.ProductSummary, error) FetchFilteredProducts(categoryID, subcategoryID, productID, applocationID, tenantID, locationID int, keyword, productStatus, approve string, pageno, pagesize int) ([]models.Tenantproducts, error) GetProductByVariant(tenantid, variantid int) ([]models.Products, error) GetProductsBySubcategory(params models.ProductFilter) (map[string]interface{}, error) UpdateProductLocation(input models.Productlocations) error CreateProductLocation(input []models.Productlocations) error CreateProductVariant(input models.Productvariant) error } type productService struct { repo repositories.ProductRepository } func NewProductService(repo repositories.ProductRepository) ProductService { return &productService{repo: repo} } func (s *productService) GetProductSubCategory(categoryID, tenantID int) ([]models.ProductSubCategory, error) { return s.repo.GetProductSubCategory(categoryID, tenantID) } func (s *productService) GetProductCount(tenantID, categoryID, subcategoryID int, approve string) ([]models.Productcount, error) { return s.repo.GetProductCount(tenantID, categoryID, subcategoryID, approve) } func (s *productService) GetProductCategory() ([]models.ProductCategory, error) { return s.repo.GetProductCategory() } func (s *productService) GetProductVariants(tenantID, subcategoryID int) ([]models.Productvariant, error) { return s.repo.GetProductVariants(tenantID, subcategoryID) } func (s *productService) GetCatalougeProducts(tenantID, locationID, subcategoryID, pageno, pagesize int, keyword string) ([]models.Products, error) { return s.repo.GetCatalougeProducts(tenantID, locationID, subcategoryID, pageno, pagesize, keyword) } func (s *productService) GetProductStocks(tenantID, locationID string) ([]models.Productstocks, error) { return s.repo.GetProductStocks(tenantID, locationID) } func (s *productService) CreateProductStock(stocks []models.Productstock) error { for i := range stocks { stocks[i].Stockdate = time.Now() } if err := s.repo.CreateProductStock(stocks); err != nil { return err } idMap := make(map[int]struct{}) var productIDs []int for _, s := range stocks { if s.Productid > 0 { if _, exists := idMap[s.Productid]; !exists { idMap[s.Productid] = struct{}{} productIDs = append(productIDs, s.Productid) } } } if len(productIDs) > 0 { if err := s.repo.UpdateProductStatus(productIDs, "available"); err != nil { return err } } return nil } func (s *productService) UpdateProductStatus(productIDs []int, status string) error { return s.repo.UpdateProductStatus(productIDs, status) } func (s *productService) CreateProduct(product models.Products) error { return s.repo.CreateProduct(product) } func (s *productService) UpdateProduct(product models.Products) error { return s.repo.UpdateProduct(product) } func (s *productService) DeleteProduct(productID int) error { return s.repo.DeleteProduct(productID) } func (s *productService) GetStockStatement(tenantID, locationID, subcategoryID, pageno, pagesize int, keyword string) ([]models.Productstockstatement, error) { return s.repo.GetStockStatement(tenantID, locationID, subcategoryID, pageno, pagesize, keyword) } func (s *productService) GetLocationProducts(tenantID, locationID, subcategoryID, pageno, pagesize int, keyword string) ([]models.Locationproducts, error) { return s.repo.GetLocationProducts(tenantID, locationID, subcategoryID, pageno, pagesize, keyword) } func (s *productService) GetLocationProductSummary(tenantID, locationID int) ([]models.ProductSummary, error) { return s.repo.GetLocationProductSummary(tenantID, locationID) } func (s *productService) FetchFilteredProducts(categoryID, subcategoryID, productID, applocationID, tenantID, locationID int, keyword, productStatus, approve string, pageno, pagesize int) ([]models.Tenantproducts, error) { return s.repo.FetchFilteredProducts(categoryID, subcategoryID, productID, applocationID, tenantID, locationID, keyword, productStatus, approve, pageno, pagesize) } func (s *productService) GetProductByVariant(tenantid, variantid int) ([]models.Products, error) { var data []models.Products result, err := s.repo.GetProductByVariant(tenantid, variantid) if err != nil { return nil, err } data = result return data, nil } func (s *productService) GetProductsBySubcategory(params models.ProductFilter) (map[string]interface{}, error) { subcategories, err := s.repo.GetSubcategories(params.CategoryID) if err != nil { return nil, err } products, err := s.repo.GetProducts(params) if err != nil { return nil, err } var details []models.SubcategoryProductResponse var uncategorized []models.Products for _, sub := range subcategories { var subProducts []models.Products for _, p := range products { if p.Subcategoryid == sub.Subcategoryid { subProducts = append(subProducts, p) } } if len(subProducts) > 0 { details = append(details, models.SubcategoryProductResponse{ SubcategoryID: sub.Subcategoryid, SubcategoryName: sub.Subcategoryname, Image: sub.Image, Products: subProducts, }) } } for _, p := range products { if p.Subcategoryid == 0 { uncategorized = append(uncategorized, p) } } if len(uncategorized) > 0 { details = append(details, models.SubcategoryProductResponse{ SubcategoryID: 0, SubcategoryName: "Uncategorized", Products: uncategorized, }) } if params.TenantID > 0 { tenantInfo, err := s.repo.GetTenantInfo(params.TenantID, params.AppLocationID) if err == nil && tenantInfo != nil { tenantInfo["details"] = details return tenantInfo, nil } } return map[string]interface{}{"details": details}, nil } func (s *productService) UpdateProductLocation(input models.Productlocations) error { return s.repo.UpdateProductLocation(input) } func (s *productService) CreateProductLocation(input []models.Productlocations) error { return s.repo.CreateProductLocation(input) } func (s *productService) CreateProductVariant(input models.Productvariant) error { return s.repo.CreateProductVariant(input) }