eta_trial.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package services
  2. import (
  3. "eta/eta_forum_admin/models/company"
  4. "eta/eta_forum_admin/models/eta_trial"
  5. "eta/eta_forum_admin/models/system"
  6. "eta/eta_forum_admin/services/alarm_msg"
  7. "eta/eta_forum_admin/services/eta_forum_hub"
  8. "eta/eta_forum_admin/utils"
  9. "fmt"
  10. "github.com/mozillazg/go-pinyin"
  11. "strconv"
  12. "time"
  13. )
  14. // UpdateOldTrialUsersManualAuth 更新原试用客户手工录入权限(一次性)
  15. func UpdateOldTrialUsersManualAuth() (err error) {
  16. // 从ETA试用平台的edb库中读取手工数据分类
  17. classifyList, e := eta_trial.GetETATrialManualClassify()
  18. if e != nil {
  19. fmt.Println(e.Error())
  20. return
  21. }
  22. classifyIds := make([]int, 0)
  23. for i := range classifyList {
  24. classifyIds = append(classifyIds, classifyList[i].ClassifyId)
  25. }
  26. // 查询未分配手工权限的试用客户Ids
  27. userList, e := eta_trial.GetNoAuthTrialUserIds()
  28. if e != nil {
  29. fmt.Println(e.Error())
  30. return
  31. }
  32. if len(userList) == 0 {
  33. fmt.Println("无客户需要更新手工权限")
  34. return
  35. }
  36. fmt.Println("classifyIds", classifyIds)
  37. // 遍历试用客户, ETA试用平台data库新增manual_user用户关联、manual_user_classify权限关联
  38. for i := range userList {
  39. fmt.Printf("用户%d-%s正在更新\n", userList[i].AdminId, userList[i].RealName)
  40. e = eta_trial.InsertTrialUserManualAuth(userList[i].AdminId, userList[i].RealName, classifyIds)
  41. if e != nil {
  42. fmt.Println(e.Error())
  43. return
  44. }
  45. fmt.Println("更新成功")
  46. //break // 试一个先
  47. }
  48. return
  49. }
  50. // CreateTrialUserManualAuth 新增试用客户手工录入权限
  51. // 鉴于DB注册时即使不同库也不能同名的情况, 以下均使用原生SQL
  52. func CreateTrialUserManualAuth(adminId int, adminRealName string) (err error) {
  53. defer func() {
  54. if err != nil {
  55. alarm_msg.SendAlarmMsg(fmt.Sprintf("新增试用客户手工录入权限失败, ErrMsg: %s", err.Error()), 3)
  56. }
  57. }()
  58. // 从ETA试用平台的edb库中读取手工数据分类
  59. classifyList, e := eta_trial.GetETATrialManualClassify()
  60. if e != nil {
  61. err = fmt.Errorf("获取手工数据分类失败, Err: %s", e.Error())
  62. return
  63. }
  64. classifyIds := make([]int, 0)
  65. for i := range classifyList {
  66. classifyIds = append(classifyIds, classifyList[i].ClassifyId)
  67. }
  68. // 新增用户及权限
  69. e = eta_trial.InsertTrialUserManualAuth(adminId, adminRealName, classifyIds)
  70. if e != nil {
  71. err = fmt.Errorf("新增手工数据权限失败, Err: %s", e.Error())
  72. return
  73. }
  74. return
  75. }
  76. // ApprovalApply 同意审批
  77. func ApprovalApply(approvalId int) (err error) {
  78. defer func() {
  79. if err != nil {
  80. tips := fmt.Sprintf("ETA试用-自动审批失败, ApprovalId: %d, Err: %s", approvalId, err.Error())
  81. utils.FileLog.Info("%s", tips)
  82. go alarm_msg.SendAlarmMsg(tips, 3)
  83. }
  84. }()
  85. // 获取审批
  86. approval, e := eta_trial.GetETATrialApprovalById(approvalId)
  87. if e != nil {
  88. err = fmt.Errorf("获取审批失败, Err: %s", e.Error())
  89. return
  90. }
  91. // 更新审批
  92. if e = eta_trial.ApprovalTrial(approvalId); e != nil {
  93. err = fmt.Errorf("更新审批失败, Err: %s", e.Error())
  94. return
  95. }
  96. // 如果是新客户申请就新增,旧客户更改账号状态即可
  97. if approval.ApplyMethod == 2 {
  98. if e = eta_trial.UpdateETATrialEnable(approval.Mobile); e != nil {
  99. err = fmt.Errorf("启用失败, Err: %s", e.Error())
  100. return
  101. }
  102. if e = eta_forum_hub.EnabledEtaTrialAdmin(approval.Mobile); e != nil {
  103. err = fmt.Errorf("启用用户失败, Err: %s", e.Error())
  104. return
  105. }
  106. } else {
  107. //新增客户
  108. newItem := eta_trial.EtaTrial{
  109. UserName: approval.UserName,
  110. CompanyName: approval.CompanyName,
  111. Position: approval.Position,
  112. Mobile: approval.Mobile,
  113. Enabled: 1,
  114. SellerId: approval.SellerId,
  115. Seller: approval.Seller,
  116. CreateTime: time.Now(),
  117. ModifyTime: time.Now(),
  118. }
  119. strResult := ""
  120. a := pinyin.NewArgs()
  121. rows := pinyin.Pinyin(newItem.UserName, a)
  122. for i := 0; i < len(rows); i++ {
  123. strResult += rows[i][0]
  124. }
  125. //若非中文
  126. if strResult == "" {
  127. strResult = newItem.UserName
  128. }
  129. list, e := eta_trial.GetETATrialByAccount(strResult)
  130. if e != nil && e.Error() != utils.ErrNoRow() {
  131. err = fmt.Errorf("获取ETA试用账号失败, Err: %s", e.Error())
  132. return
  133. }
  134. if list != nil && len(list) > 0 {
  135. newItem.Account = strResult + strconv.Itoa(len(list))
  136. } else {
  137. newItem.Account = strResult
  138. }
  139. // 初始密码固定
  140. newItem.Password = "123456a"
  141. _, e = eta_trial.AddETATrial(&newItem)
  142. if e != nil {
  143. err = fmt.Errorf("新增ETA试用账号失败, Err: %s", e.Error())
  144. return
  145. }
  146. //// 新增至试用平台的Admin
  147. //adminItem, e := eta_trial.GetSysUserByRoleAndDepartment("试用", "ETA试用客户")
  148. //if e != nil {
  149. // err = fmt.Errorf("获取ETA试用admin失败, Err: %s", e.Error())
  150. // return
  151. //}
  152. //
  153. //admin := new(eta_trial.ETATrialAdmin)
  154. //admin.AdminName = newItem.Account
  155. //admin.RealName = newItem.UserName
  156. //admin.Password = utils.MD5(newItem.Password)
  157. //admin.LastUpdatedPasswordTime = time.Now().Format(utils.FormatDateTime)
  158. //admin.Enabled = 1
  159. //admin.LastLoginTime = time.Now().Format(utils.FormatDateTime)
  160. //admin.CreatedTime = time.Now()
  161. //admin.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
  162. //admin.Mobile = newItem.Mobile
  163. //admin.RoleType = 0
  164. //admin.RoleId = adminItem.RoleId
  165. //admin.RoleName = "试用"
  166. //admin.RoleTypeCode = "管理员"
  167. //admin.DepartmentId = adminItem.DepartmentId
  168. //admin.DepartmentName = "ETA试用客户"
  169. //admin.Role = "admin"
  170. //admin.Position = approval.Position
  171. //
  172. //newId, e := eta_trial.AddAdmin(admin)
  173. //if e != nil {
  174. // err = fmt.Errorf("新增ETA试用admin失败, Err: %s", e.Error())
  175. // return
  176. //}
  177. //
  178. //// 新增试用客户手工权限
  179. //go func() {
  180. // //_ = CreateTrialUserManualAuth(newId, admin.RealName)
  181. // _ = eta_forum_hub.EtaTrialManualUserAddAuth(newId, admin.RealName)
  182. //}()
  183. err = eta_forum_hub.AddEtaTrialAdmin(&newItem)
  184. if err != nil {
  185. err = fmt.Errorf("新增试用用户失败")
  186. return
  187. }
  188. }
  189. // 推送消息给销售
  190. msgItem := new(company.CompanyApprovalMessage)
  191. msgItem.CreateUserId = 0 // 系统自动创建的消息
  192. msgItem.ReceiveUserId = approval.SellerId
  193. msgItem.MessageStatus = 0
  194. msgItem.Remark = approval.UserName + approval.ApprovalContent + "通过"
  195. msgItem.Content = approval.UserName + approval.ApprovalContent + "通过"
  196. msgItem.CompanyName = approval.CompanyName
  197. msgItem.CreateTime = time.Now()
  198. msgItem.ModifyTime = time.Now()
  199. msgItem.CompanyApprovalId = approvalId
  200. msgItem.OperationStatus = 1
  201. msgItem.MessageType = 2 //1:申请消息,2:审批结果,3:文字消息
  202. msgItem.SourceType = 9 //消息来源
  203. msgItem.ApprovalStatus = 2 //审批状态,1:待审批,2:已审批,3:已驳回
  204. err = company.AddCompanyApprovalMessage(msgItem)
  205. // 更新审批消息(发给所有管理员的)为已读
  206. //go func() {
  207. // cond := ` AND company_approval_id = ? AND source_type = ? AND message_status = ? AND receive_user_id = -1 `
  208. // pars := make([]interface{}, 0)
  209. // pars = append(pars, approvalId, 9, 0)
  210. // _ = company.ModifyCompanyApprovalMessageStatusByCond(cond, pars)
  211. //}()
  212. return
  213. }
  214. // 把crm中的销售ID转成成新的销售ID
  215. func InitEtaForumAdminId() (err error) {
  216. defer func() {
  217. if err != nil {
  218. fmt.Println("InitEtaForumAdminId err: %s", err.Error())
  219. }
  220. }()
  221. now := time.Now()
  222. beforeTime := "2025-01-03" // 改成当前执行的时间,,修改这个时间之前的所有数据,避免替换后续新产生的数据
  223. adminList, err := system.GetDifferentAdmin()
  224. if err != nil {
  225. return
  226. }
  227. for _, item := range adminList {
  228. err = system.SetDifferentAdmin(item.AdminId, item.RealName, item.CrmAdminId, item.CrmRealName, now, beforeTime)
  229. if err != nil {
  230. return
  231. }
  232. }
  233. fmt.Println("InitEtaForumAdminId Success 替换账号:", len(adminList), "个")
  234. return
  235. }