material.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. entity "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  6. "bp.jydev.jianyu360.cn/BaseService/biService/rpc/pb"
  7. "crypto/rand"
  8. "errors"
  9. "fmt"
  10. "github.com/gogf/gf/v2/util/gconv"
  11. "log"
  12. "math/big"
  13. "strings"
  14. "time"
  15. )
  16. func MaterialSave(in *pb.MaterialSaveReq, title, content string) (msgId, mId int64, err error) {
  17. if len(strings.Split(in.ReceiveUserId, ",")) < 1 {
  18. return 0, 0, errors.New("物料发送人员为空")
  19. }
  20. //先插入message_log
  21. saveMsg := map[string]interface{}{
  22. "msg_type": "14",
  23. "title": title,
  24. "content": content,
  25. "send_mode": 2,
  26. "send_time": time.Now().Format(date.Date_Full_Layout),
  27. "send_status": 4,
  28. "update_time": time.Now().Format(date.Date_Full_Layout),
  29. "link": "",
  30. "isdel": 1,
  31. "update_user": in.CreateUser,
  32. "sign": 1,
  33. "group_id": 11,
  34. "createtime": time.Now().Format(date.Date_Full_Layout),
  35. }
  36. msgId = entity.JyMysql.Insert("message_send_log", saveMsg)
  37. if msgId < 0 {
  38. return 0, 0, errors.New("插入消息表message_send_log出错")
  39. }
  40. saveMap := map[string]interface{}{
  41. "task_name": in.TaskName,
  42. "task_description": in.TaskDescription,
  43. "material_content": in.MaterialContent,
  44. "qrcode_url": in.QrcodeUrl,
  45. "receive_user_name": in.ReceiveUserName,
  46. "receive_position_id": in.ReceiveUserId,
  47. "file_url": in.FileUrl,
  48. "createtime": time.Now().Format(date.Date_Full_Layout),
  49. "create_user": in.CreateUser,
  50. "msg_id": msgId,
  51. "img_webpage": in.ImgWebpage,
  52. }
  53. mId = entity.BiService.Insert("operating_materials", saveMap)
  54. if mId < 0 {
  55. return 0, 0, errors.New("插入物料表operating_materials出错")
  56. }
  57. return msgId, mId, nil
  58. }
  59. func GetSendUserId(positionIds string, entId int64) []string {
  60. //拿职位id找mgoid
  61. userIdArr := []string{}
  62. query := fmt.Sprintf("SELECT userid FROM data_service.user_system WHERE position_id in (%s) AND ent_id = %d", positionIds, entId)
  63. log.Println("查找分发人的sql", query)
  64. useridMap := entity.JyBiTidb.SelectBySql(query)
  65. if useridMap != nil && len(*useridMap) > 0 {
  66. for _, val := range *useridMap {
  67. userIdArr = append(userIdArr, gconv.String(val["userid"]))
  68. }
  69. }
  70. return userIdArr
  71. }
  72. func PersonImageSave(imgUrl string, msgId, positionId, materialsId int64) bool {
  73. saveMap := map[string]interface{}{
  74. "file_url": imgUrl,
  75. "msg_id": msgId,
  76. "position_id": positionId,
  77. "materials_id": materialsId,
  78. "createtime": time.Now().Format(date.Date_Full_Layout),
  79. }
  80. return entity.BiService.Insert("operating_materials_attachment", saveMap) > 0
  81. }
  82. func GetPersonCode(positionId int64, name, userId string) string {
  83. //根据职位id获取到对应的渠道码
  84. code := ""
  85. data := entity.JyBiTidb.FindOne("dwd_d_userbase_belongto_rulecode", map[string]interface{}{"user_id": positionId}, "code", "")
  86. if data != nil && len(*data) > 0 {
  87. code = gconv.String((*data)["code"])
  88. } else {
  89. //没有code,生成一个
  90. code = generateRandomString(6)
  91. entity.JyBiMysql.Insert("dwd_d_userbase_belongto_rulecode", map[string]interface{}{
  92. "code": code,
  93. "pcode": "04",
  94. "level": "2",
  95. "name": name,
  96. "remark": "人员渠道码",
  97. "SZ_LEVEL": 1,
  98. "SZ_LEAF": 1,
  99. "SZ_PID0": "04",
  100. "SZ_PID1": code,
  101. "position_id": positionId,
  102. "user_id": userId,
  103. })
  104. }
  105. return code
  106. }
  107. func generateRandomString(length int) string {
  108. const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  109. b := make([]byte, length)
  110. for i := range b {
  111. // 安全地生成随机索引 (0 到 len(charset)-1)
  112. idx, _ := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
  113. b[i] = charset[idx.Int64()]
  114. }
  115. return string(b)
  116. }
  117. func GetUserIds(positionIds string) map[int]string {
  118. res := map[int]string{}
  119. data := entity.BiService.SelectBySql("SELECT userid,position_id FROM data_service.`user_system` FROM status = 1 AND position_id in (?)", positionIds)
  120. if data != nil && len(*data) > 0 {
  121. for _, v := range *data {
  122. res[common.IntAll(v["position_id"])] = common.InterfaceToStr(v["userid"])
  123. }
  124. }
  125. return res
  126. }