client.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package client
  2. import (
  3. . "JySEPlatform/util"
  4. "encoding/json"
  5. "fmt"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "time"
  10. qu "app.yhyue.com/moapp/jybase/common"
  11. "app.yhyue.com/moapp/jybase/encrypt"
  12. "app.yhyue.com/moapp/jybase/go-xweb/xweb"
  13. "app.yhyue.com/moapp/jybase/mongodb"
  14. "github.com/lauyoume/gopinyin"
  15. "go.mongodb.org/mongo-driver/bson"
  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. entId := qu.IntAll(this.GetSession("entId"))
  35. entUserArr := JyMysql.Find("entniche_user", map[string]interface{}{"ent_id": entId, "export_power": 1}, "", "id desc", -1, -1)
  36. if entUserArr != nil && *entUserArr != nil && len(*entUserArr) > 0 {
  37. for _, v := range *entUserArr {
  38. userId := qu.IntAll(v["id"])
  39. deptmentUser := JyMysql.FindOne("entniche_department_user", map[string]interface{}{"user_id": userId}, "dept_id", "")
  40. if deptmentUser != nil && *deptmentUser != nil {
  41. dId := qu.IntAll((*deptmentUser)["dept_id"])
  42. v["departId"] = dId
  43. deptment := JyMysql.FindOne("entniche_department", map[string]interface{}{"id": dId}, "name", "")
  44. if deptment != nil && *deptment != nil {
  45. dName := qu.ObjToString((*deptment)["name"])
  46. v["departName"] = dName
  47. }
  48. }
  49. v["id"] = encrypt.SE.EncodeString(strconv.Itoa(userId))
  50. }
  51. this.T["entUserArr"] = *entUserArr
  52. }
  53. this.Render("/client/adminIndex.html")
  54. }
  55. func (this *Client) PersonnelIndex() {
  56. defer qu.Catch()
  57. id := this.GetString("id")
  58. sessVal := this.Session().GetMultiple()
  59. if this.Method() == "POST" {
  60. start, _ := this.GetInt("start")
  61. limit, _ := this.GetInt("length")
  62. draw, _ := this.GetInt("draw")
  63. searchStr := this.GetString("search[value]")
  64. search := strings.TrimSpace(searchStr)
  65. entUserId := 0
  66. if id == "" {
  67. entUserId = qu.IntAll(sessVal["entUserId"])
  68. } else {
  69. entUserId, _ = strconv.Atoi(encrypt.SE.DecodeString(id))
  70. }
  71. entId := qu.IntAll(sessVal["entId"])
  72. count := Mgo.Count("entniche_rule", map[string]interface{}{"entId": entId, "entUserId": entUserId})
  73. query := map[string]interface{}{
  74. "entId": entId, "entUserId": entUserId,
  75. }
  76. if search != "" {
  77. query["s_name"] = bson.M{
  78. "$regex": search,
  79. }
  80. }
  81. entRuleArr, ok := Mgo.Find("entniche_rule", query, `{"_id":-1}`, nil, false, int(start), int(limit))
  82. if ok && entRuleArr != nil && *entRuleArr != nil && len(*entRuleArr) > 0 {
  83. for _, v := range *entRuleArr {
  84. ruleid := mongodb.BsonIdToSId(v["_id"])
  85. v["_id"] = encrypt.SE.EncodeString(ruleid)
  86. }
  87. this.ServeJson(map[string]interface{}{
  88. "draw": draw,
  89. "data": *entRuleArr,
  90. "recordsFiltered": count,
  91. "recordsTotal": count,
  92. })
  93. } else {
  94. this.ServeJson(map[string]interface{}{
  95. "draw": draw,
  96. "data": nil,
  97. "recordsFiltered": 0,
  98. "recordsTotal": 0,
  99. })
  100. }
  101. } else {
  102. this.T["id"] = id
  103. entUserId := 0
  104. if id == "" {
  105. entUserId = qu.IntAll(this.GetSession("entUserId"))
  106. } else {
  107. entUserId, _ = strconv.Atoi(encrypt.SE.DecodeString(id))
  108. }
  109. entId := qu.IntAll(sessVal["entId"])
  110. leftNumData := JyMysql.FindOne("user_account", map[string]interface{}{"user_id": entUserId, "ent_id": entId}, "left_num", "")
  111. left := 0
  112. if leftNumData != nil && *leftNumData != nil {
  113. left = int((*leftNumData)["left_num"].(int64))
  114. }
  115. this.T["left"] = left
  116. this.Render("/client/personnelIndex.html", &this.T)
  117. }
  118. }
  119. // 对外客户删除 部门规则
  120. func (c *Client) CuserRuleDel() {
  121. defer qu.Catch()
  122. _id := c.GetString("_id")
  123. b := Mgo.Del("entniche_rule", map[string]interface{}{"_id": mongodb.StringTOBsonId(encrypt.SE.DecodeString(_id))})
  124. c.ServeJson(map[string]interface{}{
  125. "rep": b,
  126. })
  127. }
  128. func (c *Client) CuserRuleCreate() {
  129. defer qu.Catch()
  130. sessVal := c.Session().GetMultiple()
  131. if c.Method() == "POST" {
  132. data := GetPostForm(c.Request)
  133. entstr := c.GetString("entUserId")
  134. entUserId, err := strconv.Atoi(entstr)
  135. if err != nil {
  136. entUserId = 0
  137. }
  138. o_rules := []map[string]interface{}{}
  139. o_rulesStr := data["o_rules"].(string)
  140. json.Unmarshal([]byte(o_rulesStr), &o_rules)
  141. data["o_rules"] = o_rules
  142. id := qu.ObjToString(data["id"])
  143. delete(data, "id")
  144. delete(data, "ids")
  145. if qu.IntAll(data["i_esquerytype"]) == 1 { //自动生成es
  146. esStr := Utiltags(data)
  147. data["s_esquery"] = esStr
  148. data["s_esquery_search"] = filter(esStr)
  149. }
  150. i_createtime := time.Now().Unix()
  151. data["i_updatetime"] = i_createtime
  152. data["s_updateuser"] = sessVal["entUserName"]
  153. query := bson.M{
  154. "phone": sessVal["phone"],
  155. "username": sessVal["name"],
  156. }
  157. entMgoInfo, _ := MgoCus.FindOne("user", query)
  158. appid := qu.ObjToString((*entMgoInfo)["appid"])
  159. var rep = false
  160. if id == "" { //新建
  161. if entUserId == 0 {
  162. data["entUserId"] = qu.IntAll(sessVal["entUserId"])
  163. } else {
  164. data["entUserId"] = entUserId
  165. }
  166. data["entId"] = qu.IntAll(sessVal["entId"])
  167. data["appid"] = appid
  168. data["i_createtime"] = i_createtime
  169. data["s_createuser"] = sessVal["entUserName"]
  170. s_namekey := gopinyin.Convert(qu.ObjToString(data["s_name"]), false)
  171. data["s_namekey"] = s_namekey
  172. data["b_delete"] = false
  173. data["s_dataid"] = SEPreview.EncodeString(fmt.Sprintf("%v", i_createtime) + s_namekey + fmt.Sprintln(sessVal["entId"]))
  174. id = Mgo.Save("entniche_rule", data)
  175. if id != "" {
  176. rep = true
  177. } else {
  178. rep = false
  179. }
  180. } else {
  181. if entUserId == 0 {
  182. data["entUserId"] = qu.IntAll(sessVal["entUserId"])
  183. } else {
  184. data["entUserId"] = entUserId
  185. }
  186. data["appid"] = appid
  187. data["entId"] = qu.IntAll(sessVal["entId"])
  188. query := bson.M{
  189. "_id": mongodb.StringTOBsonId(id),
  190. }
  191. rep = Mgo.Update("entniche_rule", query, bson.M{"$set": data}, false, false)
  192. }
  193. c.ServeJson(map[string]interface{}{
  194. "id": id,
  195. "rep": rep,
  196. "s_esquery": data["s_esquery"],
  197. "s_dataid": data["s_dataid"],
  198. })
  199. } else {
  200. c.T["did"] = sessVal["entId"]
  201. c.T["cid"] = sessVal["entUserId"]
  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. }