package service import ( "encoding/json" "history" "log" "os" mongoutil "qfw/mongodb" qu "qfw/util" "qfw/util/mail" "strconv" "strings" "sync" "time" . "util" "github.com/tealeg/xlsx" "github.com/go-xweb/xweb" "gopkg.in/mgo.v2/bson" ) var projectIdMap = sync.Map{} var xlsxArr []map[string]interface{} type SecondPush struct { *xweb.Action secondPush xweb.Mapper `xweb:"/service/secondpush/create"` getUserRule xweb.Mapper `xweb:"/service/secondpush/getrules"` saveSpushRule xweb.Mapper `xweb:"/service/secondpush/saverule"` secondPushTask xweb.Mapper `xweb:"/service/secondpush/dotask"` addSecondPushRule xweb.Mapper `xweb:"/service/secondpush/add"` //保存二次推送规则 secondPushList xweb.Mapper `xweb:"/service/secondpush/list"` secondPushClone xweb.Mapper `xweb:"/service/secondPushClone/(.*)"` delSecondPushRule xweb.Mapper `xweb:"/service/customer/secondpush/delrule"` //删除历史任务规则 } func (s *SecondPush) SecondPush() { defer qu.Catch() id := s.GetString("id") user := s.GetSession("user").(map[string]interface{}) query := bson.M{} query["_id"] = mongoutil.StringTOBsonId(id) s_pushid := s.GetString("s_pushid") secondpush_id := "" if s_pushid != "" { secondpush_id = s_pushid hData, ok := Mgo.FindOne("second_push", bson.M{"_id": mongoutil.StringTOBsonId(secondpush_id)}) if ok && hData != nil && len(*hData) > 0 { s.T["spush_rule"] = (*hData)["dep_rules"] (*hData)["_id"] = id s.T["data"] = *hData } } else { var user_name, user_customername, appid = "", "", "" userInfo, ok := Mgo.FindOne("euser", query) if ok && userInfo != nil && *userInfo != nil { user_name = qu.ObjToString((*userInfo)["s_name"]) user_customername = qu.ObjToString((*userInfo)["s_customername"]) appid = qu.ObjToString((*userInfo)["s_appid"]) } secondpush_id = Mgo.Save("second_push", map[string]interface{}{ "createtime": time.Now().Unix(), "updatetime": time.Now().Unix(), "user_id": id, "create_user": user["name"], "state": 0, "s_name": user_name, "s_customername": user_customername, "s_appid": appid, }) s.Redirect("/service/secondpush/create?id=" + id + "&s_pushid=" + secondpush_id) return } s.T["spush_id"] = secondpush_id s.Render("private/second_push_create.html", &s.T) } func (s *SecondPush) GetUserRule() { customerId := s.GetString("c_id") query := bson.M{ "s_userid": customerId, "b_delete": false, } data, _ := Mgo.Find("euserdepart", query, `{"i_createtime":-1}`, nil, false, 0, 0) if data != nil && len(*data) != 0 { res := []map[string]interface{}{} for _, m := range *data { history_rule_data := make(map[string]interface{}) q := bson.M{ "s_departid": mongoutil.BsonIdToSId(m["_id"]), "b_delete": false, } history_rule_data["department_isuse"] = m["i_isuse"] history_rule_data["depart_name"] = m["s_name"] depart_rule_data, _ := Mgo.Find("euserdepartrule", q, `{"i_createtime":-1}`, `{"s_name":1,"i_updatetime":1,"i_isuse":1,"_id":1}`, false, 0, 0) if depart_rule_data != nil && len(*depart_rule_data) != 0 { for _, j := range *depart_rule_data { j["_id"] = mongoutil.BsonIdToSId(j["_id"]) } history_rule_data["department_rule"] = depart_rule_data history_rule_data["is_slected"] = true } res = append(res, history_rule_data) } s.ServeJson(map[string]interface{}{ "status": "success", "data": res, }) } else { s.ServeJson(map[string]interface{}{ "status": "fail", "data": "", }) } } //保存二次推送规则 func (s *SecondPush) SaveSpushRule() { rules_id := s.GetString("rulesid") s_pushid := s.GetString("s_pushid") // userid := s.GetString("userid") if s_pushid != "" { user_history, _ := Mgo.FindOne("second_push", map[string]interface{}{ "_id": mongoutil.StringTOBsonId(s_pushid), }) if *user_history != nil && user_history != nil { if (*user_history)["dep_rules"] != nil { dep_rules := qu.ObjArrToMapArr((*user_history)["dep_rules"].([]interface{})) dep_new_rules := []map[string]interface{}{} ruleMap := map[string]bool{} if len(dep_rules) > 0 { for _, r := range dep_rules { dep_new_rules = append(dep_new_rules, r) ruleMap[mongoutil.BsonIdToSId(r["_id"])] = true } } rules_id_list := strings.Split(rules_id, ",") for _, rule := range rules_id_list { if !ruleMap[rule] { query := bson.M{ "_id": mongoutil.StringTOBsonId(rule), "b_delete": false, } res, _ := Mgo.FindOne("euserdepartrule", query) if res != nil && len(*res) != 0 { //获取规则所属部门信息 department_info, _ := Mgo.FindOne("euserdepart", map[string]interface{}{ "_id": mongoutil.StringTOBsonId((*res)["s_departid"].(string)), "b_delete": false, }) (*res)["is_new"] = false (*res)["s_depart_name"] = (*department_info)["s_name"] dep_new_rules = append(dep_new_rules, *res) } } } set := bson.M{ "$set": bson.M{ "dep_rules": dep_new_rules, // "tag_rules": usertags, "updatetime": time.Now().Unix(), }, } ok := Mgo.Update("second_push", map[string]interface{}{ "_id": mongoutil.StringTOBsonId(s_pushid), }, set, false, false) s.ServeJson(map[string]interface{}{ "status": ok, }) return } else { rules_id_list := strings.Split(rules_id, ",") dep_rules := []map[string]interface{}{} for _, rule := range rules_id_list { query := bson.M{ "_id": mongoutil.StringTOBsonId(rule), "b_delete": false, } res, _ := Mgo.FindOne("euserdepartrule", query) if res != nil && *res != nil { //获取规则所属部门信息 department_info, _ := Mgo.FindOne("euserdepart", map[string]interface{}{ "_id": mongoutil.StringTOBsonId((*res)["s_departid"].(string)), "b_delete": false, }) (*res)["is_new"] = false (*res)["s_depart_name"] = (*department_info)["s_name"] dep_rules = append(dep_rules, *res) } } set := bson.M{ "$set": bson.M{ "dep_rules": dep_rules, // "tag_rules": usertags, "updatetime": time.Now().Unix(), }, } isupdata := Mgo.Update("second_push", map[string]interface{}{ "_id": mongoutil.StringTOBsonId(s_pushid), }, set, false, false) s.ServeJson(map[string]interface{}{ "status": isupdata, }) return } } s.ServeJson(map[string]interface{}{ "status": "fail", }) } } func (s *SecondPush) SecondPushTask() { log.Println("开始二次推送任务...") s_pushid := s.GetString("s_pushid") if s_pushid != "" { customer, _ := Mgo.Find("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(s_pushid)}, nil, nil, false, -1, -1) if len(*customer) == 1 { c := (*customer)[0] customer_name := qu.ObjToString(c["s_name"]) //客户名称 email := qu.ObjToString(c["sendMail"]) starttime := qu.Int64All(c["starttime"]) endtime := qu.Int64All(c["endtime"]) if len(c) != 0 && c != nil { if c["dep_rules"] != nil && len(c["dep_rules"].([]interface{})) != 0 { for _, m := range c["dep_rules"].([]interface{}) { log.Println("ruleid", mongoutil.BsonIdToSId(m.(map[string]interface{})["_id"])) q := bson.M{ "ruleid": mongoutil.BsonIdToSId(m.(map[string]interface{})["_id"]), "createtime": bson.M{ "$gte": starttime, "$lte": endtime, }, } d, _ := MgoCus.Find("usermail", q, nil, nil, false, 0, 0) if len(*d) > 0 && d != nil { for _, l := range *d { xlsxArr = append(xlsxArr, l) } } } } else { q := bson.M{ "appid": c["s_appid"], "createtime": bson.M{ "$gte": starttime, "$lte": endtime, }, } d, _ := MgoCus.Find("usermail", q, nil, nil, false, 0, 0) for _, l := range *d { xlsxArr = append(xlsxArr, l) } } if len(xlsxArr) == 0 { log.Println("查询数据为空") } else { log.Println(len(xlsxArr)) GetXlsxs(xlsxArr, customer_name, email, s_pushid) go UpdateHistoryState(2, s_pushid, len(xlsxArr)) s.ServeJson(map[string]interface{}{ "status": true, }) xlsxArr = []map[string]interface{}{} } } } else { log.Println("初始化客户信息失败") } } } //func (s *SecondPush) HistoryList() { // id := s.GetString("id") // if s.Method() == "POST" { // data, _ := Mgo.Find("second_push", map[string]interface{}{"user_id": id}, `{"updatetime":-1}`, nil, false, -1, -1) // s.ServeJson(map[string]interface{}{ // "data": data, // }) // } else { // s.T["id"] = id // s.Render("private/historylog_list.html", &s.T) // } //} func UpdateHistoryState(state int, s_pushid string, count int) { if state == 2 { Mgo.Update("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(s_pushid)}, map[string]interface{}{ "$set": map[string]interface{}{ "state": state, "result_count": count, "finishtime":time.Now().Unix(), }, }, false, false) return } Mgo.Update("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(s_pushid)}, map[string]interface{}{ "$set": map[string]interface{}{ "state": state, }, }, false, false) } func (s *SecondPush) HistoryClone(hid string) { data, ok := Mgo.FindOne("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(hid)}) if ok && data != nil && *data != nil { (*data)["state"] = 0 (*data)["result_count"] = nil (*data)["createtime"] = time.Now().Unix() (*data)["updatetime"] = time.Now().Unix() Mgo.Save("second_push", *data) } } func (s *SecondPush) HistoryRuleEdit() { hid := s.GetString("hid") rid := s.GetString("rid") if s.Method() == "POST" { data := GetPostForm(s.Request) o_rules := []map[string]interface{}{} o_rulesStr := data["o_rules"].(string) json.Unmarshal([]byte(o_rulesStr), &o_rules) data["o_rules"] = o_rules data["_id"] = mongoutil.StringTOBsonId(rid) if qu.IntAll(data["i_esquerytype"]) == 1 { //自动生成es data["s_esquery"] = Utiltags(data) } datas, ok := Mgo.FindOne("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(hid)}) if ok && datas != nil && *datas != nil { dep_rules := qu.ObjArrToMapArr((*datas)["dep_rules"].([]interface{})) for k, v := range dep_rules { if rid == mongoutil.BsonIdToSId(v["_id"]) { dep_rules[k] = data } } rep := Mgo.Update("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(hid)}, map[string]interface{}{"$set": map[string]interface{}{"dep_rules": dep_rules}}, false, false) s.ServeJson(map[string]interface{}{ "rep": rep, "s_esquery": data["s_esquery"], }) } } else { id := s.GetString("id") data, ok := Mgo.FindOne("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(hid)}) if ok && data != nil && *data != nil { dep_rules := qu.ObjArrToMapArr((*data)["dep_rules"].([]interface{})) for _, v := range dep_rules { if rid == mongoutil.BsonIdToSId(v["_id"]) { s.T["data"] = v } } } s.T["did"] = id //部门id s.T["cid"] = id //客户id s.T["ids"] = id s.T["history_id"] = hid s.T["rid"] = rid s.T["province"] = Province s.T["city"] = ProvinceCitys s.T["district"] = CityDistricts s.T["topTypeArr"] = TopTypeArr s.T["subTypeArr"] = SubTypeArr s.T["matchTypeMap"] = MatchTypeMap s.T["matchTypeMap2"] = MatchTypeMap2 s.T["existField"] = ExistFiled s.T["buyerClass"] = BuyerClass s.T["scopeClass"] = ScopeClassMap s.Render("private/history_rule_edit.html", &s.T) } } func GetXlsxs(mMap []map[string]interface{}, fn, email, id string) { if id != "" { query := bson.M{ "_id": bson.ObjectIdHex(id), } data, ok := Mgo.FindOne("second_push", query) if ok && (*data) != nil && len(*data) > 0 { dataType := qu.IntAll((*data)["i_extfieldstype"]) xf, err := xlsx.OpenFile("web/res/fields.xlsx") if err != nil { log.Println("fields file not foud", err.Error()) } if dataType == 1 { sh := xf.Sheets[0] for i, v := range mMap { row := sh.AddRow() row.AddCell().SetInt(i + 1) row.AddCell().SetValue(v["matchkey"]) row.AddCell().SetValue(v["area"]) row.AddCell().SetValue(v["city"]) row.AddCell().SetValue(v["title"]) row.AddCell().SetValue(v["subtype"]) if v["publishtime"] != nil { row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02")) } else { row.AddCell() } row.AddCell().SetValue(v["buyer"]) row.AddCell().SetValue(v["winner"]) if v["bidamount"] != nil { row.AddCell().SetFloat(qu.Float64All(v["bidamount"]) / 10000) } else { row.AddCell() } row.AddCell().SetValue(v["projectname"]) row.AddCell().SetValue(v["detail"]) row.AddCell().SetValue(v["s_jyhref"]) } xf.Sheets = xf.Sheets[0:1] } else if dataType == 2 { sh := xf.Sheets[1] for i, v := range mMap { row := sh.AddRow() row.AddCell().SetInt(i + 1) row.AddCell().SetValue(v["matchkey"]) row.AddCell().SetValue(v["area"]) row.AddCell().SetValue(v["city"]) row.AddCell().SetValue(v["title"]) row.AddCell().SetValue(v["subtype"]) row.AddCell().SetValue(v["detail"]) if v["publishtime"] != nil { row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02")) } else { row.AddCell() } row.AddCell().SetValue(v["href"]) row.AddCell().SetValue(v["s_jyhref"]) row.AddCell().SetValue(v["projectcode"]) row.AddCell().SetValue(v["projectname"]) row.AddCell().SetValue(v["projectscope"]) if v["budget"] != nil { row.AddCell().SetFloat(qu.Float64All(v["budget"]) / 10000) } else { row.AddCell() } if v["bidamount"] != nil { row.AddCell().SetFloat(qu.Float64All(v["bidamount"]) / 10000) } else { row.AddCell() } if v["bidopentime"] != nil { row.AddCell().SetValue(time.Unix(qu.Int64All(v["bidopentime"]), 0).Format("2006-01-02")) } else { row.AddCell() } row.AddCell().SetValue(v["buyer"]) row.AddCell().SetValue(v["buyerperson"]) row.AddCell().SetValue(v["buyertel"]) row.AddCell().SetValue(v["agency"]) row.AddCell().SetValue(v["winner"]) row.AddCell().SetValue(v["winnerperson"]) row.AddCell().SetValue(v["winnertel"]) row.AddCell().SetValue(v["legal_person"]) row.AddCell().SetValue(v["company_phone"]) row.AddCell().SetValue(v["company_email"]) } xf.Sheets = xf.Sheets[1:2] } else { sh := xf.Sheets[2] for i, v := range mMap { row := sh.AddRow() row.AddCell().SetInt(i + 1) row.AddCell().SetValue(v["matchkey"]) row.AddCell().SetValue(v["area"]) row.AddCell().SetValue(v["city"]) row.AddCell().SetValue(v["district"]) row.AddCell().SetValue(v["title"]) row.AddCell().SetValue(v["detail"]) if v["publishtime"] != nil { row.AddCell().SetValue(time.Unix(qu.Int64All(v["publishtime"]), 0).Format("2006-01-02")) } else { row.AddCell() } row.AddCell().SetValue(v["href"]) if v["bidamount"] != nil { row.AddCell().SetFloat(qu.Float64All(v["bidamount"]) / 10000) } else { row.AddCell() } row.AddCell().SetValue(v["buyer"]) row.AddCell().SetValue(v["buyerperson"]) row.AddCell().SetValue(v["buyertel"]) row.AddCell().SetValue(v["s_winner"]) row.AddCell().SetValue(v["legal_person"]) row.AddCell().SetValue(v["company_phone"]) row.AddCell().SetValue(v["company_address"]) row.AddCell().SetValue(v["rank"]) row.AddCell().SetValue(v["purchasing"]) row.AddCell().SetValue(v["capital"]) row.AddCell().SetValue(v["establish_date"]) row.AddCell().SetValue(v["business_scope"]) row.AddCell().SetValue(v["stock_name"]) row.AddCell().SetValue(v["buyer_credit_no"]) row.AddCell().SetValue(v["winner_credit_no"]) row.AddCell().SetValue(v["id"]) } xf.Sheets = xf.Sheets[2:3] } //生文件 t := strconv.FormatInt(time.Now().Unix(), 10) dir := "./web/res/xlsx/" + t + "/" if b, _ := history.PathExists(dir); !b { err1 := os.MkdirAll(dir, os.ModePerm) if err1 != nil { log.Println("mkdir err", dir) } } fname := t + ".xlsx" err = xf.Save(dir + fname) if err != nil { log.Println("xls error", fname) } else { for i := 0; i < len(history.Gmails); i++ { gmail := history.Gmails[i] status := mail.GSendMail_q("剑鱼标讯", email, "", "", fn, "", dir+fname, fname, gmail) if status { log.Println("send mail success", fname, email) break } } } } } } func (s *SecondPush) AddSecondPushRule() { defer qu.Catch() if s.Method() == "POST" { his_id := s.GetString("s_pushid") if his_id != "" { i_updatetime, _ := strconv.ParseInt(s.GetString("i_updatetime"), 10, 64) i_extfieldstype, _ := strconv.Atoi(s.GetString("i_extfieldstype")) set := bson.M{ "$set": bson.M{ "i_updatetime": i_updatetime, "sendMail": s.GetString("sendMail"), "i_extfieldstype": i_extfieldstype, "starttime": s.GetString("starttime"), "endtime": s.GetString("endtime"), }, } isupdata := Mgo.UpdateById("second_push", mongoutil.StringTOBsonId(his_id), set) if isupdata { s.ServeJson(map[string]interface{}{ "history_id": his_id, "rep": true, }) } else { s.ServeJson(map[string]interface{}{ "history_id": "", "rep": false, }) } } } } func (s *SecondPush) SecondPushList() { id := s.GetString("id") if s.Method() == "POST" { data, _ := Mgo.Find("second_push", map[string]interface{}{"user_id": id}, `{"updatetime":-1}`, nil, false, -1, -1) s.ServeJson(map[string]interface{}{ "data": data, }) } } func (s *SecondPush) SecondPushClone(hid string) { data, ok := Mgo.FindOne("second_push", map[string]interface{}{"_id": mongoutil.StringTOBsonId(hid)}) if ok && data != nil && *data != nil { (*data)["state"] = 0 (*data)["result_count"] = nil (*data)["createtime"] = time.Now().Unix() (*data)["updatetime"] = time.Now().Unix() delete((*data),"finishtime") Mgo.Save("second_push", *data) } } func (s *SecondPush) DelSecondPushRule() { id := s.GetString("id") hid := s.GetString("s_pushid") if hid != "" { data, ok := Mgo.FindOne("second_push", bson.M{"_id": mongoutil.StringTOBsonId(hid)}) if ok && data != nil && *data != nil { if (*data)["dep_rules"] != nil { dep_rules := qu.ObjArrToMapArr((*data)["dep_rules"].([]interface{})) ruleMap := map[string]bool{id: true} ruleArr := []map[string]interface{}{} for _, m := range dep_rules { if !ruleMap[mongoutil.BsonIdToSId(m["_id"])] { ruleArr = append(ruleArr, m) } } ok := Mgo.Update("second_push", bson.M{"_id": mongoutil.StringTOBsonId(hid)}, bson.M{"$set": bson.M{"dep_rules": ruleArr}}, false, false) s.ServeJson(map[string]interface{}{ "success": ok, }) } } } }