company_report_permission.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package services
  2. import (
  3. "context"
  4. "fmt"
  5. "hongze/hongze_task/models"
  6. "hongze/hongze_task/utils"
  7. "time"
  8. )
  9. // CompanyReportPermissionTryOut 产品正式权限 -> 产品权限试用
  10. func CompanyReportPermissionTryOut(cont context.Context) (err error) {
  11. //fmt.Println("开始进入产品试用权限 -> 产品权限关闭")
  12. endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  13. items, err := models.GetCompanyReportPermissionNeedTryOut(endDate)
  14. if err != nil {
  15. utils.FileLog.Info("GetCompanyReportPermissionNeedTryOut Err:%s" + err.Error())
  16. return
  17. }
  18. // 根据客户id分组相关品种权限
  19. companyReportPermissionGroupMap := make(map[string][]*models.CompanyReportPermission)
  20. for _, v := range items {
  21. companyPermissionList, ok := companyReportPermissionGroupMap[fmt.Sprint(v.CompanyId, "-", v.ProductId)]
  22. if !ok {
  23. companyPermissionList = make([]*models.CompanyReportPermission, 0)
  24. }
  25. companyPermissionList = append(companyPermissionList, v)
  26. companyReportPermissionGroupMap[fmt.Sprint(v.CompanyId, "-", v.ProductId)] = companyPermissionList
  27. }
  28. // 根据客户id分组处理对应的品种权限
  29. for _, companyReportPermissionList := range companyReportPermissionGroupMap {
  30. startDateTime := time.Now()
  31. endDateTime := startDateTime.AddDate(0, 2, 0)
  32. var companyId, productId int
  33. for _, v := range companyReportPermissionList {
  34. companyId = v.CompanyId
  35. productId = v.ProductId
  36. //正式转试用用户产品权限
  37. v.IsUpgrade = 0
  38. v.Status = "试用"
  39. v.StartDate = startDateTime.Format(utils.FormatDate)
  40. v.EndDate = endDateTime.Format(utils.FormatDate)
  41. v.ModifyTime = time.Now().Format(utils.FormatDate)
  42. err = v.Update([]string{"IsUpgrade","Status", "StartDate", "EndDate", "ModifyTime"})
  43. //err = models.CompanyReportPermissionTryOut(v.CompanyReportPermissionId, v.CompanyId, v.ProductId)
  44. if err != nil {
  45. utils.FileLog.Info("CompanyReportPermissionTryOut Err:%s" + err.Error())
  46. continue
  47. }
  48. //添加正式转试用用户产品权限日志
  49. _ = AddCompanyReportPermissionLog(v.CompanyReportPermissionId, v.CompanyId, v.ProductId, utils.AdminId, v.ProductName, utils.RealName, "正式转试用", "try_out", "正式")
  50. }
  51. //获取客户信息
  52. companyInfo, err := models.GetCompanyProduct(companyId, productId)
  53. if err != nil {
  54. utils.FileLog.Info("CompanyReportPermissionTryOut 查找客户信息 Err:%s" + err.Error())
  55. continue
  56. }
  57. // 添加试用记录
  58. {
  59. companyProductTryOutUpdateLog := &models.CompanyProductTryOutUpdateLog{
  60. Id: 0,
  61. CompanyId: companyId,
  62. ProductId: productId,
  63. Source: "formal_to_try_out",
  64. SellerId: companyInfo.SellerId,
  65. SellerName: companyInfo.SellerName,
  66. StartDate: startDateTime,
  67. EndDate: endDateTime,
  68. RealEndDate: startDateTime,
  69. IsStop: 0,
  70. CreateTime: time.Now(),
  71. }
  72. go models.AddCompanyProductTryOutUpdateLog(companyProductTryOutUpdateLog, companyReportPermissionList)
  73. }
  74. }
  75. return
  76. }
  77. //产品试用权限 -> 产品权限关闭
  78. func CompanyReportPermissionClose(cont context.Context) (err error) {
  79. //fmt.Println("开始进入产品试用权限 -> 产品权限关闭")
  80. endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  81. items, err := models.GetCompanyReportPermissionNeedClose(endDate)
  82. if err != nil {
  83. utils.FileLog.Info("GetCompanyReportPermissionNeedClose Err:%s" + err.Error())
  84. return
  85. }
  86. //待处理的用户产品权限列表(主要目的是为了多个产品项的去重,避免冻结产品时的多余sql操作)
  87. freezeCompanyProductList := make(map[string]*models.CompanyReportPermission)
  88. for _, v := range items {
  89. //fmt.Println(k, v)
  90. //continue
  91. //查看是否有审批单,如果有审批单,那么就退出当前循环,进入下一循环
  92. count, err := models.GetCompanyApprovalCount(v.CompanyId, v.ProductId)
  93. if err != nil {
  94. continue
  95. }
  96. if count > 0 {
  97. continue
  98. }
  99. //关闭用户产品权限
  100. err = models.CompanyReportPermissionClose(v.CompanyReportPermissionId, v.CompanyId, v.ProductId)
  101. if err != nil {
  102. utils.FileLog.Info("CompanyReportPermissionClose Err:%s" + err.Error())
  103. continue
  104. }
  105. //添加关闭用户产品权限日志
  106. _ = AddCompanyReportPermissionLog(v.CompanyReportPermissionId, v.CompanyId, v.ProductId, utils.AdminId, v.ProductName, utils.RealName, "试用转关闭", "close", v.Status)
  107. //如果不存在该数据,那么将该数据放入待处理用户产品权限列表
  108. mapKey := fmt.Sprintf("%v_%v", v.CompanyId, v.ProductId)
  109. if _, ok := freezeCompanyProductList[mapKey]; ok == false {
  110. freezeCompanyProductList[mapKey] = v
  111. }
  112. }
  113. //遍历待处理的用户产品权限列表,查询用户产品权限信息并冻结用户产品
  114. for _, companyReportPermission := range freezeCompanyProductList {
  115. go queryAndFreezeCompanyProduct(companyReportPermission)
  116. }
  117. return
  118. }
  119. //查询用户产品权限信息并冻结用户产品
  120. func queryAndFreezeCompanyProduct(companyReportPermission *models.CompanyReportPermission) {
  121. //查询该用户名下是否存在还在使用的权限
  122. count, err := models.GetCompanyReportPermissionCount(companyReportPermission.CompanyId, companyReportPermission.ProductId)
  123. if err != nil {
  124. utils.FileLog.Info("CompanyReportPermissionClose query other permission Err:%s" + err.Error())
  125. }
  126. //如果还存在使用(不管是正式还是试用)的权限,那么都直接退出当前循环,不冻结用户产品
  127. if count > 0 {
  128. return
  129. }
  130. //冻结该用户产品
  131. _, err = models.CompanyFreeze(companyReportPermission.CompanyId, companyReportPermission.ProductId)
  132. if err != nil {
  133. go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode+";冻结该用户产品", "services:queryAndFreezeCompanyProduct;Err"+err.Error(), utils.EmailSendToUsers)
  134. return
  135. }
  136. //查询用户产品信息
  137. companyProduct, err := models.GetCompanyProduct(companyReportPermission.CompanyId, companyReportPermission.ProductId)
  138. //if err!=nil && err.Error() != utils.ErrNoRow() {
  139. if err != nil {
  140. if err.Error() != utils.ErrNoRow() {
  141. go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode+";查询用户产品信息", fmt.Sprint("services:queryAndFreezeCompanyProduct;Err"+err.Error(), "CompanyId:", companyReportPermission.CompanyId, ";ProductId:", companyReportPermission.ProductId), utils.EmailSendToUsers)
  142. }
  143. //go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode+";查询用户产品信息", fmt.Sprint("services:queryAndFreezeCompanyProduct;Err"+err.Error(),"CompanyId:",companyReportPermission.CompanyId,";ProductId:",companyReportPermission.ProductId), utils.EmailSendToUsers)
  144. return
  145. }
  146. //添加操作日志
  147. remark := "试用转冻结"
  148. operation := "freeze"
  149. approveContent := "冻结"
  150. err = AddCompanyOperationRecord(companyReportPermission.CompanyId, utils.AdminId, companyReportPermission.ProductId, utils.AdminId, companyProduct.CompanyName,
  151. companyProduct.ProductName, utils.RealName, remark, operation, approveContent, utils.RealName, "", companyProduct.Status)
  152. if err != nil {
  153. go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode+";添加冻结操作日志", "services:queryAndFreezeCompanyProduct;Err"+err.Error(), utils.EmailSendToUsers)
  154. return
  155. }
  156. return
  157. }
  158. //添加用户权限操作日志
  159. func AddCompanyReportPermissionLog(companyReportPermissionId int64, companyId, productId, sysUserId int, productName string, sysUserName, remark, operation, status string) (err error) {
  160. record := models.CompanyReportPermissionLog{
  161. CompanyReportPermissionId: companyReportPermissionId,
  162. CompanyId: companyId,
  163. ProductId: productId,
  164. ProductName: productName,
  165. SysUserId: sysUserId,
  166. SysUserName: sysUserName,
  167. Remark: remark,
  168. Operation: operation,
  169. Status: status,
  170. CreateTime: time.Now(),
  171. }
  172. _, err = models.AddCompanyReportPermissionLog(&record)
  173. if err != nil {
  174. go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "services:AddCompanyReportPermissionLog;Err"+err.Error(), utils.EmailSendToUsers)
  175. }
  176. return
  177. }