issue.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 ASIssue models.ASIssue
  12. const cacheIssueKey = "aftersale_issue_cache"
  13. func GetIssueList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.ASIssue, error) {
  14. if page.PageNum > 0 && page.PageSize > 0 {
  15. where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
  16. }
  17. return ASIssue.GetMulti(where, fields, retVal)
  18. }
  19. func GetIssueListByCache() ([]*models.ASIssue, error) {
  20. res, err := redis.Get(cacheIssueKey)
  21. var typeList []*models.ASIssue
  22. if err != nil {
  23. typeList, err = GetIssueList(nil, nil, app.Page{}, nil)
  24. if err != nil {
  25. return nil, err
  26. }
  27. redis.Set(cacheIssueKey, typeList, 3600)
  28. } else {
  29. utils.JsonDecode(res).To(&typeList)
  30. }
  31. return typeList, nil
  32. }
  33. func GetIssueMapByCache() map[int]string {
  34. orderTypeMap := make(map[int]string)
  35. if orderTypeList, err := GetIssueListByCache(); err == nil {
  36. for _, v := range orderTypeList {
  37. orderTypeMap[v.ID] = v.IssueName
  38. }
  39. }
  40. return orderTypeMap
  41. }
  42. func CountIssue(where map[string]interface{}) (int64, error) {
  43. return db.Count(ASIssue.TableName(), where)
  44. }
  45. func GetIssueOne(where map[string]interface{}, fields []string, dest interface{}) (*models.ASIssue, error) {
  46. return ASIssue.GetOne(where, fields, dest)
  47. }
  48. func AddIssue(form form.ASIssueAdd) (int64, error) {
  49. if CheckIssueDuplicate(form.IssueName) {
  50. return 0, errors.New("问题已存在")
  51. }
  52. issueMap := map[string]interface{}{
  53. "issue_name": form.IssueName,
  54. }
  55. issueID, err := db.InsertOne(ASIssue.TableName(), issueMap)
  56. redis.Del(cacheKey)
  57. if err != nil {
  58. return 0, nil
  59. }
  60. return issueID, nil
  61. }
  62. func EditIssue(form form.ASIssueAdd, id int) (int64, error) {
  63. typeInfo, _ := GetIssueOne(map[string]interface{}{"id": id}, nil, nil)
  64. if typeInfo.IssueName != form.IssueName && CheckDuplicate(form.IssueName) {
  65. return 0, errors.New("类别已存在")
  66. }
  67. issueMap := map[string]interface{}{
  68. "issue_name": form.IssueName,
  69. }
  70. redis.Del(cacheKey)
  71. return db.Update(ASIssue.TableName(), map[string]interface{}{"id": id}, issueMap)
  72. }
  73. func CheckIssueDuplicate(name string) bool {
  74. issueInfo, err := GetIssueOne(map[string]interface{}{"issue_name": name}, nil, nil)
  75. return issueInfo != nil && err == nil
  76. }