push.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package push
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/encrypt"
  5. "app.yhyue.com/moapp/jybase/go-xweb/xweb"
  6. "app.yhyue.com/moapp/jybase/log"
  7. "app.yhyue.com/moapp/jybase/mongodb"
  8. "app.yhyue.com/moapp/jybase/redis"
  9. "bytes"
  10. . "cmplatform/util"
  11. "encoding/json"
  12. "fmt"
  13. "github.com/tealeg/xlsx"
  14. "go.uber.org/zap"
  15. "net/url"
  16. "strings"
  17. "time"
  18. )
  19. type Push struct {
  20. *xweb.Action
  21. index xweb.Mapper `xweb:"/push/index"` //首页
  22. dataPush xweb.Mapper `xweb:"/push/dataPush"` //数据推送
  23. }
  24. var (
  25. // fields1 = "city,district,projectname,bidamount,s_winner,winner_type,buyer,publishtime,buyer_type,href,winner_style,bidtype,budget,agency,agency_fee,projectcode,jybxhref,id,projectId"
  26. fields1 = "city,projectname,budget,bidopentime,buyer,publishtime,buyer_type,href,district,bidtype,bidway,bidendtime,docamount,agency,buyerperson,buyertel,projectcode,id,projectId,jybxhref"
  27. fields2 = "city,projectname,bidamount,s_winner,winner_type,buyer,publishtime,buyer_type,href,district,winner_style,bidtype,budget,agency,agency_fee,projectcode,jybxhref,id,projectId"
  28. SE = encrypt.SimpleEncrypt{Key: "topJYBX2019"}
  29. subMap = map[string]map[string]string{
  30. "招标": map[string]string{"0": "3"},
  31. "竞谈": map[string]string{"0": "3"},
  32. "单一": map[string]string{"0": "3"},
  33. "询价": map[string]string{"0": "3"},
  34. "竞价": map[string]string{"0": "3"},
  35. "邀标": map[string]string{"0": "3"},
  36. "论证意见": map[string]string{"1": "2"},
  37. "预告": map[string]string{"1": "2"},
  38. "预审": map[string]string{"1": "2"},
  39. "预审结果": map[string]string{"1": "2"},
  40. "需求公示": map[string]string{"1": "2"},
  41. "采购意向": map[string]string{"1": "2"},
  42. "变更": map[string]string{"2": "5"},
  43. "废标": map[string]string{"3": "7"},
  44. "流标": map[string]string{"3": "8"},
  45. "中标": map[string]string{"3": "10"},
  46. "成交": map[string]string{"3": "11"},
  47. "结果变更": map[string]string{"3": "11"},
  48. "合同": map[string]string{"3": "12"},
  49. "验收": map[string]string{"5": "13"},
  50. "违规": map[string]string{"5": "14"},
  51. }
  52. bidtypeMap = map[string]string{
  53. "招标": "0",
  54. "邀标": "1",
  55. "竞谈": "2",
  56. "单一": "3",
  57. "询价": "4",
  58. "竞价": "6",
  59. }
  60. subtypeMap = map[string]string{
  61. "招标": "0",
  62. "邀标": "1",
  63. "竞谈": "2",
  64. "单一": "3",
  65. "询价": "4",
  66. "其它": "5",
  67. "竞价": "6",
  68. }
  69. )
  70. func (this *Push) Index() {
  71. this.Render("/push/index.html")
  72. }
  73. func (this *Push) DataPush() {
  74. appid := this.GetString("appid")
  75. getDataType := this.GetString("getDataType")
  76. dataType := this.GetString("dataType")
  77. historyId := this.GetString("historyId")
  78. dataSource := this.GetString("dataSource")
  79. dataMap := map[string][]map[string]interface{}{}
  80. fields := ""
  81. if dataType == "0" {
  82. fields = fields1
  83. } else {
  84. fields = fields2
  85. }
  86. if appid == "jyIhxXQQoJAgVZQURLLwRE" {
  87. errstr := ttCRM(dataSource, historyId)
  88. this.ServeJson(map[string]interface{}{"errMsg": errstr})
  89. } else {
  90. if getDataType == "0" {
  91. query := map[string]interface{}{"appid": appid, "historyId": historyId}
  92. // fieldMap := map[string]int{}
  93. // for _, v := range strings.Split(fields, ",") {
  94. // fieldMap[v] = 1
  95. // }
  96. data, ok := MgoSave.Find(dataSource, query, nil, nil, false, -1, -1)
  97. if ok && data != nil && len(*data) > 0 {
  98. datasArr := []map[string]interface{}{}
  99. for _, v := range *data {
  100. datamap := map[string]interface{}{}
  101. for _, vv := range strings.Split(fields, ",") {
  102. if v[vv] != nil {
  103. datamap[vv] = fmt.Sprint(v[vv])
  104. } else {
  105. datamap[vv] = ""
  106. }
  107. }
  108. id := common.ObjToString(v["id"])
  109. datamap["id"] = SE.EncodeString(id)
  110. datasArr = append(datasArr, datamap)
  111. }
  112. dataMap["dataList"] = datasArr
  113. }
  114. } else if getDataType == "1" {
  115. dataMap["dataList"] = ExcelParse(dataType)
  116. }
  117. log.Debug("", zap.Any("dataMap", dataMap))
  118. databyte, _ := json.Marshal(dataMap)
  119. dataStr := string(databyte)
  120. //
  121. token := getAccessToken()
  122. // token := "f9cb92a6-356b-433f-a8bd-09f1deef6d52"
  123. log.Debug("token111:" + token)
  124. //
  125. tokens := getAccessTokens(token)
  126. log.Debug("token222:" + tokens)
  127. //
  128. dataStr = strings.ReplaceAll(dataStr, `"`, `\"`)
  129. data := `{"access_token":"` + tokens + `","postjson":"` + dataStr + `"}`
  130. log.Debug("data:", zap.Any("data", data))
  131. dataTypes := ""
  132. if dataType == "0" {
  133. dataTypes = "JY_TO_DICT_fishBid2"
  134. } else {
  135. dataTypes = "JY_TO_DICT_WinBid2"
  136. }
  137. apiurl := "http://111.7.112.55:20110/oppf?method=" + dataTypes + "&appId=1068246&appKey=15c1da231a946f9615ee9d56138952a2&format=json&busiSerial=1&OPCODE=A888888&version=1.0&accessToken=" + tokens + "&timestamp=" + fmt.Sprint(time.Now().Format("20060102150405")) + "&sign=abc"
  138. log.Debug("apiurl:" + apiurl)
  139. errstr := ""
  140. response, err := HttpPostJson(apiurl, data)
  141. result := map[string]interface{}{}
  142. if err == nil {
  143. errstr = string(response)
  144. resMap := common.ObjToMap(string(response))
  145. result = *resMap
  146. log.Debug("---", zap.Any("响应信息: ", *resMap))
  147. if resMap != nil && *resMap != nil {
  148. respCode := common.ObjToString((*resMap)["respCode"])
  149. if respCode == "00000" {
  150. log.Debug("成功")
  151. } else {
  152. log.Debug("异常")
  153. }
  154. }
  155. } else {
  156. errstr = "响应失败 " + err.Error()
  157. log.Error("响应失败 ", zap.Error(err))
  158. }
  159. MgoSave.Save("dataPushLog", map[string]interface{}{
  160. "dataLenth": len(dataMap["dataList"]),
  161. "createtime": time.Now().Unix(),
  162. "appid": appid,
  163. "getDataType": getDataType,
  164. "dataType": dataType,
  165. "historyId": historyId,
  166. "dataSource": dataSource,
  167. "result": result,
  168. "errMsg": errstr,
  169. })
  170. this.ServeJson(map[string]interface{}{"errMsg": errstr})
  171. }
  172. }
  173. func ExcelParse(dataType string) []map[string]interface{} {
  174. filePath := "./web/res/push/push.xlsx"
  175. xlFile, _ := xlsx.OpenFile(filePath)
  176. fields := ""
  177. if dataType == "0" {
  178. fields = fields1
  179. } else {
  180. fields = fields2
  181. }
  182. //获取行数
  183. length := len(xlFile.Sheets[0].Rows)
  184. //开辟除表头外的行数的数组内存
  185. resourceArr := make([]map[string]interface{}, length-1)
  186. //遍历sheet
  187. for _, sheet := range xlFile.Sheets {
  188. //遍历每一行
  189. for rowIndex, row := range sheet.Rows {
  190. //跳过第一行表头信息
  191. if rowIndex == 0 {
  192. continue
  193. }
  194. dataMap := map[string]interface{}{}
  195. fieldsArr := strings.Split(fields, ",")
  196. for i := 0; i <= len(fieldsArr); i++ {
  197. dataMap[fieldsArr[i]] = row.Cells[i].String()
  198. }
  199. //遍历每一个单元
  200. // for cellIndex, cell := range row.Cells {
  201. // text := cell.String()
  202. // if text != "" {
  203. // //如果是每一行的第一个单元格
  204. // if cellIndex == 0 {
  205. // }
  206. // }
  207. // }
  208. resourceArr[rowIndex-1] = dataMap
  209. }
  210. }
  211. return resourceArr
  212. }
  213. func getAccessToken() string {
  214. res := redis.GetStr("datag", "yidongtoken1")
  215. if res != "" {
  216. return res
  217. } else {
  218. apiurls := "http://111.7.112.55:20200/aopoauth/oauth/token?app_id=1077846&app_key=c1165e4efc7b83831c4b76282f7f61c2&grant_type=client_credentials"
  219. data := make(url.Values)
  220. now := time.Now().Unix()
  221. bs, err := HttpPostForm(apiurls, map[string]string{
  222. "timestamp": fmt.Sprint(now),
  223. }, data)
  224. log.Debug("返回内容", zap.Error(err), zap.String("string(bs)", string(bs)))
  225. resMap := common.ObjToMap(string(bs))
  226. if *resMap != nil {
  227. log.Debug("调用token成功 ", zap.Any("resMap", *resMap))
  228. token := common.ObjToString((*resMap)["access_token"])
  229. if token != "" {
  230. log.Debug("成功")
  231. redis.Put("datag", "yidongtoken1", token, 60*60*24)
  232. return token
  233. } else {
  234. log.Debug("异常")
  235. return ""
  236. }
  237. } else {
  238. log.Debug("调用token失败")
  239. return ""
  240. }
  241. }
  242. }
  243. func getAccessTokens(token string) string {
  244. res := redis.GetStr("datag", "yidongtoken2")
  245. if res != "" {
  246. return res
  247. } else {
  248. apiurls := "http://111.7.112.55:20110/oppf?method=JY_TO_DICT_TOKEN&appId=1068246&appKey=15c1da231a946f9615ee9d56138952a2&format=json&busiSerial=1&OPCODE=A888888&version=1.0&accessToken=" + token + "&timestamp=" + fmt.Sprint(time.Now().Format("20060102150405")) + "&sign=abc"
  249. log.Debug("EPMtokenapiurl:" + apiurls)
  250. grant_type := "client_credentials"
  251. scope := "all"
  252. client_id := "jianyu_client"
  253. client_secret := "ab7b85f23f181936bb22078a2c83cbc3"
  254. data := make(map[string]string)
  255. data["grant_type"] = grant_type
  256. data["scope"] = scope
  257. data["client_id"] = client_id
  258. data["client_secret"] = client_secret
  259. bytestr, _ := json.Marshal(data)
  260. bs, err := HttpPostJson(apiurls, string(bytestr))
  261. if err != nil {
  262. log.Error("调用 token 失败", zap.Error(err))
  263. }
  264. resMap := common.ObjToMap(string(bs))
  265. if *resMap != nil {
  266. log.Debug("调用token成功 ", zap.Any("resMap", *resMap))
  267. resMaps := common.ObjToMap((*resMap)["result"])
  268. jsonMap := common.ObjToMap((*resMaps)["response"])
  269. redis.Put("datag", "yidongtoken2", common.ObjToString((*jsonMap)["access_token"]), 7200)
  270. return common.ObjToString((*jsonMap)["access_token"])
  271. } else {
  272. log.Debug("调用token失败")
  273. return ""
  274. }
  275. }
  276. }
  277. func ttCRM(dataSource, historyId string) string {
  278. query, errStr := map[string]interface{}{"historyId": historyId}, ""
  279. data, ok := MgoSave.Find(dataSource, query, nil, nil, false, -1, -1)
  280. if ok && data != nil && len(*data) > 0 {
  281. for _, v := range *data {
  282. id := common.ObjToString(v["infoId"])
  283. v["infoId"] = SE.EncodeString(id)
  284. bidtype := common.ObjToString(v["biddingType"])
  285. toptype := common.ObjToString(v["infoType"])
  286. subtype := common.ObjToString(v["infoTypeSegment"])
  287. bidway := common.ObjToString(v["isElectronic"])
  288. if bidtype != "" {
  289. v["biddingType"] = bidtypeMap[bidtype]
  290. } else {
  291. v["biddingType"] = subtypeMap[subtype]
  292. }
  293. if bidway == "电子投标" {
  294. v["isElectronic"] = "1"
  295. } else {
  296. v["isElectronic"] = "0"
  297. }
  298. if subtype == "其它" {
  299. if toptype == "预告" {
  300. v["infoType"] = "1"
  301. v["infoTypeSegment"] = "2"
  302. } else if toptype == "招标" {
  303. v["infoType"] = "2"
  304. v["infoTypeSegment"] = "6"
  305. } else if toptype == "结果" {
  306. v["infoType"] = "3"
  307. v["infoTypeSegment"] = "9"
  308. } else {
  309. v["infoType"] = "5"
  310. v["infoTypeSegment"] = "15"
  311. }
  312. } else {
  313. for t, s := range subMap[subtype] {
  314. v["infoType"] = t
  315. v["infoTypeSegment"] = s
  316. }
  317. }
  318. infos, ok := MgoBidding.FindOne(BiddingColl, map[string]interface{}{"_id": mongodb.StringTOBsonId(id)})
  319. if ok && infos != nil && len(*infos) > 0 {
  320. info := *infos
  321. if info["projectinfo"] != nil {
  322. projectInfo := common.ObjToMap(info["projectinfo"])
  323. if projectInfo != nil && len(*projectInfo) > 0 {
  324. if (*projectInfo)["attachments"] != nil {
  325. filesArr := []string{}
  326. fileUrl := "http://jy-datafile.oss-cn-beijing.aliyuncs.com/"
  327. if attachments, oks := (*projectInfo)["attachments"].(map[string]interface{}); oks {
  328. for _, v := range attachments {
  329. if atta, ok := v.(map[string]interface{}); ok {
  330. if atta["fid"] != nil && common.ObjToString(atta["fid"]) != "" {
  331. filesArr = append(filesArr, fileUrl+common.ObjToString(atta["fid"]))
  332. } else {
  333. if atta["org_url"] != nil {
  334. filesArr = append(filesArr, common.ObjToString(atta["org_url"]))
  335. }
  336. }
  337. }
  338. }
  339. }
  340. log.Debug("查询附件结果:" + id + ", 附件数量:" + fmt.Sprint(len(filesArr)))
  341. if len(filesArr) > 0 {
  342. v["infoFile"] = strings.Join(filesArr, ",")
  343. }
  344. }
  345. }
  346. }
  347. }
  348. delete(v, "_id")
  349. delete(v, "appid")
  350. delete(v, "createtime")
  351. delete(v, "historyId")
  352. errStr = postTT(dataSource, v)
  353. if strings.Contains(errStr, "失败") || strings.Contains(errStr, "错误") {
  354. return errStr
  355. }
  356. }
  357. }
  358. return errStr
  359. }
  360. func postTT(dataSource string, data map[string]interface{}) string {
  361. apiurl := "https://zlbss-crm.chinatowercom.cn/erp/syncdata/open/objdata/push"
  362. log.Debug("apiurl:" + apiurl)
  363. header := map[string]string{
  364. "token": "43f8e3c05a8edf8ee85bbc8755043b01",
  365. "tenantId": "40070007",
  366. "objectApiName": "object_HDngT__c",
  367. "dataCenterId": "649d4eb19ced6c0001587f26",
  368. "id": common.ObjToString(data["infoId"]),
  369. "version": "v1",
  370. "Content-Type": "application/json",
  371. "directSync": "true",
  372. "destObjectApiName": "object_HDngT__c",
  373. }
  374. dataMap := map[string]interface{}{"objAPIName": "object_HDngT__c", "masterFieldVal": data, "detailFieldVals": map[string]interface{}{}}
  375. dataJson, _ := json.Marshal(&dataMap)
  376. log.Debug("dataJson: " + string(dataJson))
  377. response, err := HttpPost(apiurl, header, bytes.NewReader(dataJson))
  378. errstr := ""
  379. if err == nil {
  380. errstr = string(response)
  381. resMap := common.ObjToMap(string(response))
  382. log.Debug("响应信息 ", zap.Any("resMap", *resMap))
  383. if resMap != nil && *resMap != nil {
  384. errMsg := common.ObjToString((*resMap)["errMsg"])
  385. if errMsg == "成功" {
  386. log.Debug("成功")
  387. }
  388. }
  389. } else {
  390. errstr = "响应失败 " + err.Error()
  391. log.Debug("响应失败 ", zap.Error(err))
  392. }
  393. return errstr
  394. }