second_push.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "history"
  6. "log"
  7. "os"
  8. mongoutil "qfw/mongodb"
  9. qu "qfw/util"
  10. "qfw/util/mail"
  11. "strconv"
  12. "strings"
  13. "time"
  14. . "util"
  15. "github.com/tealeg/xlsx"
  16. "github.com/go-xweb/xweb"
  17. "gopkg.in/mgo.v2/bson"
  18. )
  19. var (
  20. xlsxArr []map[string]interface{}
  21. SE = qu.SimpleEncrypt{Key: "topJYBX2019"}
  22. )
  23. type SecondPush struct {
  24. *xweb.Action
  25. secondPush xweb.Mapper `xweb:"/service/secondpush/create"`
  26. getUserRule xweb.Mapper `xweb:"/service/secondpush/getrules"`
  27. saveSpushRule xweb.Mapper `xweb:"/service/secondpush/saverule"`
  28. secondPushTask xweb.Mapper `xweb:"/service/secondpush/dotask"`
  29. addSecondPushRule xweb.Mapper `xweb:"/service/secondpush/add"` //保存二次推送规则
  30. secondPushList xweb.Mapper `xweb:"/service/secondpush/list"`
  31. secondPushClone xweb.Mapper `xweb:"/service/secondPushClone/(.*)"`
  32. delSecondPushRule xweb.Mapper `xweb:"/service/customer/secondpush/delrule"` //删除历史任务规则
  33. }
  34. func (s *SecondPush) SecondPush() {
  35. defer qu.Catch()
  36. id := s.GetString("id")
  37. user := s.GetSession("user").(map[string]interface{})
  38. query := bson.M{}
  39. query["_id"] = mongoutil.StringTOBsonId(id)
  40. s_pushid := s.GetString("s_pushid")
  41. secondpush_id := ""
  42. if s_pushid != "" {
  43. secondpush_id = s_pushid
  44. hData, ok := Mgo.FindOne("second_push", bson.M{"_id": mongoutil.StringTOBsonId(secondpush_id)})
  45. if ok && hData != nil && len(*hData) > 0 {
  46. s.T["spush_rule"] = (*hData)["dep_rules"]
  47. (*hData)["_id"] = id
  48. s.T["data"] = *hData
  49. }
  50. } else {
  51. var user_name, user_customername, appid = "", "", ""
  52. userInfo, ok := Mgo.FindOne("euser", query)
  53. if ok && userInfo != nil && *userInfo != nil {
  54. user_name = qu.ObjToString((*userInfo)["s_name"])
  55. user_customername = qu.ObjToString((*userInfo)["s_customername"])
  56. appid = qu.ObjToString((*userInfo)["s_appid"])
  57. }
  58. secondpush_id = Mgo.Save("second_push", map[string]interface{}{
  59. "createtime": time.Now().Unix(),
  60. "updatetime": time.Now().Unix(),
  61. "user_id": id,
  62. "create_user": user["name"],
  63. "state": 0,
  64. "s_name": user_name,
  65. "s_customername": user_customername,
  66. "s_appid": appid,
  67. })
  68. s.Redirect("/service/secondpush/create?id=" + id + "&s_pushid=" + secondpush_id)
  69. return
  70. }
  71. s.T["spush_id"] = secondpush_id
  72. s.Render("private/second_push_create.html", &s.T)
  73. }
  74. func (s *SecondPush) GetUserRule() {
  75. customerId := s.GetString("c_id")
  76. query := bson.M{
  77. "s_userid": customerId,
  78. "b_delete": false,
  79. }
  80. data, _ := Mgo.Find("euserdepart", query, `{"i_createtime":-1}`, nil, false, 0, 0)
  81. if data != nil && len(*data) != 0 {
  82. res := []map[string]interface{}{}
  83. for _, m := range *data {
  84. history_rule_data := make(map[string]interface{})
  85. q := bson.M{
  86. "s_departid": mongoutil.BsonIdToSId(m["_id"]),
  87. "b_delete": false,
  88. }
  89. history_rule_data["department_isuse"] = m["i_isuse"]
  90. history_rule_data["depart_name"] = m["s_name"]
  91. depart_rule_data, _ := Mgo.Find("euserdepartrule", q, `{"i_createtime":-1}`, `{"s_name":1,"i_updatetime":1,"i_isuse":1,"_id":1}`, false, 0, 0)
  92. if depart_rule_data != nil && len(*depart_rule_data) != 0 {
  93. for _, j := range *depart_rule_data {
  94. j["_id"] = mongoutil.BsonIdToSId(j["_id"])
  95. }
  96. history_rule_data["department_rule"] = depart_rule_data
  97. history_rule_data["is_slected"] = true
  98. }
  99. res = append(res, history_rule_data)
  100. }
  101. s.ServeJson(map[string]interface{}{
  102. "status": "success",
  103. "data": res,
  104. })
  105. } else {
  106. s.ServeJson(map[string]interface{}{
  107. "status": "fail",
  108. "data": "",
  109. })
  110. }
  111. }
  112. //保存二次推送规则
  113. func (s *SecondPush) SaveSpushRule() {
  114. rules_id := s.GetString("rulesid")
  115. s_pushid := s.GetString("s_pushid")
  116. // userid := s.GetString("userid")
  117. if s_pushid != "" {
  118. user_history, _ := Mgo.FindOne("second_push", map[string]interface{}{
  119. "_id": mongoutil.StringTOBsonId(s_pushid),
  120. })
  121. if *user_history != nil && user_history != nil {
  122. if (*user_history)["dep_rules"] != nil {
  123. dep_rules := qu.ObjArrToMapArr((*user_history)["dep_rules"].([]interface{}))
  124. dep_new_rules := []map[string]interface{}{}
  125. ruleMap := map[string]bool{}
  126. if len(dep_rules) > 0 {
  127. for _, r := range dep_rules {
  128. dep_new_rules = append(dep_new_rules, r)
  129. ruleMap[mongoutil.BsonIdToSId(r["_id"])] = true
  130. }
  131. }
  132. rules_id_list := strings.Split(rules_id, ",")
  133. for _, rule := range rules_id_list {
  134. if !ruleMap[rule] {
  135. query := bson.M{
  136. "_id": mongoutil.StringTOBsonId(rule),
  137. "b_delete": false,
  138. }
  139. res, _ := Mgo.FindOne("euserdepartrule", query)
  140. if res != nil && len(*res) != 0 {
  141. //获取规则所属部门信息
  142. department_info, _ := Mgo.FindOne("euserdepart", map[string]interface{}{
  143. "_id": mongoutil.StringTOBsonId((*res)["s_departid"].(string)),
  144. "b_delete": false,
  145. })
  146. (*res)["is_new"] = false
  147. (*res)["s_depart_name"] = (*department_info)["s_name"]
  148. dep_new_rules = append(dep_new_rules, *res)
  149. }
  150. }
  151. }
  152. set := bson.M{
  153. "$set": bson.M{
  154. "dep_rules": dep_new_rules,
  155. // "tag_rules": usertags,
  156. "updatetime": time.Now().Unix(),
  157. },
  158. }
  159. ok := Mgo.Update("second_push", map[string]interface{}{
  160. "_id": mongoutil.StringTOBsonId(s_pushid),
  161. }, set, false, false)
  162. s.ServeJson(map[string]interface{}{
  163. "status": ok,
  164. })
  165. return
  166. } else {
  167. rules_id_list := strings.Split(rules_id, ",")
  168. dep_rules := []map[string]interface{}{}
  169. for _, rule := range rules_id_list {
  170. query := bson.M{
  171. "_id": mongoutil.StringTOBsonId(rule),
  172. "b_delete": false,
  173. }
  174. res, _ := Mgo.FindOne("euserdepartrule", query)
  175. if res != nil && *res != nil {
  176. //获取规则所属部门信息
  177. department_info, _ := Mgo.FindOne("euserdepart", map[string]interface{}{
  178. "_id": mongoutil.StringTOBsonId((*res)["s_departid"].(string)),
  179. "b_delete": false,
  180. })
  181. (*res)["is_new"] = false
  182. (*res)["s_depart_name"] = (*department_info)["s_name"]
  183. dep_rules = append(dep_rules, *res)
  184. }
  185. }
  186. set := bson.M{
  187. "$set": bson.M{
  188. "dep_rules": dep_rules,
  189. // "tag_rules": usertags,
  190. "updatetime": time.Now().Unix(),
  191. },
  192. }
  193. isupdata := Mgo.Update("second_push", map[string]interface{}{
  194. "_id": mongoutil.StringTOBsonId(s_pushid),
  195. }, set, false, false)
  196. s.ServeJson(map[string]interface{}{
  197. "status": isupdata,
  198. })
  199. return
  200. }
  201. }
  202. s.ServeJson(map[string]interface{}{
  203. "status": "fail",
  204. })
  205. }
  206. }
  207. func (s *SecondPush) SecondPushTask() {
  208. log.Println("开始二次推送任务...")
  209. s_pushid := s.GetString("s_pushid")
  210. if s_pushid != "" {
  211. customer, _ := Mgo.Find("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(s_pushid)}, nil, nil, false, -1, -1)
  212. if len(*customer) == 1 {
  213. c := (*customer)[0]
  214. customer_name := qu.ObjToString(c["s_name"]) //客户名称
  215. email := qu.ObjToString(c["sendMail"])
  216. starttime := qu.Int64All(c["starttime"])
  217. endtime := qu.Int64All(c["endtime"])
  218. projectIdSearch := qu.ObjToString(c["projectIdSearch"]) // 是否匹配项目id "0"-否 "1"-是
  219. updateMap := [][]map[string]interface{}{}
  220. if len(c) != 0 && c != nil {
  221. if c["dep_rules"] != nil && len(c["dep_rules"].([]interface{})) != 0 {
  222. idMap := map[bson.ObjectId]bool{}
  223. for _, m := range c["dep_rules"].([]interface{}) {
  224. log.Println("ruleid", mongoutil.BsonIdToSId(m.(map[string]interface{})["_id"]))
  225. q := bson.M{
  226. "ruleid": bson.M{"$regex": mongoutil.BsonIdToSId(m.(map[string]interface{})["_id"])},
  227. "bget": 1,
  228. "createtime": bson.M{
  229. "$gte": starttime,
  230. "$lte": endtime,
  231. },
  232. }
  233. d, _ := MgoCus.Find("usermail", q, nil, nil, false, 0, 0)
  234. if len(*d) > 0 && d != nil {
  235. for _, l := range *d {
  236. // 根据id进行去重
  237. if _, ok := idMap[l["_id"].(bson.ObjectId)]; !ok {
  238. idMap[l["_id"].(bson.ObjectId)] = true
  239. // 项目匹配为"1" 以及项目id为空时进行项目id匹配 同时更新数据库
  240. if projectIdSearch == "1" && (l["projectId"] == "" || l["projectId"] == nil) {
  241. updateMap = searchProjectId(l, updateMap)
  242. }
  243. xlsxArr = append(xlsxArr, l)
  244. }
  245. }
  246. if len(updateMap) > 0 {
  247. MgoCus.UpdateBulk("usermail", updateMap...)
  248. }
  249. }
  250. }
  251. } else {
  252. q := bson.M{
  253. "appid": c["s_appid"],
  254. "bget": 1,
  255. "createtime": bson.M{
  256. "$gte": starttime,
  257. "$lte": endtime,
  258. },
  259. }
  260. d, _ := MgoCus.Find("usermail", q, nil, nil, false, 0, 0)
  261. for _, l := range *d {
  262. // 项目匹配为"1" 以及项目id为空时进行项目id匹配 同时更新数据库
  263. if projectIdSearch == "1" && (l["projectId"] == "" || l["projectId"] == nil) {
  264. updateMap = searchProjectId(l, updateMap)
  265. }
  266. xlsxArr = append(xlsxArr, l)
  267. }
  268. if len(updateMap) > 0 {
  269. MgoCus.UpdateBulk("usermail", updateMap...)
  270. }
  271. }
  272. if len(xlsxArr) == 0 {
  273. log.Println("查询数据为空")
  274. } else {
  275. log.Println(len(xlsxArr))
  276. GetXlsxs(xlsxArr, customer_name, email, s_pushid, qu.ObjToString(c["s_appid"]))
  277. go UpdateHistoryState(2, s_pushid, len(xlsxArr))
  278. s.ServeJson(map[string]interface{}{
  279. "status": true,
  280. })
  281. xlsxArr = []map[string]interface{}{}
  282. }
  283. }
  284. } else {
  285. log.Println("初始化客户信息失败")
  286. }
  287. }
  288. }
  289. func searchProjectId(l map[string]interface{}, updateMap [][]map[string]interface{}) [][]map[string]interface{} {
  290. _projectid := ""
  291. projectId := getProjectId(qu.ObjToString(l["id"]))
  292. if projectId != "" {
  293. l["projectId"] = projectId
  294. _projectid = projectId
  295. } else {
  296. l["projectId"] = ""
  297. _projectid = ""
  298. }
  299. updateMap = append(updateMap, []map[string]interface{}{
  300. map[string]interface{}{
  301. "_id": l["_id"],
  302. },
  303. map[string]interface{}{
  304. "$set": map[string]interface{}{
  305. "projectId": _projectid,
  306. },
  307. },
  308. })
  309. if len(updateMap) > 500 {
  310. MgoCus.UpdateBulk("usermail", updateMap...)
  311. updateMap = [][]map[string]interface{}{}
  312. }
  313. return updateMap
  314. }
  315. func getProjectId(id string) string {
  316. query := `{"query": {"bool": {"must": [{"term": {"projectset.ids": "%s"}}],"must_not": [],"should": []}}}`
  317. querys := fmt.Sprintf(query, id)
  318. projectId := ""
  319. projectIdArr := []string{}
  320. data := Es.Get("projectset", "projectset", querys)
  321. if data != nil && *data != nil && len(*data) > 0 {
  322. for _, v := range *data {
  323. projectIdArr = append(projectIdArr, qu.ObjToString(v["_id"]))
  324. }
  325. projectId = strings.Join(projectIdArr, ",")
  326. log.Println("id", id, "projectId", projectId)
  327. }
  328. return projectId
  329. }
  330. //func (s *SecondPush) HistoryList() {
  331. // id := s.GetString("id")
  332. // if s.Method() == "POST" {
  333. // data, _ := Mgo.Find("second_push", map[string]interface{}{"user_id": id}, `{"updatetime":-1}`, nil, false, -1, -1)
  334. // s.ServeJson(map[string]interface{}{
  335. // "data": data,
  336. // })
  337. // } else {
  338. // s.T["id"] = id
  339. // s.Render("private/historylog_list.html", &s.T)
  340. // }
  341. //}
  342. func UpdateHistoryState(state int, s_pushid string, count int) {
  343. if state == 2 {
  344. Mgo.Update("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(s_pushid)}, map[string]interface{}{
  345. "$set": map[string]interface{}{
  346. "state": state,
  347. "result_count": count,
  348. "finishtime": time.Now().Unix(),
  349. },
  350. }, false, false)
  351. return
  352. }
  353. Mgo.Update("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(s_pushid)}, map[string]interface{}{
  354. "$set": map[string]interface{}{
  355. "state": state,
  356. },
  357. }, false, false)
  358. }
  359. func (s *SecondPush) HistoryClone(hid string) {
  360. data, ok := Mgo.FindOne("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(hid)})
  361. if ok && data != nil && *data != nil {
  362. (*data)["state"] = 0
  363. (*data)["result_count"] = nil
  364. (*data)["createtime"] = time.Now().Unix()
  365. (*data)["updatetime"] = time.Now().Unix()
  366. Mgo.Save("second_push", *data)
  367. }
  368. }
  369. func (s *SecondPush) HistoryRuleEdit() {
  370. hid := s.GetString("hid")
  371. rid := s.GetString("rid")
  372. if s.Method() == "POST" {
  373. data := GetPostForm(s.Request)
  374. o_rules := []map[string]interface{}{}
  375. o_rulesStr := data["o_rules"].(string)
  376. json.Unmarshal([]byte(o_rulesStr), &o_rules)
  377. data["o_rules"] = o_rules
  378. data["_id"] = mongoutil.StringTOBsonId(rid)
  379. if qu.IntAll(data["i_esquerytype"]) == 1 { //自动生成es
  380. data["s_esquery"] = Utiltags(data)
  381. }
  382. datas, ok := Mgo.FindOne("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(hid)})
  383. if ok && datas != nil && *datas != nil {
  384. dep_rules := qu.ObjArrToMapArr((*datas)["dep_rules"].([]interface{}))
  385. for k, v := range dep_rules {
  386. if rid == mongoutil.BsonIdToSId(v["_id"]) {
  387. dep_rules[k] = data
  388. }
  389. }
  390. rep := Mgo.Update("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(hid)}, map[string]interface{}{"$set": map[string]interface{}{"dep_rules": dep_rules}}, false, false)
  391. s.ServeJson(map[string]interface{}{
  392. "rep": rep,
  393. "s_esquery": data["s_esquery"],
  394. })
  395. }
  396. } else {
  397. id := s.GetString("id")
  398. data, ok := Mgo.FindOne("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(hid)})
  399. if ok && data != nil && *data != nil {
  400. dep_rules := qu.ObjArrToMapArr((*data)["dep_rules"].([]interface{}))
  401. for _, v := range dep_rules {
  402. if rid == mongoutil.BsonIdToSId(v["_id"]) {
  403. s.T["data"] = v
  404. }
  405. }
  406. }
  407. s.T["did"] = id //部门id
  408. s.T["cid"] = id //客户id
  409. s.T["ids"] = id
  410. s.T["history_id"] = hid
  411. s.T["rid"] = rid
  412. s.T["province"] = Province
  413. s.T["city"] = ProvinceCitys
  414. s.T["district"] = CityDistricts
  415. s.T["topTypeArr"] = TopTypeArr
  416. s.T["subTypeArr"] = SubTypeArr
  417. s.T["matchTypeMap"] = MatchTypeMap
  418. s.T["matchTypeMap2"] = MatchTypeMap2
  419. s.T["existField"] = ExistFiled
  420. s.T["buyerClass"] = BuyerClass
  421. s.T["scopeClass"] = ScopeClassMap
  422. s.Render("private/history_rule_edit.html", &s.T)
  423. }
  424. }
  425. func GetXlsxs(mMap []map[string]interface{}, fn, email, id, appid string) {
  426. if id != "" {
  427. isfile := false
  428. isfilehref := false
  429. if history.FileTextAppidMap[appid] {
  430. isfile = true
  431. }
  432. if history.NeedFilehrefAppidMap[appid] {
  433. isfilehref = true
  434. }
  435. query := bson.M{
  436. "_id": bson.ObjectIdHex(id),
  437. }
  438. data, ok := Mgo.FindOne("second_push", query)
  439. if ok && (*data) != nil && len(*data) > 0 {
  440. dataType := qu.IntAll((*data)["i_extfieldstype"])
  441. xf, err := xlsx.OpenFile("web/res/fields.xlsx")
  442. if err != nil {
  443. log.Println("fields file not foud", err.Error())
  444. }
  445. newFile := xlsx.NewFile()
  446. style := xlsx.NewStyle()
  447. style.Font.Size = 12
  448. style.Font.Bold = true
  449. style.Alignment.Vertical = "center"
  450. style.Alignment.Horizontal = "center"
  451. if dataType == 1 {
  452. sh := xf.Sheets[0]
  453. sheetIndex := map[string]int{}
  454. for _, v := range mMap {
  455. // 拿到所选字段包的sheet页 如果item存在则追加 不存在则创建新sheet页
  456. var sheet *xlsx.Sheet
  457. if v["item"].(string) == "" {
  458. v["item"] = "其他方式"
  459. }
  460. items := strings.Split(v["item"].(string), ",")
  461. for _, i := range items {
  462. if len(items) > 1 && v["itemdist"] != nil {
  463. v["matchkey"] = v["itemdist"].(map[string]interface{})[i]
  464. v["item"] = i
  465. }
  466. if _, ok := newFile.Sheet[v["item"].(string)]; ok {
  467. sheet = newFile.Sheet[v["item"].(string)]
  468. sheetIndex[v["item"].(string)] += 1
  469. } else {
  470. sheet, err = newFile.AppendSheet(*sh, v["item"].(string))
  471. sheetIndex[v["item"].(string)] = 1
  472. }
  473. row := sheet.AddRow()
  474. row.AddCell().SetInt(sheetIndex[v["item"].(string)])
  475. row.AddCell().SetValue(v["matchkey"])
  476. row.AddCell().SetValue(v["area"])
  477. row.AddCell().SetValue(v["city"])
  478. row.AddCell().SetValue(v["title"])
  479. row.AddCell().SetValue(v["subtype"])
  480. if v["publishtime"] != nil {
  481. row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02"))
  482. } else {
  483. row.AddCell()
  484. }
  485. row.AddCell().SetValue(v["buyer"])
  486. row.AddCell().SetValue(v["s_winner"])
  487. if v["bidamount"] != nil {
  488. row.AddCell().SetFloat(qu.Float64All(v["bidamount"]))
  489. } else {
  490. row.AddCell()
  491. }
  492. row.AddCell().SetValue(v["projectname"])
  493. row.AddCell().SetValue(v["detail"])
  494. row.AddCell().SetValue(v["jybxhref"])
  495. ids := SE.EncodeString(qu.ObjToString(v["id"]))
  496. row.AddCell().SetValue(ids)
  497. }
  498. }
  499. //xf.Sheets = xf.Sheets[0:1]
  500. } else if dataType == 2 {
  501. sh := xf.Sheets[1]
  502. if isfile {
  503. cell := sh.Rows[0].AddCell()
  504. cell.SetValue("附件")
  505. cell.SetStyle(style)
  506. }
  507. if isfilehref {
  508. cell := sh.Rows[0].AddCell()
  509. cell.SetValue("附件链接")
  510. cell.SetStyle(style)
  511. }
  512. for _, v := range mMap {
  513. if v["item"].(string) == "" {
  514. v["item"] = "其他方式"
  515. }
  516. items := strings.Split(v["item"].(string), ",")
  517. for _, i := range items {
  518. if len(items) > 1 && v["itemdist"] != nil {
  519. v["matchkey"] = v["itemdist"].(map[string]interface{})[i]
  520. v["item"] = i
  521. }
  522. var sheet *xlsx.Sheet
  523. if _, ok := newFile.Sheet[v["item"].(string)]; ok {
  524. sheet = newFile.Sheet[v["item"].(string)]
  525. } else {
  526. sheet, err = newFile.AppendSheet(*sh, v["item"].(string))
  527. }
  528. row := sheet.AddRow()
  529. // row.AddCell().SetInt(i + 1)
  530. row.AddCell().SetValue(v["matchkey"])
  531. row.AddCell().SetValue(v["area"])
  532. row.AddCell().SetValue(v["city"])
  533. row.AddCell().SetValue(v["title"])
  534. row.AddCell().SetValue(v["subtype"])
  535. row.AddCell().SetValue(v["detail"])
  536. if v["publishtime"] != nil {
  537. row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02"))
  538. } else {
  539. row.AddCell()
  540. }
  541. row.AddCell().SetValue(v["href"])
  542. row.AddCell().SetValue(v["jybxhref"])
  543. row.AddCell().SetValue(v["projectname"])
  544. row.AddCell().SetValue(v["projectcode"])
  545. row.AddCell().SetValue(v["projectscope"])
  546. if v["budget"] != nil {
  547. row.AddCell().SetFloat(qu.Float64All(v["budget"]))
  548. } else {
  549. row.AddCell()
  550. }
  551. if v["bidamount"] != nil {
  552. row.AddCell().SetFloat(qu.Float64All(v["bidamount"]))
  553. } else {
  554. row.AddCell()
  555. }
  556. if v["bidopentime"] != nil {
  557. row.AddCell().SetValue(time.Unix(qu.Int64All(v["bidopentime"]), 0).Format("2006-01-02"))
  558. } else {
  559. row.AddCell()
  560. }
  561. row.AddCell().SetValue(v["buyer"])
  562. row.AddCell().SetValue(v["buyerperson"])
  563. row.AddCell().SetValue(v["buyertel"])
  564. row.AddCell().SetValue(v["agency"])
  565. row.AddCell().SetValue(v["s_winner"])
  566. row.AddCell().SetValue(v["winnerperson"])
  567. row.AddCell().SetValue(v["winnertel"])
  568. row.AddCell().SetValue(v["legal_person"])
  569. row.AddCell().SetValue(v["company_phone"])
  570. row.AddCell().SetValue(v["company_email"])
  571. ids := SE.EncodeString(qu.ObjToString(v["id"]))
  572. row.AddCell().SetValue(ids)
  573. if isfile {
  574. row.AddCell().SetValue(v["filetext"])
  575. }
  576. if isfilehref {
  577. if v["filehref"] != nil {
  578. if filelist, ok := v["filehref"].([]interface{}); ok {
  579. index := 0
  580. for _, url := range filelist {
  581. if urlMap, ok := url.(map[string]interface{}); ok {
  582. if urlMap["url"] != nil {
  583. style1 := xlsx.NewStyle()
  584. style1.Font.Name = "宋体"
  585. style1.Font.Size = 12
  586. style1.Font.Bold = false
  587. if history.FilterFilehrefAppidMap[appid] {
  588. if strings.Contains(urlMap["url"].(string), "jy-datafile.oss-cn-beijing.aliyuncs.com") {
  589. cell := row.AddCell()
  590. cell.SetStyle(style1)
  591. cell.SetValue(urlMap["url"])
  592. index++
  593. }
  594. } else {
  595. cell := row.AddCell()
  596. cell.SetStyle(style1)
  597. cell.SetValue(urlMap["url"])
  598. index++
  599. }
  600. }
  601. }
  602. }
  603. }
  604. }
  605. }
  606. }
  607. }
  608. //xf.Sheets = xf.Sheets[1:2]
  609. } else if dataType == 3 {
  610. sh := xf.Sheets[2]
  611. for _, v := range mMap {
  612. if v["item"].(string) == "" {
  613. v["item"] = "其他方式"
  614. }
  615. items := strings.Split(v["item"].(string), ",")
  616. for _, i := range items {
  617. if len(items) > 1 && v["itemdist"] != nil {
  618. v["matchkey"] = v["itemdist"].(map[string]interface{})[i]
  619. v["item"] = i
  620. }
  621. var sheet *xlsx.Sheet
  622. if _, ok := newFile.Sheet[v["item"].(string)]; ok {
  623. sheet = newFile.Sheet[v["item"].(string)]
  624. } else {
  625. sheet, err = newFile.AppendSheet(*sh, v["item"].(string))
  626. }
  627. row := sheet.AddRow()
  628. // row.AddCell().SetInt(i + 1)
  629. row.AddCell().SetValue(v["departname"])
  630. row.AddCell().SetValue(v["rulename"])
  631. row.AddCell().SetValue(v["matchkey"])
  632. row.AddCell().SetValue(v["toptype"])
  633. row.AddCell().SetValue(v["area"])
  634. row.AddCell().SetValue(v["city"])
  635. row.AddCell().SetValue(v["district"])
  636. row.AddCell().SetValue(v["title"])
  637. row.AddCell().SetValue(v["detail"])
  638. if v["publishtime"] != nil {
  639. row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02"))
  640. } else {
  641. row.AddCell()
  642. }
  643. row.AddCell().SetValue(v["href"])
  644. if v["bidamount"] != nil {
  645. row.AddCell().SetFloat(qu.Float64All(v["bidamount"]))
  646. } else {
  647. row.AddCell()
  648. }
  649. row.AddCell().SetValue(v["buyer"])
  650. row.AddCell().SetValue(v["buyerclass"])
  651. row.AddCell().SetValue(v["buyerperson"])
  652. row.AddCell().SetValue(v["buyertel"])
  653. row.AddCell().SetValue(v["s_winner"])
  654. row.AddCell().SetValue(v["legal_person"])
  655. row.AddCell().SetValue(v["company_phone"])
  656. row.AddCell().SetValue(v["company_address"])
  657. row.AddCell().SetValue(v["rank"])
  658. row.AddCell().SetValue(v["purchasing"])
  659. row.AddCell().SetValue(v["capital"])
  660. row.AddCell().SetValue(v["establish_date"])
  661. row.AddCell().SetValue(v["business_scope"])
  662. row.AddCell().SetValue(v["stock_name"])
  663. row.AddCell().SetValue(v["buyer_credit_no"])
  664. row.AddCell().SetValue(v["winner_credit_no"])
  665. ids := SE.EncodeString(qu.ObjToString(v["id"]))
  666. row.AddCell().SetValue(ids)
  667. }
  668. }
  669. //xf.Sheets = xf.Sheets[2:3]
  670. } else if dataType == 4 {
  671. sh := xf.Sheets[3]
  672. for _, v := range mMap {
  673. if v["item"].(string) == "" {
  674. v["item"] = "其他方式"
  675. }
  676. items := strings.Split(v["item"].(string), ",")
  677. for _, i := range items {
  678. if len(items) > 1 && v["itemdist"] != nil {
  679. v["matchkey"] = v["itemdist"].(map[string]interface{})[i]
  680. v["item"] = i
  681. }
  682. var sheet *xlsx.Sheet
  683. if _, ok := newFile.Sheet[v["item"].(string)]; ok {
  684. sheet = newFile.Sheet[v["item"].(string)]
  685. } else {
  686. sheet, err = newFile.AppendSheet(*sh, v["item"].(string))
  687. }
  688. row := sheet.AddRow()
  689. // row.AddCell().SetInt(i + 1)
  690. row.AddCell().SetValue(v["departname"])
  691. row.AddCell().SetValue(v["rulename"])
  692. row.AddCell().SetValue(v["matchkey"])
  693. row.AddCell().SetValue(v["area"])
  694. row.AddCell().SetValue(v["city"])
  695. row.AddCell().SetValue(v["title"])
  696. row.AddCell().SetValue(v["subtype"])
  697. row.AddCell().SetValue(v["detail"])
  698. if v["publishtime"] != nil {
  699. row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02"))
  700. } else {
  701. row.AddCell()
  702. }
  703. row.AddCell().SetValue(v["href"])
  704. row.AddCell().SetValue(v["jybxhref"])
  705. row.AddCell().SetValue(v["projectname"])
  706. row.AddCell().SetValue(v["projectcode"])
  707. row.AddCell().SetValue(v["projectscope"])
  708. if v["budget"] != nil {
  709. row.AddCell().SetFloat(qu.Float64All(v["budget"]))
  710. } else {
  711. row.AddCell()
  712. }
  713. if v["bidamount"] != nil {
  714. row.AddCell().SetFloat(qu.Float64All(v["bidamount"]))
  715. } else {
  716. row.AddCell()
  717. }
  718. if v["bidopentime"] != nil {
  719. row.AddCell().SetValue(time.Unix(qu.Int64All(v["bidopentime"]), 0).Format("2006-01-02"))
  720. } else {
  721. row.AddCell()
  722. }
  723. row.AddCell().SetValue(v["buyer"])
  724. row.AddCell().SetValue(v["buyerperson"])
  725. row.AddCell().SetValue(v["buyertel"])
  726. row.AddCell().SetValue(v["agency"])
  727. row.AddCell().SetValue(v["s_winner"])
  728. row.AddCell().SetValue(v["winnerperson"])
  729. row.AddCell().SetValue(v["winnertel"])
  730. row.AddCell().SetValue(v["legal_person"])
  731. row.AddCell().SetValue(v["company_phone"])
  732. row.AddCell().SetValue(v["company_email"])
  733. ids := SE.EncodeString(qu.ObjToString(v["id"]))
  734. row.AddCell().SetValue(ids)
  735. }
  736. }
  737. //xf.Sheets = xf.Sheets[3:4]
  738. } else if dataType == 5 {
  739. sh := xf.Sheets[4]
  740. for _, v := range mMap {
  741. if v["item"].(string) == "" {
  742. v["item"] = "其他方式"
  743. }
  744. items := strings.Split(v["item"].(string), ",")
  745. for _, i := range items {
  746. if len(items) > 1 && v["itemdist"] != nil {
  747. v["matchkey"] = v["itemdist"].(map[string]interface{})[i]
  748. v["item"] = i
  749. }
  750. var sheet *xlsx.Sheet
  751. if _, ok := newFile.Sheet[v["item"].(string)]; ok {
  752. sheet = newFile.Sheet[v["item"].(string)]
  753. } else {
  754. sheet, err = newFile.AppendSheet(*sh, v["item"].(string))
  755. }
  756. row := sheet.AddRow()
  757. // row.AddCell().SetInt(i + 1)
  758. row.AddCell().SetValue(v["matchkey"]) //信息匹配词
  759. row.AddCell().SetValue(v["city"]) //城市
  760. row.AddCell().SetValue(v["district"]) //县区
  761. row.AddCell().SetValue(v["projectname"]) //项目名称
  762. row.AddCell().SetValue(v["buyer"]) //采购单位
  763. row.AddCell().SetValue(v["buyer_type"]) //采购单位类别---私有标签tagname
  764. if v["bidamount"] != nil { //中标金额
  765. row.AddCell().SetFloat(qu.Float64All(v["bidamount"]))
  766. } else {
  767. row.AddCell()
  768. }
  769. if v["budget"] != nil { //预算
  770. row.AddCell().SetFloat(qu.Float64All(v["budget"]))
  771. } else {
  772. row.AddCell()
  773. }
  774. if v["publishtime"] != nil { //公告发布时间
  775. row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02"))
  776. } else {
  777. row.AddCell()
  778. }
  779. if v["bidopentime"] != nil { //开标日期
  780. row.AddCell().SetValue(time.Unix(qu.Int64All(v["bidopentime"]), 0).Format("2006-01-02"))
  781. } else {
  782. row.AddCell()
  783. }
  784. row.AddCell().SetValue(v["s_winner"]) //中标单位
  785. row.AddCell().SetValue(v["winner_type"]) //中标模式--移动、电信、联通
  786. row.AddCell().SetValue(v["title"]) //公告标题
  787. row.AddCell().SetValue(v["subtype"]) //公告类别
  788. row.AddCell().SetValue(v["area"]) //省份
  789. row.AddCell().SetValue(v["projectcode"]) //项目编号
  790. row.AddCell().SetValue(v["projectscope"]) //项目范围
  791. row.AddCell().SetValue(v["agency"]) //招标代理机构
  792. row.AddCell().SetValue(v["detail"]) //公告内容
  793. row.AddCell().SetValue(v["href"]) //公告地址
  794. row.AddCell().SetValue(v["buyerperson"]) //采购单位联系人
  795. row.AddCell().SetValue(v["buyertel"]) //采购单位联系电话
  796. row.AddCell().SetValue(v["winnerperson"]) //中标单位联系人
  797. row.AddCell().SetValue(v["winnertel"]) //中标单位联系电话
  798. row.AddCell().SetValue(v["legal_person"]) //中标企业联系人
  799. row.AddCell().SetValue(v["company_phone"]) //中标企业联系电话
  800. row.AddCell().SetValue(v["company_email"]) //中标企业邮箱
  801. ids := SE.EncodeString(qu.ObjToString(v["id"])) //唯一标识
  802. row.AddCell().SetValue(ids)
  803. row.AddCell().SetValue(SE.EncodeString(qu.ObjToString(v["projectId"]))) //项目标识
  804. row.AddCell().SetValue(v["jybxhref"])
  805. //row.AddCell().SetValue(v["projectcode"])
  806. }
  807. }
  808. //xf.Sheets = xf.Sheets[4:5]
  809. }
  810. //xf.Sheets[0].Name = "详细数据"
  811. //生文件
  812. //t := strconv.FormatInt(time.Now().Unix(), 10)
  813. t := time.Now().Format("20060102")
  814. dir := "./web/res/xlsx/" + t + "/"
  815. if b, _ := history.PathExists(dir); !b {
  816. err1 := os.MkdirAll(dir, os.ModePerm)
  817. if err1 != nil {
  818. log.Println("mkdir err", dir)
  819. }
  820. }
  821. fname := fmt.Sprintf("%s_%s_%s.xlsx", fn, t, qu.GetRandom(4))
  822. log.Println("fname", fname)
  823. err = newFile.Save(dir + fname)
  824. if err != nil {
  825. log.Println("xls error", err, fname)
  826. } else {
  827. for i := 0; i < len(history.Gmails); i++ {
  828. gmail := history.Gmails[i]
  829. status := mail.GSendMail_q("剑鱼标讯", email, "", "", fn, "", dir+fname, fname, gmail)
  830. if status {
  831. log.Println("send mail success", fname, email)
  832. break
  833. }
  834. }
  835. }
  836. }
  837. }
  838. }
  839. func (s *SecondPush) AddSecondPushRule() {
  840. defer qu.Catch()
  841. if s.Method() == "POST" {
  842. his_id := s.GetString("s_pushid")
  843. if his_id != "" {
  844. i_updatetime, _ := strconv.ParseInt(s.GetString("i_updatetime"), 10, 64)
  845. i_extfieldstype, _ := strconv.Atoi(s.GetString("i_extfieldstype"))
  846. set := bson.M{
  847. "$set": bson.M{
  848. "i_updatetime": i_updatetime,
  849. "sendMail": s.GetString("sendMail"),
  850. "i_extfieldstype": i_extfieldstype,
  851. "starttime": s.GetString("starttime"),
  852. "endtime": s.GetString("endtime"),
  853. "projectIdSearch": s.GetString("projectIdSearch"),
  854. },
  855. }
  856. isupdata := Mgo.UpdateById("second_push", mongoutil.StringTOBsonId(his_id), set)
  857. if isupdata {
  858. s.ServeJson(map[string]interface{}{
  859. "history_id": his_id,
  860. "rep": true,
  861. })
  862. } else {
  863. s.ServeJson(map[string]interface{}{
  864. "history_id": "",
  865. "rep": false,
  866. })
  867. }
  868. }
  869. }
  870. }
  871. func (s *SecondPush) SecondPushList() {
  872. id := s.GetString("id")
  873. if s.Method() == "POST" {
  874. data, _ := Mgo.Find("second_push", map[string]interface{}{"user_id": id}, `{"updatetime":-1}`, nil, false, -1, -1)
  875. s.ServeJson(map[string]interface{}{
  876. "data": data,
  877. })
  878. }
  879. }
  880. func (s *SecondPush) SecondPushClone(hid string) {
  881. data, ok := Mgo.FindOne("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(hid)})
  882. user := s.GetSession("user").(map[string]interface{})
  883. if ok && data != nil && *data != nil {
  884. (*data)["state"] = 0
  885. (*data)["result_count"] = nil
  886. (*data)["createtime"] = time.Now().Unix()
  887. (*data)["updatetime"] = time.Now().Unix()
  888. (*data)["create_user"] = user["name"]
  889. delete((*data), "finishtime")
  890. Mgo.Save("second_push", *data)
  891. }
  892. }
  893. func (s *SecondPush) DelSecondPushRule() {
  894. id := s.GetString("id")
  895. hid := s.GetString("s_pushid")
  896. if hid != "" {
  897. data, ok := Mgo.FindOne("second_push", bson.M{"_id": mongoutil.StringTOBsonId(hid)})
  898. if ok && data != nil && *data != nil {
  899. if (*data)["dep_rules"] != nil {
  900. dep_rules := qu.ObjArrToMapArr((*data)["dep_rules"].([]interface{}))
  901. ruleMap := map[string]bool{id: true}
  902. ruleArr := []map[string]interface{}{}
  903. for _, m := range dep_rules {
  904. if !ruleMap[mongoutil.BsonIdToSId(m["_id"])] {
  905. ruleArr = append(ruleArr, m)
  906. }
  907. }
  908. ok := Mgo.Update("second_push", bson.M{"_id": mongoutil.StringTOBsonId(hid)}, bson.M{"$set": bson.M{"dep_rules": ruleArr}}, false, false)
  909. s.ServeJson(map[string]interface{}{
  910. "success": ok,
  911. })
  912. }
  913. }
  914. }
  915. }