ServiceType.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package services
  2. import (
  3. "ERP_storage/dto"
  4. db "ERP_storage/initialize"
  5. "ERP_storage/logs"
  6. models "ERP_storage/models/ContractReview"
  7. "errors"
  8. "git.baozhida.cn/ERP_libs/lib"
  9. "gorm.io/gorm"
  10. )
  11. type ServiceType struct {
  12. }
  13. func (e *ServiceType) GetPage(c *dto.ServiceTypePageReq) (list []models.ServiceType, cnt int64) {
  14. var err error
  15. err = db.DB.Model(&models.ServiceType{}).
  16. Scopes(
  17. dto.MakeCondition(c.GetNeedSearch()),
  18. dto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  19. dto.WithNormalState(),
  20. ).
  21. Find(&list).Limit(-1).Offset(-1).
  22. Count(&cnt).Error
  23. if err != nil {
  24. logs.Error("db error: %s ", err)
  25. return
  26. }
  27. return
  28. }
  29. func (e *ServiceType) Get(Id int) (r models.ServiceType, err error) {
  30. err = db.DB.Scopes(dto.WithNormalState()).First(&r, Id).Error
  31. if err != nil {
  32. logs.Error("db error: %s", err)
  33. return
  34. }
  35. return
  36. }
  37. // 添加
  38. func (e *ServiceType) Insert(c *dto.ServiceTypeInsertReq) (id int, err error) {
  39. var data models.ServiceType
  40. err = db.DB.Scopes(dto.WithNormalState()).Where("t_name = ?", c.T_name).First(&data).Error
  41. if err != nil {
  42. // 没有则创建
  43. if errors.Is(err, gorm.ErrRecordNotFound) {
  44. c.Generate(&data)
  45. err = db.DB.Create(&data).Error
  46. if err != nil {
  47. logs.Error("db error: %s", err)
  48. return
  49. }
  50. id = data.Id
  51. return
  52. }
  53. logs.Error("db error: %s", err)
  54. return
  55. }
  56. id = data.Id
  57. return
  58. }
  59. // 修改
  60. func (e *ServiceType) Update(c *dto.ServiceTypeUpdateReq) error {
  61. var serviceType = models.ServiceType{}
  62. err := db.DB.Scopes(dto.WithNormalState()).First(&serviceType, c.GetId()).Error
  63. if err != nil {
  64. logs.Error("db error: %s", err)
  65. return dto.GetNotFoundErr
  66. }
  67. c.Generate(&serviceType)
  68. err = db.DB.Save(&serviceType).Error
  69. if err != nil {
  70. logs.Error("db error: %s", err)
  71. return dto.UpdateFailedErr
  72. }
  73. return nil
  74. }
  75. // 删除
  76. func (e *ServiceType) Delete(c *dto.ServiceTypeDeleteReq) error {
  77. var serviceType = models.ServiceType{}
  78. err := db.DB.Scopes(dto.WithNormalState()).First(&serviceType, c.GetId()).Error
  79. if err != nil {
  80. logs.Error("db error: %s", err)
  81. return dto.GetNotFoundErr
  82. }
  83. var ServiceItemCount int64
  84. err = db.DB.Model(&models.ServiceItem{}).
  85. Scopes(dto.WithNormalState()).
  86. Where("t_service_type_id = ?", serviceType.Id).
  87. Count(&ServiceItemCount).Error
  88. if err != nil {
  89. logs.Error("db error: %s", err)
  90. return dto.GetFailedErr
  91. }
  92. if ServiceItemCount > 0 {
  93. return errors.New("存在已关联的服务内容,无法删除!")
  94. }
  95. serviceType.T_State = 0
  96. err = db.DB.Save(&serviceType).Error
  97. if err != nil {
  98. logs.Error(lib.FuncName(), err)
  99. return dto.DeleteFailedErr
  100. }
  101. return nil
  102. }