rulemanager.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. package audit
  2. import (
  3. "fmt"
  4. "html/template"
  5. . "jy/admin"
  6. . "jy/mongodbutil"
  7. ju "jy/util"
  8. qu "qfw/util"
  9. "regexp"
  10. "strings"
  11. "time"
  12. "github.com/gin-contrib/sessions"
  13. "github.com/gin-gonic/gin"
  14. )
  15. func init() {
  16. //列表、编辑
  17. //识别字段
  18. Admin.GET("/audit/recogfield", func(c *gin.Context) {
  19. c.HTML(200, "audit_recogfield.html", nil)
  20. })
  21. Admin.POST("/rulemanager/getrecogfield", GetRecogField) //获取识别字段
  22. Admin.POST("/rulemanager/saverecogfield", SaveRecogField) //保存
  23. Admin.POST("/rulemanager/delrecogfield", DelRecogField) //删除
  24. //class
  25. Admin.GET("/rulemanager/getclasslist", func(c *gin.Context) {
  26. fname := c.Query("fname")
  27. fid := c.Query("id")
  28. c.HTML(200, "audit_classlist.html", gin.H{"fname": fname, "fid": fid})
  29. })
  30. Admin.POST("/rulemanager/getclass", GetClass) //获取分类
  31. Admin.POST("/rulemanager/saveclass", SaveClass) //保存
  32. Admin.POST("/rulemanager/getauditfield", GetAuditField) //获取要审核的字段
  33. Admin.POST("/rulemanager/delclass", DelClass) //删除
  34. //Admin.POST("/rulemanager/getclist", GetCList) //获取父分类
  35. //rule
  36. Admin.GET("/rulemanager/getrulelist", func(c *gin.Context) {
  37. cid := c.Query("id")
  38. cname := c.Query("cname")
  39. fid := c.Query("fid")
  40. // class, _ := Mgo.FindById("rc_class", cid, `{"_id":1,"s_name":1,"s_pid":1}`)
  41. // //级联查询,往上查
  42. // if class != nil && *class != nil {
  43. // pid := qu.ObjToString((*class)["s_pid"])
  44. // if pid != "" {
  45. // GetParent(pid, "rc_class", class)
  46. // }
  47. // }
  48. rule, _ := Mgo.Find("rc_rule", `{"s_classid":"`+cid+`","delete":false}`, `{"i_order":1}`, nil, false, 0, 200)
  49. //Finds := map[string]interface{}{}
  50. data := map[string]interface{}{}
  51. // for _, r := range *rule {
  52. // rpid := qu.ObjToString(r["s_pid"])
  53. // if rpid != "" {
  54. // if Finds[rpid] != nil {
  55. // r["p"] = Finds[rpid]
  56. // } else {
  57. // GetParent(rpid, "rc_rule", &r)
  58. // Finds[rpid] = r["p"]
  59. // }
  60. // }
  61. // }
  62. data["rule"] = rule
  63. //data["class"] = class
  64. c.HTML(200, "audit_rulelist.html", gin.H{"cid": cid, "cname": cname, "data": data, "fid": fid})
  65. })
  66. Admin.POST("/rulemanager/getrule", GetRule) //获取规则
  67. Admin.POST("/rulemanager/saverule", SaveRule) //保存规则
  68. // Admin.POST("/rulemanager/getrlist", GetRList) //获取父分类中的所有规则
  69. Admin.POST("/rulemanager/delrule", DelRule) //删除
  70. Admin.POST("/rulemanager/shift", Shift) //移动
  71. Admin.POST("/rulemanager/runruletest", RunRuleTest) //规则测试
  72. }
  73. func GetRecogField(c *gin.Context) {
  74. data, _ := Mgo.Find("rc_field", `{"delete":false}`, `{"i_order":1}`, nil, false, -1, -1)
  75. for _, d := range *data {
  76. timeStr := time.Unix(d["l_lasttime"].(int64), 0).Format(Date_Short_Layout)
  77. d["l_lasttime"] = timeStr
  78. }
  79. c.JSON(200, gin.H{"data": data})
  80. }
  81. func SaveRecogField(c *gin.Context) {
  82. data := GetPostForm(c)
  83. _id, _ := c.GetPostForm("_id")
  84. session := sessions.Default(c)
  85. if _id != "" {
  86. Mgo.UpdateById("rc_field", _id, map[string]interface{}{"$set": data})
  87. c.JSON(200, gin.H{"rep": true})
  88. } else {
  89. //s_class, _ := c.GetPostForm("s_class")
  90. s_recogfield, _ := c.GetPostForm("s_recogfield")
  91. d, _ := Mgo.FindOne("rc_field", `{"s_recogfield":"`+s_recogfield+`","delete":false}`)
  92. if len(*d) > 0 {
  93. c.JSON(200, gin.H{"msg": "已存在!"})
  94. } else {
  95. data["l_lasttime"] = time.Now().Unix()
  96. //data["l_date"] = time.Now().Unix()
  97. data["s_user"] = session.Get("username")
  98. data["delete"] = false
  99. Mgo.Save("rc_field", data)
  100. c.JSON(200, gin.H{"rep": true})
  101. }
  102. }
  103. }
  104. func DelRecogField(c *gin.Context) {
  105. //删除识别字段,对应的删除识别字段下的分类和规则
  106. _id, _ := c.GetPostForm("_id")
  107. b := Mgo.Update("rc_field", `{"_id":"`+_id+`"}`, map[string]interface{}{
  108. "$set": map[string]interface{}{"delete": true},
  109. }, false, false)
  110. go DelClassAndRule(_id)
  111. if b {
  112. c.JSON(200, gin.H{"rep": true})
  113. } else {
  114. c.JSON(200, gin.H{"rep": false})
  115. }
  116. }
  117. func GetClass(c *gin.Context) {
  118. s_fid, _ := c.GetPostForm("fid")
  119. data, _ := Mgo.Find("rc_class", `{"s_fid":"`+s_fid+`","delete":false}`, `{"i_order":1}`, nil, false, -1, -1)
  120. for _, d := range *data {
  121. timeStr := time.Unix(d["l_lasttime"].(int64), 0).Format(Date_Short_Layout)
  122. d["l_lasttime"] = timeStr
  123. }
  124. c.JSON(200, gin.H{"data": data})
  125. }
  126. func SaveClass(c *gin.Context) {
  127. data := GetPostForm(c)
  128. _id, _ := c.GetPostForm("_id")
  129. session := sessions.Default(c)
  130. if _id != "" {
  131. Mgo.UpdateById("rc_class", _id, map[string]interface{}{"$set": data})
  132. c.JSON(200, gin.H{"rep": true})
  133. } else {
  134. s_name, _ := c.GetPostForm("s_name")
  135. s_recogfield, _ := c.GetPostForm("s_recogfield")
  136. class, _ := Mgo.FindOne("rc_class", `{"s_name":"`+s_name+`","s_recogfield":"`+s_recogfield+`","delete":false}`)
  137. if len(*class) > 0 {
  138. c.JSON(200, gin.H{"rep": false})
  139. } else {
  140. data["l_lasttime"] = time.Now().Unix()
  141. //data["l_date"] = time.Now().Unix()
  142. data["s_user"] = session.Get("username")
  143. data["i_order"] = GetOrder("class")
  144. data["delete"] = false
  145. Mgo.Save("rc_class", data)
  146. c.JSON(200, gin.H{"rep": true})
  147. }
  148. }
  149. }
  150. func GetAuditField(c *gin.Context) {
  151. v, _ := Mgo.FindOne("version", `{"isuse":true,"delete":false}`)
  152. if len(*v) > 0 {
  153. vid := qu.BsonIdToSId((*v)["_id"])
  154. query := map[string]interface{}{
  155. "isaudit": true,
  156. "delete": false,
  157. "vid": vid,
  158. }
  159. data, _ := Mgo.Find("versioninfo", query, `{"_id":-1}`, nil, false, -1, -1)
  160. for _, d := range *data {
  161. d["_id"] = qu.BsonIdToSId(d["s_field"])
  162. d["s_name"] = d["s_field"]
  163. }
  164. c.JSON(200, gin.H{"data": data})
  165. } else {
  166. c.JSON(200, gin.H{"data": map[string]interface{}{}})
  167. }
  168. }
  169. func DelClass(c *gin.Context) {
  170. _id, _ := c.GetPostForm("_id")
  171. b := Mgo.Update("rc_class", `{"_id":"`+_id+`"}`, map[string]interface{}{
  172. "$set": map[string]interface{}{"delete": true},
  173. }, false, false)
  174. go DelRuleByClass(_id)
  175. if b {
  176. c.JSON(200, gin.H{"rep": true})
  177. } else {
  178. c.JSON(200, gin.H{"rep": false})
  179. }
  180. }
  181. func GetOrder(sel string) int {
  182. q := `{"s_name":"` + sel + `"}`
  183. res, _ := Mgo.FindOneByField("rc_order", q, `{"i_order":1}`)
  184. v := qu.IntAllDef((*res)["i_order"], 1)
  185. Mgo.Update("rc_order", q, &map[string]interface{}{
  186. "s_name": sel,
  187. "i_order": (v + 1),
  188. }, true, false)
  189. return v
  190. }
  191. func GetRule(c *gin.Context) {
  192. cid, _ := c.GetPostForm("cid")
  193. data, _ := Mgo.Find("rc_rule", `{"s_classid":"`+cid+`","delete":false}`, `{"i_order":1}`, nil, false, -1, -1)
  194. for _, d := range *data {
  195. timeStr := time.Unix(d["l_lasttime"].(int64), 0).Format(Date_Short_Layout)
  196. d["l_lasttime"] = timeStr
  197. }
  198. c.JSON(200, gin.H{"data": data})
  199. }
  200. func SaveRule(c *gin.Context) {
  201. data := GetPostForm(c)
  202. _id, _ := c.GetPostForm("_id")
  203. session := sessions.Default(c)
  204. var s_ruleArr []string
  205. rule, _ := c.GetPostForm("s_rule")
  206. rule = regexp.MustCompile("\\n|\\s+").ReplaceAllString(rule, "")
  207. s_rule := strings.Split(rule, "'")
  208. for k, r := range s_rule {
  209. if k%2 == 1 { //奇数为正则
  210. r = "'" + r + "'" //为正则加上''
  211. if r != "" {
  212. s_ruleArr = append(s_ruleArr, r)
  213. }
  214. } else {
  215. fr := strings.Split(r, ",")
  216. for _, frs := range fr {
  217. if frs != "" {
  218. s_ruleArr = append(s_ruleArr, frs)
  219. }
  220. }
  221. }
  222. }
  223. data["s_rule"] = s_ruleArr
  224. if _id != "" {
  225. Mgo.UpdateById("rc_rule", _id, map[string]interface{}{"$set": data})
  226. c.JSON(200, gin.H{"rep": true})
  227. } else {
  228. s_name, _ := c.GetPostForm("s_name")
  229. d, _ := Mgo.FindOne("rc_rule", `{"s_name":"`+s_name+`","delete":false}`)
  230. if len(*d) > 0 {
  231. c.JSON(200, gin.H{"msg": "已存在!"})
  232. } else {
  233. data["l_lasttime"] = time.Now().Unix()
  234. data["l_date"] = time.Now().Unix()
  235. data["s_user"] = session.Get("username")
  236. data["i_order"] = GetOrder("rule")
  237. data["delete"] = false
  238. Mgo.Save("rc_rule", data)
  239. c.JSON(200, gin.H{"rep": true})
  240. }
  241. }
  242. }
  243. func DelRule(c *gin.Context) {
  244. _id, _ := c.GetPostForm("_id")
  245. b := Mgo.Update("rc_rule", `{"_id":"`+_id+`"}`, map[string]interface{}{
  246. "$set": map[string]interface{}{"delete": true},
  247. }, false, false)
  248. if b {
  249. c.JSON(200, gin.H{"rep": true})
  250. } else {
  251. c.JSON(200, gin.H{"rep": false})
  252. }
  253. }
  254. func Shift(c *gin.Context) {
  255. str, _ := c.GetPostForm("str")
  256. strs := strings.Split(str, ",")
  257. if str == "" {
  258. c.JSON(200, gin.H{"rep": false})
  259. } else {
  260. b := Mgo.Update("rc_rule", `{"_id":"`+strs[0]+`"}`, &map[string]interface{}{
  261. "$set": map[string]interface{}{
  262. "i_order": qu.IntAll(strs[3]),
  263. },
  264. }, false, false)
  265. if b {
  266. Mgo.Update("rc_rule", `{"_id":"`+strs[2]+`"}`, &map[string]interface{}{
  267. "$set": map[string]interface{}{
  268. "i_order": qu.IntAll(strs[1]),
  269. },
  270. }, false, false)
  271. c.JSON(200, gin.H{"rep": true})
  272. } else {
  273. c.JSON(200, gin.H{"rep": false})
  274. }
  275. }
  276. }
  277. func RunRuleTest(c *gin.Context) {
  278. _id, _ := c.GetPostForm("_id")
  279. s_con, _ := c.GetPostForm("s_con")
  280. data := map[string]interface{}{
  281. "b": false,
  282. "rules": "",
  283. "text": "",
  284. "pos": 0,
  285. }
  286. if _id == "" || s_con == "" {
  287. c.JSON(200, gin.H{"rep": false})
  288. } else {
  289. rule, _ := Mgo.FindById("rc_rule", _id, `{"s_rule":1,"s_rule_prerule":1}`)
  290. s_rule := qu.ObjArrToStringArr((*rule)["s_rule"].([]interface{}))
  291. s_con = ju.PreFilter(s_con, qu.ObjToString((*rule)["s_rule_prerule"])) //对文本先进行前置过滤
  292. text, rules, pos, b := ju.ClassificationText(s_con, ju.AnalyRules(s_rule))
  293. data["b"] = b
  294. data["pos"] = pos + 1
  295. data["rules"] = func() template.HTML {
  296. ts := []string{}
  297. for ppos, s := range rules {
  298. ts = append(ts, fmt.Sprintf("<p>%d.%s</p>", ppos+1, strings.Join(s, "")))
  299. }
  300. return template.HTML(strings.Join(ts, ""))
  301. }()
  302. data["text"] = template.HTML(text)
  303. c.HTML(200, "checkres.html", gin.H{"data": data})
  304. }
  305. }
  306. //func GetParent(s_pid, coll string, pm *map[string]interface{}) {
  307. // if s_pid != "" {
  308. // //可能有多个父类
  309. // ids := strings.Split(s_pid, ",")
  310. // idsmap := []map[string]interface{}{}
  311. // for _, val := range ids {
  312. // idsmap = append(idsmap, map[string]interface{}{
  313. // "_id": qu.StringTOBsonId(val),
  314. // })
  315. // }
  316. // class, b := Mgo.Find(coll, &map[string]interface{}{
  317. // "$or": idsmap,
  318. // }, nil, `{"_id":1,"s_name":1,"s_pid":1}`, false, 0, 10)
  319. // if b && class != nil && len(*class) > 0 {
  320. // for _, cla := range *class {
  321. // pid := qu.ObjToString(cla["s_pid"])
  322. // GetParent(pid, coll, &cla)
  323. // }
  324. // (*pm)["p"] = class
  325. // }
  326. // }
  327. // return
  328. //}
  329. func DelClassAndRule(fid string) {
  330. class, _ := Mgo.Find("rc_class", `{"s_fid":"`+fid+`"}`, nil, `{"_id":1}`, false, -1, -1)
  331. for _, c := range *class {
  332. Mgo.UpdateById("rc_class", c["_id"], map[string]interface{}{ //删除class
  333. "$set": map[string]interface{}{"delete": true},
  334. })
  335. classid := qu.BsonIdToSId(c["_id"])
  336. Mgo.Update("rc_rule", `{"s_classid":"`+classid+`"}`, map[string]interface{}{ //删除class
  337. "$set": map[string]interface{}{"delete": true},
  338. }, false, true)
  339. }
  340. }
  341. func DelRuleByClass(classid string) {
  342. Mgo.Update("rc_rule", `{"s_classid":"`+classid+`"}`, map[string]interface{}{ //删除class
  343. "$set": map[string]interface{}{"delete": true},
  344. }, false, true)
  345. }
  346. //func GetCList(c *gin.Context) {
  347. // nid := c.Query("nid")
  348. // fname := c.Query("fname")
  349. // query := map[string]interface{}{
  350. // "delete": false,
  351. // }
  352. // if nid != "" {
  353. // query = map[string]interface{}{
  354. // "_id": map[string]interface{}{
  355. // "$ne": qu.StringTOBsonId(nid),
  356. // },
  357. // }
  358. // }
  359. // if fname != "" {
  360. // query = map[string]interface{}{
  361. // "s_recogfield": map[string]interface{}{
  362. // "$ne": fname,
  363. // },
  364. // }
  365. // }
  366. // class, _ := Mgo.Find("rc_class", query, nil, `{"s_name":1}`, false, 0, 200)
  367. // c.JSON(200, gin.H{"data": class})
  368. //}
  369. //func GetRList(c *gin.Context) {
  370. // cid := c.Query("id")
  371. // if cid == "" {
  372. // c.JSON(200, gin.H{"data": map[string]interface{}{}})
  373. // } else {
  374. // //先找类
  375. // ids := strings.Split(cid, ",")
  376. // ids2 := make([]string, len(ids))
  377. // idsmap := []map[string]interface{}{}
  378. // for n, val := range ids {
  379. // idsmap = append(idsmap, map[string]interface{}{
  380. // "_id": qu.StringTOBsonId(val),
  381. // })
  382. // ids2[n] = fmt.Sprintf(`{"s_classid":"%s"}`, val)
  383. // }
  384. // class, _ := Mgo.Find("rc_class", &map[string]interface{}{
  385. // "$or": idsmap,
  386. // }, nil, `{"s_name":1}`, false, 0, 10)
  387. // rule, _ := Mgo.Find("rc_rule", fmt.Sprintf(`{"$or":[%s]}`, strings.Join(ids2, ",")), `{"i_order":1}`, `{"s_name":1,"s_code":1,"s_classid":1}`, false, 0, 200)
  388. // for _, ru := range *rule {
  389. // for _, c := range *class {
  390. // if qu.BsonIdToSId(c["_id"]) == ru["s_classid"] {
  391. // ru["s_name"] = fmt.Sprintf("%s[%s]", ru["s_name"], c["s_name"])
  392. // }
  393. // }
  394. // }
  395. // c.JSON(200, gin.H{"data": rule})
  396. // }
  397. //}