product.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package service
  2. import (
  3. "cc-officialweb/models"
  4. "cc-officialweb/utils"
  5. )
  6. // GetIndexProductServe 首页获取产品服务
  7. func GetIndexProductServe(types string) []models.Products {
  8. var serve []models.Products
  9. tx := utils.DB.Where("type = ?", types).Where("is_index = ?", true).Order("sort asc").Find(&serve)
  10. if tx.Error != nil {
  11. return nil
  12. }
  13. return serve
  14. }
  15. // GetProduct 获取所有产品
  16. func GetProduct(ptype string) []models.Products {
  17. var serve []models.Products
  18. tx := utils.DB.Where("type = ?", "product").Where("ptype = ?", ptype).Order("sort asc").Find(&serve)
  19. if tx.Error != nil {
  20. return nil
  21. }
  22. return serve
  23. }
  24. // GetProductDetail 获取产品详情
  25. func GetProductDetail(id int) models.Products {
  26. var server models.Products
  27. tx := utils.DB.Where("id = ?", id).First(&server)
  28. if tx.Error != nil {
  29. return server
  30. }
  31. return server
  32. }
  33. // AddProduct 添加产品
  34. func AddProduct(product models.ProductDto) bool {
  35. products := models.Products{
  36. Title: &product.Title,
  37. Synopsis: &product.Synopsis,
  38. Detail: &product.Detail,
  39. Type: &product.Type,
  40. Ptype: &product.Ptype,
  41. IsIndex: &product.IsIndex,
  42. Url: &product.Url,
  43. ProductIntroduction: &product.ProductIntroduction,
  44. TechnicalParameters: &product.TechnicalParameters,
  45. Instructions: &product.Instructions,
  46. SupportingSoftware: &product.SupportingSoftware,
  47. OptionalAccessories: &product.OptionalAccessories,
  48. IsActive: &product.IsActive,
  49. }
  50. tx := utils.DB.Create(&products)
  51. if tx.RowsAffected > 0 {
  52. return true
  53. }
  54. return false
  55. }
  56. // AddProductType 添加分类
  57. func AddProductType(productType models.ProductType) bool {
  58. tx := utils.DB.Create(&productType)
  59. if tx.RowsAffected > 0 {
  60. return true
  61. }
  62. return false
  63. }
  64. // GetProductType 获取所有分类
  65. func GetProductType() []models.ProductType {
  66. var serve []models.ProductType
  67. tx := utils.DB.Find(&serve)
  68. if tx.Error != nil {
  69. return nil
  70. }
  71. return serve
  72. }
  73. // UpdateProductType 修改分类
  74. func UpdateProductType(id int, productType models.ProductType) bool {
  75. tx := utils.DB.Model(&productType).Where("id =?", id).Updates(&productType)
  76. if tx.RowsAffected > 0 {
  77. return true
  78. }
  79. return false
  80. }
  81. func DeleteProduct(id int) bool {
  82. tx := utils.DB.Delete(&models.Products{}, id)
  83. if tx.RowsAffected > 0 {
  84. return true
  85. }
  86. return false
  87. }