push.go 13 KB

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