service.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. es "github.com/olivere/elastic/v7"
  7. "google.golang.org/grpc"
  8. jyProto "jy_member/proto"
  9. "jygit.jydev.jianyu360.cn/BP/servicerd/proto"
  10. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  11. "jygit.jydev.jianyu360.cn/data_processing/common_utils/redis"
  12. "net/http"
  13. "strings"
  14. )
  15. type JyService struct {
  16. }
  17. // 附件
  18. type AtRequest struct {
  19. InfoId string
  20. }
  21. type AtResponse struct {
  22. Rep []map[string]interface{}
  23. }
  24. // 预测
  25. type FcRequest struct {
  26. RedisFKey string
  27. Id string
  28. Pname string
  29. Buyer string
  30. Area string
  31. City string
  32. Budget float64
  33. BuyerContent []string
  34. }
  35. type FcResponse struct {
  36. Rep bool
  37. }
  38. func (j *JyService) Download(req AtRequest, res *AtResponse) error {
  39. //util.Debug("Download----infoid:", req.InfoId)
  40. if req.InfoId == "" {
  41. return fmt.Errorf(" _id error")
  42. }
  43. var err error
  44. res.Rep, err = Method(req.InfoId)
  45. return err
  46. }
  47. func (j *JyService) Forecast(req FcRequest, res *FcResponse) error {
  48. util.Debug("Forecast----ProjectId:", req)
  49. if req.RedisFKey == "" {
  50. res.Rep = false
  51. return fmt.Errorf("RedisFKey is nil")
  52. }
  53. if req.Id == "" {
  54. res.Rep = false
  55. return fmt.Errorf("ProjectId is nil")
  56. }
  57. if req.Pname == "" {
  58. res.Rep = false
  59. return fmt.Errorf("ProjectName is nil")
  60. }
  61. if len(req.BuyerContent) <= 0 {
  62. res.Rep = false
  63. return fmt.Errorf("BuyerContent is nil")
  64. }
  65. go ForecastMethod(&req)
  66. res.Rep = true
  67. return nil
  68. }
  69. func Method(infoid string) ([]map[string]interface{}, error) {
  70. fields := map[string]interface{}{"projectinfo": 1, "site": 1, "isValidFile": 1}
  71. info := &map[string]interface{}{}
  72. var resp []map[string]interface{}
  73. b := false
  74. info, b = MongoTool.FindById(MgoColl, infoid, fields)
  75. if !b {
  76. info, b = MongoTool.FindById(MgoColl1, infoid, fields)
  77. if !b {
  78. util.Debug("Not found data.")
  79. return resp, fmt.Errorf("Not found data.")
  80. }
  81. }
  82. if (*info)["isValidFile"] != nil && (*info)["isValidFile"].(bool) {
  83. if att, ok := (*info)["projectinfo"].(map[string]interface{}); ok {
  84. file := util.ObjToMap(att["attachments"])
  85. for _, v := range *file {
  86. attachment := map[string]interface{}{}
  87. if v1, ok := v.(map[string]interface{}); ok {
  88. if valiType(v1) {
  89. if ossid, ok := v1["ossid"].(string); ok {
  90. attachment["downurl"] = down_url + ossid
  91. attachment["filename"] = util.ObjToString(v1["filename"])
  92. attachment["org_url"] = util.ObjToString(v1["org_url"])
  93. attachment["size"] = util.ObjToString(v1["size"])
  94. resp = append(resp, attachment)
  95. } else if v1["fid"] != nil {
  96. if util.ObjToString((*info)["site"]) == "中国招标投标公共服务平台" && util.ObjToString(v1["size"]) == "8.4 KB" {
  97. break
  98. }
  99. attachment["downurl"] = down_url + util.ObjToString(v1["fid"])
  100. attachment["org_url"] = util.ObjToString(v1["org_url"])
  101. attachment["filename"] = util.ObjToString(v1["filename"])
  102. attachment["size"] = util.ObjToString(v1["size"])
  103. resp = append(resp, attachment)
  104. } else {
  105. // 不提供原文下载链接 2022.06.02
  106. //if govalidator.IsURL(util.ObjToString(v1["org_url"])) && util.ObjToString((*info)["site"]) != "中国招标投标公共服务平台" &&
  107. // util.ObjToString(v1["filename"]) != "文件下载.jpg" && util.ObjToString(v1["filename"]) != "文件下载.png" {
  108. // attachment["downurl"] = util.ObjToString(v1["org_url"])
  109. // attachment["org_url"] = util.ObjToString(v1["org_url"])
  110. // attachment["filename"] = util.ObjToString(v1["filename"])
  111. // attachment["size"] = util.ObjToString(v1["size"])
  112. // resp = append(resp, attachment)
  113. //}
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. return resp, nil
  121. }
  122. func FindProject(projectId string, query map[string]interface{}) map[string]interface{} {
  123. client := Es.GetEsConn()
  124. defer Es.DestoryEsConn(client)
  125. //esquery := `{"query": {"bool": {"must": [{"match": {"_id": "` + projectId + `"}}]}}}`
  126. q := es.NewBoolQuery().Must(es.NewTermQuery("_id", projectId))
  127. data := Es.Get(Index, q)
  128. util.Debug(*data)
  129. if len(*data) > 0 {
  130. tmp := (*data)[0]
  131. if CheckContain(util.ObjToString(query["projectname"]), util.ObjToString(tmp["projectname"])) != 3 &&
  132. CheckContain(util.ObjToString(query["purchasing"]), util.ObjToString(tmp["purchasing"])) != 3 &&
  133. CheckContain(util.ObjToString(query["buyer"]), util.ObjToString(tmp["buyer"])) != 3 &&
  134. CheckContain(util.ObjToString(query["area"]), util.ObjToString(tmp["area"])) != 3 &&
  135. CheckContain(util.ObjToString(query["city"]), util.ObjToString(tmp["city"])) != 3 {
  136. return tmp
  137. } else {
  138. //项目名称或者标的物不相同,查询项目
  139. findPro := EsFindPro(query)
  140. if util.ObjToString(findPro["_id"]) == projectId {
  141. return findPro
  142. } else {
  143. //用户填写的内容
  144. findPro["projectname"] = query["projectname"]
  145. findPro["purchasing"] = query["purchasing"]
  146. if query["buyer"] != "" {
  147. findPro["buyer"] = query["buyer"]
  148. }
  149. if query["area"] != "" {
  150. findPro["area"] = query["area"]
  151. }
  152. if query["city"] != "" {
  153. findPro["city"] = query["city"]
  154. }
  155. if query["budget"] != 0 {
  156. findPro["budget"] = query["budget"]
  157. }
  158. }
  159. return findPro
  160. }
  161. } else {
  162. return EsFindPro(query)
  163. }
  164. }
  165. func EsFindPro(q map[string]interface{}) map[string]interface{} {
  166. esObj := NesEsObject{}
  167. if util.ObjToString(q["projectname"]) != "" {
  168. pobj := PnameObj{Match: ProjectName{
  169. Pname: util.ObjToString(q["projectname"]),
  170. }}
  171. esObj.Query.Should = append(esObj.Query.Should, pobj)
  172. }
  173. if q["purchasing"] != nil {
  174. purObj := PurObj{Match: Purchasing{
  175. Pur: q["purchasing"].([]string),
  176. }}
  177. esObj.Query.Should = append(esObj.Query.Should, purObj)
  178. }
  179. if util.ObjToString(q["buyer"]) != "" {
  180. bObj := BuyerObj{Match: Buyer{
  181. Buyer: util.ObjToString(q["buyer"]),
  182. }}
  183. esObj.Query.Should = append(esObj.Query.Should, bObj)
  184. }
  185. if util.ObjToString(q["area"]) != "" {
  186. areaObj := AreaObj{Match: Area{
  187. Area: util.ObjToString(q["area"]),
  188. }}
  189. esObj.Query.Should = append(esObj.Query.Should, areaObj)
  190. }
  191. if util.ObjToString(q["city"]) != "" {
  192. cityObj := CityObj{Match: City{
  193. City: util.ObjToString(q["city"]),
  194. }}
  195. esObj.Query.Should = append(esObj.Query.Should, cityObj)
  196. }
  197. rdata := make(map[string]interface{})
  198. rdata["query"] = esObj
  199. util.Debug(esObj)
  200. util.Debug(rdata)
  201. //esbytes, _ := json.Marshal(rdata)
  202. //util.Debug(string(esbytes))
  203. //data := Es.Get(Index, string(esbytes))
  204. //if len(*data) > 0 {
  205. // return (*data)[0]
  206. //} else {
  207. return map[string]interface{}{}
  208. //}
  209. }
  210. func getSize(url string) int64 {
  211. resp, err := http.Get(url)
  212. if err != nil {
  213. fmt.Printf("HEAD failed: %v", err)
  214. }
  215. fmt.Printf("resp: %s\n", resp.Status)
  216. fmt.Printf("size: %d\n", resp.ContentLength)
  217. return resp.ContentLength
  218. }
  219. func valiType(tmp map[string]interface{}) bool {
  220. for _, v := range FileType {
  221. if util.ObjToString(v) == "*" {
  222. return true
  223. }
  224. if strings.Contains(util.ObjToString(tmp["filename"]), util.ObjToString(v)) {
  225. return true
  226. }
  227. if strings.Contains(util.ObjToString(tmp["ftype"]), util.ObjToString(v)) {
  228. return true
  229. }
  230. }
  231. return false
  232. }
  233. func CheckContain(b1, b2 string) (res int) {
  234. if b1 == b2 {
  235. res = 1 //相等
  236. return
  237. }
  238. bs1 := []rune(b1)
  239. bs2 := []rune(b2)
  240. tmp := ""
  241. for i := 0; i < len(bs1); i++ {
  242. for j := 0; j < len(bs2); j++ {
  243. if bs1[i] == bs2[j] {
  244. tmp += string(bs1[i])
  245. } else if tmp != "" {
  246. b1 = strings.Replace(b1, tmp, "", -1)
  247. b2 = strings.Replace(b2, tmp, "", -1)
  248. tmp = ""
  249. }
  250. }
  251. }
  252. if tmp != "" {
  253. b1 = strings.Replace(b1, tmp, "", -1)
  254. b2 = strings.Replace(b2, tmp, "", -1)
  255. }
  256. if b1 == b2 {
  257. res = 1 //相等
  258. } else if b1 == "" || b2 == "" {
  259. res = 2 //包含
  260. } else {
  261. res = 3 //不相同
  262. }
  263. return
  264. }
  265. func ForecastMethod(req *FcRequest) {
  266. query := map[string]interface{}{}
  267. query["projectname"] = req.Pname
  268. query["purchasing"] = req.BuyerContent
  269. if req.Buyer != "" {
  270. query["buyer"] = req.Buyer
  271. }
  272. if req.Area != "" {
  273. query["area"] = req.Area
  274. }
  275. if req.City != "" {
  276. query["city"] = req.City
  277. }
  278. query["budget"] = req.Budget
  279. result := FindProject(req.Id, query)
  280. wins := []string{}
  281. budget := 0.0
  282. if len(result) > 0 {
  283. if result["winnerorder"] != nil {
  284. wins = util.ObjArrToStringArr(result["winnerorder"].([]interface{}))
  285. }
  286. if result["buyer"] != nil && query["buyer"] == nil {
  287. query["buyer"] = result["buyer"]
  288. }
  289. if result["area"] != nil && query["area"] == nil {
  290. query["area"] = result["area"]
  291. }
  292. if result["city"] != nil && query["city"] == nil {
  293. query["city"] = result["city"]
  294. }
  295. if result["budget"] != nil {
  296. budget = util.Float64All(result["budget"])
  297. }
  298. if result["winner"] != nil && _ent.MatchString(util.ObjToString(result["winner"])) {
  299. query["winner"] = result["winner"]
  300. }
  301. }
  302. reqs := &jyProto.PredictReq{
  303. ProjectName: req.Pname,
  304. Mount: float32(budget),
  305. BidUnit: util.ObjToString(query["buyer"]),
  306. Area: util.ObjToString(query["area"]),
  307. City: util.ObjToString(query["city"]),
  308. Goods: req.BuyerContent,
  309. Winner: util.ObjToString(query["winner"]),
  310. WinnerOrder: wins}
  311. // 调用gRPC接口
  312. util.Debug(reqs)
  313. grpcConn(reqs, req.RedisFKey)
  314. }
  315. func grpcConn(reqs *jyProto.PredictReq, risKey string) {
  316. conn, err := grpc.Dial(ClientAddr, grpc.WithInsecure())
  317. if err != nil {
  318. redis.Put("predict_pro", risKey, "", -1)
  319. util.Debug(err)
  320. }
  321. var client proto.ServiceClient
  322. client = proto.NewServiceClient(conn)
  323. repl, err := client.Apply(context.Background(), &proto.ApplyReqData{Name: "winner_predict", Balance: 0})
  324. if err != nil {
  325. util.Debug(err)
  326. redis.Put("predict_pro", risKey, "", -1)
  327. return
  328. }
  329. util.Debug("结果", repl.Ip, repl.Port)
  330. //2.业务调用
  331. addr := fmt.Sprintf("%s:%d", repl.Ip, repl.Port)
  332. conn_b, err := grpc.Dial(addr, grpc.WithInsecure())
  333. if err != nil {
  334. util.Debug(err)
  335. redis.Put("predict_pro", risKey, "", -1)
  336. return
  337. }
  338. defer conn_b.Close()
  339. pc := jyProto.NewWinnerPredictServiceClient(conn_b)
  340. rep, err := pc.Predict(context.Background(), reqs)
  341. if err != nil {
  342. redis.Put("predict_pro", risKey, "", -1)
  343. util.Debug(err)
  344. } else {
  345. util.Debug(rep.Item)
  346. jsonStr, _ := json.Marshal(rep.Item)
  347. bol := redis.Put("predict_pro", risKey, string(jsonStr), -1)
  348. util.Debug("预测结果:", bol)
  349. }
  350. }