123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- package services
- import (
- "eta/eta_forum_admin/models/company"
- "eta/eta_forum_admin/models/eta_trial"
- "eta/eta_forum_admin/models/system"
- "eta/eta_forum_admin/services/alarm_msg"
- "eta/eta_forum_admin/services/eta_forum_hub"
- "eta/eta_forum_admin/utils"
- "fmt"
- "github.com/mozillazg/go-pinyin"
- "strconv"
- "time"
- )
- // UpdateOldTrialUsersManualAuth 更新原试用客户手工录入权限(一次性)
- func UpdateOldTrialUsersManualAuth() (err error) {
- // 从ETA试用平台的edb库中读取手工数据分类
- classifyList, e := eta_trial.GetETATrialManualClassify()
- if e != nil {
- fmt.Println(e.Error())
- return
- }
- classifyIds := make([]int, 0)
- for i := range classifyList {
- classifyIds = append(classifyIds, classifyList[i].ClassifyId)
- }
- // 查询未分配手工权限的试用客户Ids
- userList, e := eta_trial.GetNoAuthTrialUserIds()
- if e != nil {
- fmt.Println(e.Error())
- return
- }
- if len(userList) == 0 {
- fmt.Println("无客户需要更新手工权限")
- return
- }
- fmt.Println("classifyIds", classifyIds)
- // 遍历试用客户, ETA试用平台data库新增manual_user用户关联、manual_user_classify权限关联
- for i := range userList {
- fmt.Printf("用户%d-%s正在更新\n", userList[i].AdminId, userList[i].RealName)
- e = eta_trial.InsertTrialUserManualAuth(userList[i].AdminId, userList[i].RealName, classifyIds)
- if e != nil {
- fmt.Println(e.Error())
- return
- }
- fmt.Println("更新成功")
- //break // 试一个先
- }
- return
- }
- // CreateTrialUserManualAuth 新增试用客户手工录入权限
- // 鉴于DB注册时即使不同库也不能同名的情况, 以下均使用原生SQL
- func CreateTrialUserManualAuth(adminId int, adminRealName string) (err error) {
- defer func() {
- if err != nil {
- alarm_msg.SendAlarmMsg(fmt.Sprintf("新增试用客户手工录入权限失败, ErrMsg: %s", err.Error()), 3)
- }
- }()
- // 从ETA试用平台的edb库中读取手工数据分类
- classifyList, e := eta_trial.GetETATrialManualClassify()
- if e != nil {
- err = fmt.Errorf("获取手工数据分类失败, Err: %s", e.Error())
- return
- }
- classifyIds := make([]int, 0)
- for i := range classifyList {
- classifyIds = append(classifyIds, classifyList[i].ClassifyId)
- }
- // 新增用户及权限
- e = eta_trial.InsertTrialUserManualAuth(adminId, adminRealName, classifyIds)
- if e != nil {
- err = fmt.Errorf("新增手工数据权限失败, Err: %s", e.Error())
- return
- }
- return
- }
- // ApprovalApply 同意审批
- func ApprovalApply(approvalId int) (err error) {
- defer func() {
- if err != nil {
- tips := fmt.Sprintf("ETA试用-自动审批失败, ApprovalId: %d, Err: %s", approvalId, err.Error())
- utils.FileLog.Info("%s", tips)
- go alarm_msg.SendAlarmMsg(tips, 3)
- }
- }()
- // 获取审批
- approval, e := eta_trial.GetETATrialApprovalById(approvalId)
- if e != nil {
- err = fmt.Errorf("获取审批失败, Err: %s", e.Error())
- return
- }
- // 更新审批
- if e = eta_trial.ApprovalTrial(approvalId); e != nil {
- err = fmt.Errorf("更新审批失败, Err: %s", e.Error())
- return
- }
- // 如果是新客户申请就新增,旧客户更改账号状态即可
- if approval.ApplyMethod == 2 {
- if e = eta_trial.UpdateETATrialEnable(approval.Mobile); e != nil {
- err = fmt.Errorf("启用失败, Err: %s", e.Error())
- return
- }
- if e = eta_forum_hub.EnabledEtaTrialAdmin(approval.Mobile); e != nil {
- err = fmt.Errorf("启用用户失败, Err: %s", e.Error())
- return
- }
- } else {
- //新增客户
- newItem := eta_trial.EtaTrial{
- UserName: approval.UserName,
- CompanyName: approval.CompanyName,
- Position: approval.Position,
- Mobile: approval.Mobile,
- Enabled: 1,
- SellerId: approval.SellerId,
- Seller: approval.Seller,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- strResult := ""
- a := pinyin.NewArgs()
- rows := pinyin.Pinyin(newItem.UserName, a)
- for i := 0; i < len(rows); i++ {
- strResult += rows[i][0]
- }
- //若非中文
- if strResult == "" {
- strResult = newItem.UserName
- }
- list, e := eta_trial.GetETATrialByAccount(strResult)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = fmt.Errorf("获取ETA试用账号失败, Err: %s", e.Error())
- return
- }
- if list != nil && len(list) > 0 {
- newItem.Account = strResult + strconv.Itoa(len(list))
- } else {
- newItem.Account = strResult
- }
- // 初始密码固定
- newItem.Password = "123456a"
- _, e = eta_trial.AddETATrial(&newItem)
- if e != nil {
- err = fmt.Errorf("新增ETA试用账号失败, Err: %s", e.Error())
- return
- }
- //// 新增至试用平台的Admin
- //adminItem, e := eta_trial.GetSysUserByRoleAndDepartment("试用", "ETA试用客户")
- //if e != nil {
- // err = fmt.Errorf("获取ETA试用admin失败, Err: %s", e.Error())
- // return
- //}
- //
- //admin := new(eta_trial.ETATrialAdmin)
- //admin.AdminName = newItem.Account
- //admin.RealName = newItem.UserName
- //admin.Password = utils.MD5(newItem.Password)
- //admin.LastUpdatedPasswordTime = time.Now().Format(utils.FormatDateTime)
- //admin.Enabled = 1
- //admin.LastLoginTime = time.Now().Format(utils.FormatDateTime)
- //admin.CreatedTime = time.Now()
- //admin.LastUpdatedTime = time.Now().Format(utils.FormatDateTime)
- //admin.Mobile = newItem.Mobile
- //admin.RoleType = 0
- //admin.RoleId = adminItem.RoleId
- //admin.RoleName = "试用"
- //admin.RoleTypeCode = "管理员"
- //admin.DepartmentId = adminItem.DepartmentId
- //admin.DepartmentName = "ETA试用客户"
- //admin.Role = "admin"
- //admin.Position = approval.Position
- //
- //newId, e := eta_trial.AddAdmin(admin)
- //if e != nil {
- // err = fmt.Errorf("新增ETA试用admin失败, Err: %s", e.Error())
- // return
- //}
- //
- //// 新增试用客户手工权限
- //go func() {
- // //_ = CreateTrialUserManualAuth(newId, admin.RealName)
- // _ = eta_forum_hub.EtaTrialManualUserAddAuth(newId, admin.RealName)
- //}()
- err = eta_forum_hub.AddEtaTrialAdmin(&newItem)
- if err != nil {
- err = fmt.Errorf("新增试用用户失败")
- return
- }
- }
- // 推送消息给销售
- msgItem := new(company.CompanyApprovalMessage)
- msgItem.CreateUserId = 0 // 系统自动创建的消息
- msgItem.ReceiveUserId = approval.SellerId
- msgItem.MessageStatus = 0
- msgItem.Remark = approval.UserName + approval.ApprovalContent + "通过"
- msgItem.Content = approval.UserName + approval.ApprovalContent + "通过"
- msgItem.CompanyName = approval.CompanyName
- msgItem.CreateTime = time.Now()
- msgItem.ModifyTime = time.Now()
- msgItem.CompanyApprovalId = approvalId
- msgItem.OperationStatus = 1
- msgItem.MessageType = 2 //1:申请消息,2:审批结果,3:文字消息
- msgItem.SourceType = 9 //消息来源
- msgItem.ApprovalStatus = 2 //审批状态,1:待审批,2:已审批,3:已驳回
- err = company.AddCompanyApprovalMessage(msgItem)
- // 更新审批消息(发给所有管理员的)为已读
- //go func() {
- // cond := ` AND company_approval_id = ? AND source_type = ? AND message_status = ? AND receive_user_id = -1 `
- // pars := make([]interface{}, 0)
- // pars = append(pars, approvalId, 9, 0)
- // _ = company.ModifyCompanyApprovalMessageStatusByCond(cond, pars)
- //}()
- return
- }
- // 把crm中的销售ID转成成新的销售ID
- func InitEtaForumAdminId() (err error) {
- defer func() {
- if err != nil {
- fmt.Println("InitEtaForumAdminId err: %s", err.Error())
- }
- }()
- now := time.Now()
- beforeTime := "2025-01-03" // 改成当前执行的时间,,修改这个时间之前的所有数据,避免替换后续新产生的数据
- adminList, err := system.GetDifferentAdmin()
- if err != nil {
- return
- }
- for _, item := range adminList {
- err = system.SetDifferentAdmin(item.AdminId, item.RealName, item.CrmAdminId, item.CrmRealName, now, beforeTime)
- if err != nil {
- return
- }
- }
- fmt.Println("InitEtaForumAdminId Success 替换账号:", len(adminList), "个")
- return
- }
|