repair_service.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/go-xweb/xweb"
  6. "github.com/tealeg/xlsx"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. "io/ioutil"
  10. "log"
  11. mu "mfw/util"
  12. "mongodb"
  13. "net"
  14. qu "qfw/util"
  15. "qfw/util/elastic"
  16. "qfw/util/redis"
  17. "regexp"
  18. "strings"
  19. "time"
  20. "udptask"
  21. . "util"
  22. )
  23. var (
  24. rpre = regexp.MustCompile("https://www.jianyu360.cn/article/content/")
  25. rsuf = regexp.MustCompile("(.html).*")
  26. AddSpiderCode = "sdxzbiddingsjzypc" // 新增bidding数据spider_code
  27. )
  28. type RepairRule struct {
  29. *xweb.Action
  30. repairList xweb.Mapper `xweb:"/service/jy/repair"`
  31. searchID xweb.Mapper `xweb:"/service/jy/searchID"`
  32. searchJyurl xweb.Mapper `xweb:"/service/jy/searchJyurl"`
  33. repairEdit xweb.Mapper `xweb:"/service/jy/edit"`
  34. repairDelete xweb.Mapper `xweb:"/service/jy/delete"`
  35. repairSave xweb.Mapper `xweb:"/service/jy/save"`
  36. repairCreate xweb.Mapper `xweb:"/service/jy/create"`
  37. repairNewSave xweb.Mapper `xweb:"/service/jy/newSave"`
  38. repairBulk xweb.Mapper `xweb:"/service/jy/bulk_repair"`
  39. repairImport xweb.Mapper `xweb:"/service/jy/importData"`
  40. repairImport1 xweb.Mapper `xweb:"/service/jy/importData1"`
  41. repairModify xweb.Mapper `xweb:"/service/jy/modifySave"`
  42. repairDel xweb.Mapper `xweb:"/service/jy/delSave"`
  43. repairRecord xweb.Mapper `xweb:"/service/jy/modifyRecord"`
  44. repairPro xweb.Mapper `xweb:"/service/jy/repair/pro"`
  45. repairFindPro xweb.Mapper `xweb:"/service/jy/repair/queryPro"`
  46. repairProSave xweb.Mapper `xweb:"/service/jy/repair/pro/save"`
  47. repairProRecord xweb.Mapper `xweb:"/service/jy/repair/pro/record"`
  48. repairNewAdd xweb.Mapper `xweb:"/service/jy/repair/newAdd"`
  49. repairPub xweb.Mapper `xweb:"/service/jy/repair/pubSave"`
  50. redisRepair xweb.Mapper `xweb:"/service/jy/repair/redis"`
  51. esDel xweb.Mapper `xweb:"/service/jy/repair/es/del"`
  52. esDelRecord xweb.Mapper `xweb:"/service/jy/repair/es/del/record"`
  53. esCount xweb.Mapper `xweb:"/service/jy/repair/es/count/byField"`
  54. esDelBy xweb.Mapper `xweb:"/service/jy/repair/es/del/byField"`
  55. }
  56. func (jy *RepairRule) RepairList() {
  57. defer qu.Catch()
  58. _ = jy.Render("repair/jy_repair.html")
  59. }
  60. func (jy *RepairRule) RedisRepair() {
  61. defer qu.Catch()
  62. _ = jy.Render("repair/jy_redis_repair.html")
  63. }
  64. func (jy *RepairRule) EsDel() {
  65. defer qu.Catch()
  66. _ = jy.Render("repair/jy_es_del.html")
  67. }
  68. //新增数据
  69. func (jy *RepairRule) RepairCreate() {
  70. defer qu.Catch()
  71. jy.T["data"] = map[string]string{
  72. "title": "",
  73. "site": "",
  74. "href": "",
  75. "channel": "",
  76. "publishtime": "",
  77. "area": "",
  78. "city": "",
  79. "district": "",
  80. "infoformat": "1"}
  81. jy.Render("repair/jy_create.html", &jy.T)
  82. }
  83. //新增数据
  84. func (jy *RepairRule) RepairNewSave() {
  85. defer qu.Catch()
  86. log.Println("新增数据")
  87. //mongo新增
  88. request_data := GetPostForm(jy.Request)
  89. updata := qu.ObjToMap(request_data["data"])
  90. reasons := qu.ObjToString(request_data["reasons"])
  91. contenthtml := qu.ObjToString(request_data["contenthtml"])
  92. detail := detailClear(qu.ObjToString(request_data["detail"]))
  93. summary := qu.ObjToString(request_data["summary"])
  94. (*updata)["contenthtml"] = contenthtml
  95. (*updata)["detail"] = detail
  96. if summary != "" {
  97. (*updata)["summary"] = summary
  98. }
  99. //处理个别字段
  100. (*updata)["_id"] = primitive.NewObjectID()
  101. if hf := qu.ObjToString((*updata)["href"]); hf == "" {
  102. var Url = "https://www.jianyu360.cn/article/content/%s.html"
  103. (*updata)["href"] = fmt.Sprintf(Url, qu.CommonEncodeArticle("content", mongodb.BsonIdToSId((*updata)["_id"])))
  104. }
  105. // spider_code
  106. (*updata)["spidercode"] = AddSpiderCode
  107. // s_sha
  108. (*updata)["s_sha"] = Sha(detail)
  109. // dataging
  110. (*updata)["dataging"] = 0
  111. // comeintime
  112. (*updata)["comeintime"] = time.Now().Unix()
  113. // publishtime
  114. if val, ok := (*updata)["publishtime"]; ok {
  115. (*updata)["publishtime"] = qu.Int64All(val)
  116. } else {
  117. pt := int64(0)
  118. (*updata)["publishtime"] = pt
  119. }
  120. // infoformat
  121. if val, ok := (*updata)["infoformat"]; ok {
  122. (*updata)["infoformat"] = qu.IntAll(val)
  123. } else {
  124. (*updata)["infoformat"] = 1
  125. }
  126. b := JYMgo.SaveByOriID(JyCollNameOne, *updata)
  127. // 保存服务
  128. href := qu.ObjToString((*updata)["href"])
  129. db := HexToBigIntMod(href)
  130. hashHref := HexText(href)
  131. shaid := Sha(detail)
  132. if !strings.Contains(href, "https://www.jianyu360.cn/") {
  133. // 剑鱼链接 不需要存redis
  134. PutRedis("title_repeat_fulljudgement", db, hashHref, mongodb.BsonIdToSId((*updata)["_id"]), -1) // 全量redis
  135. }
  136. PutRedis("shaid", 0, shaid, mongodb.BsonIdToSId((*updata)["_id"]), 5184000) // 增量reids
  137. //PutRedis("title_repeat_listpagehref", 0, href, "", 3600*24*30*24) // 列表页增量redis
  138. if b {
  139. log.Println("当前新增id:", mongodb.BsonIdToSId((*updata)["_id"]))
  140. //日志记录
  141. user := jy.GetSession("user").(map[string]interface{})
  142. log_data := map[string]interface{}{
  143. "s_modifyuser": user["name"],
  144. "s_type": 1,
  145. "i_modifytime": time.Now().Unix(),
  146. "s_modifyreason": reasons,
  147. "s_backupid": mongodb.BsonIdToSId((*updata)["_id"]),
  148. "o_oldinfo": map[string]interface{}{},
  149. "o_newinfo": updata,
  150. }
  151. Mgo.Save(JyRecord, log_data)
  152. jy.ServeJson(map[string]interface{}{
  153. "rep": true,
  154. })
  155. } else {
  156. jy.ServeJson(map[string]interface{}{
  157. "rep": false,
  158. })
  159. }
  160. }
  161. // RepairNewAdd 新增数据(无数据流程)
  162. func (jy *RepairRule) RepairNewAdd() {
  163. defer qu.Catch()
  164. jy.T["province"] = Province
  165. jy.T["city"] = ProvinceCitys
  166. jy.T["district"] = CityDistricts
  167. jy.T["topTypeArr"] = TopTypeArr
  168. jy.T["subTypeMap"] = SubTypeMap
  169. jy.T["buyerClass"] = BuyerClass
  170. jy.T["scopeClass"] = ScopeClassMap
  171. jy.Render("repair/jy_create_new.html", &jy.T)
  172. }
  173. func (jy *RepairRule) RepairPub() {
  174. defer qu.Catch()
  175. request_data := GetPostForm(jy.Request)
  176. jsdata := qu.ObjToMap(request_data["data"])
  177. qu.Debug(*jsdata)
  178. save := make(map[string]interface{})
  179. save["_id"] = primitive.NewObjectID()
  180. for k, v := range *jsdata {
  181. if v == "" {
  182. continue
  183. }
  184. if k == "publishtime" {
  185. save[k] = qu.Int64All(v)
  186. } else if k == "href" && v == "#" {
  187. var Url = "https://www.jianyu360.cn/article/content/%s.html"
  188. save["href"] = fmt.Sprintf(Url, qu.CommonEncodeArticle("content", mongodb.BsonIdToSId(save["_id"])))
  189. } else if k == "detail" {
  190. v = strings.Replace(qu.ObjToString(v), "&#60;", "<", -1)
  191. v = strings.Replace(qu.ObjToString(v), "&#62;", ">", -1)
  192. save[k] = v
  193. } else {
  194. save[k] = v
  195. }
  196. }
  197. // spider_code
  198. save["spidercode"] = AddSpiderCode
  199. // s_sha
  200. save["s_sha"] = Sha(qu.ObjToString(save["detail"]))
  201. // dataging
  202. save["dataging"] = 0
  203. // comeintime
  204. save["comeintime"] = time.Now().Unix()
  205. save["infoformat"] = 1
  206. save["extracttype"] = 0
  207. b := JYMgo.SaveByOriID(JyCollNameOne, save)
  208. if b {
  209. // 保存服务
  210. href := qu.ObjToString(save["href"])
  211. db := HexToBigIntMod(href)
  212. hashHref := HexText(href)
  213. shaid := Sha(qu.ObjToString(save["detail"]))
  214. if !strings.Contains(href, "https://www.jianyu360.cn/") {
  215. // 剑鱼链接 不需要存redis
  216. PutRedis("title_repeat_fulljudgement", db, hashHref, mongodb.BsonIdToSId(save["_id"]), -1)
  217. }
  218. PutRedis("shaid", 0, shaid, "", 5184000)
  219. log.Println("当前新增id:", mongodb.BsonIdToSId(save["_id"]))
  220. //日志记录
  221. user := jy.GetSession("user").(map[string]interface{})
  222. log_data := map[string]interface{}{
  223. "s_modifyuser": user["name"],
  224. "s_type": 1,
  225. "i_modifytime": time.Now().Unix(),
  226. "s_modifyreason": "剑鱼直接新增数据",
  227. "s_backupid": mongodb.BsonIdToSId(save["_id"]),
  228. "o_newinfo": save,
  229. }
  230. Mgo.Save(JyRecord, log_data)
  231. indexNode := *qu.ObjToMap(Sysconfig["indexNode"])
  232. by, _ := json.Marshal(map[string]interface{}{
  233. "query": map[string]interface{}{
  234. "_id": bson.M{
  235. "$gte": save["_id"],
  236. "$lte": save["_id"],
  237. }},
  238. "stype": qu.ObjToString(indexNode["stype"]),
  239. "coll": JyCollNameOne,
  240. })
  241. addr := &net.UDPAddr{
  242. IP: net.ParseIP(indexNode["addr"].(string)),
  243. Port: qu.IntAll(indexNode["port"]),
  244. }
  245. qu.Debug("udp---1---------", string(by))
  246. udptask.Udpclient.WriteUdp(by, mu.OP_TYPE_DATA, addr)
  247. jy.ServeJson(map[string]interface{}{
  248. "rep": true,
  249. })
  250. } else {
  251. jy.ServeJson(map[string]interface{}{
  252. "rep": false,
  253. })
  254. }
  255. }
  256. //编辑
  257. func (jy *RepairRule) RepairEdit() {
  258. defer qu.Catch()
  259. id := jy.GetString("id")
  260. coll := jy.GetString("coll")
  261. data, _ := JYMgo.FindById(coll, id, "")
  262. delete(*data, "_id")
  263. //contenthtml detail 单独拿出来
  264. detail := qu.ObjToString((*data)["detail"])
  265. contenthtml := qu.ObjToString((*data)["contenthtml"])
  266. summary := qu.ObjToString((*data)["summary"])
  267. jy.T["detail"] = detail
  268. jy.T["contenthtml"] = contenthtml
  269. jy.T["summary"] = summary
  270. delete(*data, "detail")
  271. delete(*data, "contenthtml")
  272. delete(*data, "summary")
  273. jy.T["id"] = id
  274. jy.T["data"] = *data
  275. jy.T["coll"] = coll
  276. jy.Render("repair/jy_edit.html", &jy.T)
  277. }
  278. //删除
  279. func (jy *RepairRule) RepairDelete() {
  280. defer qu.Catch()
  281. id := jy.GetString("_id")
  282. coll := jy.GetString("coll")
  283. old_data, _ := JYMgo.FindById(coll, id, "") //记录当前表的源数据
  284. log.Println(id, coll)
  285. set := bson.M{"_id": mongodb.StringTOBsonId(id)}
  286. b := JYMgo.Del(coll, set)
  287. if !b {
  288. jy.ServeJson(map[string]interface{}{
  289. "rep": b,
  290. })
  291. } else {
  292. //删除es
  293. client := elastic.GetEsConn()
  294. defer elastic.DestoryEsConn(client)
  295. _, err := client.Delete().Index(EsIndex).Type(EsType).Id(id).Refresh(true).Do()
  296. if err != nil {
  297. log.Println("delete es err:", err)
  298. }
  299. //删除redis 指定key
  300. delName1 := RedisDelKey1 + id + "_*"
  301. redis.DelByCodePattern(RedisJYName, delName1)
  302. //jyredis := redis.RedisPool[RedisJYName].Get()
  303. //defer jyredis.Close()
  304. //if _, err := jyredis.Do("SELECT", 0); err != nil {
  305. // log.Println("删除-redis-select-db失败")
  306. //} else {
  307. // delName1 := RedisDelKey1 + id + "_*"
  308. // if _, err1 := jyredis.Do("DEL", delName1); err1 != nil {
  309. // log.Println("删除-del-redis-fail:", delName1)
  310. // }
  311. //delName2 := RedisDelKey2 + id
  312. //if _, err2 := jyredis.Do("DEL", delName2); err2 != nil {
  313. // log.Println("删除-del-redis-fail:", delName2)
  314. //}
  315. //delName3 := RedisDelKey3 + id
  316. //if _, err3 := jyredis.Do("DEL", delName3); err3 != nil {
  317. // log.Println("删除-del-redis-fail:", delName3)
  318. //}
  319. //delName4 := RedisDelKey4 + id
  320. //if _, err4 := jyredis.Do("DEL", delName4); err4 != nil {
  321. // log.Println("更新-del-redis-fail:", delName4)
  322. //}
  323. //delName5 := RedisDelKey5 + id
  324. //if _, err5 := jyredis.Do("DEL", delName5); err5 != nil {
  325. // log.Println("更新-del-redis-fail:", delName5)
  326. //}
  327. //}
  328. //日志记录
  329. user := jy.GetSession("user").(map[string]interface{})
  330. log_data := map[string]interface{}{
  331. "s_modifyuser": user["name"],
  332. "s_type": 3,
  333. "i_modifytime": time.Now().Unix(),
  334. "s_modifyreason": "单条删除",
  335. "s_backupid": id,
  336. "o_oldinfo": old_data,
  337. "modifyinfo": "删除数据",
  338. }
  339. Mgo.Save(JyRecord, log_data)
  340. // udp 项目
  341. nextNode := *qu.ObjToMap(Sysconfig["jy_pro_node"])
  342. by, _ := json.Marshal(map[string]interface{}{
  343. "infoid": id,
  344. "stype": "deleteInfo",
  345. })
  346. addr := &net.UDPAddr{
  347. IP: net.ParseIP(nextNode["addr"].(string)),
  348. Port: qu.IntAll(nextNode["port"]),
  349. }
  350. qu.Debug(string(by))
  351. udptask.Udpclient.WriteUdp(by, mu.OP_TYPE_DATA, addr)
  352. jy.ServeJson(map[string]interface{}{
  353. "rep": b,
  354. })
  355. }
  356. }
  357. //修改-mongo -es
  358. func (jy *RepairRule) RepairSave() {
  359. defer qu.Catch()
  360. if jy.Method() == "POST" {
  361. //request_data := GetPostForm(jy.Request)
  362. dataStr := jy.GetString("data")
  363. var updata map[string]interface{}
  364. if err := json.Unmarshal([]byte(dataStr), &updata); err != nil {
  365. qu.Debug("data Unmarshal Failed:", err)
  366. }
  367. coll := jy.GetString("coll")
  368. id := jy.GetString("id")
  369. old_data, _ := JYMgo.FindById(coll, id, nil) //记录当前表的源数据
  370. delete(*old_data, "_id")
  371. reasons := jy.GetString("reasons")
  372. //contenthtml := qu.ObjToString(request_data["contenthtml"])
  373. //detail := qu.ObjToString(request_data["detail"])
  374. //summary := qu.ObjToString(request_data["summary"])
  375. modifyStr := jy.GetString("modifyinfo")
  376. var modifyinfo map[string]interface{}
  377. if err := json.Unmarshal([]byte(modifyStr), &modifyinfo); err != nil {
  378. qu.Debug("modifyinfo Unmarshal Failed:", err)
  379. }
  380. //(*updata)["contenthtml"] = contenthtml
  381. if detail := qu.ObjToString(updata["detail"]); detail != "" {
  382. detail = strings.Replace(detail, "&#60;", "<", -1)
  383. detail = strings.Replace(detail, "&#62;", ">", -1)
  384. updata["detail"] = detail
  385. }
  386. if contenthtml := qu.ObjToString(updata["contenthtml"]); contenthtml != "" {
  387. contenthtml = strings.Replace(contenthtml, "&#60;", "<", -1)
  388. contenthtml = strings.Replace(contenthtml, "&#62;", ">", -1)
  389. updata["contenthtml"] = contenthtml
  390. }
  391. //变更字段
  392. if len(modifyinfo) == 0 {
  393. jy.ServeJson(map[string]interface{}{
  394. "rep": false,
  395. })
  396. } else {
  397. if (*old_data)["modifyinfo"] != nil {
  398. if m, ok := (*old_data)["modifyinfo"].(map[string]interface{}); ok {
  399. for k, v := range m {
  400. modifyinfo[k] = v
  401. }
  402. }
  403. }
  404. }
  405. //处理个别字段
  406. //if val, ok := (*old_data)["publishtime"]; ok {
  407. // (*old_data)["publishtime"] = qu.Int64All(val)
  408. //} else {
  409. // pt := int64(0)
  410. // (*old_data)["publishtime"] = pt
  411. //}
  412. //if val, ok := (*old_data)["infoformat"]; ok {
  413. // (*old_data)["infoformat"] = qu.IntAll(val)
  414. //} else {
  415. // (*old_data)["infoformat"] = 1
  416. //}
  417. query := bson.M{
  418. "_id": mongodb.StringTOBsonId(id),
  419. }
  420. delete(updata, "_id")
  421. for k, v := range updata {
  422. if k == "extracttype" {
  423. (updata)[k] = qu.IntAll(v)
  424. } else if k == "publishtime" || k == "bidopentime" {
  425. (updata)[k] = qu.Int64All(v)
  426. } else if k == "budget" || k == "bidamount" {
  427. (updata)[k] = qu.Float64All(v)
  428. }
  429. }
  430. updata["modifyinfo"] = modifyinfo
  431. var set map[string]interface{}
  432. set = bson.M{
  433. "$set": updata,
  434. }
  435. //if modifyinfo["summary"] == nil {
  436. // delete(*updata, "summary")
  437. // set = bson.M{
  438. // "$set": *updata,
  439. // "$unset": bson.M{
  440. // "summary": "",
  441. // },
  442. // }
  443. //} else {
  444. // set = bson.M{
  445. // "$set": *updata,
  446. // }
  447. //}
  448. FormatNumber(updata)
  449. qu.Debug(query, updata)
  450. rep := JYMgo.Update(coll, query, set, true, false)
  451. if !rep {
  452. jy.ServeJson(map[string]interface{}{
  453. "rep": rep,
  454. })
  455. } else {
  456. //调udp生索引
  457. indexNode := *qu.ObjToMap(Sysconfig["indexNode"])
  458. by, _ := json.Marshal(map[string]interface{}{
  459. "query": map[string]interface{}{
  460. "_id": bson.M{
  461. "$gte": mongodb.StringTOBsonId(id),
  462. "$lte": mongodb.StringTOBsonId(id),
  463. }},
  464. "stype": qu.ObjToString(indexNode["stype"]),
  465. "coll": coll,
  466. })
  467. addr := &net.UDPAddr{
  468. IP: net.ParseIP(indexNode["addr"].(string)),
  469. Port: qu.IntAll(indexNode["port"]),
  470. }
  471. qu.Debug("udp---1---------", string(by))
  472. udptask.Udpclient.WriteUdp(by, mu.OP_TYPE_DATA, addr)
  473. if IsModifyPro(modifyinfo) {
  474. // udp 项目
  475. nextNode := *qu.ObjToMap(Sysconfig["jy_pro_node"])
  476. by1, _ := json.Marshal(map[string]interface{}{
  477. "infoid": id,
  478. "stype": "updateInfo",
  479. "coll": coll,
  480. })
  481. addr1 := &net.UDPAddr{
  482. IP: net.ParseIP(nextNode["addr"].(string)),
  483. Port: qu.IntAll(nextNode["port"]),
  484. }
  485. qu.Debug("udp---2---------", string(by1))
  486. udptask.Udpclient.WriteUdp(by1, mu.OP_TYPE_DATA, addr1)
  487. }
  488. //删除redis 指定key
  489. delName1 := RedisDelKey1 + id + "_*"
  490. redis.DelByCodePattern(RedisJYName, delName1)
  491. //jyredis := redis.RedisPool[RedisJYName].Get()
  492. //defer jyredis.Close()
  493. //if _, err := jyredis.Do("SELECT", 0); err != nil {
  494. // log.Println("更新-redis-select-db失败")
  495. //} else {
  496. // delName1 := RedisDelKey1 + id + "_*"
  497. // if _, err1 := jyredis.Do("DEL", delName1); err1 != nil {
  498. // log.Println("更新-del-redis-fail:", delName1)
  499. // }
  500. //delName2 := RedisDelKey2 + id
  501. //if _, err2 := jyredis.Do("DEL", delName2); err2 != nil {
  502. // log.Println("更新-del-redis-fail:", delName2)
  503. //}
  504. //delName3 := RedisDelKey3 + id
  505. //if _, err3 := jyredis.Do("DEL", delName3); err3 != nil {
  506. // log.Println("更新-del-redis-fail:", delName3)
  507. //}
  508. //delName4 := RedisDelKey4 + id
  509. //if _, err4 := jyredis.Do("DEL", delName4); err4 != nil {
  510. // log.Println("更新-del-redis-fail:", delName4)
  511. //}
  512. //delName5 := RedisDelKey5 + id
  513. //if _, err5 := jyredis.Do("DEL", delName5); err5 != nil {
  514. // log.Println("更新-del-redis-fail:", delName5)
  515. //}
  516. //}
  517. //日志记录
  518. user := jy.GetSession("user").(map[string]interface{})
  519. log_data := map[string]interface{}{
  520. "s_modifyuser": user["name"],
  521. "s_type": 2,
  522. "i_modifytime": time.Now().Unix(),
  523. "s_modifyreason": reasons,
  524. "s_backupid": id,
  525. "o_oldinfo": old_data,
  526. "o_newinfo": updata,
  527. "modifyinfo": modifyinfo,
  528. }
  529. Mgo.Save(JyRecord, log_data)
  530. jy.ServeJson(map[string]interface{}{
  531. "rep": rep,
  532. })
  533. }
  534. }
  535. }
  536. //查询
  537. func (jy *RepairRule) SearchID() {
  538. defer qu.Catch()
  539. if jy.Method() == "POST" {
  540. id := jy.GetString("_id")
  541. //先查one 没有在查two
  542. if !mongodb.IsObjectIdHex(id) {
  543. jy.ServeJson(map[string]interface{}{
  544. "rep": false,
  545. "msg": "id格式不正确",
  546. })
  547. } else {
  548. data_one, rep_one := JYMgo.Find(JyCollNameOne, bson.M{"_id": mongodb.StringTOBsonId(id)}, nil, nil, false, -1, -1)
  549. if !rep_one || len(*data_one) <= 0 {
  550. data_two, rep_two := JYMgo.Find(JyCollNameTwo, bson.M{"_id": mongodb.StringTOBsonId(id)}, nil, nil, false, -1, -1)
  551. qu.Debug(JyCollNameOne, id)
  552. if !rep_two || len(*data_two) <= 0 {
  553. jy.ServeJson(map[string]interface{}{
  554. "rep": false,
  555. "msg": "未查询到数据",
  556. })
  557. } else {
  558. jy.ServeJson(map[string]interface{}{
  559. "orgColl": JyCollNameTwo,
  560. "rep": rep_two,
  561. "data": data_two,
  562. })
  563. }
  564. } else {
  565. qu.Debug(JyCollNameOne, id)
  566. jy.ServeJson(map[string]interface{}{
  567. "orgColl": JyCollNameOne,
  568. "rep": rep_one,
  569. "data": data_one,
  570. })
  571. }
  572. }
  573. }
  574. }
  575. func (jy *RepairRule) SearchJyurl() {
  576. defer qu.Catch()
  577. if jy.Method() == "POST" {
  578. jyurl := jy.GetString("jyurl")
  579. jyurl = rpre.ReplaceAllString(jyurl, "")
  580. jyurl = rsuf.ReplaceAllString(jyurl, "")
  581. new_id := qu.CommonDecodeArticle("content", jyurl)[0]
  582. qu.Debug(new_id)
  583. if !mongodb.IsObjectIdHex(new_id) {
  584. jy.ServeJson(map[string]interface{}{
  585. "rep": false,
  586. "msg": "href格式不正确",
  587. })
  588. } else {
  589. data_one, rep_one := JYMgo.Find(JyCollNameOne, bson.M{"_id": mongodb.StringTOBsonId(new_id)}, nil, nil, false, -1, -1)
  590. if !rep_one || len(*data_one) <= 0 {
  591. data_two, rep_two := JYMgo.Find(JyCollNameTwo, bson.M{"_id": mongodb.StringTOBsonId(new_id)}, nil, nil, false, -1, -1)
  592. if !rep_two || len(*data_two) <= 0 {
  593. jy.ServeJson(map[string]interface{}{
  594. "rep": false,
  595. "msg": "未查询到数据",
  596. })
  597. } else {
  598. jy.ServeJson(map[string]interface{}{
  599. "orgColl": JyCollNameTwo,
  600. "rep": rep_two,
  601. "data": data_two,
  602. })
  603. }
  604. } else {
  605. jy.ServeJson(map[string]interface{}{
  606. "orgColl": JyCollNameOne,
  607. "rep": rep_one,
  608. "data": data_one,
  609. })
  610. }
  611. }
  612. }
  613. }
  614. func detailClear(detail string) string {
  615. cut := NewCut()
  616. new_s := cut.ClearHtml(detail)
  617. return new_s
  618. }
  619. func (jy *RepairRule) RepairBulk() {
  620. defer qu.Catch()
  621. jy.Render("repair/jy_bulk.html")
  622. }
  623. func (jy *RepairRule) RepairRecord() {
  624. defer qu.Catch()
  625. if jy.Method() == "POST" {
  626. start, _ := jy.GetInteger("start")
  627. limit, _ := jy.GetInteger("length")
  628. draw, _ := jy.GetInteger("draw")
  629. searchStr := jy.GetString("search[value]")
  630. search := strings.TrimSpace(searchStr)
  631. query := bson.M{}
  632. if search != "" {
  633. query["$or"] = []interface{}{
  634. //bson.M{"s_customer": bson.M{"$regex": search}},
  635. //bson.M{"s_tagname": bson.M{"$regex": search}},
  636. }
  637. }
  638. field := map[string]interface{}{"s_backupid": 1, "s_modifyuser": 1, "i_modifytime": 1, "modifyinfo": 1, "s_modifyreason": 1}
  639. data, _ := Mgo.Find("jy_modify_log", query, `{"i_modifytime": -1}`, field, false, start, limit)
  640. count := Mgo.Count("jy_modify_log", query)
  641. jy.ServeJson(map[string]interface{}{
  642. "draw": draw,
  643. "data": data,
  644. "recordsFiltered": count,
  645. "recordsTotal": count,
  646. })
  647. } else {
  648. jy.Render("repair/jy_bulk.html")
  649. }
  650. }
  651. /**
  652. 批量修改
  653. */
  654. func (jy *RepairRule) RepairImport() {
  655. defer qu.Catch()
  656. if jy.Method() == "POST" {
  657. mf, _, err := jy.GetFile("xlsx")
  658. if err == nil {
  659. binary, err := ioutil.ReadAll(mf)
  660. if err == nil {
  661. data, err := ParsJyData(binary)
  662. if err == nil {
  663. jy.ServeJson(map[string]interface{}{
  664. "data": data,
  665. "rep": true,
  666. })
  667. return
  668. }
  669. }
  670. }
  671. jy.ServeJson(map[string]interface{}{
  672. "rep": false,
  673. })
  674. }
  675. }
  676. /**
  677. * 导入剑鱼批量修改数据
  678. */
  679. func ParsJyData(filebyte []byte) ([]map[string]interface{}, error) {
  680. var jyData []map[string]interface{}
  681. var keyName []string
  682. file, err := xlsx.OpenBinary(filebyte)
  683. if err != nil {
  684. return jyData, err
  685. }
  686. for i, v := range file.Sheets[0].Rows {
  687. data := make(map[string]interface{})
  688. for ii, vv := range v.Cells {
  689. if i == 0 {
  690. keyName = append(keyName, vv.Value)
  691. } else {
  692. if vv.Value != "" {
  693. data[keyName[ii]] = vv.Value
  694. }
  695. }
  696. }
  697. if len(data) > 0 {
  698. jyData = append(jyData, data)
  699. }
  700. }
  701. return jyData, nil
  702. }
  703. /**
  704. 批量删除
  705. */
  706. func (jy *RepairRule) RepairImport1() {
  707. defer qu.Catch()
  708. if jy.Method() == "POST" {
  709. mf, _, err := jy.GetFile("xlsx")
  710. if err == nil {
  711. binary, err := ioutil.ReadAll(mf)
  712. if err == nil {
  713. data, err := ParsJyData(binary)
  714. if err == nil {
  715. jy.ServeJson(map[string]interface{}{
  716. "data": data,
  717. "rep": true,
  718. })
  719. return
  720. }
  721. }
  722. }
  723. jy.ServeJson(map[string]interface{}{
  724. "rep": false,
  725. })
  726. }
  727. }
  728. /**
  729. 批量修改保存
  730. */
  731. func (jy *RepairRule) RepairModify() {
  732. defer qu.Catch()
  733. if jy.Method() == "POST" {
  734. user := jy.GetSession("user").(map[string]interface{})
  735. data := GetPostForm(jy.Request)
  736. var updata []map[string]interface{}
  737. json.Unmarshal([]byte(qu.ObjToString(data["data"])), &updata)
  738. var errs []map[string]interface{}
  739. for _, tmp := range updata {
  740. err := ModifyData(tmp, user)
  741. if err != nil {
  742. errs = append(errs, err)
  743. }
  744. time.Sleep(time.Millisecond * 1)
  745. }
  746. if len(errs) > 0 {
  747. jy.ServeJson(map[string]interface{}{
  748. "rep": false,
  749. "data": errs,
  750. })
  751. } else {
  752. jy.ServeJson(map[string]interface{}{
  753. "rep": true,
  754. })
  755. }
  756. }
  757. }
  758. func ModifyData(tmp map[string]interface{}, user map[string]interface{}) (err map[string]interface{}) {
  759. id := qu.ObjToString(tmp["_id"])
  760. old_data, _ := JYMgo.FindById(JyCollNameOne, id, `{}`)
  761. coll := ""
  762. if len(*old_data) > 0 {
  763. coll = JyCollNameOne
  764. } else {
  765. qu.Debug("ModifyData--------", id)
  766. old_data, _ = JYMgo.FindById(JyCollNameTwo, id, `{}`)
  767. coll = JyCollNameTwo
  768. }
  769. delete(*old_data, "_id")
  770. //new_data := Copy(*old_data).(map[string]interface{})
  771. new_data := make(map[string]interface{})
  772. modifyinfo := make(map[string]interface{})
  773. del_data := make(map[string]interface{})
  774. for k, v := range tmp {
  775. if k == "_id" {
  776. continue
  777. }
  778. if strings.EqualFold(strings.ToUpper(qu.ObjToString(v)), "DEL") {
  779. del_data[k] = ""
  780. delete(new_data, k)
  781. } else if k == "budget" || k == "bidamount" {
  782. new_data[k] = qu.Float64All(v)
  783. } else if strings.Contains(k, "time") && 10 == len(fmt.Sprint(v)) {
  784. new_data[k] = qu.Int64All(v)
  785. } else {
  786. new_data[k] = v
  787. }
  788. modifyinfo[k] = "剑鱼维护"
  789. }
  790. //变更字段
  791. if new_data["modifyinfo"] != nil {
  792. tmpinfo := new_data["modifyinfo"].(map[string]interface{})
  793. for k, v := range modifyinfo {
  794. tmpinfo[k] = v
  795. }
  796. new_data["modifyinfo"] = tmpinfo
  797. } else {
  798. new_data["modifyinfo"] = modifyinfo
  799. }
  800. query := bson.M{
  801. "_id": mongodb.StringTOBsonId(id),
  802. }
  803. delete(new_data, "_id")
  804. var set map[string]interface{}
  805. if len(del_data) == 0 {
  806. set = bson.M{
  807. "$set": new_data,
  808. }
  809. } else {
  810. set = bson.M{
  811. "$set": new_data,
  812. "$unset": del_data,
  813. }
  814. }
  815. rep := JYMgo.Update(coll, query, set, false, false)
  816. if !rep {
  817. return map[string]interface{}{"_id": id, "err": "更新失败"}
  818. }
  819. indexNode := *qu.ObjToMap(Sysconfig["indexNode"])
  820. by, _ := json.Marshal(map[string]interface{}{
  821. "query": map[string]interface{}{
  822. "_id": bson.M{
  823. "$gte": mongodb.StringTOBsonId(id),
  824. "$lte": mongodb.StringTOBsonId(id),
  825. }},
  826. "stype": qu.ObjToString(indexNode["stype"]),
  827. "coll": coll,
  828. })
  829. addr := &net.UDPAddr{
  830. IP: net.ParseIP(indexNode["addr"].(string)),
  831. Port: qu.IntAll(indexNode["port"]),
  832. }
  833. qu.Debug("udp------------", string(by))
  834. udptask.Udpclient.WriteUdp(by, mu.OP_TYPE_DATA, addr)
  835. if IsModifyPro(modifyinfo) {
  836. // udp 项目
  837. nextNode := *qu.ObjToMap(Sysconfig["jy_pro_node"])
  838. by1, _ := json.Marshal(map[string]interface{}{
  839. "infoid": id,
  840. "stype": "updateInfo",
  841. })
  842. addr1 := &net.UDPAddr{
  843. IP: net.ParseIP(nextNode["addr"].(string)),
  844. Port: qu.IntAll(nextNode["port"]),
  845. }
  846. qu.Debug("udp---2---------", string(by1))
  847. udptask.Udpclient.WriteUdp(by1, mu.OP_TYPE_DATA, addr1)
  848. }
  849. //删除redis 指定key
  850. delName1 := RedisDelKey1 + id + "_*"
  851. redis.DelByCodePattern(RedisJYName, delName1)
  852. //jyredis := redis.RedisPool[RedisJYName].Get()
  853. //defer jyredis.Close()
  854. //if _, err := jyredis.Do("SELECT", 0); err != nil {
  855. // log.Println("更新-redis-select-db失败")
  856. //} else {
  857. // delName1 := RedisDelKey1 + id + "_*"
  858. // if _, err1 := jyredis.Do("DEL", delName1); err1 != nil {
  859. // log.Println("更新-del-redis-fail:", delName1)
  860. // }
  861. //delName2 := RedisDelKey2 + id
  862. //if _, err2 := jyredis.Do("DEL", delName2); err2 != nil {
  863. // log.Println("更新-del-redis-fail:", delName2)
  864. //}
  865. //delName3 := RedisDelKey3 + id
  866. //if _, err3 := jyredis.Do("DEL", delName3); err3 != nil {
  867. // log.Println("更新-del-redis-fail:", delName3)
  868. //}
  869. //delName4 := RedisDelKey4 + id
  870. //if _, err4 := jyredis.Do("DEL", delName4); err4 != nil {
  871. // log.Println("更新-del-redis-fail:", delName4)
  872. //}
  873. //delName5 := RedisDelKey5 + id
  874. //if _, err5 := jyredis.Do("DEL", delName5); err5 != nil {
  875. // log.Println("更新-del-redis-fail:", delName5)
  876. //}
  877. //}
  878. delete(new_data, "modifyinfo")
  879. log_data := map[string]interface{}{
  880. "s_modifyuser": user["name"],
  881. "s_type": 2,
  882. "i_modifytime": time.Now().Unix(),
  883. "s_modifyreason": "批量修改",
  884. "s_backupid": id,
  885. "o_oldinfo": old_data,
  886. "o_newinfo": new_data,
  887. "modifyinfo": modifyinfo,
  888. }
  889. Mgo.Save(JyRecord, log_data)
  890. return nil
  891. }
  892. func (jy *RepairRule) RepairDel() {
  893. defer qu.Catch()
  894. if jy.Method() == "POST" {
  895. user := jy.GetSession("user").(map[string]interface{})
  896. data := GetPostForm(jy.Request)
  897. var updata []map[string]interface{}
  898. json.Unmarshal([]byte(qu.ObjToString(data["data"])), &updata)
  899. var errs []map[string]interface{}
  900. for _, tmp := range updata {
  901. err := ModifyData1(tmp, user)
  902. if err != nil {
  903. errs = append(errs, err)
  904. }
  905. time.Sleep(time.Millisecond * 1)
  906. }
  907. if len(errs) > 0 {
  908. jy.ServeJson(map[string]interface{}{
  909. "rep": false,
  910. "data": errs,
  911. })
  912. } else {
  913. jy.ServeJson(map[string]interface{}{
  914. "rep": true,
  915. })
  916. }
  917. }
  918. }
  919. func ModifyData1(tmp map[string]interface{}, user map[string]interface{}) (err map[string]interface{}) {
  920. id := qu.ObjToString(tmp["_id"])
  921. old_data, _ := JYMgo.FindById(JyCollNameOne, id, `{}`)
  922. coll := ""
  923. if len(*old_data) > 0 {
  924. coll = JyCollNameOne
  925. } else {
  926. old_data, _ = JYMgo.FindById(JyCollNameTwo, id, `{}`)
  927. coll = JyCollNameTwo
  928. }
  929. query := map[string]interface{}{
  930. "_id": mongodb.StringTOBsonId(id),
  931. }
  932. rep := JYMgo.Del(coll, query)
  933. if !rep {
  934. return map[string]interface{}{"_id": id, "err": "删除失败"}
  935. }
  936. //删除es
  937. client := elastic.GetEsConn()
  938. defer elastic.DestoryEsConn(client)
  939. _, err1 := client.Delete().Index(EsIndex).Type(EsType).Id(id).Refresh(true).Do()
  940. if err1 != nil {
  941. log.Println("delete es err:", err1)
  942. }
  943. // udp 项目
  944. nextNode := *qu.ObjToMap(Sysconfig["jy_pro_node"])
  945. by, _ := json.Marshal(map[string]interface{}{
  946. "infoid": id,
  947. "stype": "deleteInfo",
  948. })
  949. addr := &net.UDPAddr{
  950. IP: net.ParseIP(nextNode["addr"].(string)),
  951. Port: qu.IntAll(nextNode["port"]),
  952. }
  953. qu.Debug(string(by))
  954. udptask.Udpclient.WriteUdp(by, mu.OP_TYPE_DATA, addr)
  955. //删除redis 指定key
  956. delName1 := RedisDelKey1 + id + "_*"
  957. redis.DelByCodePattern(RedisJYName, delName1)
  958. //jyredis := redis.RedisPool[RedisJYName].Get()
  959. //defer jyredis.Close()
  960. //if _, err := jyredis.Do("SELECT", 0); err != nil {
  961. // log.Println("更新-redis-select-db失败")
  962. //} else {
  963. // delName1 := RedisDelKey1 + id + "_*"
  964. // if _, err1 := jyredis.Do("DEL", delName1); err1 != nil {
  965. // log.Println("更新-del-redis-fail:", delName1)
  966. // }
  967. //delName2 := RedisDelKey2 + id
  968. //if _, err2 := jyredis.Do("DEL", delName2); err2 != nil {
  969. // log.Println("更新-del-redis-fail:", delName2)
  970. //}
  971. //delName3 := RedisDelKey3 + id
  972. //if _, err3 := jyredis.Do("DEL", delName3); err3 != nil {
  973. // log.Println("更新-del-redis-fail:", delName3)
  974. //}
  975. //delName4 := RedisDelKey4 + id
  976. //if _, err4 := jyredis.Do("DEL", delName4); err4 != nil {
  977. // log.Println("更新-del-redis-fail:", delName4)
  978. //}
  979. //delName5 := RedisDelKey5 + id
  980. //if _, err5 := jyredis.Do("DEL", delName5); err5 != nil {
  981. // log.Println("更新-del-redis-fail:", delName5)
  982. //}
  983. //}
  984. log_data := map[string]interface{}{
  985. "s_modifyuser": user["name"],
  986. "s_type": 2,
  987. "i_modifytime": time.Now().Unix(),
  988. "s_modifyreason": "批量删除",
  989. "s_backupid": id,
  990. "o_oldinfo": old_data,
  991. "modifyinfo": "删除数据",
  992. }
  993. Mgo.Save(JyRecord, log_data)
  994. return nil
  995. }
  996. func (jy *RepairRule) EsCount() {
  997. defer qu.Catch()
  998. value := jy.GetString("data")
  999. field := jy.GetString("field")
  1000. esquery := `{"query":{"bool":{"must":[{"term":{"` + field + `":"` + value + `"}}]}}}`
  1001. count := elastic.Count(EsIndex, EsType, esquery)
  1002. jy.ServeJson(map[string]interface{}{
  1003. "rep": true,
  1004. "count": count,
  1005. })
  1006. }
  1007. func (jy *RepairRule) EsDelBy() {
  1008. defer qu.Catch()
  1009. value := jy.GetString("data")
  1010. field := jy.GetString("field")
  1011. count, _ := jy.GetInt("count")
  1012. esquery := `{"query":{"bool":{"must":[{"term":{"` + field + `":"` + value + `"}}]}}}`
  1013. bol := elastic.Del(EsIndex, EsType, esquery)
  1014. Mgo.Save("jy_es_del_log", bson.M{"type": field, "value": value, "count": count, "createtime": time.Now().Unix()})
  1015. jy.ServeJson(map[string]interface{}{
  1016. "rep": bol,
  1017. })
  1018. }
  1019. func (jy *RepairRule) EsDelRecord() {
  1020. defer qu.Catch()
  1021. if jy.Method() == "POST" {
  1022. start, _ := jy.GetInteger("start")
  1023. limit, _ := jy.GetInteger("length")
  1024. draw, _ := jy.GetInteger("draw")
  1025. searchStr := jy.GetString("search[value]")
  1026. search := strings.TrimSpace(searchStr)
  1027. query := bson.M{}
  1028. if search != "" {
  1029. query["$or"] = []interface{}{
  1030. //bson.M{"s_customer": bson.M{"$regex": search}},
  1031. //bson.M{"s_tagname": bson.M{"$regex": search}},
  1032. }
  1033. }
  1034. data, _ := Mgo.Find("jy_es_del_log", query, `{"createtime": -1}`, nil, false, start, limit)
  1035. count := Mgo.Count("jy_es_del_log", query)
  1036. jy.ServeJson(map[string]interface{}{
  1037. "draw": draw,
  1038. "data": data,
  1039. "recordsFiltered": count,
  1040. "recordsTotal": count,
  1041. })
  1042. }
  1043. }