client.go 9.5 KB

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