eta_trial.go 6.7 KB

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