company.go 896 B

12345678910111213141516171819202122232425262728293031323334
  1. package company
  2. import (
  3. "context"
  4. jsoniter "github.com/json-iterator/go"
  5. "hongze/hongze_yb/global"
  6. model "hongze/hongze_yb/models/tables/company"
  7. "hongze/hongze_yb/utils"
  8. "strconv"
  9. "time"
  10. )
  11. // GetByCompanyId 根据客户id获取信息
  12. func GetByCompanyId(companyId int64) (item *model.Company, err error) {
  13. cacheKey := utils.HONGZEYB_ + "company:GetByCompanyId:" + strconv.Itoa(int(companyId))
  14. companyInfo, _ := global.Redis.Get(context.TODO(), cacheKey).Result()
  15. if companyInfo != "" {
  16. err = jsoniter.UnmarshalFromString(companyInfo, &item)
  17. if err != nil {
  18. return
  19. }
  20. item.CompanyID = uint64(companyId)
  21. return
  22. }
  23. item, err = model.GetByCompanyId(companyId)
  24. if err == nil && item.CompanyID > 0 {
  25. companyInfo, err = jsoniter.MarshalToString(item)
  26. if err != nil {
  27. return
  28. }
  29. _ = global.Redis.SetEX(context.TODO(), cacheKey, companyInfo, 2*time.Hour)
  30. }
  31. return
  32. }