package client import ( . "JySEPlatform/util" "encoding/json" "fmt" "log" "regexp" "strconv" "strings" "time" qu "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/encrypt" "app.yhyue.com/moapp/jybase/go-xweb/xweb" "app.yhyue.com/moapp/jybase/mongodb" "github.com/lauyoume/gopinyin" "go.mongodb.org/mongo-driver/bson" ) type Client struct { *xweb.Action index xweb.Mapper `xweb:"/client/index"` //客户首页 cuserRuleDel xweb.Mapper `xweb:"/client/rule/del"` //删除规则 adminIndex xweb.Mapper `xweb:"/admin/index"` //管理员首页 personnelIndex xweb.Mapper `xweb:"/personnel/index"` //员工首页 cuserRuleCreate xweb.Mapper `xweb:"/client/rule/create"` //新建规则 cuserRuleEdit xweb.Mapper `xweb:"/client/rule/edit"` //修改规则 exportLog xweb.Mapper `xweb:"/client/exportLog"` //导出页面 } var ( replaceField_detail = map[string]bool{`"title"`: true, `"projectname.pname"`: true} reg_detail = regexp.MustCompile("\"fields\":\\[([^]]*detail[^]]*)\\]") reg_single = regexp.MustCompile("\"query\":\"[^\"{:]\"") ) func (this *Client) AdminIndex() { defer qu.Catch() entId := qu.IntAll(this.GetSession("entId")) entUserArr := JyMysql.Find("entniche_user", map[string]interface{}{"ent_id": entId, "export_power": 1}, "", "id desc", -1, -1) if entUserArr != nil && *entUserArr != nil && len(*entUserArr) > 0 { for _, v := range *entUserArr { userId := qu.IntAll(v["id"]) deptmentUser := JyMysql.FindOne("entniche_department_user", map[string]interface{}{"user_id": userId}, "dept_id", "") if deptmentUser != nil && *deptmentUser != nil { dId := qu.IntAll((*deptmentUser)["dept_id"]) v["departId"] = dId deptment := JyMysql.FindOne("entniche_department", map[string]interface{}{"id": dId}, "name", "") if deptment != nil && *deptment != nil { dName := qu.ObjToString((*deptment)["name"]) v["departName"] = dName } } v["id"] = encrypt.SE.EncodeString(strconv.Itoa(userId)) } this.T["entUserArr"] = *entUserArr } this.Render("/client/adminIndex.html") } func (this *Client) PersonnelIndex() { defer qu.Catch() id := this.GetString("id") sessVal := this.Session().GetMultiple() if this.Method() == "POST" { start, _ := this.GetInt("start") limit, _ := this.GetInt("length") draw, _ := this.GetInt("draw") searchStr := this.GetString("search[value]") search := strings.TrimSpace(searchStr) entUserId := 0 if id == "" { entUserId = qu.IntAll(sessVal["entUserId"]) } else { entUserId, _ = strconv.Atoi(encrypt.SE.DecodeString(id)) } entId := qu.IntAll(sessVal["entId"]) count := Mgo.Count("entniche_rule", map[string]interface{}{"entId": entId, "entUserId": entUserId}) query := map[string]interface{}{ "entId": entId, "entUserId": entUserId, } if search != "" { query["s_name"] = bson.M{ "$regex": search, } } entRuleArr, ok := Mgo.Find("entniche_rule", query, `{"_id":-1}`, nil, false, int(start), int(limit)) if ok && entRuleArr != nil && *entRuleArr != nil && len(*entRuleArr) > 0 { for _, v := range *entRuleArr { ruleid := mongodb.BsonIdToSId(v["_id"]) v["_id"] = encrypt.SE.EncodeString(ruleid) } this.ServeJson(map[string]interface{}{ "draw": draw, "data": *entRuleArr, "recordsFiltered": count, "recordsTotal": count, }) } else { this.ServeJson(map[string]interface{}{ "draw": draw, "data": nil, "recordsFiltered": 0, "recordsTotal": 0, }) } } else { this.T["id"] = id entUserId := 0 if id == "" { entUserId = qu.IntAll(this.GetSession("entUserId")) } else { entUserId, _ = strconv.Atoi(encrypt.SE.DecodeString(id)) } entId := qu.IntAll(sessVal["entId"]) leftNumData := JyMysql.FindOne("user_account", map[string]interface{}{"user_id": entUserId, "ent_id": entId}, "left_num", "") left := 0 if leftNumData != nil && *leftNumData != nil { left = int((*leftNumData)["left_num"].(int64)) } this.T["left"] = left this.Render("/client/personnelIndex.html", &this.T) } } // 对外客户删除 部门规则 func (c *Client) CuserRuleDel() { defer qu.Catch() _id := c.GetString("_id") b := Mgo.Del("entniche_rule", map[string]interface{}{"_id": mongodb.StringTOBsonId(encrypt.SE.DecodeString(_id))}) c.ServeJson(map[string]interface{}{ "rep": b, }) } func (c *Client) CuserRuleCreate() { defer qu.Catch() sessVal := c.Session().GetMultiple() if c.Method() == "POST" { data := GetPostForm(c.Request) entstr := c.GetString("entUserId") entUserId, err := strconv.Atoi(entstr) if err != nil { entUserId = 0 } o_rules := []map[string]interface{}{} o_rulesStr := data["o_rules"].(string) json.Unmarshal([]byte(o_rulesStr), &o_rules) data["o_rules"] = o_rules id := qu.ObjToString(data["id"]) delete(data, "id") delete(data, "ids") if qu.IntAll(data["i_esquerytype"]) == 1 { //自动生成es esStr := Utiltags(data) data["s_esquery"] = esStr data["s_esquery_search"] = filter(esStr) } i_createtime := time.Now().Unix() data["i_updatetime"] = i_createtime data["s_updateuser"] = sessVal["entUserName"] entMgoInfo, _ := MgoCus.FindOne("user", bson.M{"entid": sessVal["entId"]}) appid := qu.ObjToString((*entMgoInfo)["appid"]) if appid == "" { log.Println("appid 异常, ", sessVal["entUserId"]) c.ServeJson(map[string]interface{}{ "rep": false, }) return } var rep = false if id == "" { //新建 if entUserId == 0 { data["entUserId"] = qu.IntAll(sessVal["entUserId"]) } else { data["entUserId"] = entUserId } data["entId"] = qu.IntAll(sessVal["entId"]) data["appid"] = appid data["i_createtime"] = i_createtime data["s_createuser"] = sessVal["entUserName"] s_namekey := gopinyin.Convert(qu.ObjToString(data["s_name"]), false) data["s_namekey"] = s_namekey data["b_delete"] = false data["s_dataid"] = SEPreview.EncodeString(fmt.Sprintf("%v", i_createtime) + s_namekey + fmt.Sprintln(sessVal["entId"])) id = Mgo.Save("entniche_rule", data) if id != "" { rep = true } else { rep = false } } else { if entUserId == 0 { data["entUserId"] = qu.IntAll(sessVal["entUserId"]) } else { data["entUserId"] = entUserId } data["appid"] = appid data["entId"] = qu.IntAll(sessVal["entId"]) query := bson.M{ "_id": mongodb.StringTOBsonId(id), } rep = Mgo.Update("entniche_rule", query, bson.M{"$set": data}, false, false) } c.ServeJson(map[string]interface{}{ "id": id, "rep": rep, "s_esquery": data["s_esquery"], "s_dataid": data["s_dataid"], }) } else { c.T["did"] = sessVal["entId"] c.T["cid"] = sessVal["entUserId"] entstr := encrypt.SE.DecodeString(c.GetString("entUserId")) c.T["entUserId"], _ = strconv.Atoi(entstr) c.T["province"] = Province c.T["city"] = ProvinceCitys c.T["district"] = CityDistricts c.T["topTypeArr"] = TopTypeArr c.T["subTypeArr"] = SubTypeArr c.T["matchTypeMap"] = MatchTypeMap c.T["matchTypeMap2"] = MatchTypeMap2 c.T["existField"] = ExistFiled c.T["buyerClass"] = BuyerClass c.T["buyerClassMap"] = BuyerClassMap c.T["scopeClass"] = ScopeClassMap c.T["preview_href"] = PreviewHref c.Render("client/cuser_rule_create.html", &c.T) } } func (c *Client) CuserRuleEdit() { defer qu.Catch() id := encrypt.SE.DecodeString(c.GetString("id")) query := bson.M{"_id": mongodb.StringTOBsonId(id)} data, _ := Mgo.FindOneByField("entniche_rule", query, `{}`) (*data)["id"] = mongodb.BsonIdToSId((*data)["_id"]) c.T["cid"] = (*data)["s_userid"] c.T["ids"] = qu.ObjToString((*data)["s_departid"]) + "," + qu.ObjToString((*data)["s_userid"]) c.T["data"] = *data c.T["province"] = Province c.T["city"] = ProvinceCitys c.T["district"] = CityDistricts c.T["topTypeArr"] = TopTypeArr c.T["subTypeArr"] = SubTypeArr c.T["matchTypeMap"] = MatchTypeMap c.T["matchTypeMap2"] = MatchTypeMap2 c.T["existField"] = ExistFiled c.T["buyerClass"] = BuyerClass c.T["buyerClassMap"] = BuyerClassMap c.T["scopeClass"] = ScopeClassMap c.T["preview_href"] = PreviewHref c.Render("client/cuser_rule_edit.html", &c.T) } func (this *Client) DataPreview() { this.Render("client/preview.html") } func (this *Client) ExportLog() { if this.Method() == "POST" { id, _ := this.GetInt("id") start, _ := this.GetInt("start") limit, _ := this.GetInt("length") draw, _ := this.GetInt("draw") sqls := fmt.Sprintf("select count(1) from user_expend_record where user_id = %d", id) count := JyMysql.CountBySql(sqls) if count > 0 { sql := fmt.Sprintf("select * from user_expend_record where user_id = %d order by id desc limit %d,%d", id, start, limit) exportLog := JyMysql.SelectBySql(sql) for _, v := range *exportLog { data := JyMysql.FindOne("entniche_export_log", map[string]interface{}{"id": v["export_id"]}, "user_name,export_num,deduct_num,download_url", "") if data != nil && *data != nil { v["export_num"] = (*data)["export_num"] v["deduct_num"] = (*data)["deduct_num"] v["download_url"] = (*data)["download_url"] v["user_name"] = (*data)["user_name"] } } this.ServeJson(map[string]interface{}{ "draw": draw, "data": *exportLog, "recordsFiltered": count, "recordsTotal": count, }) } else { this.ServeJson(map[string]interface{}{ "draw": draw, "data": nil, "recordsFiltered": 0, "recordsTotal": 0, }) } } else { id, _ := strconv.Atoi(encrypt.SE.DecodeString(this.GetString("id"))) this.T["id"] = id this.Render("client/exportLog.html", &this.T) } }