task.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. package main
  2. import (
  3. "app.yhyue.com/BP/servicerd/proto"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "google.golang.org/grpc"
  8. "gopkg.in/mgo.v2/bson"
  9. jypb "jy_publishing/proto/common"
  10. pb "jy_publishing/proto/proto"
  11. "net"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "utils"
  16. . "utils/log"
  17. "utils/mongodb"
  18. )
  19. var (
  20. JyUrl = "https://www.jianyu360.cn/article/content/%s.html"
  21. InfoFields = []string{"title", "project_code", "province", "city", "industry", "buyer", "budget", "winner", "amount",
  22. "detail", "attch", "contract_person", "contract_phone", "attach", "discern_attach"}
  23. )
  24. var SaveFields = map[string]string{
  25. "title": "title",
  26. "project_code": "projectcode",
  27. "province": "area",
  28. "city": "city",
  29. "buyer": "buyer",
  30. "budget": "budget",
  31. "winner": "s_winner",
  32. "amount": "bidamount",
  33. "detail": "detail",
  34. "contract_person": "buyerperson",
  35. "contract_phone": "buyertel",
  36. "discern_attach": "attach_text",
  37. //"attch": "",
  38. //"industry": "",
  39. //"contract_overt": "",
  40. }
  41. // @Description 信息处理(信息发布和附件识别)
  42. // 1、敏感词处理,2、信息发布,3、信息删除
  43. // @Author J 2022/4/9 11:47 AM
  44. func taskInfo(obj interface{}) {
  45. info, _ := obj.(map[string]interface{})
  46. if util.ObjToString(info["action"]) == "1" {
  47. // 敏感词处理
  48. Sensitive(info)
  49. } else if util.ObjToString(info["action"]) == "2" {
  50. // 数据处理
  51. InfoPub(info)
  52. } else if util.ObjToString(info["action"]) == "3" {
  53. //id := util.ObjToString(info["id"])
  54. tmp := info["appendInfo"].(map[string]interface{})
  55. DelMethod(util.ObjToString(tmp["publish_id"]))
  56. }
  57. }
  58. // @Description 敏感词处理(title, content, attachment)
  59. // @Author J 2022/4/11 9:36 AM
  60. func Sensitive(info map[string]interface{}) {
  61. tmp := info["appendInfo"].(map[string]interface{})
  62. tArr := WordsIdentify(util.ObjToString(tmp["title"]))
  63. dArr := WordsIdentify(util.ObjToString(tmp["detail"]))
  64. if tmp["attach"] != nil {
  65. attsMap := tmp["attach"].(map[string]interface{})
  66. other := map[string]interface{}{
  67. "id": tmp["id"],
  68. "action": tmp["action"],
  69. "msgType": tmp["msgType"],
  70. "title": strings.Join(tArr, ","),
  71. "detail": strings.Join(dArr, ","),
  72. }
  73. otherJson, _ := json.Marshal(other)
  74. var attsArr []*pb.Request
  75. for _, m := range attsMap {
  76. m1 := m.(map[string]interface{})
  77. attsArr = append(attsArr, &pb.Request{
  78. FileUrl: util.ObjToString(m1["fid"]),
  79. FileName: util.ObjToString(m1["filename"]),
  80. FileType: util.ObjToString(m1["ftype"]),
  81. ReturnType: 2,
  82. ExtractType: 2,
  83. })
  84. }
  85. msginfo := &pb.FileRequest{
  86. Message: attsArr,
  87. Other: string(otherJson),
  88. }
  89. Logger.Debug("file extract: " + fmt.Sprint(msginfo))
  90. _ = MProducer.Publish(msginfo)
  91. } else {
  92. // 没有附件
  93. Logger.Debug("title sensitive array: " + fmt.Sprint(tArr))
  94. Logger.Debug("title sensitive array: " + fmt.Sprint(dArr))
  95. req := &jypb.SensitiveRequest{
  96. Id: util.ObjToString(info["id"]),
  97. MsgType: util.ObjToString(info["msgType"]),
  98. Title: tArr,
  99. Detail: dArr,
  100. }
  101. Logger.Debug("JyRpcSensitive request: " + fmt.Sprint(req))
  102. JyRpcSensitive(req)
  103. }
  104. //atts := tmp["attachment"].(map[string]interface{})
  105. //resultAtts := make(map[string]interface{})
  106. //resultAttach := make(map[string]interface{})
  107. //for k, v := range atts {
  108. // attach := make(map[string]interface{}) // attach_text字段
  109. // resp, err := AttsMethod(v.(map[string]interface{}))
  110. // if err != nil {
  111. // return nil
  112. // }
  113. // for i, r := range resp.Result {
  114. // if w := WordsIdentify(r.TextContent); w != nil {
  115. // resultAtts[k] = w
  116. // }
  117. // attach[strconv.Itoa(i)] = map[string]interface{}{"file_name": r.FileName, "attach_url": r.TextUrl}
  118. // }
  119. // resultAttach[k] = attach
  120. //}
  121. //resultMap["attach_text"] = resultAttach
  122. //resultMap["attachment"] = resultAtts
  123. }
  124. // @Description 敏感词识别
  125. // @Author J 2022/4/12 1:33 PM
  126. func WordsIdentify(str string) []string {
  127. if str == "" {
  128. return nil
  129. }
  130. ret := Ms.Discern(str, 2)
  131. if len(ret) > 0 {
  132. var words []string
  133. for _, r := range ret {
  134. words = append(words, r.Line)
  135. }
  136. return words
  137. }
  138. return []string{}
  139. }
  140. // @Description 附件调用gRpc接口处理
  141. // @Author J 2022/4/12 10:02 AM
  142. // Deprecated
  143. func AttsMethod(att map[string]interface{}) (*pb.FileResponse, error) {
  144. reqs := &pb.FileRequest{
  145. Message: []*pb.Request{{
  146. FileName: "",
  147. FileType: "",
  148. FileUrl: ""}},
  149. Other: "",
  150. }
  151. // 1.调用gRPC接口
  152. conn, err := grpc.Dial(ClientAddr, grpc.WithInsecure())
  153. if err != nil {
  154. return nil, err
  155. }
  156. var client proto.ServiceClient
  157. client = proto.NewServiceClient(conn)
  158. repl, err := client.Apply(context.Background(), &proto.ApplyReqData{Name: "extract_service", Balance: 0})
  159. if err != nil {
  160. return nil, err
  161. }
  162. //2.业务调用
  163. addr := fmt.Sprintf("%s:%d", repl.Ip, repl.Port)
  164. conn_b, err := grpc.Dial(addr, grpc.WithInsecure())
  165. if err != nil {
  166. return nil, err
  167. }
  168. defer func(conn_b *grpc.ClientConn) {
  169. _ = conn_b.Close()
  170. }(conn_b)
  171. pc := pb.NewFileExtractClient(conn_b)
  172. rep, err := pc.FileExtract(context.Background(), reqs)
  173. if err != nil {
  174. return nil, err
  175. }
  176. return rep, nil
  177. }
  178. // @Description 信息发布
  179. // @Author J 2022/4/12 1:57 PM
  180. func InfoPub(info map[string]interface{}) {
  181. tmp := info["appendInfo"].(map[string]interface{})
  182. saveMap := make(map[string]interface{})
  183. jyMap := make(map[string]interface{})
  184. for _, f := range InfoFields {
  185. if tmp[f] == nil {
  186. continue
  187. }
  188. if f == "budget" || f == "amount" {
  189. saveMap[SaveFields[f]] = util.Float64All(tmp[f])
  190. jyMap[f] = util.Float64All(tmp[f])
  191. } else if f == "industry" {
  192. // topscopeclass/subcopeclass
  193. //if s := util.ObjToString(tmp[f]); s != "" {
  194. //
  195. // for _, s2 := range strings.Split(s, ",") {
  196. // arr := strings.Split(s2, "_")
  197. // // todo
  198. // }
  199. //}
  200. } else if f == "winner" {
  201. if s := util.ObjToString(tmp[f]); s != "" {
  202. s = strings.ReplaceAll(s, ",", ",") //中文变英文
  203. saveMap[SaveFields[f]] = s
  204. saveMap[f] = s
  205. jyMap[f] = s
  206. jyMap[SaveFields[f]] = s
  207. }
  208. } else if f == "attach" {
  209. s := util.ObjToString(tmp[f])
  210. if s != "" {
  211. atts := map[string]interface{}{}
  212. if err := json.Unmarshal([]byte(s), &atts); err != nil {
  213. Logger.Error("data Unmarshal Failed:", Field("error", err))
  214. }
  215. saveMap[SaveFields[f]] = atts
  216. }
  217. } else if f == "discern_attach" {
  218. if s := util.ObjToString(tmp[f]); s != "" {
  219. atts := map[string]interface{}{}
  220. if err := json.Unmarshal([]byte(s), &atts); err != nil {
  221. Logger.Error("data Unmarshal Failed:", Field("error", err))
  222. }
  223. saveMap[SaveFields[f]] = atts
  224. }
  225. } else {
  226. if s := util.ObjToString(tmp[f]); s != "" {
  227. if SaveFields[f] == "" {
  228. Logger.Info("field ---: " + f)
  229. }
  230. saveMap[SaveFields[f]] = s
  231. if SaveFields[f] == "buyer" || SaveFields[f] == "buyerperson" || SaveFields[f] == "buyertel" ||
  232. SaveFields[f] == "area" || SaveFields[f] == "city" {
  233. jyMap[SaveFields[f]] = s
  234. }
  235. }
  236. }
  237. }
  238. now := time.Now()
  239. saveMap["comeintime"] = now.Unix()
  240. saveMap["publishtime"] = now.Unix()
  241. saveMap["contenthtml"] = tmp["detail"]
  242. saveMap["site"] = "剑鱼信息发布平台"
  243. saveMap["channel"] = "公告"
  244. saveMap["spidercode"] = "a_jyxxfbpt_gg"
  245. saveMap["extracttype"] = 0
  246. saveMap["areaval"] = 0
  247. saveMap["infoformat"] = 1
  248. saveMap["dataging"] = 0
  249. saveMap["buyerhint"] = util.IntAll(tmp["contact_overt"])
  250. _id := bson.NewObjectIdWithTime(now)
  251. saveMap["href"] = fmt.Sprintf(JyUrl, util.CommonEncodeArticle("content", mongodb.BsonIdToSId(_id)))
  252. saveMap["competehref"] = "#"
  253. saveMap["jyfb_data"] = jyMap
  254. saveMap["jyfb_id"] = util.ObjToString(info["id"])
  255. Logger.Debug("InfoPub mgo save: " + fmt.Sprint(saveMap))
  256. MgoBid.SaveByOriID(BidColl, saveMap)
  257. }
  258. type AttsResponse struct {
  259. Other AttsOther `json:"other"`
  260. Result []*AttsResult `json:"result"`
  261. }
  262. type AttsOther struct {
  263. Id string `json:"id"`
  264. Action string `json:"action"`
  265. MsgType string `json:"msgType"`
  266. Detail []string `json:"detail"`
  267. Title []string `json:"title"`
  268. }
  269. type AttsResult struct {
  270. FileName string `json:"fileName"`
  271. TextUrl string `json:"textUrl"`
  272. TextContent string `json:"textContent"`
  273. FilePath string `json:"filePath"`
  274. ErrorState string `json:"errorState"`
  275. }
  276. // @Description 附件处理完成队列
  277. // @Author J 2022/4/13 3:29 PM
  278. func taskAtts(obj *AttsResponse) {
  279. atts := make(map[string]interface{})
  280. atts_text := make(map[string]interface{})
  281. for i, r := range obj.Result {
  282. at := make(map[string]interface{})
  283. text := make(map[string]interface{})
  284. at["state"] = r.ErrorState
  285. if r.ErrorState == "200" {
  286. at["sensitive"] = WordsIdentify(r.TextContent)
  287. text["file_name"] = r.FileName
  288. text["attach_url"] = r.TextUrl
  289. }
  290. atts[strconv.Itoa(i)] = at
  291. if len(text) > 0 {
  292. atts_text[strconv.Itoa(i)] = text
  293. }
  294. }
  295. attsJson, _ := json.Marshal(atts)
  296. attsTextJson, _ := json.Marshal(atts_text)
  297. // 直接调用剑鱼接口
  298. req := &jypb.SensitiveRequest{
  299. Id: util.ObjToString(obj.Other.Id),
  300. MsgType: util.ObjToString(obj.Other.MsgType),
  301. //Action: util.ObjToString(obj.Other.Action),
  302. Title: obj.Other.Title,
  303. Detail: obj.Other.Detail,
  304. Attachments: string(attsJson),
  305. AttachTxt: string(attsTextJson),
  306. }
  307. Logger.Debug("JyRpcSensitive request: " + fmt.Sprint(req))
  308. JyRpcSensitive(req)
  309. }
  310. // @Description 敏感词识别完成调用剑鱼接口
  311. // @Author J 2022/4/13 11:16 AM
  312. func JyRpcSensitive(req *jypb.SensitiveRequest) {
  313. conn := JyRpcClient.Conn()
  314. defer conn.Close()
  315. jyIntf := jypb.NewCommonClient(conn)
  316. resp, err := jyIntf.SensitiveMethod(context.Background(), req)
  317. if err != nil {
  318. Logger.Error(err.Error())
  319. return
  320. }
  321. Logger.Info("JyRpcSensitive response: " + resp.String())
  322. }
  323. // @Description 信息删除(es、bidding、extract、project)
  324. // @Author J 2022/4/8 4:37 PM:00
  325. func DelMethod(res string) {
  326. q := map[string]interface{}{"_id": mongodb.StringTOBsonId(res)}
  327. b := MgoBid.Del(BidColl, q)
  328. if !b {
  329. Logger.Error(" bidding del fail...")
  330. }
  331. b = MgoExt.Del(ExtColl, q)
  332. if !b {
  333. Logger.Error(" extract del fail...")
  334. }
  335. Es.DelById(Index, Itype, res)
  336. Es.DelById(IndexAll, Itype, res)
  337. project := Sysconfig["project"].(map[string]interface{})
  338. by, _ := json.Marshal(map[string]interface{}{
  339. "infoid": res,
  340. "stype": "deleteInfo",
  341. })
  342. addr := &net.UDPAddr{
  343. IP: net.ParseIP(project["addr"].(string)),
  344. Port: util.IntAll(project["port"]),
  345. }
  346. Logger.Debug(string(by))
  347. _ = UdpClient.WriteUdp(by, util.OP_TYPE_DATA, addr)
  348. }
  349. // @Description 数据处理完成调用jy接口
  350. // @Author J 2022/4/9 11:41 AM
  351. func JyRpcDataFin(_id string) {
  352. info, _ := MgoBid.FindById(BidColl, _id, `{"jyfb_id": 1}`)
  353. if len(*info) == 0 {
  354. Logger.Error("JyRpcDataFin mgo not find, id: " + _id)
  355. return
  356. }
  357. conn := JyRpcClient.Conn()
  358. defer conn.Close()
  359. req := &jypb.StateRequest{
  360. Id: util.ObjToString((*info)["jyfb_id"]),
  361. PublishId: _id,
  362. }
  363. jyIntf := jypb.NewCommonClient(conn)
  364. Logger.Info("JyRpcDataFin request: " + req.String())
  365. resp, err := jyIntf.StateMethod(context.Background(), req)
  366. if err != nil {
  367. Logger.Error(err.Error())
  368. }
  369. Logger.Info("JyRpcDataFin response: " + resp.String())
  370. }