client.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package client
  2. import (
  3. qu "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/encrypt"
  5. "app.yhyue.com/moapp/jybase/go-xweb/xweb"
  6. "app.yhyue.com/moapp/jybase/mongodb"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/lauyoume/gopinyin"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "regexp"
  12. . "seplatform/util"
  13. "strconv"
  14. "time"
  15. )
  16. type Client struct {
  17. *xweb.Action
  18. index xweb.Mapper `xweb:"/client/index"` //客户首页
  19. cuserRuleDel xweb.Mapper `xweb:"/client/rule/del"` //删除规则
  20. adminIndex xweb.Mapper `xweb:"/admin/index"` //管理员首页
  21. personnelIndex xweb.Mapper `xweb:"/personnel/index"` //员工首页
  22. cuserRuleCreate xweb.Mapper `xweb:"/client/rule/create"` //新建规则
  23. cuserRuleEdit xweb.Mapper `xweb:"/client/rule/edit"` //修改规则
  24. exportLog xweb.Mapper `xweb:"/client/exportLog"` //导出页面
  25. }
  26. var (
  27. replaceField_detail = map[string]bool{`"title"`: true, `"projectname.pname"`: true}
  28. reg_detail = regexp.MustCompile("\"fields\":\\[([^]]*detail[^]]*)\\]")
  29. reg_single = regexp.MustCompile("\"query\":\"[^\"{:]\"")
  30. )
  31. func (this *Client) AdminIndex() {
  32. defer qu.Catch()
  33. user := this.GetSession("user").(map[string]interface{})
  34. entId := qu.IntAll(user["ent_id"])
  35. // entUserId := qu.IntAll(user["id"])
  36. // appid := qu.ObjToString(user["appid"])
  37. // phone := qu.ObjToString(user["phone"])
  38. entUserArr := JyMysql.Find("entniche_user", map[string]interface{}{"ent_id": entId, "export_power": 1}, "", "id desc", -1, -1)
  39. if entUserArr != nil && *entUserArr != nil && len(*entUserArr) > 0 {
  40. for _, v := range *entUserArr {
  41. userId := qu.IntAll(v["id"])
  42. deptmentUser := JyMysql.FindOne("entniche_department_user", map[string]interface{}{"user_id": userId}, "dept_id", "")
  43. if deptmentUser != nil && *deptmentUser != nil {
  44. dId := qu.IntAll((*deptmentUser)["dept_id"])
  45. v["departId"] = dId
  46. deptment := JyMysql.FindOne("entniche_department", map[string]interface{}{"id": dId}, "name", "")
  47. if deptment != nil && *deptment != nil {
  48. dName := qu.ObjToString((*deptment)["name"])
  49. v["departName"] = dName
  50. }
  51. }
  52. v["id"] = encrypt.SE.EncodeString(strconv.Itoa(userId))
  53. }
  54. this.T["entUserArr"] = *entUserArr
  55. }
  56. this.Render("/client/adminIndex.html")
  57. }
  58. func (this *Client) PersonnelIndex() {
  59. defer qu.Catch()
  60. id := this.GetString("id")
  61. if this.Method() == "POST" {
  62. start, _ := this.GetInt("start")
  63. limit, _ := this.GetInt("length")
  64. draw, _ := this.GetInt("draw")
  65. entUserId := 0
  66. user := this.GetSession("user").(map[string]interface{})
  67. if id == "" {
  68. entUserId = qu.IntAll(user["id"])
  69. } else {
  70. entUserId, _ = strconv.Atoi(encrypt.SE.DecodeString(id))
  71. }
  72. entId := qu.IntAll(user["ent_id"])
  73. count := Mgo.Count("entniche_rule", map[string]interface{}{"entId": entId, "entUserId": entUserId})
  74. entRuleArr, ok := Mgo.Find("entniche_rule", map[string]interface{}{"entId": entId, "entUserId": entUserId}, `{"_id":-1}`, nil, false, int(start), int(limit))
  75. if ok && entRuleArr != nil && *entRuleArr != nil && len(*entRuleArr) > 0 {
  76. for _, v := range *entRuleArr {
  77. ruleid := mongodb.BsonIdToSId(v["_id"])
  78. v["_id"] = encrypt.SE.EncodeString(ruleid)
  79. }
  80. this.ServeJson(map[string]interface{}{
  81. "draw": draw,
  82. "data": *entRuleArr,
  83. "recordsFiltered": count,
  84. "recordsTotal": count,
  85. })
  86. } else {
  87. this.ServeJson(map[string]interface{}{
  88. "draw": draw,
  89. "data": nil,
  90. "recordsFiltered": 0,
  91. "recordsTotal": 0,
  92. })
  93. }
  94. } else {
  95. this.T["id"] = id
  96. entUserId := 0
  97. user := this.GetSession("user").(map[string]interface{})
  98. if id == "" {
  99. entUserId = qu.IntAll(user["id"])
  100. } else {
  101. entUserId, _ = strconv.Atoi(encrypt.SE.DecodeString(id))
  102. }
  103. entId := qu.IntAll(user["ent_id"])
  104. leftNumData := JyMysql.FindOne("user_account", map[string]interface{}{"user_id": entUserId, "ent_id": entId}, "left_num", "")
  105. left := 0
  106. if leftNumData != nil && *leftNumData != nil {
  107. left = int((*leftNumData)["left_num"].(int64))
  108. }
  109. this.T["left"] = left
  110. this.Render("/client/personnelIndex.html", &this.T)
  111. }
  112. }
  113. // 对外客户删除 部门规则
  114. func (c *Client) CuserRuleDel() {
  115. defer qu.Catch()
  116. _id := c.GetString("_id")
  117. b := Mgo.Del("entniche_rule", map[string]interface{}{"_id": mongodb.StringTOBsonId(encrypt.SE.DecodeString(_id))})
  118. c.ServeJson(map[string]interface{}{
  119. "rep": b,
  120. })
  121. }
  122. func (c *Client) CuserRuleCreate() {
  123. defer qu.Catch()
  124. if c.Method() == "POST" {
  125. user := c.GetSession("user").(map[string]interface{})
  126. data := GetPostForm(c.Request)
  127. entstr := c.GetString("entUserId")
  128. entUserId, err := strconv.Atoi(entstr)
  129. if err != nil {
  130. entUserId = 0
  131. }
  132. o_rules := []map[string]interface{}{}
  133. o_rulesStr := data["o_rules"].(string)
  134. json.Unmarshal([]byte(o_rulesStr), &o_rules)
  135. data["o_rules"] = o_rules
  136. id := qu.ObjToString(data["id"])
  137. delete(data, "id")
  138. delete(data, "ids")
  139. if qu.IntAll(data["i_esquerytype"]) == 1 { //自动生成es
  140. esStr := Utiltags(data)
  141. data["s_esquery"] = esStr
  142. data["s_esquery_search"] = filter(esStr)
  143. }
  144. i_createtime := time.Now().Unix()
  145. data["i_updatetime"] = i_createtime
  146. data["s_updateuser"] = user["name"]
  147. var rep = false
  148. if id == "" { //新建
  149. if entUserId == 0 {
  150. data["entUserId"] = qu.IntAll(user["id"])
  151. } else {
  152. data["entUserId"] = entUserId
  153. }
  154. data["entId"] = qu.IntAll(user["ent_id"])
  155. data["appid"] = qu.ObjToString(user["appid"])
  156. data["i_createtime"] = i_createtime
  157. data["s_createuser"] = user["name"]
  158. s_namekey := gopinyin.Convert(qu.ObjToString(data["s_name"]), false)
  159. data["s_namekey"] = s_namekey
  160. data["b_delete"] = false
  161. data["s_dataid"] = SEPreview.EncodeString(fmt.Sprintf("%v", i_createtime) + s_namekey + fmt.Sprintln(user["ent_id"]))
  162. id = Mgo.Save("entniche_rule", data)
  163. if id != "" {
  164. rep = true
  165. } else {
  166. rep = false
  167. }
  168. } else {
  169. if entUserId == 0 {
  170. data["entUserId"] = qu.IntAll(user["id"])
  171. } else {
  172. data["entUserId"] = entUserId
  173. }
  174. data["appid"] = qu.ObjToString(user["appid"])
  175. data["entId"] = qu.IntAll(user["ent_id"])
  176. query := bson.M{
  177. "_id": mongodb.StringTOBsonId(id),
  178. }
  179. rep = Mgo.Update("entniche_rule", query, bson.M{"$set": data}, false, false)
  180. }
  181. c.ServeJson(map[string]interface{}{
  182. "id": id,
  183. "rep": rep,
  184. "s_esquery": data["s_esquery"],
  185. "s_dataid": data["s_dataid"],
  186. })
  187. } else {
  188. user := c.GetSession("user").(map[string]interface{})
  189. c.T["did"] = user["ent_id"]
  190. c.T["cid"] = user["id"]
  191. entstr := encrypt.SE.DecodeString(c.GetString("entUserId"))
  192. c.T["entUserId"], _ = strconv.Atoi(entstr)
  193. c.T["province"] = Province
  194. c.T["city"] = ProvinceCitys
  195. c.T["district"] = CityDistricts
  196. c.T["topTypeArr"] = TopTypeArr
  197. c.T["subTypeArr"] = SubTypeArr
  198. c.T["matchTypeMap"] = MatchTypeMap
  199. c.T["matchTypeMap2"] = MatchTypeMap2
  200. c.T["existField"] = ExistFiled
  201. c.T["buyerClass"] = BuyerClass
  202. c.T["buyerClassMap"] = BuyerClassMap
  203. c.T["scopeClass"] = ScopeClassMap
  204. c.T["preview_href"] = PreviewHref
  205. c.Render("client/cuser_rule_create.html", &c.T)
  206. }
  207. }
  208. func (c *Client) CuserRuleEdit() {
  209. defer qu.Catch()
  210. id := encrypt.SE.DecodeString(c.GetString("id"))
  211. query := bson.M{"_id": mongodb.StringTOBsonId(id)}
  212. data, _ := Mgo.FindOneByField("entniche_rule", query, `{}`)
  213. (*data)["id"] = mongodb.BsonIdToSId((*data)["_id"])
  214. c.T["cid"] = (*data)["s_userid"]
  215. c.T["ids"] = qu.ObjToString((*data)["s_departid"]) + "," + qu.ObjToString((*data)["s_userid"])
  216. c.T["data"] = *data
  217. c.T["province"] = Province
  218. c.T["city"] = ProvinceCitys
  219. c.T["district"] = CityDistricts
  220. c.T["topTypeArr"] = TopTypeArr
  221. c.T["subTypeArr"] = SubTypeArr
  222. c.T["matchTypeMap"] = MatchTypeMap
  223. c.T["matchTypeMap2"] = MatchTypeMap2
  224. c.T["existField"] = ExistFiled
  225. c.T["buyerClass"] = BuyerClass
  226. c.T["buyerClassMap"] = BuyerClassMap
  227. c.T["scopeClass"] = ScopeClassMap
  228. c.T["preview_href"] = PreviewHref
  229. c.Render("client/cuser_rule_edit.html", &c.T)
  230. }
  231. func (this *Client) DataPreview() {
  232. this.Render("client/preview.html")
  233. }
  234. func (this *Client) ExportLog() {
  235. if this.Method() == "POST" {
  236. id, _ := this.GetInt("id")
  237. start, _ := this.GetInt("start")
  238. limit, _ := this.GetInt("length")
  239. draw, _ := this.GetInt("draw")
  240. sqls := fmt.Sprintf("select count(1) from user_expend_record where user_id = %d", id)
  241. count := JyMysql.CountBySql(sqls)
  242. if count > 0 {
  243. sql := fmt.Sprintf("select * from user_expend_record where user_id = %d order by id desc limit %d,%d", id, start, limit)
  244. exportLog := JyMysql.SelectBySql(sql)
  245. for _, v := range *exportLog {
  246. data := JyMysql.FindOne("entniche_export_log", map[string]interface{}{"id": v["export_id"]}, "user_name,export_num,deduct_num,download_url", "")
  247. if data != nil && *data != nil {
  248. v["export_num"] = (*data)["export_num"]
  249. v["deduct_num"] = (*data)["deduct_num"]
  250. v["download_url"] = (*data)["download_url"]
  251. v["user_name"] = (*data)["user_name"]
  252. }
  253. }
  254. this.ServeJson(map[string]interface{}{
  255. "draw": draw,
  256. "data": *exportLog,
  257. "recordsFiltered": count,
  258. "recordsTotal": count,
  259. })
  260. } else {
  261. this.ServeJson(map[string]interface{}{
  262. "draw": draw,
  263. "data": nil,
  264. "recordsFiltered": 0,
  265. "recordsTotal": 0,
  266. })
  267. }
  268. } else {
  269. id, _ := strconv.Atoi(encrypt.SE.DecodeString(this.GetString("id")))
  270. this.T["id"] = id
  271. this.Render("client/exportLog.html", &this.T)
  272. }
  273. }