client.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. c.T["preview_href"] = util.PreviewHref
  74. c.Render("client/cuser_rule_list.html", &c.T)
  75. }
  76. }
  77. // 对外客户部门规则设置是否启用
  78. func (c *Client) CuserRuleSetup() {
  79. defer common.Catch()
  80. if c.Method() == "POST" {
  81. _id := c.GetString("_id")
  82. i_isuse, _ := c.GetInt("i_isuse")
  83. set := bson.M{
  84. "$set": bson.M{
  85. "i_isuse": i_isuse,
  86. },
  87. }
  88. b := util.Mgo.UpdateById("cuserdepartrule", _id, set)
  89. c.ServeJson(map[string]interface{}{
  90. "rep": b,
  91. })
  92. }
  93. }
  94. // 对外客户删除 部门规则
  95. func (c *Client) CuserRuleDel() {
  96. defer common.Catch()
  97. _id := c.GetString("_id")
  98. set := bson.M{
  99. "$set": bson.M{
  100. "b_delete": true,
  101. },
  102. }
  103. b := util.Mgo.UpdateById("cuserdepartrule", _id, set)
  104. c.ServeJson(map[string]interface{}{
  105. "rep": b,
  106. })
  107. }
  108. // 删除部门
  109. func (c *Client) DepartDel() {
  110. defer common.Catch()
  111. _id := c.GetString("id")
  112. ids := strings.Split(_id, ",")
  113. update := [][]map[string]interface{}{}
  114. for _, v := range ids {
  115. query := bson.M{
  116. "s_departid": v,
  117. "b_delete": false,
  118. }
  119. count := util.Mgo.Count("cuserdepartrule", query)
  120. if count > 0 {
  121. c.ServeJson(map[string]interface{}{
  122. "rep": false,
  123. "rea": false,
  124. })
  125. return
  126. } else {
  127. idAndSet := []map[string]interface{}{}
  128. idAndSet = append(idAndSet, bson.M{"_id": mongodb.StringTOBsonId(v)}) //查询条件
  129. idAndSet = append(idAndSet, bson.M{"$set": bson.M{"b_delete": true}}) //修改的数据
  130. update = append(update, idAndSet)
  131. /*update = append(update, []map[string]interface{}{
  132. map[string]interface{}{
  133. "_id": mongodb.StringTOBsonId(v),
  134. },
  135. map[string]interface{}{
  136. "$set": map[string]interface{}{
  137. "b_delete": true,
  138. },
  139. },
  140. })*/
  141. }
  142. }
  143. b := util.Mgo.UpdateBulk("cuserdepart", update...)
  144. c.ServeJson(map[string]interface{}{
  145. "rep": b,
  146. })
  147. }
  148. // 保存部门
  149. func (c *Client) DepartEdit() {
  150. defer common.Catch()
  151. data := util.GetPostForm(c.Request)
  152. id := common.ObjToString(data["id"])
  153. var b = true
  154. if id == "" {
  155. data["i_createtime"] = time.Now().Unix()
  156. data["i_updatetime"] = time.Now().Unix()
  157. data["b_delete"] = false
  158. _id := util.Mgo.Save("cuserdepart", data)
  159. if _id == "" {
  160. b = false
  161. }
  162. } else {
  163. set := bson.M{
  164. "$set": data,
  165. }
  166. data["i_updatetime"] = time.Now().Unix()
  167. b = util.Mgo.UpdateById("cuserdepart", id, set)
  168. }
  169. c.ServeJson(map[string]interface{}{
  170. "rep": b,
  171. })
  172. }
  173. // 部门启用、停用
  174. func (c *Client) SetupDepart() {
  175. defer common.Catch()
  176. if c.Method() == "POST" {
  177. _id := c.GetString("_id")
  178. i_isuse, _ := c.GetInt("i_isuse")
  179. nowtime := time.Now().Unix()
  180. set := bson.M{
  181. "$set": bson.M{
  182. "i_updatetime": nowtime,
  183. "i_isuse": i_isuse,
  184. },
  185. }
  186. b := util.Mgo.UpdateById("cuserdepart", _id, set)
  187. if b {
  188. //部门下的规则
  189. update := [][]map[string]interface{}{}
  190. ruleMap, _ := util.Mgo.Find("cuserdepartrule", bson.M{"s_userid": _id}, nil, nil, false, -1, -1)
  191. for _, v := range *ruleMap {
  192. idAndSet := []map[string]interface{}{}
  193. idAndSet = append(idAndSet, bson.M{"_id": v["_id"]}) //查询条件
  194. idAndSet = append(idAndSet, bson.M{"i_isuse": i_isuse, "i_updatetime": nowtime}) //修改的数据
  195. update = append(update, idAndSet)
  196. }
  197. b = util.Mgo.UpdateBulk("cuserdepartrule", update...)
  198. }
  199. c.ServeJson(map[string]interface{}{
  200. "rep": b,
  201. })
  202. }
  203. }
  204. // 点击规则列表中编辑按钮
  205. func (c *Client) CuserRuleEdit() {
  206. defer common.Catch()
  207. id := c.GetString("id")
  208. query := bson.M{"_id": mongodb.StringTOBsonId(id)}
  209. data, _ := util.Mgo.FindOneByField("cuserdepartrule", query, `{}`)
  210. (*data)["id"] = mongodb.BsonIdToSId((*data)["_id"])
  211. userData, _ := util.Mgo.FindById("cuser", common.ObjToString((*data)["s_userid"]), nil)
  212. (*data)["i_exact"] = (*userData)["i_exact"]
  213. c.T["cid"] = (*data)["s_userid"]
  214. c.T["ids"] = common.ObjToString((*data)["s_departid"]) + "," + common.ObjToString((*data)["s_userid"])
  215. c.T["data"] = *data
  216. c.T["province"] = util.Province
  217. c.T["city"] = util.ProvinceCitys
  218. c.T["district"] = util.CityDistricts
  219. c.T["topTypeArr"] = util.TopTypeArr
  220. c.T["subTypeArr"] = util.SubTypeArr
  221. c.T["matchTypeMap"] = util.MatchTypeMap
  222. c.T["matchTypeMap2"] = util.MatchTypeMap2
  223. c.T["existField"] = util.ExistFiled
  224. c.T["buyerClass"] = util.BuyerClass
  225. c.T["buyerClassMap"] = util.BuyerClassMap
  226. c.T["scopeClass"] = util.ScopeClassMap
  227. c.Render("client/cuser_rule_edit.html", &c.T)
  228. }
  229. // 同步
  230. func (c *Client) SynEuser() {
  231. defer common.Catch()
  232. _id := c.GetString("_id")
  233. cuser, _ := util.Mgo.FindById("cuser", _id, nil)
  234. nowTime := time.Now().Unix()
  235. (*cuser)["i_lastsynctime"] = nowTime //同步时间
  236. set := bson.M{
  237. "$set": bson.M{
  238. "i_lastsynctime": nowTime,
  239. },
  240. }
  241. b := util.Mgo.Update("cuser", bson.M{"_id": mongodb.StringTOBsonId(_id)}, set, false, false)
  242. if !b {
  243. c.ServeJson(map[string]interface{}{
  244. "rep": b,
  245. })
  246. return
  247. }
  248. b = util.Mgo.Update("euser", bson.M{"_id": mongodb.StringTOBsonId(_id)}, cuser, true, false)
  249. if !b {
  250. c.ServeJson(map[string]interface{}{
  251. "rep": b,
  252. })
  253. return
  254. }
  255. departMap, _ := util.Mgo.Find("cuserdepart", bson.M{"s_userid": _id}, nil, nil, false, -1, -1)
  256. //util.Mgo.SaveBulk("euserdepart", *departMap...)
  257. update := [][]map[string]interface{}{}
  258. for _, v := range *departMap {
  259. idAndSet := []map[string]interface{}{}
  260. idAndSet = append(idAndSet, bson.M{"_id": v["_id"]}) //查询条件
  261. idAndSet = append(idAndSet, v) //修改的数据
  262. update = append(update, idAndSet)
  263. }
  264. util.Mgo.UpSertBulk("euserdepart", update...)
  265. ruleMap, _ := util.Mgo.Find("cuserdepartrule", bson.M{"s_userid": _id}, nil, nil, false, -1, -1)
  266. //b = util.Mgo.SaveBulk("euserdepartrule", *ruleMap...)
  267. update2 := [][]map[string]interface{}{}
  268. for _, v := range *ruleMap {
  269. idAndSet := []map[string]interface{}{}
  270. idAndSet = append(idAndSet, bson.M{"_id": v["_id"]}) //查询条件
  271. idAndSet = append(idAndSet, v) //修改的数据
  272. update2 = append(update2, idAndSet)
  273. }
  274. b = util.Mgo.UpSertBulk("euserdepartrule", update2...)
  275. c.ServeJson(map[string]interface{}{
  276. "rep": b,
  277. })
  278. }