report.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package english_report
  2. import (
  3. "fmt"
  4. "hongze/hongze_yb_en_api/models/en_classify_permission"
  5. "hongze/hongze_yb_en_api/models/en_company_permission"
  6. "hongze/hongze_yb_en_api/models/english_company"
  7. "hongze/hongze_yb_en_api/models/english_report"
  8. "hongze/hongze_yb_en_api/models/english_report_email"
  9. "hongze/hongze_yb_en_api/services/alarm_msg"
  10. "hongze/hongze_yb_en_api/utils"
  11. "time"
  12. )
  13. func DealEmail(reportItem *english_report.Report, reportCode string, shareEmailId int) (err error, errMsg string) {
  14. defer func() {
  15. if err != nil {
  16. alarm_msg.SendAlarmMsg(utils.APPNAME+"更新英文报告邮箱PV失败, ErrMsg: "+errMsg, 2)
  17. }
  18. }()
  19. emailItem := new(english_report_email.Email)
  20. userEmail, err := emailItem.GetById(shareEmailId)
  21. if err != nil && err != utils.ErrNoRow {
  22. errMsg = "获取客户邮箱信息失败, Err:" + err.Error()
  23. return
  24. }
  25. if userEmail == nil {
  26. return
  27. }
  28. // 更新报告邮箱pv(冗余)
  29. if err = reportItem.UpdatePvEmailByReportCode(reportCode); err != nil {
  30. errMsg = "更新报告邮箱PV失败, Err:" + err.Error()
  31. return
  32. }
  33. count, err := reportItem.GetUvEmailByReportIdAndEmailId(int(reportItem.Id), int(userEmail.Id))
  34. if err != nil {
  35. errMsg = "获取邮箱阅读量失败, Err:" + err.Error()
  36. return
  37. }
  38. if count == 0 {
  39. if err = reportItem.UpdateUvEmailByReportCode(reportCode); err != nil {
  40. errMsg = "更新报告邮箱UV失败, Err:" + err.Error()
  41. return
  42. }
  43. }
  44. // 记录邮箱PV
  45. pv := &english_report_email.EmailPv{
  46. ReportId: reportItem.Id,
  47. EmailId: userEmail.Id,
  48. CreateTime: time.Now().Local(),
  49. }
  50. if err = pv.Add(); err != nil {
  51. errMsg = "新增邮箱PV失败, Err: " + err.Error()
  52. return
  53. }
  54. // 更新阅读量
  55. if err = emailItem.UpdateViewTotalById(userEmail.Id); err != nil {
  56. errMsg = "更新英文联系人阅读量失败, Err: " + err.Error()
  57. return
  58. }
  59. if userEmail.CompanyId > 0 {
  60. companyItem := new(english_company.Company)
  61. if err = companyItem.UpdateViewTotalByCompanyId(userEmail.CompanyId); err != nil {
  62. errMsg = "更新英文客户阅读量, Err: " + err.Error()
  63. return
  64. }
  65. }
  66. return
  67. }
  68. // CheckUserReportAuthByCompanyAndClassify 校验用户报告权限
  69. func CheckUserReportAuthByCompanyAndClassify(companyId, classifyId int) (authOk bool, err error) {
  70. if companyId <= 0 || classifyId <= 0 {
  71. return
  72. }
  73. // 客户权限
  74. hasPermissions := make([]*en_company_permission.EnCompanyPermission, 0)
  75. {
  76. cond := `en_company_id = ?`
  77. pars := make([]interface{}, 0)
  78. pars = append(pars, companyId)
  79. ob := new(en_company_permission.EnCompanyPermission)
  80. list, e := ob.GetList(cond, pars)
  81. if e != nil {
  82. err = fmt.Errorf("company permission GetList err: %s", e.Error())
  83. return
  84. }
  85. hasPermissions = list
  86. }
  87. // 报告权限
  88. reportPermissions := make([]*en_classify_permission.EnClassifyPermission, 0)
  89. {
  90. cond := `en_classify_id = ?`
  91. pars := make([]interface{}, 0)
  92. pars = append(pars, classifyId)
  93. ob := new(en_classify_permission.EnClassifyPermission)
  94. list, e := ob.GetList(cond, pars)
  95. if e != nil {
  96. err = fmt.Errorf("classify permission GetList err: %s", e.Error())
  97. return
  98. }
  99. reportPermissions = list
  100. }
  101. for _, r := range reportPermissions {
  102. for _, p := range hasPermissions {
  103. if r.EnPermissionId == p.EnPermissionId {
  104. authOk = true
  105. return
  106. }
  107. }
  108. }
  109. return
  110. }