package service import ( "app.yhyue.com/moapp/jybase/date" entity "bp.jydev.jianyu360.cn/BaseService/biService/entity" "bp.jydev.jianyu360.cn/BaseService/biService/rpc/pb" "crypto/rand" "encoding/binary" "errors" "fmt" "github.com/gogf/gf/v2/util/gconv" "net/url" "strings" "time" ) func MaterialSave(in *pb.MaterialSaveReq, title, content string) (msgId, mId int64, err error) { if len(strings.Split(in.ReceiveUserId, ",")) < 1 { return 0, 0, errors.New("物料发送人员为空") } //先插入message_log saveMsg := map[string]interface{}{ "msg_type": "14", "title": title, "content": content, "send_mode": 2, "send_time": time.Now().Format(date.Date_Full_Layout), "send_status": 4, "update_time": time.Now().Format(date.Date_Full_Layout), "link": "", "isdel": 1, "update_user": in.CreateUser, "Sign": 1, "group_id": 11, "createtime": time.Now().Format(date.Date_Full_Layout), } msgId = entity.JyMysql.Insert("message_send_log", saveMsg) if msgId < 0 { return 0, 0, errors.New("插入消息表message_send_log出错") } saveMap := map[string]interface{}{ "task_name": in.TaskName, "task_description": in.TaskDescription, "material_content": in.MaterialContent, "qrcode_url": in.QrcodeUrl, "receive_user_name": in.ReceiveUserName, "receive_position_id": in.ReceiveUserId, "file_url": in.FileUrl, "createtime": time.Now().Format(date.Date_Full_Layout), "create_user": in.CreateUser, "msg_id": msgId, "img_webpage": in.ImgWebpage, } mId = entity.BiService.Insert("operating_materials", saveMap) if mId < 0 { return 0, 0, errors.New("插入物料表operating_materials出错") } return msgId, mId, nil } func GetSendUserId(positionIds string, entId int64) []string { //拿职位id找mgoid userIdArr := []string{} useridMap := entity.JyBiTidb.SelectBySql("SELECT userid FROM `dwd_f_userbase_id_mapping` WHERE position_id in (?) AND ent_id = ?", positionIds, entId) if useridMap != nil && len(*useridMap) > 0 { for _, val := range *useridMap { userIdArr = append(userIdArr, gconv.String(val["userid"])) } } return userIdArr } func PersonImageSave(imgUrl string, msgId, positionId, materialsId int64) bool { saveMap := map[string]interface{}{ "file_url": imgUrl, "msg_id": msgId, "position_id": positionId, "materials_id": materialsId, "createtime": time.Now().Format(date.Date_Full_Layout), } return entity.BiService.Insert("operating_materials_attachment", saveMap) > 0 } func PersonImgSaveComposite(fileUrl, qrcodeUrl, name string) ([]byte, error) { //根据职位id获取到对应的渠道码 code := "" data := entity.JyBiTidb.FindOne("dwd_d_userbase_belongto_rulecode", map[string]interface{}{"name": name}, "code", "") if data != nil && len(*data) > 0 { code = gconv.String((*data)["code"]) } else { //没有code,生成一个 code = GenerateUniqueRandomString(6) entity.JyBiTidb.Insert("dwd_d_userbase_belongto_rulecode", map[string]interface{}{ "code": code, "pcode": "04", "level": "2", "name": name, "remark": "人员渠道码", "SZ_LEVEL": 1, "SZ_LEAF": 1, "SZ_PID0": "04", "SZ_PID1": code, }) } //合成图片 ok, err := hasAnyParameters(qrcodeUrl) if err != nil { return nil, err } if ok { qrcodeUrl = fmt.Sprintf("%s&personnelChannel=%s", qrcodeUrl, code) } else { qrcodeUrl = fmt.Sprintf("%s?personnelChannel=%s", qrcodeUrl, code) } err, imgByte := compositeImage(fileUrl, qrcodeUrl) return imgByte, err } func GenerateUniqueRandomString(length int) string { // 获取当前时间(纳秒级) now := time.Now().UnixNano() // 生成随机数 var randNum uint64 if err := binary.Read(rand.Reader, binary.BigEndian, &randNum); err != nil { randNum = uint64(now) } // 组合时间和随机数 uniqueValue := now ^ int64(randNum) // 转换为base36字符串(0-9a-z) charset := "0123456789abcdefghijklmnopqrstuvwxyz" result := make([]byte, length) for i := range result { // 使用模运算选择字符 result[i] = charset[uniqueValue%36] uniqueValue /= 36 // 如果值耗尽,重新生成 if uniqueValue == 0 { uniqueValue = time.Now().UnixNano() } } return string(result) } func hasAnyParameters(rawURL string) (bool, error) { parsedURL, err := url.Parse(rawURL) if err != nil { return false, err } // 检查查询参数或片段标识符中的参数 return parsedURL.RawQuery != "" || (parsedURL.Fragment != "" && strings.Contains(parsedURL.Fragment, "=")), nil }