second_push.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. package service
  2. import (
  3. "encoding/json"
  4. "history"
  5. "log"
  6. "os"
  7. mongoutil "qfw/mongodb"
  8. qu "qfw/util"
  9. "qfw/util/mail"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. "time"
  14. . "util"
  15. "github.com/tealeg/xlsx"
  16. "github.com/go-xweb/xweb"
  17. "gopkg.in/mgo.v2/bson"
  18. )
  19. var projectIdMap = sync.Map{}
  20. var xlsxArr []map[string]interface{}
  21. type SecondPush struct {
  22. *xweb.Action
  23. secondPush xweb.Mapper `xweb:"/service/secondpush/create"`
  24. getUserRule xweb.Mapper `xweb:"/service/secondpush/getrules"`
  25. saveSpushRule xweb.Mapper `xweb:"/service/secondpush/saverule"`
  26. secondPushTask xweb.Mapper `xweb:"/service/secondpush/dotask"`
  27. addSecondPushRule xweb.Mapper `xweb:"/service/secondpush/add"` //保存二次推送规则
  28. secondPushList xweb.Mapper `xweb:"/service/secondpush/list"`
  29. secondPushClone xweb.Mapper `xweb:"/service/secondPushClone/(.*)"`
  30. delSecondPushRule xweb.Mapper `xweb:"/service/customer/secondpush/delrule"` //删除历史任务规则
  31. }
  32. func (s *SecondPush) SecondPush() {
  33. defer qu.Catch()
  34. id := s.GetString("id")
  35. user := s.GetSession("user").(map[string]interface{})
  36. query := bson.M{}
  37. query["_id"] = mongoutil.StringTOBsonId(id)
  38. s_pushid := s.GetString("s_pushid")
  39. secondpush_id := ""
  40. if s_pushid != "" {
  41. secondpush_id = s_pushid
  42. hData, ok := Mgo.FindOne("second_push", bson.M{"_id": mongoutil.StringTOBsonId(secondpush_id)})
  43. if ok && hData != nil && len(*hData) > 0 {
  44. s.T["spush_rule"] = (*hData)["dep_rules"]
  45. (*hData)["_id"] = id
  46. s.T["data"] = *hData
  47. }
  48. } else {
  49. var user_name, user_customername, appid = "", "", ""
  50. userInfo, ok := Mgo.FindOne("euser", query)
  51. if ok && userInfo != nil && *userInfo != nil {
  52. user_name = qu.ObjToString((*userInfo)["s_name"])
  53. user_customername = qu.ObjToString((*userInfo)["s_customername"])
  54. appid = qu.ObjToString((*userInfo)["s_appid"])
  55. }
  56. secondpush_id = Mgo.Save("second_push", map[string]interface{}{
  57. "createtime": time.Now().Unix(),
  58. "updatetime": time.Now().Unix(),
  59. "user_id": id,
  60. "create_user": user["name"],
  61. "state": 0,
  62. "s_name": user_name,
  63. "s_customername": user_customername,
  64. "s_appid": appid,
  65. })
  66. s.Redirect("/service/secondpush/create?id=" + id + "&s_pushid=" + secondpush_id)
  67. return
  68. }
  69. s.T["spush_id"] = secondpush_id
  70. s.Render("private/second_push_create.html", &s.T)
  71. }
  72. func (s *SecondPush) GetUserRule() {
  73. customerId := s.GetString("c_id")
  74. query := bson.M{
  75. "s_userid": customerId,
  76. "b_delete": false,
  77. }
  78. data, _ := Mgo.Find("euserdepart", query, `{"i_createtime":-1}`, nil, false, 0, 0)
  79. if data != nil && len(*data) != 0 {
  80. res := []map[string]interface{}{}
  81. for _, m := range *data {
  82. history_rule_data := make(map[string]interface{})
  83. q := bson.M{
  84. "s_departid": mongoutil.BsonIdToSId(m["_id"]),
  85. "b_delete": false,
  86. }
  87. history_rule_data["department_isuse"] = m["i_isuse"]
  88. history_rule_data["depart_name"] = m["s_name"]
  89. depart_rule_data, _ := Mgo.Find("euserdepartrule", q, `{"i_createtime":-1}`, `{"s_name":1,"i_updatetime":1,"i_isuse":1,"_id":1}`, false, 0, 0)
  90. if depart_rule_data != nil && len(*depart_rule_data) != 0 {
  91. for _, j := range *depart_rule_data {
  92. j["_id"] = mongoutil.BsonIdToSId(j["_id"])
  93. }
  94. history_rule_data["department_rule"] = depart_rule_data
  95. history_rule_data["is_slected"] = true
  96. }
  97. res = append(res, history_rule_data)
  98. }
  99. s.ServeJson(map[string]interface{}{
  100. "status": "success",
  101. "data": res,
  102. })
  103. } else {
  104. s.ServeJson(map[string]interface{}{
  105. "status": "fail",
  106. "data": "",
  107. })
  108. }
  109. }
  110. //保存二次推送规则
  111. func (s *SecondPush) SaveSpushRule() {
  112. rules_id := s.GetString("rulesid")
  113. s_pushid := s.GetString("s_pushid")
  114. // userid := s.GetString("userid")
  115. if s_pushid != "" {
  116. user_history, _ := Mgo.FindOne("second_push", map[string]interface{}{
  117. "_id": mongoutil.StringTOBsonId(s_pushid),
  118. })
  119. if *user_history != nil && user_history != nil {
  120. if (*user_history)["dep_rules"] != nil {
  121. dep_rules := qu.ObjArrToMapArr((*user_history)["dep_rules"].([]interface{}))
  122. dep_new_rules := []map[string]interface{}{}
  123. ruleMap := map[string]bool{}
  124. if len(dep_rules) > 0 {
  125. for _, r := range dep_rules {
  126. dep_new_rules = append(dep_new_rules, r)
  127. ruleMap[mongoutil.BsonIdToSId(r["_id"])] = true
  128. }
  129. }
  130. rules_id_list := strings.Split(rules_id, ",")
  131. for _, rule := range rules_id_list {
  132. if !ruleMap[rule] {
  133. query := bson.M{
  134. "_id": mongoutil.StringTOBsonId(rule),
  135. "b_delete": false,
  136. }
  137. res, _ := Mgo.FindOne("euserdepartrule", query)
  138. if res != nil && len(*res) != 0 {
  139. //获取规则所属部门信息
  140. department_info, _ := Mgo.FindOne("euserdepart", map[string]interface{}{
  141. "_id": mongoutil.StringTOBsonId((*res)["s_departid"].(string)),
  142. "b_delete": false,
  143. })
  144. (*res)["is_new"] = false
  145. (*res)["s_depart_name"] = (*department_info)["s_name"]
  146. dep_new_rules = append(dep_new_rules, *res)
  147. }
  148. }
  149. }
  150. set := bson.M{
  151. "$set": bson.M{
  152. "dep_rules": dep_new_rules,
  153. // "tag_rules": usertags,
  154. "updatetime": time.Now().Unix(),
  155. },
  156. }
  157. ok := Mgo.Update("second_push", map[string]interface{}{
  158. "_id": mongoutil.StringTOBsonId(s_pushid),
  159. }, set, false, false)
  160. s.ServeJson(map[string]interface{}{
  161. "status": ok,
  162. })
  163. return
  164. } else {
  165. rules_id_list := strings.Split(rules_id, ",")
  166. dep_rules := []map[string]interface{}{}
  167. for _, rule := range rules_id_list {
  168. query := bson.M{
  169. "_id": mongoutil.StringTOBsonId(rule),
  170. "b_delete": false,
  171. }
  172. res, _ := Mgo.FindOne("euserdepartrule", query)
  173. if res != nil && *res != nil {
  174. //获取规则所属部门信息
  175. department_info, _ := Mgo.FindOne("euserdepart", map[string]interface{}{
  176. "_id": mongoutil.StringTOBsonId((*res)["s_departid"].(string)),
  177. "b_delete": false,
  178. })
  179. (*res)["is_new"] = false
  180. (*res)["s_depart_name"] = (*department_info)["s_name"]
  181. dep_rules = append(dep_rules, *res)
  182. }
  183. }
  184. set := bson.M{
  185. "$set": bson.M{
  186. "dep_rules": dep_rules,
  187. // "tag_rules": usertags,
  188. "updatetime": time.Now().Unix(),
  189. },
  190. }
  191. isupdata := Mgo.Update("second_push", map[string]interface{}{
  192. "_id": mongoutil.StringTOBsonId(s_pushid),
  193. }, set, false, false)
  194. s.ServeJson(map[string]interface{}{
  195. "status": isupdata,
  196. })
  197. return
  198. }
  199. }
  200. s.ServeJson(map[string]interface{}{
  201. "status": "fail",
  202. })
  203. }
  204. }
  205. func (s *SecondPush) SecondPushTask() {
  206. log.Println("开始二次推送任务...")
  207. s_pushid := s.GetString("s_pushid")
  208. if s_pushid != "" {
  209. customer, _ := Mgo.Find("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(s_pushid)}, nil, nil, false, -1, -1)
  210. if len(*customer) == 1 {
  211. c := (*customer)[0]
  212. customer_name := qu.ObjToString(c["s_name"]) //客户名称
  213. email := qu.ObjToString(c["sendMail"])
  214. starttime := qu.Int64All(c["starttime"])
  215. endtime := qu.Int64All(c["endtime"])
  216. if len(c) != 0 && c != nil {
  217. if c["dep_rules"] != nil && len(c["dep_rules"].([]interface{})) != 0 {
  218. for _, m := range c["dep_rules"].([]interface{}) {
  219. log.Println("ruleid", mongoutil.BsonIdToSId(m.(map[string]interface{})["_id"]))
  220. q := bson.M{
  221. "ruleid": mongoutil.BsonIdToSId(m.(map[string]interface{})["_id"]),
  222. "createtime": bson.M{
  223. "$gte": starttime,
  224. "$lte": endtime,
  225. },
  226. }
  227. d, _ := MgoCus.Find("usermail", q, nil, nil, false, 0, 0)
  228. if len(*d) > 0 && d != nil {
  229. for _, l := range *d {
  230. xlsxArr = append(xlsxArr, l)
  231. }
  232. }
  233. }
  234. } else {
  235. q := bson.M{
  236. "appid": c["s_appid"],
  237. "createtime": bson.M{
  238. "$gte": starttime,
  239. "$lte": endtime,
  240. },
  241. }
  242. d, _ := MgoCus.Find("usermail", q, nil, nil, false, 0, 0)
  243. for _, l := range *d {
  244. xlsxArr = append(xlsxArr, l)
  245. }
  246. }
  247. if len(xlsxArr) == 0 {
  248. log.Println("查询数据为空")
  249. } else {
  250. log.Println(len(xlsxArr))
  251. GetXlsxs(xlsxArr, customer_name, email, s_pushid)
  252. go UpdateHistoryState(2, s_pushid, len(xlsxArr))
  253. s.ServeJson(map[string]interface{}{
  254. "status": true,
  255. })
  256. xlsxArr = []map[string]interface{}{}
  257. }
  258. }
  259. } else {
  260. log.Println("初始化客户信息失败")
  261. }
  262. }
  263. }
  264. //func (s *SecondPush) HistoryList() {
  265. // id := s.GetString("id")
  266. // if s.Method() == "POST" {
  267. // data, _ := Mgo.Find("second_push", map[string]interface{}{"user_id": id}, `{"updatetime":-1}`, nil, false, -1, -1)
  268. // s.ServeJson(map[string]interface{}{
  269. // "data": data,
  270. // })
  271. // } else {
  272. // s.T["id"] = id
  273. // s.Render("private/historylog_list.html", &s.T)
  274. // }
  275. //}
  276. func UpdateHistoryState(state int, s_pushid string, count int) {
  277. if state == 2 {
  278. Mgo.Update("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(s_pushid)}, map[string]interface{}{
  279. "$set": map[string]interface{}{
  280. "state": state,
  281. "result_count": count,
  282. "finishtime":time.Now().Unix(),
  283. },
  284. }, false, false)
  285. return
  286. }
  287. Mgo.Update("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(s_pushid)}, map[string]interface{}{
  288. "$set": map[string]interface{}{
  289. "state": state,
  290. },
  291. }, false, false)
  292. }
  293. func (s *SecondPush) HistoryClone(hid string) {
  294. data, ok := Mgo.FindOne("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(hid)})
  295. if ok && data != nil && *data != nil {
  296. (*data)["state"] = 0
  297. (*data)["result_count"] = nil
  298. (*data)["createtime"] = time.Now().Unix()
  299. (*data)["updatetime"] = time.Now().Unix()
  300. Mgo.Save("second_push", *data)
  301. }
  302. }
  303. func (s *SecondPush) HistoryRuleEdit() {
  304. hid := s.GetString("hid")
  305. rid := s.GetString("rid")
  306. if s.Method() == "POST" {
  307. data := GetPostForm(s.Request)
  308. o_rules := []map[string]interface{}{}
  309. o_rulesStr := data["o_rules"].(string)
  310. json.Unmarshal([]byte(o_rulesStr), &o_rules)
  311. data["o_rules"] = o_rules
  312. data["_id"] = mongoutil.StringTOBsonId(rid)
  313. if qu.IntAll(data["i_esquerytype"]) == 1 { //自动生成es
  314. data["s_esquery"] = Utiltags(data)
  315. }
  316. datas, ok := Mgo.FindOne("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(hid)})
  317. if ok && datas != nil && *datas != nil {
  318. dep_rules := qu.ObjArrToMapArr((*datas)["dep_rules"].([]interface{}))
  319. for k, v := range dep_rules {
  320. if rid == mongoutil.BsonIdToSId(v["_id"]) {
  321. dep_rules[k] = data
  322. }
  323. }
  324. 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)
  325. s.ServeJson(map[string]interface{}{
  326. "rep": rep,
  327. "s_esquery": data["s_esquery"],
  328. })
  329. }
  330. } else {
  331. id := s.GetString("id")
  332. data, ok := Mgo.FindOne("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(hid)})
  333. if ok && data != nil && *data != nil {
  334. dep_rules := qu.ObjArrToMapArr((*data)["dep_rules"].([]interface{}))
  335. for _, v := range dep_rules {
  336. if rid == mongoutil.BsonIdToSId(v["_id"]) {
  337. s.T["data"] = v
  338. }
  339. }
  340. }
  341. s.T["did"] = id //部门id
  342. s.T["cid"] = id //客户id
  343. s.T["ids"] = id
  344. s.T["history_id"] = hid
  345. s.T["rid"] = rid
  346. s.T["province"] = Province
  347. s.T["city"] = ProvinceCitys
  348. s.T["district"] = CityDistricts
  349. s.T["topTypeArr"] = TopTypeArr
  350. s.T["subTypeArr"] = SubTypeArr
  351. s.T["matchTypeMap"] = MatchTypeMap
  352. s.T["matchTypeMap2"] = MatchTypeMap2
  353. s.T["existField"] = ExistFiled
  354. s.T["buyerClass"] = BuyerClass
  355. s.T["scopeClass"] = ScopeClassMap
  356. s.Render("private/history_rule_edit.html", &s.T)
  357. }
  358. }
  359. func GetXlsxs(mMap []map[string]interface{}, fn, email, id string) {
  360. if id != "" {
  361. query := bson.M{
  362. "_id": bson.ObjectIdHex(id),
  363. }
  364. data, ok := Mgo.FindOne("second_push", query)
  365. if ok && (*data) != nil && len(*data) > 0 {
  366. dataType := qu.IntAll((*data)["i_extfieldstype"])
  367. xf, err := xlsx.OpenFile("web/res/fields.xlsx")
  368. if err != nil {
  369. log.Println("fields file not foud", err.Error())
  370. }
  371. if dataType == 1 {
  372. sh := xf.Sheets[0]
  373. for i, v := range mMap {
  374. row := sh.AddRow()
  375. row.AddCell().SetInt(i + 1)
  376. row.AddCell().SetValue(v["matchkey"])
  377. row.AddCell().SetValue(v["area"])
  378. row.AddCell().SetValue(v["city"])
  379. row.AddCell().SetValue(v["title"])
  380. row.AddCell().SetValue(v["subtype"])
  381. if v["publishtime"] != nil {
  382. row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02"))
  383. } else {
  384. row.AddCell()
  385. }
  386. row.AddCell().SetValue(v["buyer"])
  387. row.AddCell().SetValue(v["winner"])
  388. if v["bidamount"] != nil {
  389. row.AddCell().SetFloat(qu.Float64All(v["bidamount"]) / 10000)
  390. } else {
  391. row.AddCell()
  392. }
  393. row.AddCell().SetValue(v["projectname"])
  394. row.AddCell().SetValue(v["detail"])
  395. row.AddCell().SetValue(v["s_jyhref"])
  396. }
  397. xf.Sheets = xf.Sheets[0:1]
  398. } else if dataType == 2 {
  399. sh := xf.Sheets[1]
  400. for i, v := range mMap {
  401. row := sh.AddRow()
  402. row.AddCell().SetInt(i + 1)
  403. row.AddCell().SetValue(v["matchkey"])
  404. row.AddCell().SetValue(v["area"])
  405. row.AddCell().SetValue(v["city"])
  406. row.AddCell().SetValue(v["title"])
  407. row.AddCell().SetValue(v["subtype"])
  408. row.AddCell().SetValue(v["detail"])
  409. if v["publishtime"] != nil {
  410. row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02"))
  411. } else {
  412. row.AddCell()
  413. }
  414. row.AddCell().SetValue(v["href"])
  415. row.AddCell().SetValue(v["s_jyhref"])
  416. row.AddCell().SetValue(v["projectcode"])
  417. row.AddCell().SetValue(v["projectname"])
  418. row.AddCell().SetValue(v["projectscope"])
  419. if v["budget"] != nil {
  420. row.AddCell().SetFloat(qu.Float64All(v["budget"]) / 10000)
  421. } else {
  422. row.AddCell()
  423. }
  424. if v["bidamount"] != nil {
  425. row.AddCell().SetFloat(qu.Float64All(v["bidamount"]) / 10000)
  426. } else {
  427. row.AddCell()
  428. }
  429. if v["bidopentime"] != nil {
  430. row.AddCell().SetValue(time.Unix(qu.Int64All(v["bidopentime"]), 0).Format("2006-01-02"))
  431. } else {
  432. row.AddCell()
  433. }
  434. row.AddCell().SetValue(v["buyer"])
  435. row.AddCell().SetValue(v["buyerperson"])
  436. row.AddCell().SetValue(v["buyertel"])
  437. row.AddCell().SetValue(v["agency"])
  438. row.AddCell().SetValue(v["winner"])
  439. row.AddCell().SetValue(v["winnerperson"])
  440. row.AddCell().SetValue(v["winnertel"])
  441. row.AddCell().SetValue(v["legal_person"])
  442. row.AddCell().SetValue(v["company_phone"])
  443. row.AddCell().SetValue(v["company_email"])
  444. }
  445. xf.Sheets = xf.Sheets[1:2]
  446. } else {
  447. sh := xf.Sheets[2]
  448. for i, v := range mMap {
  449. row := sh.AddRow()
  450. row.AddCell().SetInt(i + 1)
  451. row.AddCell().SetValue(v["matchkey"])
  452. row.AddCell().SetValue(v["area"])
  453. row.AddCell().SetValue(v["city"])
  454. row.AddCell().SetValue(v["district"])
  455. row.AddCell().SetValue(v["title"])
  456. row.AddCell().SetValue(v["detail"])
  457. if v["publishtime"] != nil {
  458. row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02"))
  459. } else {
  460. row.AddCell()
  461. }
  462. row.AddCell().SetValue(v["href"])
  463. if v["bidamount"] != nil {
  464. row.AddCell().SetFloat(qu.Float64All(v["bidamount"]) / 10000)
  465. } else {
  466. row.AddCell()
  467. }
  468. row.AddCell().SetValue(v["buyer"])
  469. row.AddCell().SetValue(v["buyerperson"])
  470. row.AddCell().SetValue(v["buyertel"])
  471. row.AddCell().SetValue(v["s_winner"])
  472. row.AddCell().SetValue(v["legal_person"])
  473. row.AddCell().SetValue(v["company_phone"])
  474. row.AddCell().SetValue(v["company_address"])
  475. row.AddCell().SetValue(v["rank"])
  476. row.AddCell().SetValue(v["purchasing"])
  477. row.AddCell().SetValue(v["capital"])
  478. row.AddCell().SetValue(v["establish_date"])
  479. row.AddCell().SetValue(v["business_scope"])
  480. row.AddCell().SetValue(v["stock_name"])
  481. row.AddCell().SetValue(v["buyer_credit_no"])
  482. row.AddCell().SetValue(v["winner_credit_no"])
  483. row.AddCell().SetValue(v["id"])
  484. }
  485. xf.Sheets = xf.Sheets[2:3]
  486. }
  487. //生文件
  488. t := strconv.FormatInt(time.Now().Unix(), 10)
  489. dir := "./web/res/xlsx/" + t + "/"
  490. if b, _ := history.PathExists(dir); !b {
  491. err1 := os.MkdirAll(dir, os.ModePerm)
  492. if err1 != nil {
  493. log.Println("mkdir err", dir)
  494. }
  495. }
  496. fname := t + ".xlsx"
  497. err = xf.Save(dir + fname)
  498. if err != nil {
  499. log.Println("xls error", fname)
  500. } else {
  501. for i := 0; i < len(history.Gmails); i++ {
  502. gmail := history.Gmails[i]
  503. status := mail.GSendMail_q("剑鱼标讯", email, "", "", fn, "", dir+fname, fname, gmail)
  504. if status {
  505. log.Println("send mail success", fname, email)
  506. break
  507. }
  508. }
  509. }
  510. }
  511. }
  512. }
  513. func (s *SecondPush) AddSecondPushRule() {
  514. defer qu.Catch()
  515. if s.Method() == "POST" {
  516. his_id := s.GetString("s_pushid")
  517. if his_id != "" {
  518. i_updatetime, _ := strconv.ParseInt(s.GetString("i_updatetime"), 10, 64)
  519. i_extfieldstype, _ := strconv.Atoi(s.GetString("i_extfieldstype"))
  520. set := bson.M{
  521. "$set": bson.M{
  522. "i_updatetime": i_updatetime,
  523. "sendMail": s.GetString("sendMail"),
  524. "i_extfieldstype": i_extfieldstype,
  525. "starttime": s.GetString("starttime"),
  526. "endtime": s.GetString("endtime"),
  527. },
  528. }
  529. isupdata := Mgo.UpdateById("second_push", mongoutil.StringTOBsonId(his_id), set)
  530. if isupdata {
  531. s.ServeJson(map[string]interface{}{
  532. "history_id": his_id,
  533. "rep": true,
  534. })
  535. } else {
  536. s.ServeJson(map[string]interface{}{
  537. "history_id": "",
  538. "rep": false,
  539. })
  540. }
  541. }
  542. }
  543. }
  544. func (s *SecondPush) SecondPushList() {
  545. id := s.GetString("id")
  546. if s.Method() == "POST" {
  547. data, _ := Mgo.Find("second_push", map[string]interface{}{"user_id": id}, `{"updatetime":-1}`, nil, false, -1, -1)
  548. s.ServeJson(map[string]interface{}{
  549. "data": data,
  550. })
  551. }
  552. }
  553. func (s *SecondPush) SecondPushClone(hid string) {
  554. data, ok := Mgo.FindOne("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(hid)})
  555. if ok && data != nil && *data != nil {
  556. (*data)["state"] = 0
  557. (*data)["result_count"] = nil
  558. (*data)["createtime"] = time.Now().Unix()
  559. (*data)["updatetime"] = time.Now().Unix()
  560. delete((*data),"finishtime")
  561. Mgo.Save("second_push", *data)
  562. }
  563. }
  564. func (s *SecondPush) DelSecondPushRule() {
  565. id := s.GetString("id")
  566. hid := s.GetString("s_pushid")
  567. if hid != "" {
  568. data, ok := Mgo.FindOne("second_push", bson.M{"_id": mongoutil.StringTOBsonId(hid)})
  569. if ok && data != nil && *data != nil {
  570. if (*data)["dep_rules"] != nil {
  571. dep_rules := qu.ObjArrToMapArr((*data)["dep_rules"].([]interface{}))
  572. ruleMap := map[string]bool{id: true}
  573. ruleArr := []map[string]interface{}{}
  574. for _, m := range dep_rules {
  575. if !ruleMap[mongoutil.BsonIdToSId(m["_id"])] {
  576. ruleArr = append(ruleArr, m)
  577. }
  578. }
  579. ok := Mgo.Update("second_push", bson.M{"_id": mongoutil.StringTOBsonId(hid)}, bson.M{"$set": bson.M{"dep_rules": ruleArr}}, false, false)
  580. s.ServeJson(map[string]interface{}{
  581. "success": ok,
  582. })
  583. }
  584. }
  585. }
  586. }