service.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. jyProto "jy_member/proto"
  9. "net/http"
  10. "strings"
  11. "utils"
  12. "utils/redis"
  13. "utils/validator"
  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}
  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 att, ok := (*info)["projectinfo"].(map[string]interface{}); ok {
  83. file := util.ObjToMap(att["attachments"])
  84. for _, v := range *file {
  85. attachment := map[string]interface{}{}
  86. if v1, ok := v.(map[string]interface{}); ok {
  87. if valiType(v1) {
  88. if ossid, ok := v1["ossid"].(string); ok {
  89. attachment["downurl"] = down_url + ossid
  90. attachment["filename"] = util.ObjToString(v1["filename"])
  91. attachment["org_url"] = util.ObjToString(v1["org_url"])
  92. attachment["size"] = util.ObjToString(v1["size"])
  93. resp = append(resp, attachment)
  94. } else if v1["fid"] != nil {
  95. if util.ObjToString((*info)["site"]) == "中国招标投标公共服务平台" && util.ObjToString(v1["size"]) == "8.4 KB" {
  96. break
  97. }
  98. attachment["downurl"] = down_url + util.ObjToString(v1["fid"])
  99. attachment["org_url"] = util.ObjToString(v1["org_url"])
  100. attachment["filename"] = util.ObjToString(v1["filename"])
  101. attachment["size"] = util.ObjToString(v1["size"])
  102. resp = append(resp, attachment)
  103. } else {
  104. if govalidator.IsURL(util.ObjToString(v1["org_url"])) && util.ObjToString((*info)["site"]) != "中国招标投标公共服务平台" &&
  105. util.ObjToString(v1["filename"]) != "文件下载.jpg" && util.ObjToString(v1["filename"]) != "文件下载.png" {
  106. attachment["downurl"] = util.ObjToString(v1["org_url"])
  107. attachment["org_url"] = util.ObjToString(v1["org_url"])
  108. attachment["filename"] = util.ObjToString(v1["filename"])
  109. attachment["size"] = util.ObjToString(v1["size"])
  110. resp = append(resp, attachment)
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. return resp, nil
  118. }
  119. func FindProject(projectId string, query map[string]interface{}) map[string]interface{} {
  120. client := Es.GetEsConn()
  121. defer Es.DestoryEsConn(client)
  122. esquery := `{"query": {"bool": {"must": [{"match": {"_id": "` + projectId + `"}}]}}}`
  123. data := Es.Get(Index, Itype, esquery)
  124. util.Debug(*data)
  125. if len(*data) > 0 {
  126. tmp := (*data)[0]
  127. if CheckContain(util.ObjToString(query["projectname"]), util.ObjToString(tmp["projectname"])) != 3 &&
  128. CheckContain(util.ObjToString(query["purchasing"]), util.ObjToString(tmp["purchasing"])) != 3 &&
  129. CheckContain(util.ObjToString(query["buyer"]), util.ObjToString(tmp["buyer"])) != 3 &&
  130. CheckContain(util.ObjToString(query["area"]), util.ObjToString(tmp["area"])) != 3 &&
  131. CheckContain(util.ObjToString(query["city"]), util.ObjToString(tmp["city"])) != 3 {
  132. return tmp
  133. } else {
  134. //项目名称或者标的物不相同,查询项目
  135. findPro := EsFindPro(query)
  136. if util.ObjToString(findPro["_id"]) == projectId {
  137. return findPro
  138. } else {
  139. //用户填写的内容
  140. findPro["projectname"] = query["projectname"]
  141. findPro["purchasing"] = query["purchasing"]
  142. if query["buyer"] != "" {
  143. findPro["buyer"] = query["buyer"]
  144. }
  145. if query["area"] != "" {
  146. findPro["area"] = query["area"]
  147. }
  148. if query["city"] != "" {
  149. findPro["city"] = query["city"]
  150. }
  151. if query["budget"] != 0 {
  152. findPro["budget"] = query["budget"]
  153. }
  154. }
  155. return findPro
  156. }
  157. } else {
  158. return EsFindPro(query)
  159. }
  160. }
  161. func EsFindPro(q map[string]interface{}) map[string]interface{} {
  162. esObj := NesEsObject{}
  163. if util.ObjToString(q["projectname"]) != "" {
  164. pobj := PnameObj{Match: ProjectName{
  165. Pname: util.ObjToString(q["projectname"]),
  166. }}
  167. esObj.Query.Should = append(esObj.Query.Should, pobj)
  168. }
  169. if q["purchasing"] != nil {
  170. purObj := PurObj{Match: Purchasing{
  171. Pur: q["purchasing"].([]string),
  172. }}
  173. esObj.Query.Should = append(esObj.Query.Should, purObj)
  174. }
  175. if util.ObjToString(q["buyer"]) != "" {
  176. bObj := BuyerObj{Match: Buyer{
  177. Buyer: util.ObjToString(q["buyer"]),
  178. }}
  179. esObj.Query.Should = append(esObj.Query.Should, bObj)
  180. }
  181. if util.ObjToString(q["area"]) != "" {
  182. areaObj := AreaObj{Match: Area{
  183. Area: util.ObjToString(q["area"]),
  184. }}
  185. esObj.Query.Should = append(esObj.Query.Should, areaObj)
  186. }
  187. if util.ObjToString(q["city"]) != "" {
  188. cityObj := CityObj{Match: City{
  189. City: util.ObjToString(q["city"]),
  190. }}
  191. esObj.Query.Should = append(esObj.Query.Should, cityObj)
  192. }
  193. rdata := make(map[string]interface{})
  194. rdata["query"] = esObj
  195. util.Debug(esObj)
  196. util.Debug(rdata)
  197. esbytes, _ := json.Marshal(rdata)
  198. util.Debug(string(esbytes))
  199. data := Es.Get(Index, Itype, string(esbytes))
  200. if len(*data) > 0 {
  201. return (*data)[0]
  202. } else {
  203. return map[string]interface{}{}
  204. }
  205. }
  206. func getSize(url string) int64 {
  207. resp, err := http.Get(url)
  208. if err != nil {
  209. fmt.Printf("HEAD failed: %v", err)
  210. }
  211. fmt.Printf("resp: %s\n", resp.Status)
  212. fmt.Printf("size: %d\n", resp.ContentLength)
  213. return resp.ContentLength
  214. }
  215. func valiType(tmp map[string]interface{}) bool {
  216. for _, v := range FileType {
  217. if util.ObjToString(v) == "*" {
  218. return true
  219. }
  220. if strings.Contains(util.ObjToString(tmp["filename"]), util.ObjToString(v)) {
  221. return true
  222. }
  223. if strings.Contains(util.ObjToString(tmp["ftype"]), util.ObjToString(v)) {
  224. return true
  225. }
  226. }
  227. return false
  228. }
  229. func CheckContain(b1, b2 string) (res int) {
  230. if b1 == b2 {
  231. res = 1 //相等
  232. return
  233. }
  234. bs1 := []rune(b1)
  235. bs2 := []rune(b2)
  236. tmp := ""
  237. for i := 0; i < len(bs1); i++ {
  238. for j := 0; j < len(bs2); j++ {
  239. if bs1[i] == bs2[j] {
  240. tmp += string(bs1[i])
  241. } else if tmp != "" {
  242. b1 = strings.Replace(b1, tmp, "", -1)
  243. b2 = strings.Replace(b2, tmp, "", -1)
  244. tmp = ""
  245. }
  246. }
  247. }
  248. if tmp != "" {
  249. b1 = strings.Replace(b1, tmp, "", -1)
  250. b2 = strings.Replace(b2, tmp, "", -1)
  251. }
  252. if b1 == b2 {
  253. res = 1 //相等
  254. } else if b1 == "" || b2 == "" {
  255. res = 2 //包含
  256. } else {
  257. res = 3 //不相同
  258. }
  259. return
  260. }
  261. func ForecastMethod(req *FcRequest) {
  262. query := map[string]interface{}{}
  263. query["projectname"] = req.Pname
  264. query["purchasing"] = req.BuyerContent
  265. if req.Buyer != "" {
  266. query["buyer"] = req.Buyer
  267. }
  268. if req.Area != "" {
  269. query["area"] = req.Area
  270. }
  271. if req.City != "" {
  272. query["city"] = req.City
  273. }
  274. query["budget"] = req.Budget
  275. result := FindProject(req.Id, query)
  276. wins := []string{}
  277. budget := 0.0
  278. if len(result) > 0 {
  279. if result["winnerorder"] != nil {
  280. wins = util.ObjArrToStringArr(result["winnerorder"].([]interface{}))
  281. }
  282. if result["buyer"] != nil && query["buyer"] == nil {
  283. query["buyer"] = result["buyer"]
  284. }
  285. if result["area"] != nil && query["area"] == nil {
  286. query["area"] = result["area"]
  287. }
  288. if result["city"] != nil && query["city"] == nil {
  289. query["city"] = result["city"]
  290. }
  291. if result["budget"] != nil {
  292. budget = util.Float64All(result["budget"])
  293. }
  294. if result["winner"] != nil && _ent.MatchString(util.ObjToString(result["winner"])) {
  295. query["winner"] = result["winner"]
  296. }
  297. }
  298. reqs := &jyProto.PredictReq{
  299. ProjectName: req.Pname,
  300. Mount: float32(budget),
  301. BidUnit: util.ObjToString(query["buyer"]),
  302. Area: util.ObjToString(query["area"]),
  303. City: util.ObjToString(query["city"]),
  304. Goods: req.BuyerContent,
  305. Winner: util.ObjToString(query["winner"]),
  306. WinnerOrder: wins}
  307. // 调用gRPC接口
  308. util.Debug(reqs)
  309. grpcConn(reqs, req.RedisFKey)
  310. }
  311. func grpcConn(reqs *jyProto.PredictReq, risKey string) {
  312. conn, err := grpc.Dial(ClientAddr, grpc.WithInsecure())
  313. if err != nil {
  314. redis.Put("predict_pro", risKey, "", -1)
  315. util.Debug(err)
  316. }
  317. var client proto.ServiceClient
  318. client = proto.NewServiceClient(conn)
  319. repl, err := client.Apply(context.Background(), &proto.ApplyReqData{Name: "winner_predict", Balance: 0})
  320. if err != nil {
  321. util.Debug(err)
  322. redis.Put("predict_pro", risKey, "", -1)
  323. return
  324. }
  325. util.Debug("结果", repl.Ip, repl.Port)
  326. //2.业务调用
  327. addr := fmt.Sprintf("%s:%d", repl.Ip, repl.Port)
  328. conn_b, err := grpc.Dial(addr, grpc.WithInsecure())
  329. if err != nil {
  330. util.Debug(err)
  331. redis.Put("predict_pro", risKey, "", -1)
  332. return
  333. }
  334. defer conn_b.Close()
  335. pc := jyProto.NewWinnerPredictServiceClient(conn_b)
  336. rep, err := pc.Predict(context.Background(), reqs)
  337. if err != nil {
  338. redis.Put("predict_pro", risKey, "", -1)
  339. util.Debug(err)
  340. } else {
  341. util.Debug(rep.Item)
  342. jsonStr, _ := json.Marshal(rep.Item)
  343. bol := redis.Put("predict_pro", risKey, string(jsonStr), -1)
  344. util.Debug("预测结果:", bol)
  345. }
  346. }