client.go 9.5 KB

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