client.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package client
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/go-xweb/xweb"
  5. "app.yhyue.com/moapp/jybase/mongodb"
  6. "cmplatform/util"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "strings"
  9. "time"
  10. )
  11. type Client struct {
  12. *xweb.Action
  13. index xweb.Mapper `xweb:"/client/index"` //客户首页
  14. cuserRule xweb.Mapper `xweb:"/client/cuser/rule/list"` //规则列表 部门
  15. cuserRuleSetup xweb.Mapper `xweb:"/client/cuser/rule/setup"` //设置是否启用
  16. cuserRuleDel xweb.Mapper `xweb:"/client/cuser/rule/Del"` //删除规则
  17. departDel xweb.Mapper `xweb:"/client/depart/del"` //删除部门
  18. departEdit xweb.Mapper `xweb:"/client/depart/edit"` //保存和修改部门
  19. setupDepart xweb.Mapper `xweb:"/client/customer/setup"` //部门列表中启用、禁用
  20. cuserRuleEdit xweb.Mapper `xweb:"/client/cuser/rule/Edit"`
  21. //同步
  22. synEuser xweb.Mapper `xweb:"/client/cuser/synchronous"`
  23. }
  24. func (this *Client) Index() {
  25. id := this.GetString("id")
  26. query := map[string]interface{}{}
  27. query["_id"] = mongodb.StringTOBsonId(id)
  28. data, _ := util.Mgo.FindOneByField("cuser", query, `{}`)
  29. depart, _ := util.Mgo.Find("cuserdepart", map[string]interface{}{"s_userid": id, "b_delete": false}, nil, nil, false, -1, -1)
  30. (*data)["_id"] = mongodb.BsonIdToSId((*data)["_id"])
  31. // if (*data)["i_state"] == 1 {
  32. tagRule, _ := util.Mgo.Find("eusertagrule", map[string]interface{}{"s_userid": id, "b_delete": false}, nil, nil, false, -1, -1)
  33. this.T["tagrule"] = *tagRule
  34. appid := common.ObjToString((*data)["s_appid"])
  35. mails, _ := util.MgoCus.FindOneByField("user", map[string]interface{}{"appid": appid}, `{"mails":1,"username":1}`)
  36. this.T["mails"] = mails
  37. // }
  38. this.T["data"] = *data
  39. this.T["depart"] = *depart
  40. this.Render("/client/index.html")
  41. }
  42. // 对外客户部门规则列表
  43. func (c *Client) CuserRule() {
  44. defer common.Catch()
  45. ids := strings.Split(c.GetString("ids"), ",")
  46. if c.Method() == "POST" {
  47. start, _ := c.GetInt("start")
  48. limit, _ := c.GetInt("length")
  49. draw, _ := c.GetInt("draw")
  50. searchStr := c.GetString("search[value]")
  51. search := strings.TrimSpace(searchStr)
  52. query := bson.M{
  53. "s_userid": ids[1],
  54. "s_departid": ids[0],
  55. "b_delete": false,
  56. }
  57. if search != "" {
  58. query["$or"] = []interface{}{
  59. bson.M{"s_name": bson.M{"$regex": search}},
  60. }
  61. }
  62. data, _ := util.Mgo.Find("cuserdepartrule", query, `{"i_createtime":-1}`, nil, false, int(start), int(limit))
  63. count := util.Mgo.Count("cuserdepartrule", query)
  64. c.ServeJson(map[string]interface{}{
  65. "draw": draw,
  66. "data": data,
  67. "recordsFiltered": count,
  68. "recordsTotal": count,
  69. })
  70. } else {
  71. c.T["did"] = ids[0] //部门id
  72. c.T["cid"] = ids[1] //客户id
  73. users, ok := util.Mgo.FindOne("cuser", map[string]interface{}{"_id": mongodb.StringTOBsonId(ids[1]), "b_delete": false})
  74. downloadRule := 0
  75. if users != nil && ok {
  76. downloadRule = common.IntAll((*users)["i_downloadRule"])
  77. }
  78. c.T["i_downloadRule"] = downloadRule // 导出规则开关
  79. c.T["preview_href"] = util.PreviewHref
  80. c.Render("client/cuser_rule_list.html", &c.T)
  81. }
  82. }
  83. // 对外客户部门规则设置是否启用
  84. func (c *Client) CuserRuleSetup() {
  85. defer common.Catch()
  86. if c.Method() == "POST" {
  87. _id := c.GetString("_id")
  88. i_isuse, _ := c.GetInt("i_isuse")
  89. set := bson.M{
  90. "$set": bson.M{
  91. "i_isuse": i_isuse,
  92. },
  93. }
  94. b := util.Mgo.UpdateById("cuserdepartrule", _id, set)
  95. c.ServeJson(map[string]interface{}{
  96. "rep": b,
  97. })
  98. }
  99. }
  100. // 对外客户删除 部门规则
  101. func (c *Client) CuserRuleDel() {
  102. defer common.Catch()
  103. _id := c.GetString("_id")
  104. set := bson.M{
  105. "$set": bson.M{
  106. "b_delete": true,
  107. },
  108. }
  109. b := util.Mgo.UpdateById("cuserdepartrule", _id, set)
  110. c.ServeJson(map[string]interface{}{
  111. "rep": b,
  112. })
  113. }
  114. // 删除部门
  115. func (c *Client) DepartDel() {
  116. defer common.Catch()
  117. _id := c.GetString("id")
  118. ids := strings.Split(_id, ",")
  119. update := [][]map[string]interface{}{}
  120. for _, v := range ids {
  121. query := bson.M{
  122. "s_departid": v,
  123. "b_delete": false,
  124. }
  125. count := util.Mgo.Count("cuserdepartrule", query)
  126. if count > 0 {
  127. c.ServeJson(map[string]interface{}{
  128. "rep": false,
  129. "rea": false,
  130. })
  131. return
  132. } else {
  133. idAndSet := []map[string]interface{}{}
  134. idAndSet = append(idAndSet, bson.M{"_id": mongodb.StringTOBsonId(v)}) //查询条件
  135. idAndSet = append(idAndSet, bson.M{"$set": bson.M{"b_delete": true}}) //修改的数据
  136. update = append(update, idAndSet)
  137. /*update = append(update, []map[string]interface{}{
  138. map[string]interface{}{
  139. "_id": mongodb.StringTOBsonId(v),
  140. },
  141. map[string]interface{}{
  142. "$set": map[string]interface{}{
  143. "b_delete": true,
  144. },
  145. },
  146. })*/
  147. }
  148. }
  149. b := util.Mgo.UpdateBulk("cuserdepart", update...)
  150. c.ServeJson(map[string]interface{}{
  151. "rep": b,
  152. })
  153. }
  154. // 保存部门
  155. func (c *Client) DepartEdit() {
  156. defer common.Catch()
  157. data := util.GetPostForm(c.Request)
  158. id := common.ObjToString(data["id"])
  159. var b = true
  160. if id == "" {
  161. data["i_createtime"] = time.Now().Unix()
  162. data["i_updatetime"] = time.Now().Unix()
  163. data["b_delete"] = false
  164. _id := util.Mgo.Save("cuserdepart", data)
  165. if _id == "" {
  166. b = false
  167. }
  168. } else {
  169. set := bson.M{
  170. "$set": data,
  171. }
  172. data["i_updatetime"] = time.Now().Unix()
  173. b = util.Mgo.UpdateById("cuserdepart", id, set)
  174. }
  175. c.ServeJson(map[string]interface{}{
  176. "rep": b,
  177. })
  178. }
  179. // 部门启用、停用
  180. func (c *Client) SetupDepart() {
  181. defer common.Catch()
  182. if c.Method() == "POST" {
  183. _id := c.GetString("_id")
  184. i_isuse, _ := c.GetInt("i_isuse")
  185. nowtime := time.Now().Unix()
  186. set := bson.M{
  187. "$set": bson.M{
  188. "i_updatetime": nowtime,
  189. "i_isuse": i_isuse,
  190. },
  191. }
  192. b := util.Mgo.UpdateById("cuserdepart", _id, set)
  193. if b {
  194. //部门下的规则
  195. update := [][]map[string]interface{}{}
  196. ruleMap, _ := util.Mgo.Find("cuserdepartrule", bson.M{"s_userid": _id}, nil, nil, false, -1, -1)
  197. for _, v := range *ruleMap {
  198. idAndSet := []map[string]interface{}{}
  199. idAndSet = append(idAndSet, bson.M{"_id": v["_id"]}) //查询条件
  200. idAndSet = append(idAndSet, bson.M{"i_isuse": i_isuse, "i_updatetime": nowtime}) //修改的数据
  201. update = append(update, idAndSet)
  202. }
  203. b = util.Mgo.UpdateBulk("cuserdepartrule", update...)
  204. }
  205. c.ServeJson(map[string]interface{}{
  206. "rep": b,
  207. })
  208. }
  209. }
  210. // 点击规则列表中编辑按钮
  211. func (c *Client) CuserRuleEdit() {
  212. defer common.Catch()
  213. id := c.GetString("id")
  214. query := bson.M{"_id": mongodb.StringTOBsonId(id)}
  215. data, _ := util.Mgo.FindOneByField("cuserdepartrule", query, `{}`)
  216. (*data)["id"] = mongodb.BsonIdToSId((*data)["_id"])
  217. userData, _ := util.Mgo.FindById("cuser", common.ObjToString((*data)["s_userid"]), nil)
  218. (*data)["i_exact"] = (*userData)["i_exact"]
  219. c.T["cid"] = (*data)["s_userid"]
  220. c.T["ids"] = common.ObjToString((*data)["s_departid"]) + "," + common.ObjToString((*data)["s_userid"])
  221. c.T["data"] = *data
  222. c.T["province"] = util.Province
  223. c.T["city"] = util.ProvinceCitys
  224. c.T["district"] = util.CityDistricts
  225. c.T["topTypeArr"] = util.TopTypeArr
  226. c.T["subTypeArr"] = util.SubTypeArr
  227. c.T["matchTypeMap"] = util.MatchTypeMap
  228. c.T["matchTypeMap2"] = util.MatchTypeMap2
  229. c.T["existField"] = util.ExistFiled
  230. c.T["buyerClass"] = util.BuyerClass
  231. c.T["buyerClassMap"] = util.BuyerClassMap
  232. c.T["scopeClass"] = util.ScopeClassMap
  233. c.Render("client/cuser_rule_edit.html", &c.T)
  234. }
  235. // 同步
  236. func (c *Client) SynEuser() {
  237. defer common.Catch()
  238. _id := c.GetString("_id")
  239. cuser, _ := util.Mgo.FindById("cuser", _id, nil)
  240. nowTime := time.Now().Unix()
  241. (*cuser)["i_lastsynctime"] = nowTime //同步时间
  242. set := bson.M{
  243. "$set": bson.M{
  244. "i_lastsynctime": nowTime,
  245. },
  246. }
  247. b := util.Mgo.Update("cuser", bson.M{"_id": mongodb.StringTOBsonId(_id)}, set, false, false)
  248. if !b {
  249. c.ServeJson(map[string]interface{}{
  250. "rep": b,
  251. })
  252. return
  253. }
  254. delete(*cuser, "_id")
  255. b = util.Mgo.Update("euser", bson.M{"_id": mongodb.StringTOBsonId(_id)}, bson.M{"$set": *cuser}, true, false)
  256. if !b {
  257. c.ServeJson(map[string]interface{}{
  258. "rep": b,
  259. })
  260. return
  261. }
  262. departMap, _ := util.Mgo.Find("cuserdepart", bson.M{"s_userid": _id}, nil, nil, false, -1, -1)
  263. //util.Mgo.SaveBulk("euserdepart", *departMap...)
  264. for _, v := range *departMap {
  265. if count := util.Mgo.Count("euserdepart", map[string]interface{}{"_id": v["_id"]}); count > 0 {
  266. util.Mgo.Update("euserdepart", map[string]interface{}{"_id": v["_id"]}, map[string]interface{}{"$set": v}, false, false)
  267. } else {
  268. util.Mgo.SaveByOriID("euserdepart", v)
  269. }
  270. }
  271. ruleMap, _ := util.Mgo.Find("cuserdepartrule", bson.M{"s_userid": _id}, nil, nil, false, -1, -1)
  272. for _, v := range *ruleMap {
  273. if count := util.Mgo.Count("euserdepartrule", map[string]interface{}{"_id": v["_id"]}); count > 0 {
  274. util.Mgo.Update("euserdepartrule", map[string]interface{}{"_id": v["_id"]}, map[string]interface{}{"$set": v}, false, false)
  275. } else {
  276. util.Mgo.SaveByOriID("euserdepartrule", v)
  277. }
  278. }
  279. c.ServeJson(map[string]interface{}{
  280. "rep": b,
  281. })
  282. }