type.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package aftersale
  2. import (
  3. "errors"
  4. "zhiyuan/models"
  5. "zhiyuan/pkg/app"
  6. "zhiyuan/pkg/db"
  7. "zhiyuan/pkg/redis"
  8. "zhiyuan/pkg/utils"
  9. "zhiyuan/services/form"
  10. )
  11. var ASType models.ASType
  12. const cacheKey = "aftersale_type_cache"
  13. func GetTypeList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.ASType, error) {
  14. if page.PageNum > 0 && page.PageSize > 0 {
  15. where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
  16. }
  17. return ASType.GetMulti(where, fields, retVal)
  18. }
  19. func GetTypeListByCache() ([]*models.ASType, error) {
  20. res, err := redis.Get(cacheKey)
  21. var typeList []*models.ASType
  22. if err != nil {
  23. typeList, err = GetTypeList(nil, nil, app.Page{}, nil)
  24. if err != nil {
  25. return nil, err
  26. }
  27. redis.Set(cacheKey, typeList, 3600)
  28. } else {
  29. utils.JsonDecode(res).To(&typeList)
  30. }
  31. return typeList, nil
  32. }
  33. func GetTypeMapByCache() map[int]string {
  34. orderTypeMap := make(map[int]string)
  35. if orderTypeList, err := GetTypeListByCache(); err == nil {
  36. for _, v := range orderTypeList {
  37. orderTypeMap[v.ID] = v.TypeName
  38. }
  39. }
  40. return orderTypeMap
  41. }
  42. func CountType(where map[string]interface{}) (int64, error) {
  43. return db.Count(ASType.TableName(), where)
  44. }
  45. func GetTypeOne(where map[string]interface{}, fields []string, dest interface{}) (*models.ASType, error) {
  46. return ASType.GetOne(where, fields, dest)
  47. }
  48. func AddType(form form.ASTypeAdd) (int64, error) {
  49. typeMap := map[string]interface{}{
  50. "type_name": form.TypeName,
  51. "pid": form.PID,
  52. "`show`": form.Show,
  53. }
  54. if form.PID == 0 {
  55. if form.WarrantyPeriod == 0 {
  56. return 0, errors.New("请填写保修年限")
  57. }
  58. typeMap["warranty_period"] = form.WarrantyPeriod
  59. } else {
  60. typeMap["warranty_period"] = 0
  61. }
  62. typeID, err := db.InsertOne(ASType.TableName(), typeMap)
  63. redis.Del(cacheKey)
  64. if err != nil {
  65. return 0, nil
  66. }
  67. return typeID, nil
  68. }
  69. func EditType(form form.ASTypeAdd, id int) (int64, error) {
  70. typeInfo, _ := GetTypeOne(map[string]interface{}{"id": id}, nil, nil)
  71. if typeInfo.TypeName != form.TypeName && CheckDuplicate(form.TypeName) {
  72. return 0, errors.New("类别已存在")
  73. }
  74. typeMap := map[string]interface{}{
  75. "type_name": form.TypeName,
  76. "pid": form.PID,
  77. "`show`": form.Show,
  78. }
  79. if form.PID == 0 {
  80. if form.WarrantyPeriod == 0 {
  81. return 0, errors.New("请填写保修年限")
  82. }
  83. typeMap["warranty_period"] = form.WarrantyPeriod
  84. typeMap["repair_days"] = form.RepairDays
  85. } else {
  86. typeMap["warranty_period"] = 0
  87. }
  88. redis.Del(cacheKey)
  89. return db.Update(ASType.TableName(), map[string]interface{}{"id": id}, typeMap)
  90. }
  91. func CheckDuplicate(name string) bool {
  92. typeInfo, err := GetTypeOne(map[string]interface{}{"type_name": name}, nil, nil)
  93. return typeInfo != nil && err == nil
  94. }