package service import ( "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/date" entity "bp.jydev.jianyu360.cn/BaseService/biService/entity" "bp.jydev.jianyu360.cn/BaseService/biService/rpc/pb" "crypto/rand" "errors" "fmt" "github.com/gogf/gf/v2/util/gconv" "log" "math/big" "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, map[int]string) { //拿职位id找mgoid userIdArr := []string{} res := map[int]string{} query := fmt.Sprintf("SELECT userid,position_id FROM data_service.user_system WHERE status = 1 AND position_id in (%s) AND ent_id = %d", positionIds, entId) log.Println("查找分发人的sql", query) useridMap := entity.JyBiTidb.SelectBySql(query) if useridMap != nil && len(*useridMap) > 0 { for _, val := range *useridMap { userIdArr = append(userIdArr, gconv.String(val["userid"])) res[common.IntAll(val["position_id"])] = common.InterfaceToStr(val["userid"]) } } return userIdArr, res } 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 GetPersonCode(positionId int64, name, userId string) string { //根据职位id获取到对应的渠道码 if userId == "" { log.Println("GetPersonCode userId为空", positionId, userId) return "" } code := "" data := entity.JyBiTidb.FindOne("dwd_d_userbase_belongto_rulecode", map[string]interface{}{"user_id": userId}, "code", "") if data != nil && len(*data) > 0 { code = gconv.String((*data)["code"]) log.Println("查询到code:", positionId, userId, code) } else { //没有code,生成一个 code = generateRandomString(6) log.Println("生成code:", positionId, userId, code) entity.JyBiMysql.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, "position_id": positionId, "user_id": userId, }) } return code } func generateRandomString(length int) string { const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" b := make([]byte, length) for i := range b { // 安全地生成随机索引 (0 到 len(charset)-1) idx, _ := rand.Int(rand.Reader, big.NewInt(int64(len(charset)))) b[i] = charset[idx.Int64()] } return string(b) } func GetUserIds(positionIds string) map[int]string { res := map[int]string{} data := entity.BiService.SelectBySql("SELECT userid,position_id FROM data_service.user_system WHERE status = 1 AND position_id in (?)", positionIds) if data != nil && len(*data) > 0 { for _, v := range *data { res[common.IntAll(v["position_id"])] = common.InterfaceToStr(v["userid"]) } } return res }