ownTag.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. package client
  2. import (
  3. "cmplatform/util"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "app.yhyue.com/moapp/jybase/common"
  8. "app.yhyue.com/moapp/jybase/encrypt"
  9. "app.yhyue.com/moapp/jybase/go-xweb/xweb"
  10. "app.yhyue.com/moapp/jybase/mongodb"
  11. "github.com/lauyoume/gopinyin"
  12. "go.mongodb.org/mongo-driver/bson"
  13. "go.mongodb.org/mongo-driver/bson/primitive"
  14. "log"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. type OwnTag struct {
  20. *xweb.Action
  21. customerSave xweb.Mapper `xweb:"/client/customer/save"` //客户信息保存
  22. euserTagEdit xweb.Mapper `xweb:"/client/customer/tag/edit"` //客户信息保存
  23. euserImport xweb.Mapper `xweb:"/client/customer/tag/import"` //私有标签导入关键词
  24. customerHistory xweb.Mapper `xweb:"/client/customer/history"` //私有标签导入关键词
  25. customerEdit xweb.Mapper `xweb:"/client/customer/edit"` //编辑客户信息
  26. tagSetup xweb.Mapper `xweb:"/client/customer/tag/setup"` //标签的启用禁用
  27. tagDel xweb.Mapper `xweb:"/client/customer/tag/del"` //删除标签
  28. cuserRuleList xweb.Mapper `xweb:"/client/tag/ruleList"` //规则列表 客户
  29. tagAssociated xweb.Mapper `xweb:"/client/customer/tag/associated"` //私有标签关联规则
  30. addhistoryrule xweb.Mapper `xweb:"/client/rule/history/add"` //新增历史任务
  31. historyRuleCreate xweb.Mapper `xweb:"/client/customer/rule/history/edit"` //新增历史任务规则
  32. historyTagDel xweb.Mapper `xweb:"/client/tag/history/del"` //历史任务删除私有标签
  33. historyTagAssociated xweb.Mapper `xweb:"/client/customer/historytag/associated"` //私有标签关联历史规则
  34. saveHistoryRule xweb.Mapper `xweb:"/client/customer/history/saverule"` //保存历史规则
  35. delHistoryRule xweb.Mapper `xweb:"/client/customer/history/delrule"` //删除历史任务规则
  36. historyRuleEdit xweb.Mapper `xweb:"/client/historyRule/edit"` //编辑历史规则
  37. customerAllRule xweb.Mapper `xweb:"/client/customer/history/allrule"` //用户历史规则
  38. }
  39. // 保存客户信息
  40. func (c *OwnTag) CustomerSave() {
  41. defer common.Catch()
  42. if c.Method() == "POST" {
  43. //data := GetPostForm(c.Request)
  44. data := map[string]interface{}{}
  45. err := json.Unmarshal(c.Body(), &data)
  46. if err != nil {
  47. log.Println("err", err)
  48. }
  49. log.Println("接收数据:", data)
  50. id := common.ObjToString(data["id"])
  51. delete(data, "id")
  52. i_createtime := time.Now().Unix()
  53. data["i_updatetime"] = i_createtime
  54. var rep = false
  55. if id == "" {
  56. departArr := []map[string]interface{}{}
  57. departArrStr := data["depart"].(string)
  58. json.Unmarshal([]byte(departArrStr), &departArr)
  59. delete(data, "depart")
  60. data["b_delete"] = false
  61. data["i_createtime"] = i_createtime
  62. data["i_state"] = 0
  63. id = util.Mgo.Save("cuser", data)
  64. if id != "" {
  65. for _, v := range departArr {
  66. v["s_userid"] = id
  67. v["i_createtime"] = time.Now().Unix()
  68. v["i_updatetime"] = time.Now().Unix()
  69. v["i_isuse"] = common.IntAll(v["i_isuse"])
  70. v["b_delete"] = false
  71. }
  72. if len(departArr) == 0 {
  73. rep = true
  74. } else {
  75. rep = util.Mgo.SaveBulk("cuserdepart", departArr...)
  76. }
  77. } else {
  78. rep = false
  79. }
  80. } else {
  81. if data["mails"] != nil {
  82. mailsMap := map[string]interface{}{}
  83. mails := common.ObjArrToStringArr(data["mails"].([]interface{}))
  84. for _, val := range mails {
  85. mailArr := strings.Split(val, "+")
  86. mailsMap[mailArr[0]] = mailArr[1]
  87. }
  88. log.Println(mailsMap)
  89. appid := common.ObjToString(data["s_appid"])
  90. q := bson.M{
  91. "appid": appid,
  92. }
  93. setMap := map[string]interface{}{
  94. "mails": mailsMap,
  95. }
  96. rep = util.MgoCus.Update("user", q, bson.M{"$set": setMap}, false, false)
  97. c.ServeJson(map[string]interface{}{
  98. "rep": rep,
  99. })
  100. return
  101. }
  102. delete(data, "mails")
  103. query := bson.M{
  104. "_id": mongodb.StringTOBsonId(id),
  105. }
  106. rep = util.Mgo.Update("cuser", query, bson.M{"$set": data}, false, false)
  107. }
  108. c.ServeJson(map[string]interface{}{
  109. "rep": rep,
  110. })
  111. }
  112. }
  113. // 新建/编辑标签
  114. func (c *OwnTag) EuserTagEdit() {
  115. defer common.Catch()
  116. if c.Method() == "POST" {
  117. _id := c.GetString("_id")
  118. data := util.GetPostForm(c.Request)
  119. cid := common.ObjToString(data["s_userid"])
  120. createTime := time.Now().Unix()
  121. o_rules := []map[string]interface{}{}
  122. o_rulesStr := data["o_list"].(string)
  123. json.Unmarshal([]byte(o_rulesStr), &o_rules)
  124. data["o_list"] = o_rules
  125. hid := common.ObjToString(data["hid"])
  126. var rep = true
  127. if hid == "" {
  128. if _id == "" {
  129. data["i_createtime"] = createTime
  130. data["i_updatetime"] = createTime
  131. data["b_delete"] = false
  132. _id = util.Mgo.Save("eusertagrule", data)
  133. } else {
  134. query := bson.M{
  135. "_id": mongodb.StringTOBsonId(_id),
  136. }
  137. rep = util.Mgo.Update("eusertagrule", query, bson.M{"$set": data}, false, false)
  138. }
  139. } else {
  140. if _id == "" {
  141. data["i_createtime"] = createTime
  142. data["i_updatetime"] = createTime
  143. data["i_type"] = "historyData"
  144. data["b_delete"] = true
  145. _id = util.Mgo.Save("eusertagrule", data)
  146. rep = AddHistoryLogTag(data, false, _id)
  147. } else {
  148. query := bson.M{
  149. "_id": mongodb.StringTOBsonId(_id),
  150. }
  151. reps := util.Mgo.Update("eusertagrule", query, bson.M{"$set": data}, false, false)
  152. if reps {
  153. rep = AddHistoryLogTag(data, true, _id)
  154. }
  155. }
  156. }
  157. c.ServeJson(map[string]interface{}{
  158. "id": cid,
  159. "rep": rep,
  160. })
  161. } else {
  162. if c.GetString("id") != "" {
  163. data, _ := util.Mgo.FindById("eusertagrule", c.GetString("id"), nil)
  164. c.T["tagrule"] = *data
  165. }
  166. cid := c.GetString("cid")
  167. c.T["cid"] = cid
  168. c.T["matchTypeMap"] = util.MatchTypeMap3
  169. c.T["hid"] = c.GetString("hid")
  170. c.Render("client/customer_tag_rule.html", &c.T)
  171. }
  172. }
  173. func (c *OwnTag) CustomerHistory() {
  174. defer common.Catch()
  175. id := c.GetString("id")
  176. user := c.GetSession("user").(map[string]interface{})
  177. query := bson.M{}
  178. query["_id"] = mongodb.StringTOBsonId(id)
  179. hid := c.GetString("hid")
  180. history_id := ""
  181. if hid != "" {
  182. history_id = hid
  183. hData, ok := util.Mgo.FindOne("historylog", bson.M{"_id": mongodb.StringTOBsonId(history_id)})
  184. if ok && hData != nil && len(*hData) > 0 {
  185. c.T["history_rule"] = (*hData)["dep_rules"]
  186. c.T["tagrule"] = (*hData)["tag_rules"]
  187. (*hData)["_id"] = id
  188. c.T["data"] = *hData
  189. }
  190. } else {
  191. var user_name, user_customername, user_salesperson, appid = "", "", "", ""
  192. var push_model, push_type, i_push = 0, 0, 0
  193. userInfo, ok := util.Mgo.FindOne("euser", query)
  194. if ok && userInfo != nil && *userInfo != nil {
  195. user_name = common.ObjToString((*userInfo)["s_name"])
  196. user_customername = common.ObjToString((*userInfo)["s_customername"])
  197. user_salesperson = common.ObjToString((*userInfo)["s_salesperson"])
  198. push_model = common.IntAll((*userInfo)["i_pushmodel"])
  199. push_type = common.IntAll((*userInfo)["i_pushtype"])
  200. i_push = common.IntAll((*userInfo)["i_push"])
  201. appid = common.ObjToString((*userInfo)["s_appid"])
  202. }
  203. history_id = util.Mgo.Save("historylog", map[string]interface{}{
  204. "createtime": time.Now().Unix(),
  205. "updatetime": time.Now().Unix(),
  206. "user_id": id,
  207. "create_user": user["name"],
  208. "state": 0,
  209. "s_name": user_name,
  210. "s_customername": user_customername,
  211. "s_salesperson": user_salesperson,
  212. "i_pushmodel": push_model,
  213. "i_pushtype": push_type,
  214. "i_push": i_push,
  215. "s_appid": appid,
  216. })
  217. c.Redirect("/client/customer/history?id=" + id + "&hid=" + history_id)
  218. return
  219. }
  220. c.T["history_id"] = history_id
  221. c.Render("client/customer_history.html", &c.T)
  222. }
  223. // 导入关键词
  224. func (c *OwnTag) EuserImport() {
  225. defer common.Catch()
  226. if c.Method() == "POST" {
  227. mf, _, err := c.GetFile("xlsx")
  228. if err == nil {
  229. binary, err := io.ReadAll(mf)
  230. if err == nil {
  231. rdata, err := util.Parsxlsx1(binary)
  232. if err == nil {
  233. c.ServeJson(map[string]interface{}{
  234. "rdata": rdata,
  235. "rep": true,
  236. })
  237. return
  238. }
  239. }
  240. }
  241. c.ServeJson(map[string]interface{}{
  242. "rep": false,
  243. })
  244. }
  245. }
  246. // 编辑客户信息
  247. func (c *OwnTag) CustomerEdit() {
  248. defer common.Catch()
  249. id := c.GetString("id")
  250. query := bson.M{}
  251. query["_id"] = mongodb.StringTOBsonId(id)
  252. data, _ := util.Mgo.FindOneByField("cuser", query, `{}`)
  253. depart, _ := util.Mgo.Find("cuserdepart", bson.M{"s_userid": id, "b_delete": false}, nil, nil, false, -1, -1)
  254. (*data)["_id"] = mongodb.BsonIdToSId((*data)["_id"])
  255. if (*data)["i_state"] == 1 {
  256. tagRule, _ := util.Mgo.Find("eusertagrule", bson.M{"s_userid": id, "b_delete": false}, nil, nil, false, -1, -1)
  257. c.T["tagrule"] = *tagRule
  258. }
  259. c.T["data"] = *data
  260. c.T["depart"] = *depart
  261. c.Render("client/index.html", &c.T)
  262. }
  263. // 标签的启用禁用
  264. func (c *OwnTag) TagSetup() {
  265. defer common.Catch()
  266. if c.Method() == "POST" {
  267. _id := c.GetString("_id")
  268. i_isuse, _ := c.GetInt("i_isuse")
  269. set := bson.M{
  270. "$set": bson.M{
  271. "i_isuse": i_isuse,
  272. },
  273. }
  274. b := util.Mgo.UpdateById("eusertagrule", _id, set)
  275. c.ServeJson(map[string]interface{}{
  276. "rep": b,
  277. })
  278. }
  279. }
  280. // 删除标签
  281. func (c *OwnTag) TagDel() {
  282. defer common.Catch()
  283. _id := c.GetString("_id")
  284. set := bson.M{
  285. "$set": bson.M{
  286. "b_delete": true,
  287. },
  288. }
  289. b := util.Mgo.UpdateById("eusertagrule", _id, set)
  290. c.ServeJson(map[string]interface{}{
  291. "rep": b,
  292. })
  293. }
  294. // 关联规则列表
  295. func (c *OwnTag) CuserRuleList() {
  296. defer common.Catch()
  297. if c.Method() == "POST" {
  298. eid := c.GetString("eid")
  299. start, _ := c.GetInt("start")
  300. limit, _ := c.GetInt("length")
  301. draw, _ := c.GetInt("draw")
  302. searchStr := c.GetString("search[value]")
  303. search := strings.TrimSpace(searchStr)
  304. query := bson.M{
  305. "s_userid": eid,
  306. "i_isuse": 1,
  307. "b_delete": false,
  308. }
  309. if search != "" {
  310. query["$or"] = []interface{}{
  311. bson.M{"s_name": bson.M{"$regex": search}},
  312. }
  313. }
  314. data, _ := util.Mgo.Find("euserdepartrule", query, `{"i_createtime":-1}`, nil, false, int(start), int(limit))
  315. for _, v := range *data {
  316. d, _ := util.Mgo.FindById("euserdepart", common.ObjToString(v["s_departid"]), nil)
  317. v["s_depart"] = common.ObjToString((*d)["s_name"])
  318. }
  319. count := util.Mgo.Count("euserdepartrule", query)
  320. c.ServeJson(map[string]interface{}{
  321. "draw": draw,
  322. "data": data,
  323. "recordsFiltered": count,
  324. "recordsTotal": count,
  325. })
  326. }
  327. }
  328. func (c *OwnTag) TagAssociated() {
  329. defer common.Catch()
  330. _id := c.GetString("_id")
  331. ids := c.GetString("ids")
  332. set := bson.M{
  333. "$set": bson.M{
  334. "o_departruleids": ids,
  335. },
  336. }
  337. b := util.Mgo.UpdateById("eusertagrule", _id, set)
  338. c.ServeJson(map[string]interface{}{
  339. "rep": b,
  340. })
  341. }
  342. func (c *OwnTag) Addhistoryrule() {
  343. defer common.Catch()
  344. if c.Method() == "POST" {
  345. his_id := c.GetString("history_id")
  346. //log.Println("hisid", his_id)
  347. if his_id != "" {
  348. //newHistory["tag_rules"] = usertags
  349. push_model, _ := strconv.Atoi(c.GetString("send_mode"))
  350. push_type, _ := strconv.Atoi(c.GetString("push_type"))
  351. isDup, _ := strconv.Atoi(c.GetString("isDup"))
  352. i_updatetime, _ := strconv.ParseInt(c.GetString("i_updatetime"), 10, 64)
  353. i_extfieldstype, _ := strconv.Atoi(c.GetString("i_extfieldstype"))
  354. set := bson.M{
  355. "$set": bson.M{
  356. "i_pushmodel": push_model,
  357. "s_customername": c.GetString("s_customername"),
  358. "s_salesperson": c.GetString("s_salesperson"),
  359. "s_name": c.GetString("s_name"),
  360. "i_pushtype": push_type,
  361. "s_extends": c.GetString("s_extends"),
  362. "i_updatetime": i_updatetime,
  363. "sendMail": c.GetString("sendMail"),
  364. "isDup": isDup,
  365. "i_extfieldstype": i_extfieldstype,
  366. },
  367. }
  368. isupdata := util.Mgo.UpdateById("historylog", mongodb.StringTOBsonId(his_id), set)
  369. if isupdata {
  370. c.ServeJson(map[string]interface{}{
  371. "history_id": his_id,
  372. "rep": true,
  373. })
  374. } else {
  375. //log.Println(666)
  376. c.ServeJson(map[string]interface{}{
  377. "history_id": "",
  378. "rep": false,
  379. })
  380. }
  381. }
  382. }
  383. }
  384. func (c *OwnTag) HistoryRuleCreate() {
  385. defer common.Catch()
  386. if c.Method() == "POST" {
  387. user := c.GetSession("user").(map[string]interface{})
  388. data := util.GetPostForm(c.Request)
  389. o_rules := []map[string]interface{}{}
  390. o_rulesStr := data["o_rules"].(string)
  391. json.Unmarshal([]byte(o_rulesStr), &o_rules)
  392. data["o_rules"] = o_rules
  393. id := common.ObjToString(data["id"])
  394. user_id := data["ids"]
  395. delete(data, "id")
  396. delete(data, "ids")
  397. if common.IntAll(data["i_esquerytype"]) == 1 { //自动生成es
  398. data["s_esquery"] = util.Utiltags(data, id)
  399. }
  400. i_createtime := time.Now().Unix()
  401. data["i_updatetime"] = i_createtime
  402. data["s_updateuser"] = user["name"]
  403. data["s_depart_name"] = data["departmentName"]
  404. var rep = false
  405. s_id := ""
  406. if id == "" { //新建
  407. dData, ok := util.Mgo.FindOne("euserdepart", bson.M{"s_userid": user_id, "s_name": data["departmentName"]})
  408. if dData == nil || *dData == nil || !ok {
  409. d_data := map[string]interface{}{
  410. "i_isuse": 1,
  411. "s_userid": user_id,
  412. "s_name": data["s_depart_name"],
  413. "i_createtime": time.Now().Unix(),
  414. "i_updatetime": time.Now().Unix(),
  415. "b_delete": true,
  416. "i_type": "historyData",
  417. }
  418. s_id = util.Mgo.Save("euserdepart", d_data)
  419. } else {
  420. s_id = mongodb.BsonIdToSId((*dData)["_id"])
  421. }
  422. data["s_userid"] = user_id
  423. data["s_departid"] = s_id
  424. data["i_createtime"] = i_createtime
  425. data["s_createuser"] = user["name"]
  426. s_namekey := gopinyin.Convert(common.ObjToString(data["s_name"]), false)
  427. data["s_namekey"] = s_namekey
  428. data["b_delete"] = true
  429. data["s_dataid"] = encrypt.SE.EncodeString(fmt.Sprintf("%v", i_createtime) + s_namekey + s_id)
  430. data["i_type"] = "historyData"
  431. id = util.Mgo.Save("euserdepartrule", data)
  432. if id != "" {
  433. rep = AddHistoryLogDep(data, false, id)
  434. } else {
  435. rep = false
  436. }
  437. } else {
  438. query := bson.M{
  439. "_id": mongodb.StringTOBsonId(id),
  440. }
  441. reps := util.Mgo.Update("euserdepartrule", query, bson.M{"$set": data}, false, false)
  442. if reps {
  443. rep = AddHistoryLogDep(data, true, id)
  444. }
  445. }
  446. c.ServeJson(map[string]interface{}{
  447. "id": id,
  448. "rep": rep,
  449. "s_esquery": data["s_esquery"],
  450. "s_dataid": data["s_dataid"],
  451. })
  452. } else {
  453. id := c.GetString("id")
  454. c.T["did"] = id //部门id
  455. c.T["cid"] = id //客户id
  456. c.T["ids"] = id
  457. c.T["history_id"] = c.GetString("hid")
  458. c.T["province"] = util.Province
  459. c.T["city"] = util.ProvinceCitys
  460. c.T["district"] = util.CityDistricts
  461. c.T["topTypeArr"] = util.TopTypeArr
  462. c.T["subTypeArr"] = util.SubTypeArr
  463. c.T["matchTypeMap"] = util.MatchTypeMap
  464. c.T["matchTypeMap2"] = util.MatchTypeMap2
  465. c.T["existField"] = util.ExistFiled
  466. c.T["buyerClass"] = util.BuyerClass
  467. c.T["scopeClass"] = util.ScopeClassMap
  468. c.Render("client/cuser_rule_history_edit.html", &c.T)
  469. }
  470. }
  471. // 历史任务删除私有标签
  472. func (c *OwnTag) HistoryTagDel() {
  473. defer common.Catch()
  474. // userid := c.GetString("userid")
  475. tagid := c.GetString("tagid")
  476. hid := c.GetString("hid")
  477. user_info, _ := util.Mgo.FindOne("historylog", bson.M{"_id": mongodb.StringTOBsonId(hid)})
  478. if user_info != nil && *user_info != nil {
  479. if (*user_info)["tag_rules"] != nil {
  480. user_tag_rules := common.ObjArrToMapArr((*user_info)["tag_rules"].([]interface{}))
  481. ruleMap := map[string]bool{tagid: true}
  482. ruleArr := []map[string]interface{}{}
  483. for _, m := range user_tag_rules {
  484. if !ruleMap[mongodb.BsonIdToSId(m["_id"])] {
  485. ruleArr = append(ruleArr, m)
  486. }
  487. }
  488. is_updata := util.Mgo.Update("historylog", bson.M{"_id": mongodb.StringTOBsonId(hid)}, bson.M{"$set": bson.M{"tag_rules": ruleArr}}, false, false)
  489. c.ServeJson(map[string]interface{}{
  490. "rep": is_updata,
  491. })
  492. }
  493. return
  494. }
  495. c.ServeJson(map[string]interface{}{
  496. "rep": false,
  497. })
  498. }
  499. // 私有标签关联数据
  500. func (c *OwnTag) HistoryTagAssociated() {
  501. defer common.Catch()
  502. userid := c.GetString("userid")
  503. rule_id := c.GetString("_id")
  504. tags := c.GetString("ids")
  505. hid := c.GetString("hid")
  506. if userid != "" {
  507. user_info, _ := util.Mgo.FindOne("historylog", map[string]interface{}{
  508. "_id": mongodb.StringTOBsonId(hid),
  509. })
  510. if user_info != nil && *user_info != nil {
  511. tagrules := (*user_info)["tag_rules"]
  512. if tagrules != nil {
  513. tagrulesArr := common.ObjArrToMapArr(tagrules.([]interface{}))
  514. for _, m := range tagrulesArr {
  515. if rule_id == mongodb.BsonIdToSId(m["_id"]) {
  516. m["o_departruleids"] = tags
  517. }
  518. }
  519. set := bson.M{
  520. "$set": bson.M{
  521. "tag_rules": tagrulesArr,
  522. },
  523. }
  524. is_updata := util.Mgo.Update("historylog", map[string]interface{}{
  525. "_id": mongodb.StringTOBsonId(hid),
  526. }, set, false, false)
  527. c.ServeJson(map[string]interface{}{
  528. "rep": is_updata,
  529. })
  530. }
  531. }
  532. }
  533. }
  534. // 保存历史规则
  535. func (c *OwnTag) SaveHistoryRule() {
  536. rules_id := c.GetString("rulesid")
  537. history_id := c.GetString("hid")
  538. // userid := c.GetString("userid")
  539. if history_id != "" {
  540. user_history, _ := util.Mgo.FindOne("historylog", map[string]interface{}{
  541. "_id": mongodb.StringTOBsonId(history_id),
  542. })
  543. if *user_history != nil && user_history != nil {
  544. //获取私有标签
  545. // usertags := getTserTag(userid)
  546. if (*user_history)["dep_rules"] != nil {
  547. dep_rules := common.ObjArrToMapArr((*user_history)["dep_rules"].([]interface{}))
  548. dep_new_rules := []map[string]interface{}{}
  549. ruleMap := map[string]bool{}
  550. if len(dep_rules) > 0 {
  551. for _, r := range dep_rules {
  552. dep_new_rules = append(dep_new_rules, r)
  553. ruleMap[mongodb.BsonIdToSId(r["_id"])] = true
  554. }
  555. }
  556. rules_id_list := strings.Split(rules_id, ",")
  557. for _, rule := range rules_id_list {
  558. if !ruleMap[rule] {
  559. query := bson.M{
  560. "_id": mongodb.StringTOBsonId(rule),
  561. "b_delete": false,
  562. }
  563. res, _ := util.Mgo.FindOne("euserdepartrule", query)
  564. if res != nil && len(*res) != 0 {
  565. //获取规则所属部门信息
  566. department_info, _ := util.Mgo.FindOne("euserdepart", map[string]interface{}{
  567. "_id": mongodb.StringTOBsonId((*res)["s_departid"].(string)),
  568. "b_delete": false,
  569. })
  570. (*res)["is_new"] = false
  571. (*res)["s_depart_name"] = (*department_info)["s_name"]
  572. dep_new_rules = append(dep_new_rules, *res)
  573. }
  574. }
  575. }
  576. set := bson.M{
  577. "$set": bson.M{
  578. "dep_rules": dep_new_rules,
  579. // "tag_rules": usertags,
  580. "updatetime": time.Now().Unix(),
  581. },
  582. }
  583. ok := util.Mgo.Update("historylog", map[string]interface{}{
  584. "_id": mongodb.StringTOBsonId(history_id),
  585. }, set, false, false)
  586. //log.Println("ok", ok)
  587. c.ServeJson(map[string]interface{}{
  588. "status": ok,
  589. })
  590. return
  591. } else {
  592. rules_id_list := strings.Split(rules_id, ",")
  593. dep_rules := []map[string]interface{}{}
  594. for _, rule := range rules_id_list {
  595. query := bson.M{
  596. "_id": mongodb.StringTOBsonId(rule),
  597. "b_delete": false,
  598. }
  599. res, _ := util.Mgo.FindOne("euserdepartrule", query)
  600. if res != nil && *res != nil {
  601. //获取规则所属部门信息
  602. department_info, _ := util.Mgo.FindOne("euserdepart", map[string]interface{}{
  603. "_id": mongodb.StringTOBsonId((*res)["s_departid"].(string)),
  604. "b_delete": false,
  605. })
  606. (*res)["is_new"] = false
  607. (*res)["s_depart_name"] = (*department_info)["s_name"]
  608. dep_rules = append(dep_rules, *res)
  609. }
  610. }
  611. set := bson.M{
  612. "$set": bson.M{
  613. "dep_rules": dep_rules,
  614. // "tag_rules": usertags,
  615. "updatetime": time.Now().Unix(),
  616. },
  617. }
  618. isupdata := util.Mgo.Update("historylog", map[string]interface{}{
  619. "_id": mongodb.StringTOBsonId(history_id),
  620. }, set, false, false)
  621. c.ServeJson(map[string]interface{}{
  622. "status": isupdata,
  623. })
  624. return
  625. }
  626. }
  627. c.ServeJson(map[string]interface{}{
  628. "status": "fail",
  629. })
  630. }
  631. }
  632. func (c *OwnTag) DelHistoryRule() {
  633. id := c.GetString("id")
  634. hid := c.GetString("hid")
  635. if hid != "" {
  636. data, ok := util.Mgo.FindOne("historylog", bson.M{"_id": mongodb.StringTOBsonId(hid)})
  637. if ok && data != nil && *data != nil {
  638. if (*data)["dep_rules"] != nil {
  639. dep_rules := common.ObjArrToMapArr((*data)["dep_rules"].([]interface{}))
  640. ruleMap := map[string]bool{id: true}
  641. ruleArr := []map[string]interface{}{}
  642. for _, m := range dep_rules {
  643. if !ruleMap[mongodb.BsonIdToSId(m["_id"])] {
  644. ruleArr = append(ruleArr, m)
  645. }
  646. }
  647. ok := util.Mgo.Update("historylog", bson.M{"_id": mongodb.StringTOBsonId(hid)}, bson.M{"$set": bson.M{"dep_rules": ruleArr}}, false, false)
  648. c.ServeJson(map[string]interface{}{
  649. "success": ok,
  650. })
  651. }
  652. }
  653. }
  654. }
  655. func (c *OwnTag) HistoryRuleEdit() {
  656. hid := c.GetString("hid")
  657. rid := c.GetString("rid")
  658. if c.Method() == "POST" {
  659. data := util.GetPostForm(c.Request)
  660. o_rules := []map[string]interface{}{}
  661. o_rulesStr := data["o_rules"].(string)
  662. json.Unmarshal([]byte(o_rulesStr), &o_rules)
  663. data["o_rules"] = o_rules
  664. data["_id"] = mongodb.StringTOBsonId(rid)
  665. if common.IntAll(data["i_esquerytype"]) == 1 { //自动生成es
  666. data["s_esquery"] = util.Utiltags(data, rid)
  667. }
  668. datas, ok := util.Mgo.FindOne("historylog", map[string]interface{}{"_id": mongodb.StringTOBsonId(hid)})
  669. if ok && datas != nil && *datas != nil {
  670. dep_rules := common.ObjArrToMapArr((*datas)["dep_rules"].([]interface{}))
  671. for k, v := range dep_rules {
  672. if rid == mongodb.BsonIdToSId(v["_id"]) {
  673. dep_rules[k] = data
  674. }
  675. }
  676. rep := util.Mgo.Update("historylog", map[string]interface{}{"_id": mongodb.StringTOBsonId(hid)}, map[string]interface{}{"$set": map[string]interface{}{"dep_rules": dep_rules}}, false, false)
  677. c.ServeJson(map[string]interface{}{
  678. "rep": rep,
  679. "s_esquery": data["s_esquery"],
  680. })
  681. }
  682. } else {
  683. id := c.GetString("id")
  684. data, ok := util.Mgo.FindOne("historylog", map[string]interface{}{"_id": mongodb.StringTOBsonId(hid)})
  685. if ok && data != nil && *data != nil {
  686. dep_rules := common.ObjArrToMapArr((*data)["dep_rules"].([]interface{}))
  687. for _, v := range dep_rules {
  688. if rid == mongodb.BsonIdToSId(v["_id"]) {
  689. c.T["data"] = v
  690. }
  691. }
  692. }
  693. c.T["did"] = id //部门id
  694. c.T["cid"] = id //客户id
  695. c.T["ids"] = id
  696. c.T["history_id"] = hid
  697. c.T["rid"] = rid
  698. c.T["province"] = util.Province
  699. c.T["city"] = util.ProvinceCitys
  700. c.T["district"] = util.CityDistricts
  701. c.T["topTypeArr"] = util.TopTypeArr
  702. c.T["subTypeArr"] = util.SubTypeArr
  703. c.T["matchTypeMap"] = util.MatchTypeMap
  704. c.T["matchTypeMap2"] = util.MatchTypeMap2
  705. c.T["existField"] = util.ExistFiled
  706. c.T["buyerClass"] = util.BuyerClass
  707. c.T["scopeClass"] = util.ScopeClassMap
  708. c.Render("private/history_rule_edit.html", &c.T)
  709. }
  710. }
  711. // 选择历史规则
  712. func (c *OwnTag) CustomerAllRule() {
  713. customerId := c.GetString("c_id")
  714. history_id := c.GetString("hid")
  715. query := bson.M{
  716. "s_userid": customerId,
  717. "b_delete": false,
  718. }
  719. inc_query := bson.M{
  720. "user_id": customerId,
  721. "_id": mongodb.StringTOBsonId(history_id),
  722. }
  723. //获取新增历史规则
  724. inc_rules := []map[string]interface{}{}
  725. inc_rule := make(map[string]interface{})
  726. inc_data, _ := util.Mgo.FindOne("historylog", inc_query)
  727. if inc_data != nil && *inc_data != nil {
  728. for _, t := range (*inc_data)["dep_rules"].([]interface{}) {
  729. if !t.(map[string]interface{})["is_new"].(bool) {
  730. inc_rule["id"] = t.(map[string]interface{})["_id"].(primitive.ObjectID).Hex() //规则id
  731. inc_rule["s_depart_name"] = t.(map[string]interface{})["s_depart_name"]
  732. inc_rule["s_name"] = t.(map[string]interface{})["s_name"]
  733. inc_rule["s_depart_status"] = t.(map[string]interface{})["s_depart_status"]
  734. inc_rule["s_rule_status"] = t.(map[string]interface{})["s_rule_status"]
  735. inc_rule["rule_updatetime"] = t.(map[string]interface{})["i_updatetime"]
  736. inc_rule["isinc"] = true
  737. inc_rules = append(inc_rules, inc_rule)
  738. }
  739. }
  740. }
  741. data, _ := util.Mgo.Find("euserdepart", query, `{"i_createtime":-1}`, nil, false, 0, 0)
  742. if data != nil && len(*data) != 0 {
  743. res := []map[string]interface{}{}
  744. for _, m := range *data {
  745. history_rule_data := make(map[string]interface{})
  746. q := bson.M{
  747. "s_departid": m["_id"].(primitive.ObjectID).Hex(),
  748. "b_delete": false,
  749. }
  750. history_rule_data["department_isuse"] = m["i_isuse"]
  751. history_rule_data["depart_name"] = m["s_name"]
  752. depart_rule_data, _ := util.Mgo.Find("euserdepartrule", q, `{"i_createtime":-1}`, `{"s_name":1,"i_updatetime":1,"i_isuse":1,"_id":1}`, false, 0, 0)
  753. if depart_rule_data != nil && len(*depart_rule_data) != 0 {
  754. for _, j := range *depart_rule_data {
  755. j["_id"] = j["_id"].(primitive.ObjectID).Hex()
  756. }
  757. history_rule_data["department_rule"] = depart_rule_data
  758. history_rule_data["is_slected"] = true
  759. }
  760. res = append(res, history_rule_data)
  761. }
  762. c.ServeJson(map[string]interface{}{
  763. "status": "success",
  764. "data": res,
  765. "inc_rules": inc_rules,
  766. })
  767. } else {
  768. c.ServeJson(map[string]interface{}{
  769. "status": "fail",
  770. "data": "",
  771. })
  772. }
  773. }
  774. func AddHistoryLogTag(ruleData map[string]interface{}, isUpdate bool, id string) bool {
  775. history_id := mongodb.StringTOBsonId(common.ObjToString(ruleData["hid"]))
  776. hData, ok := util.Mgo.FindOne("historylog", bson.M{"_id": history_id})
  777. if ok && hData != nil && *hData != nil && len(*hData) > 0 {
  778. tag_rules := []map[string]interface{}{}
  779. if (*hData)["tag_rules"] != nil {
  780. tag_rules = common.ObjArrToMapArr((*hData)["tag_rules"].([]interface{}))
  781. }
  782. ruleData["is_new"] = true
  783. if isUpdate {
  784. for k, v := range tag_rules {
  785. if mongodb.BsonIdToSId(v["_id"]) == id {
  786. tag_rules[k] = ruleData
  787. }
  788. }
  789. } else {
  790. tag_rules = append(tag_rules, ruleData)
  791. }
  792. return util.Mgo.Update("historylog", bson.M{"_id": history_id}, bson.M{"$set": bson.M{"tag_rules": tag_rules}}, false, false)
  793. } else {
  794. return false
  795. }
  796. }
  797. func AddHistoryLogDep(ruleData map[string]interface{}, isUpdate bool, id string) bool {
  798. history_id := mongodb.StringTOBsonId(ruleData["history_id"].(string))
  799. hData, ok := util.Mgo.FindOne("historylog", bson.M{"_id": history_id})
  800. if ok && hData != nil && *hData != nil && len(*hData) > 0 {
  801. dep_rules := []map[string]interface{}{}
  802. if (*hData)["dep_rules"] != nil {
  803. dep_rules = common.ObjArrToMapArr((*hData)["dep_rules"].([]interface{}))
  804. }
  805. ruleData["is_new"] = true
  806. if isUpdate {
  807. for k, v := range dep_rules {
  808. if mongodb.BsonIdToSId(v["_id"]) == id {
  809. dep_rules[k] = ruleData
  810. }
  811. }
  812. } else {
  813. dep_rules = append(dep_rules, ruleData)
  814. }
  815. return util.Mgo.Update("historylog", bson.M{"_id": history_id}, bson.M{"$set": bson.M{"dep_rules": dep_rules}}, false, false)
  816. } else {
  817. return false
  818. }
  819. }