Explorar o código

数据包接口提交

wangkaiyue %!s(int64=4) %!d(string=hai) anos
pai
achega
3e0484ea7d

+ 1 - 37
src/jfw/front/dataExport.go

@@ -382,35 +382,7 @@ func (d *DataExport) ToCreateOrderPage(_id string) error {
 	if userId == "" {
 		return errors.New("未登录")
 	}
-	user_phone := ""
-	userData, ok := mongodb.FindById("user", userId, `{"s_phone":1,"s_m_phone":1}`)
-	if userData != nil && ok {
-		s_phone := util.ObjToString((*userData)["s_phone"])
-		if s_phone != "" {
-			user_phone = s_phone
-		} else {
-			s_m_phone := util.ObjToString((*userData)["s_m_phone"])
-			user_phone = s_m_phone
-		}
-	}
-	if user_phone != "" {
-		ud := public.Mysql.Find("entniche_user", map[string]interface{}{"phone": user_phone}, "ent_id,export_power", "createtime", 0, 0)
-		if ud != nil && len(*ud) > 0 {
-			if len(*ud) == 1 {
-				export_power := util.IntAll((*ud)[0]["export_power"])
-				if export_power == 1 {
-					d.T["isEntUser"] = true
-				}
-			} else if len(*ud) > 1 {
-				for _, v := range *ud {
-					export_power := util.IntAll(v["export_power"])
-					if export_power == 1 {
-						d.T["isEntUser"] = true
-					}
-				}
-			}
-		}
-	}
+
 	// }
 	// 企业数据导出结束
 	//是否是从结构化数据推广页过来的
@@ -477,14 +449,6 @@ func (d *DataExport) ToCreateOrderPage(_id string) error {
 			d.T["phone"] = lastPhone
 		}
 	}
-	//每日流量包查询
-	vipStatus := jy.GetBigVipUserBaseMsg(userId, public.Mysql, public.MQFW)
-	if vipStatus.Status > 0 && vipStatus.DailyNum > 0 {
-		packetMsg := dataexport.GetDataPacketMsg(vipStatus.GetUseId(), vipStatus.DailyNum)
-		d.T["hasDaily"] = true
-		d.T["dailyNum"] = vipStatus.DailyNum       //每日限量
-		d.T["dailyUsed"] = packetMsg.GetUsed(true) //已使用
-	}
 	d.T["_id"] = _id
 	d.T["msgCount"] = msgCount
 	incurKey := fmt.Sprintf("PreviewData_%s_%d", d.GetSession("userId"), time.Now().Day())

+ 11 - 17
src/jfw/modules/common/src/qfw/util/dataexport/dataExportRecord.go

@@ -137,25 +137,19 @@ func (dpm *DataPacketMsg) AddNum(num int) (err error) {
 	return fmt.Errorf("稍后重试")
 }
 
-func (dpm *DataPacketMsg) SaveRecord(db *mysql.Mysql, useId, selectId string, t int, list *[]map[string]interface{}, filePath string) error {
-	listId := []string{}
-	//rMap := dpm.GetRepeatMap(db) //去重
-	for _, v := range *list {
-		if id, _ := v["_id"].(string); id != "" {
-			listId = append(listId, id)
-		}
-	}
-
+func (dpm *DataPacketMsg) SaveRecord(db *mysql.Mysql, useId, selectId string, t int, idList []string, filePath string) error {
 	if db.Insert(recordTable, map[string]interface{}{
-		"infoids":   strings.Join(listId, ","),
-		"type":      t,
-		"master_id": dpm.masterId,
-		"useid":     useId,
-		"query_id":  selectId,
-		"date":      time.Now().Unix(),
-		"path":      filePath,
+		"infoids":    strings.Join(idList, ","),
+		"type":       t,
+		"master_id":  dpm.masterId,
+		"useid":      useId,
+		"query_id":   selectId,
+		"date":       time.Now().Unix(),
+		"path":       filePath,
+		"deduct_num": len(idList),
+		"export_num": len(idList),
 	}) == 0 {
-		return fmt.Errorf("数据导出记录保存出错 userId:%s,selectId:%s,类型:%d,导出数据:%v", useId, selectId, t, listId)
+		return fmt.Errorf("数据导出记录保存出错 userId:%s,selectId:%s,类型:%d,导出数据:%v", useId, selectId, t, idList)
 	}
 	return nil
 }

+ 408 - 0
src/jfw/modules/common/src/qfw/util/dataexport/entdataexport.go

@@ -0,0 +1,408 @@
+package dataexport
+
+import (
+	"encoding/json"
+	"fmt"
+	"github.com/tealeg/xlsx"
+	"go.mongodb.org/mongo-driver/bson"
+	"io/ioutil"
+	"log"
+	"mongodb"
+	mg "mongodb"
+	"net/http"
+	"os"
+	"qfw/util"
+	"qfw/util/elastic"
+	"qfw/util/mysql"
+	"strconv"
+	"strings"
+	"sync"
+	"time"
+)
+
+//作者:一组开发
+type Filters struct {
+	FilterId string
+}
+
+func GetEntDataExportCount(sim, bid mg.MongodbSim, bidMgoDBName, elasticAddress, _id string, entId, entUserId int, isFirst bool, url string, maxCount int) (count, newCount int, data *[]map[string]interface{}) {
+	defer util.Catch()
+	var (
+		searchsWaitGroup = &sync.WaitGroup{}
+	)
+	count = GetDataExportSearchCountByScdId(sim, elasticAddress, _id)
+	log.Println("count", count)
+	if count > maxCount {
+		count = maxCount
+	}
+	dataType := "2"
+	//数据导出数据查询
+
+	res, err := GetDataExportSearchResultByScdId(sim, bid, bidMgoDBName, elasticAddress, _id, dataType, count)
+	if err != nil {
+		log.Println("企业数据导出错误 ", err)
+		return 0, 0, nil
+	}
+	//  20210716 由原来的redis判重改为调用判重中台接口进行判重
+	m := map[string]bool{}
+	infoIdList := []string{}
+	insertFlag := "false"
+	if !isFirst {
+		insertFlag = "true"
+	}
+	for _, v := range *res {
+		id := util.ObjToString(v["_id"])
+		if m[id] {
+			continue
+		}
+		m[id] = true
+		//  20210716  redis判重调整为调用判重中台接口  每一千个调用一次
+		infoIdList = append(infoIdList, id)
+		if len(infoIdList) > 1000 {
+			//	 调接口
+			rs, err5 := Post(url, map[string]string{
+				"personId": "0", // 没有使用这个参数
+				"infoId":   strings.Join(infoIdList, ","),
+				"entId":    fmt.Sprintf("%d", entId),
+				"isInsert": insertFlag,
+				"isEnt":    "true",
+			})
+			log.Println("响应结果:", rs)
+			if err5 != nil || util.IntAll(rs["code"]) != 0 {
+				log.Println("企业订阅数据导出接口判重失败", err5)
+				log.Println("企业订阅数据导出接口判重失败rs:", rs)
+				log.Println("企业订阅数据导出接口判重失败rs[code]:", rs["code"])
+				log.Println("企业订阅数据导出接口判重失败code是否为0", util.IntAll(rs["code"]) != 0)
+				log.Println("企业订阅数据导出接口判重失败", err5, "rs:", rs, " rs[code]:", rs["code"], " ", util.IntAll(rs["code"]), "code是否为0", util.IntAll(rs["code"]) != 0)
+			} else {
+				log.Println("企业订阅数据导出")
+				// 置空
+				infoIdList = []string{}
+				// 本次数据累计
+				returnData := rs["data"].(map[string]interface{})
+				log.Println(newCount, "加之前")
+				newCount += util.IntAll(returnData["newCount"])
+				//newCount += int(returnData["newCount"].(float64))
+				log.Println(newCount, "加之后")
+			}
+
+		}
+		if !isFirst {
+			delete(v, "_id")
+			v["entid"] = entId
+			v["userid"] = entUserId
+			v["infoid"] = id
+			v["createtime"] = time.Now().Unix()
+		}
+	}
+	if len(infoIdList) > 0 {
+		rs, err5 := Post(url, map[string]string{
+			"personId": "0", // 没有使用这个参数
+			"infoId":   strings.Join(infoIdList, ","),
+			"entId":    fmt.Sprintf("%d", entId),
+			"isInsert": insertFlag,
+			"isEnt":    "true",
+		})
+		log.Println(rs)
+		if err5 != nil || util.IntAll(rs["code"]) != 0 {
+			log.Println("企业订阅数据导出接口判重失败", err5)
+			log.Println("企业订阅数据导出接口判重失败rs:", rs)
+			log.Println("企业订阅数据导出接口判重失败rs[code]:", rs["code"])
+			log.Println("企业订阅数据导出接口判重失败code是否为0", util.IntAll(rs["code"]) != 0)
+			log.Println("企业订阅数据导出接口判重失败", err5, "rs:", rs, " rs[code]:", rs["code"], " ", util.IntAll(rs["code"]), "code是否为0", util.IntAll(rs["code"]) != 0)
+		} else {
+			log.Println("企业订阅数据导出")
+			// 置空
+			infoIdList = []string{}
+			// 本次数据累计
+			returnData := rs["data"].(map[string]interface{})
+			log.Println(newCount, "加之前")
+			newCount += util.IntAll(returnData["newCount"])
+			//newCount += int(returnData["newCount"].(float64))
+			log.Println(newCount, "加之后")
+
+		}
+	}
+	searchsWaitGroup.Wait()
+	log.Println("企业数据导出--数据遍历完成")
+	//newCount = len(newCountPool)
+	log.Println("new", newCount)
+	data = res
+	return
+}
+
+func FormatExportDatas(Mgo_Ent mongodb.MongodbSim, data *[]map[string]interface{}, webdomain string, dataType string, entId int) *[]map[string]interface{} {
+	//格式化输出
+	var (
+		entexportPool      = make(chan bool, 20)
+		entexportWaitGroup = &sync.WaitGroup{}
+	)
+	log.Println("补充信息开始")
+	for _, v := range *data {
+		entexportWaitGroup.Add(1)
+		entexportPool <- true
+		go func(v map[string]interface{}) {
+			defer func() {
+				entexportWaitGroup.Done()
+				<-entexportPool
+			}()
+			//有中标企业 且 高级字段查询
+			if dataType == "2" {
+				//查询企业公示 法人 公司电话 公司邮箱地址
+				s_winner := strings.Split(util.ObjToString(v["s_winner"]), ",")[0]
+				if entData, ok := Mgo_Ent.Find("winner_enterprise", bson.M{"company_name": s_winner}, nil,
+					`{"company_name":1,"company_email":1,"legal_person":1,"company_phone":1}`, false, -1, -1); ok {
+					if entData != nil && *entData != nil && len(*entData) > 0 {
+						for _, ev := range *entData {
+							if v["s_winner"] == ev["company_name"] {
+								legal_person := ""
+								if ev["legal_person"] != nil {
+									legal_person = ev["legal_person"].(string)
+								}
+								company_phone := ""
+								if ev["company_phone"] != nil {
+									company_phone = ev["company_phone"].(string)
+								}
+								company_email := ""
+								if ev["company_email"] != nil && ev["company_email"] != "无" {
+									company_email = ev["company_email"].(string)
+								}
+								v["legal_person"] = legal_person
+								v["company_phone"] = company_phone
+								v["company_email"] = company_email
+							}
+						}
+					}
+				}
+			}
+			//====================字段补漏=========================
+			if v["toptype"] == "结果" && dataType == "2" && !(v["agency"] != nil && v["budget"] != nil && v["buyerperson"] != nil && v["buyertel"] != nil) {
+				r := elastic.Get("projectset", "projectset", fmt.Sprintf(`{"query":{"term":{"list.infoid":"%s"}},"_source": ["list"]}`, v["_id"]))
+				if len(*r) > 0 {
+					MsgList := (*r)[0]["list"]
+					if MsgList != nil {
+						list := util.ObjArrToMapArr(MsgList.([]interface{}))
+						for _, vv := range list {
+							if vv["subtype"] == "招标" {
+								if v["agency"] == nil && vv["agency"] != nil {
+									v["agency"] = vv["agency"]
+								}
+								if v["budget"] == nil && vv["budget"] != nil {
+									v["budget"] = vv["budget"]
+								}
+								if v["buyerperson"] == nil && vv["buyerperson"] != nil {
+									v["buyerperson"] = vv["buyerperson"]
+								}
+								if v["buyertel"] == nil && vv["buyertel"] != nil {
+									v["buyertel"] = vv["buyertel"]
+								}
+								break
+							}
+						}
+					}
+				}
+			}
+			if v["area"] == "A" {
+				v["area"] = "全国"
+			}
+			if v["publishtime"] != nil {
+				date := v["publishtime"]
+				v["publishtime"] = util.FormatDateWithObj(&date, util.Date_Short_Layout)
+			}
+			if v["bidopentime"] != nil {
+				date := v["bidopentime"]
+				v["bidopentime"] = util.FormatDateWithObj(&date, util.Date_Short_Layout)
+			}
+			if v["currency"] == "" || v["currency"] == nil {
+				v["currency"] = "人民币"
+			}
+			if v["subtype"] == nil && v["toptype"] != nil {
+				v["subtype"] = v["toptype"]
+			}
+			if v["detail"] != "" && v["detail"] != nil {
+				str := ClearHtml.ReplaceAllString(v["detail"].(string), "")
+				str = ClearOther.ReplaceAllString(str, "")
+				str = strings.Replace(str, " ", "", -1)
+				v["detail"] = str
+			}
+			if v["_id"] != nil {
+				v["url"] = webdomain + "/article/content/" + util.CommonEncodeArticle("content", v["_id"].(string)) + ".html"
+			}
+		}(v)
+	}
+	entexportWaitGroup.Wait()
+	log.Println("补充信息结束")
+	return data
+}
+
+func Post(url string, form map[string]string) (data map[string]interface{}, err error) {
+	str := ""
+	for k, v := range form {
+		str += "&" + k + "=" + v
+	}
+	//log.Println(str)
+	res, err1 := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(str))
+	log.Println(res)
+	if err1 != nil {
+		log.Println("post err:", err1.Error())
+		return nil, err1
+
+	} else if res.Body != nil {
+		defer res.Body.Close()
+		bs, _ := ioutil.ReadAll(res.Body)
+		err2 := json.Unmarshal(bs, &data)
+		if err2 != nil {
+			return nil, err2
+		}
+
+	}
+	return data, nil
+}
+
+//生成xlsx
+func GetXlsx(mMap []map[string]interface{}, entId, entUserId int) string {
+	xf, err := xlsx.OpenFile("./web/staticres/fields.xlsx")
+	if err != nil {
+		log.Println("fields file not foud", err.Error())
+	}
+	sh := xf.Sheets[1]
+	for _, v := range mMap {
+		row := sh.AddRow()
+		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(v["publishtime"])
+		} else {
+			row.AddCell()
+		}
+		row.AddCell().SetValue(v["href"])
+		row.AddCell().SetValue(v["url"])
+		row.AddCell().SetValue(v["projectname"])
+		row.AddCell().SetValue(v["projectcode"])
+		row.AddCell().SetValue(v["projectscope"])
+		if v["budget"] != nil {
+			row.AddCell().SetFloat(util.Float64All(v["budget"]))
+		} else {
+			row.AddCell()
+		}
+		if v["bidamount"] != nil {
+			row.AddCell().SetFloat(util.Float64All(v["bidamount"]))
+		} else {
+			row.AddCell()
+		}
+		if v["bidopentime"] != nil {
+			row.AddCell().SetValue(v["bidopentime"])
+		} 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["s_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]
+	xf.Sheets[0].Name = "数据导出"
+	//生文件
+	t := strconv.FormatInt(time.Now().Unix(), 10)
+	entIds := strconv.Itoa(entId)
+	entUserIds := strconv.Itoa(entUserId)
+	dir := "./web/staticres/xlsx/entsearchexport/" + entIds + "_" + entUserIds + "_" + t + "/"
+	if b, _ := PathExists(dir); !b {
+		err1 := os.MkdirAll(dir, os.ModePerm)
+		if err1 != nil {
+			log.Println("mkdir err", dir)
+		}
+	}
+	fname := entIds + "_" + entUserIds + "_" + "entdataexport.xlsx"
+	xlsxUrl := dir + fname
+	xlsxUrls := "/xlsx/entsearchexport/" + entIds + "_" + entUserIds + "_" + t + "/" + fname
+	err = xf.Save(xlsxUrl)
+	if err != nil {
+		log.Println("xls error", fname)
+		xlsxUrls = ""
+	}
+	return xlsxUrls
+}
+
+func PathExists(path string) (bool, error) {
+	_, err := os.Stat(path)
+	if err == nil {
+		return true, nil
+	}
+	if os.IsNotExist(err) {
+		return false, nil
+	}
+	return false, err
+}
+
+func SaveExportLog(mysqlSess *mysql.Mysql, entId, entUserId, count, newCount, remain_nums, export_nums int, xlsxUrl, types, filterStr string) {
+	query := map[string]interface{}{
+		"id":     entUserId,
+		"ent_id": entId,
+	}
+	set := map[string]interface{}{
+		"remain_nums": remain_nums - newCount,
+		"export_nums": export_nums + newCount,
+	}
+	ok := mysqlSess.Update("entniche_export_limit", map[string]interface{}{"ent_id": entId, "user_id": entUserId}, set)
+	if !ok {
+		log.Println("修改导出条数失败", query, remain_nums, newCount)
+	}
+	userData := mysqlSess.FindOne("entniche_user", query, "name,phone", "")
+	if userData != nil {
+		name := util.ObjToString((*userData)["name"])
+		phone := util.ObjToString((*userData)["phone"])
+		now := time.Now()
+		mysqlSess.Insert("entniche_export_log", map[string]interface{}{
+			"user_name":    name,
+			"export_time":  util.FormatDate(&now, util.Date_Full_Layout),
+			"data_source":  "2",
+			"export_num":   count,
+			"deduct_num":   newCount,
+			"download_url": xlsxUrl,
+			"ent_id":       entId,
+			"phone":        phone,
+			"user_id":      entUserId,
+			"filter":       filterStr,
+		})
+	}
+}
+
+func DeductNum(m *mysql.Mysql, qyfw mongodb.MongodbSim, entId, newCount int) {
+	query := map[string]interface{}{
+		"id": entId,
+	}
+	userData := m.FindOne("entniche_info", query, "name,phone", "")
+	if userData != nil {
+		name := util.ObjToString((*userData)["name"])
+		phone := util.ObjToString((*userData)["phone"])
+		qyfw.Update("user", map[string]interface{}{"phone": phone, "username": name}, map[string]interface{}{
+			"$inc": map[string]interface{}{
+				"plan.current": -newCount,
+			},
+		}, false, false)
+	}
+}
+func GetCurrentCount(m *mysql.Mysql, qyfw mongodb.MongodbSim, entId int) int {
+	count := 0
+	userData := m.FindOne("entniche_info", map[string]interface{}{"id": entId}, "name,phone", "")
+	if userData == nil {
+		return count
+	}
+	current, ok := qyfw.FindOne("user", map[string]interface{}{"phone": util.ObjToString((*userData)["phone"]), "username": util.ObjToString((*userData)["name"])})
+	if current == nil || !ok {
+		return count
+	}
+	plan, _ := (*current)["plan"].(map[string]interface{})
+	count = util.IntAll(plan["current"])
+	return count
+}

+ 1 - 0
src/jfw/modules/subscribepay/src/config/config.go

@@ -14,6 +14,7 @@ type config struct {
 	Mongoent        *mgoConf
 	Mongobidding    *mgoConf
 	Mongolog        *mgoConf
+	MongoQyfw       *mgoConf
 	Elasticsearch   string
 	ElasticPoolSize int
 	Redisaddrs      string

+ 138 - 11
src/jfw/modules/subscribepay/src/entity/dataExportPackStruct.go

@@ -1,6 +1,7 @@
 package entity
 
 import (
+	"api"
 	"config"
 	"encoding/json"
 	"fmt"
@@ -12,6 +13,7 @@ import (
 	"qfw/util/jy"
 	"qfw/util/redis"
 	"strings"
+	"sync"
 	"time"
 	"util"
 )
@@ -283,14 +285,26 @@ func (this *dataExportPackStruct) GetDataExportRecordList(userId, memberPid stri
 }
 
 // DoEntPackRepeatCheck 查询企业去重(旧企业导出逻辑)
-func (this *dataExportPackStruct) DoEntPackRepeatCheck(sess *httpsession.Session, userId, entId string, ids []string) (deduct int, err error) {
+func (this *dataExportPackStruct) DoEntPackRepeatCheck(sess *httpsession.Session, userId, entId, selectId string, selectCount int) (deduct int, err error) {
 	//校验当前用户是否所属此企业
 	if phone := getAndCacheUserPhone(sess, userId); phone != "" {
-		if util.Mysql.CountBySql(`SELECT count(*) FROM entniche_user where phone = ? AND ent_id = ?`) != 1 {
+		res := util.Mysql.FindOne("entniche_user", map[string]interface{}{"phone": phone, "ent_id": entId}, "id,ent_id", "")
+		if res == nil || len(*res) == 0 {
 			return -1, fmt.Errorf("企业校验异常")
 		}
+
+		entUserId := qutil.IntAll((*res)["id"])
+		entIdInt := qutil.IntAll((*res)["ent_id"])
+
+		exportLock.Lock()
+		exportLockMap[entIdInt] = sync.Mutex{}
+		entLock := exportLockMap[entIdInt]
+		exportLock.Unlock()
+		entLock.Lock()
 		//开始去重
-		newCount, err := doEntRepeatCheck(entId, ids)
+		_, newCount, _ := dataexport.GetEntDataExportCount(util.MQFW, util.Mgo_bidding, config.Config.Mongobidding.DbName, config.Config.Elasticsearch,
+			selectId, entIdInt, entUserId, true, config.JyApiConfig.ApiList.EntDedupUrl, config.ExConf.MsgMaxCount)
+		entLock.Unlock()
 		return newCount, err
 	}
 	return -1, fmt.Errorf("未查询所属企业")
@@ -320,9 +334,76 @@ func GetExcelFile(selectId string, checkCount int, isSenior bool, success func(e
 	}
 }
 
+var (
+	exportLock    = sync.Mutex{}
+	exportLockMap = map[int]sync.Mutex{}
+)
+
+// UseEntPack 企业数据包扣除
+func (this *dataExportPackStruct) UseEntPack(sess *httpsession.Session, userId, entId string, selectId string) error {
+	if phone := getAndCacheUserPhone(sess, userId); phone != "" {
+		res := util.Mysql.FindOne("entniche_user", map[string]interface{}{"phone": phone, "ent_id": entId}, "id,ent_id", "")
+		if res == nil || len(*res) == 0 {
+			return fmt.Errorf("企业校验异常")
+		}
+		entUserId := qutil.IntAll((*res)["id"])
+		entIdInt := qutil.IntAll((*res)["ent_id"])
+
+		exportLock.Lock()
+		exportLockMap[entIdInt] = sync.Mutex{}
+		entLock := exportLockMap[entIdInt]
+		exportLock.Unlock()
+		entLock.Lock()
+
+		query := map[string]interface{}{"ent_id": entId, "user_id": entUserId}
+		remain_nums, export_nums := 0, 0
+		limit := util.Mysql.FindOne("entniche_export_limit", query, "data_limit,remain_nums,export_nums", "")
+		if limit != nil {
+			remain_nums = qutil.IntAll((*limit)["remain_nums"])
+			export_nums = qutil.IntAll((*limit)["export_nums"])
+		}
+
+		count := 0
+		newCount := 0
+		data := &[]map[string]interface{}{}
+
+		current := dataexport.GetCurrentCount(util.Mysql, util.Mgo_Qyfw, entIdInt)
+		log.Println("企业总条数", current)
+		//开始去重
+		count, newCount, data = dataexport.GetEntDataExportCount(util.MQFW, util.Mgo_bidding, config.Config.Mongobidding.DbName, config.Config.Elasticsearch,
+			selectId, entIdInt, entUserId, true, config.JyApiConfig.ApiList.EntDedupUrl, config.ExConf.MsgMaxCount)
+
+		if newCount > current {
+			return fmt.Errorf("企业账户余额不足")
+		}
+		if newCount > remain_nums {
+			return fmt.Errorf("已超出进入导出上限")
+		}
+		go func() {
+			data = dataexport.FormatExportDatas(util.MQFWENT, data, config.Config.WebDomain, "2", entIdInt)
+			xlsxUrl := dataexport.GetXlsx(*data, entIdInt, entUserId)
+			if xlsxUrl != "" {
+				filter := dataexport.Filters{
+					FilterId: selectId,
+				}
+				filterStr, _ := json.Marshal(filter)
+				dataexport.SaveExportLog(util.Mysql, entIdInt, entUserId, count, newCount, remain_nums, export_nums, xlsxUrl, "2", string(filterStr))
+				dataexport.DeductNum(util.Mysql, util.Mgo_Qyfw, entIdInt, newCount)
+				cacheResult(nil, xlsxUrl, fmt.Sprintf("entPackResult_%s_%s", userId, selectId))
+
+				for _, v := range *data {
+					util.MQFW.Save("entdataexport", v)
+				}
+			}
+		}()
+		entLock.Unlock()
+	}
+	return nil
+}
+
 // UsePersonalPack 个人数据包扣除
-func (this *dataExportPackStruct) UsePersonalPack(userId, selectId string, isSenior bool, deduct, checkCount int) {
-	GetExcelFile(selectId, checkCount, isSenior, func(excelPath string, exportIds []string) {
+func (this *dataExportPackStruct) UsePersonalPack(userId, selectId string, isSenior bool, deduct, checkCount int) error {
+	go GetExcelFile(selectId, checkCount, isSenior, func(excelPath string, exportIds []string) {
 		_, err := usePerPack(userId, deduct, isSenior, &PersonalPackRemark{
 			QueryId:         selectId,
 			ExcelUrl:        excelPath,
@@ -330,16 +411,62 @@ func (this *dataExportPackStruct) UsePersonalPack(userId, selectId string, isSen
 			ExportTimeStamp: time.Now().Unix(),
 		}, exportIds)
 		if err == nil {
-			redis.Put("other", fmt.Sprintf("UsePersonalPackResult_%s_%s", userId, selectId), excelPath, 60*5)
-		} else {
-			log.Printf("UsePerPack %s 查询 %s 扣除异常%v\n", userId, selectId, err)
-			redis.Put("other", fmt.Sprintf("UsePersonalPackResult_%s_%s", userId, selectId), "", 60*5)
+			go util.Mysql.Insert("datapacket_record", map[string]interface{}{
+				"infoids":    strings.Join(exportIds, ","),
+				"type":       2,
+				"master_id":  userId,
+				"useid":      userId,
+				"query_id":   selectId,
+				"date":       time.Now().Unix(),
+				"path":       excelPath,
+				"deduct_num": deduct,
+				"export_num": checkCount,
+			})
 		}
+		cacheResult(err, excelPath, fmt.Sprintf("personalPackResult_%s_%s_%v", userId, selectId, isSenior))
+	}, func(err error) {
+		cacheResult(err, "", fmt.Sprintf("personalPackResult_%s_%s_%v", userId, selectId, isSenior))
+		errMeg := fmt.Sprintf("数据包导出异常\n用户id:%s\n数据导出筛选id:%s\n异常信息:%v:", userId, selectId, err)
+		util.SendErrNoteToMail(errMeg)
+	})
+	return nil
+}
+
+func cacheResult(err error, filePath, key string) {
+	var returnData api.Result
+	if err != nil {
+		returnData = api.NewResult(nil, err)
+	} else {
+		returnData = api.NewResult(filePath, err)
+	}
+	bytes, _ := json.Marshal(returnData)
+	redis.Put("other", key, bytes, 60*5)
+}
+
+// UseDailyPack 每日限量包
+func (this *dataExportPackStruct) UseDailyPack(userId, selectId string, selectCount int) error {
+	vipStatus := jy.GetBigVipUserBaseMsg(userId, util.Mysql, util.MQFW)
+	if vipStatus.Status <= 0 || vipStatus.DailyNum == 0 {
+		return fmt.Errorf("非法请求")
+	}
+	go GetExcelFile(selectId, selectCount, true, func(excelPath string, exportIds []string) {
+		packetMsg := dataexport.GetDataPacketMsg(vipStatus.GetUseId(), vipStatus.DailyNum)
+		//余额扣除
+		err := func() error {
+			if err := packetMsg.AddNum(selectCount); err != nil {
+				return err
+			}
+			//保存导出记录
+			packetMsg.SaveRecord(util.Mysql, userId, selectId, dataexport.BigMemberDaily, exportIds, excelPath)
+			return nil
+		}()
+		cacheResult(err, excelPath, fmt.Sprintf("dailyPackResult_%s_%s", userId, selectId))
 	}, func(err error) {
+		cacheResult(err, "", fmt.Sprintf("dailyPackResult_%s_%s", userId, selectId))
 		errMeg := fmt.Sprintf("数据包导出异常\n用户id:%s\n数据导出筛选id:%s\n异常信息:%v:", userId, selectId, err)
 		util.SendErrNoteToMail(errMeg)
-		redis.Put("other", fmt.Sprintf("UsePersonalPackResult_%s_%s", userId, selectId), "", 60*5)
 	})
+	return nil
 }
 
 // PerRechargeList 个人数据包充值记录
@@ -352,7 +479,7 @@ func (this *dataExportPackStruct) PerRechargeList(userId string, pageNum, pageSi
 
 //查询企业账户余额
 func getCurrEntCount(entName, entPhone string) int {
-	current, ok := util.MQFWENT.FindOne("user", map[string]interface{}{"phone": entPhone, "username": entName})
+	current, ok := util.Mgo_Qyfw.FindOne("user", map[string]interface{}{"phone": entPhone, "username": entName})
 	if current == nil || !ok {
 		return 0
 	}

+ 84 - 80
src/jfw/modules/subscribepay/src/service/dataExportPay.go

@@ -1,17 +1,13 @@
 package service
 
 import (
-	"api"
 	"bytes"
 	"config"
-	"fmt"
-	"qfw/util/dataexport"
-	"qfw/util/jy"
-	"qfw/util/redis"
-
 	"encoding/json"
+	"fmt"
 	"log"
 	"pay"
+	"qfw/util/dataexport"
 	"regexp"
 	"time"
 	"util"
@@ -23,10 +19,10 @@ import (
 
 type DataExportPay struct {
 	*xweb.Action
-	getPrice                  xweb.Mapper `xweb:"/dataexport/getPrice"`        //获取数据导出价格
-	createOrder               xweb.Mapper `xweb:"/dataExport/createOrder"`     //移动端创建订单
-	vipDailyDataExport        xweb.Mapper `xweb:"/dataExport/vipDaily"`        //大会员数据包数据导出
-	vipDailyDataExportSuccess xweb.Mapper `xweb:"/dataExport/vipDailySuccess"` //大会员数据包导出生成是否成功
+	getPrice    xweb.Mapper `xweb:"/dataexport/getPrice"`    //获取数据导出价格
+	createOrder xweb.Mapper `xweb:"/dataExport/createOrder"` //移动端创建订单
+	//vipDailyDataExport        xweb.Mapper `xweb:"/dataExport/vipDaily"`        //大会员数据包数据导出
+	//vipDailyDataExportSuccess xweb.Mapper `xweb:"/dataExport/vipDailySuccess"` //大会员数据包导出生成是否成功
 }
 
 var reg *regexp.Regexp //匹配特殊字符
@@ -238,73 +234,81 @@ func (p *DataExportPay) GetPrice() {
 	})
 }
 
-func (this *DataExportPay) VipDailyDataExport() {
-	userId, _ := this.GetSession("userId").(string)
-	rData, errMsg := func() (interface{}, error) {
-		selectId := qutil.SE.Decode4Hex(this.GetString("selectId"))
-		if selectId == "" {
-			return nil, fmt.Errorf("参数异常")
-		}
-		vipStatus := jy.GetBigVipUserBaseMsg(userId, util.Mysql, util.MQFW)
-		if vipStatus.Status <= 0 || vipStatus.DailyNum == 0 {
-			return nil, fmt.Errorf("非法请求")
-		}
-		selectCount := dataexport.GetDataExportSearchCountByScdId(util.MQFW, config.Config.Elasticsearch, selectId)
-		if selectCount > config.ExConf.MsgMaxCount {
-			selectCount = config.ExConf.MsgMaxCount
-		} else if selectCount == 0 {
-			return nil, fmt.Errorf("未查询到数据")
-		}
-
-		packetMsg := dataexport.GetDataPacketMsg(vipStatus.GetUseId(), vipStatus.DailyNum)
-		//余额扣除
-		if err := packetMsg.AddNum(selectCount); err != nil {
-			return nil, err
-		}
-		//生成excel文件&存储导出记录
-		go func() {
-			list, err := dataexport.GetDataExportSearchResultByScdId(util.MQFW, util.Mgo_bidding, config.Config.Mongobidding.DbName, config.Config.Elasticsearch, selectId, "2", selectCount)
-			url := util.GetExcelFilePath(pay.GetOrderCode(selectId))
-			if err == nil && list != nil && len(*list) > 0 {
-				list = dataexport.FormatExportData(util.MQFWENT, list, config.Config.WebDomain, "2")
-				err = util.CreateDataExportExcelFile(list, true, config.ExConf.ExcelPayedPath+url) //创建excel文件
-			}
-			if err != nil {
-				errMeg := fmt.Sprintf("数据导出每日限量包异常\n用户id:%s\n数据导出筛选id:%s\n异常信息:%v:", userId, selectId, err)
-				util.SendErrNoteToMail(errMeg)
-			} else {
-				redis.Put("other", fmt.Sprintf(VipDailySuccess, selectId), url, 24*60*60) //导出成功
-				//保存导出记录
-				if err := packetMsg.SaveRecord(util.Mysql, userId, selectId, dataexport.BigMemberDaily, list, url); err != nil {
-					log.Println(err)
-				}
-			}
-		}()
-		return map[string]interface{}{"success": true}, nil
-	}()
-	if errMsg != nil {
-		log.Printf("%s DataExport VipDailyDataExport 大会员数据包导出异常:%s\n", userId, errMsg.Error())
-	}
-	this.ServeJson(api.NewResult(rData, errMsg))
-}
-
-const VipDailySuccess = "vipDailySuccess_%s"
-
-func (this *DataExportPay) VipDailyDataExportSuccess() {
-	userId, _ := this.GetSession("userId").(string)
-	rData, errMsg := func() (interface{}, error) {
-		selectId := qutil.SE.Decode4Hex(this.GetString("selectId"))
-		if selectId == "" {
-			return nil, fmt.Errorf("参数异常")
-		}
-		if url := redis.GetStr("other", fmt.Sprintf(VipDailySuccess, selectId)); url != "" {
-			return map[string]interface{}{"excelHref": url}, nil
-		}
-		return map[string]interface{}{"status": -1}, nil
-
-	}()
-	if errMsg != nil {
-		log.Printf("%s DataExport VipDailySuccess 大会员数据包导出结果查询异常:%s\n", userId, errMsg.Error())
-	}
-	this.ServeJson(api.NewResult(rData, errMsg))
-}
+//
+//func (this *DataExportPay) VipDailyDataExport() {
+//	userId, _ := this.GetSession("userId").(string)
+//	rData, errMsg := func() (interface{}, error) {
+//		selectId := qutil.SE.Decode4Hex(this.GetString("selectId"))
+//		if selectId == "" {
+//			return nil, fmt.Errorf("参数异常")
+//		}
+//		vipStatus := jy.GetBigVipUserBaseMsg(userId, util.Mysql, util.MQFW)
+//		if vipStatus.Status <= 0 || vipStatus.DailyNum == 0 {
+//			return nil, fmt.Errorf("非法请求")
+//		}
+//		selectCount := dataexport.GetDataExportSearchCountByScdId(util.MQFW, config.Config.Elasticsearch, selectId)
+//		if selectCount > config.ExConf.MsgMaxCount {
+//			selectCount = config.ExConf.MsgMaxCount
+//		} else if selectCount == 0 {
+//			return nil, fmt.Errorf("未查询到数据")
+//		}
+//
+//		packetMsg := dataexport.GetDataPacketMsg(vipStatus.GetUseId(), vipStatus.DailyNum)
+//		//余额扣除
+//		if err := packetMsg.AddNum(selectCount); err != nil {
+//			return nil, err
+//		}
+//		//生成excel文件&存储导出记录
+//		go func() {
+//			list, err := dataexport.GetDataExportSearchResultByScdId(util.MQFW, util.Mgo_bidding, config.Config.Mongobidding.DbName, config.Config.Elasticsearch, selectId, "2", selectCount)
+//			url := util.GetExcelFilePath(pay.GetOrderCode(selectId))
+//			if err == nil && list != nil && len(*list) > 0 {
+//				list = dataexport.FormatExportData(util.MQFWENT, list, config.Config.WebDomain, "2")
+//				err = util.CreateDataExportExcelFile(list, true, config.ExConf.ExcelPayedPath+url) //创建excel文件
+//			}
+//			if err != nil {
+//				errMeg := fmt.Sprintf("数据导出每日限量包异常\n用户id:%s\n数据导出筛选id:%s\n异常信息:%v:", userId, selectId, err)
+//				util.SendErrNoteToMail(errMeg)
+//			} else {
+//				redis.Put("other", fmt.Sprintf(VipDailySuccess, selectId), url, 24*60*60) //导出成功
+//				listId := []string{}
+//				//rMap := dpm.GetRepeatMap(db) //去重
+//				for _, v := range *list {
+//					if id, _ := v["_id"].(string); id != "" {
+//						listId = append(listId, id)
+//					}
+//				}
+//				//保存导出记录
+//				if err := packetMsg.SaveRecord(util.Mysql, userId, selectId, dataexport.BigMemberDaily, listId, url); err != nil {
+//					log.Println(err)
+//				}
+//			}
+//		}()
+//		return map[string]interface{}{"success": true}, nil
+//	}()
+//	if errMsg != nil {
+//		log.Printf("%s DataExport VipDailyDataExport 大会员数据包导出异常:%s\n", userId, errMsg.Error())
+//	}
+//	this.ServeJson(api.NewResult(rData, errMsg))
+//}
+//
+//const VipDailySuccess = "vipDailySuccess_%s"
+//
+//func (this *DataExportPay) VipDailyDataExportSuccess() {
+//	userId, _ := this.GetSession("userId").(string)
+//	rData, errMsg := func() (interface{}, error) {
+//		selectId := qutil.SE.Decode4Hex(this.GetString("selectId"))
+//		if selectId == "" {
+//			return nil, fmt.Errorf("参数异常")
+//		}
+//		if url := redis.GetStr("other", fmt.Sprintf(VipDailySuccess, selectId)); url != "" {
+//			return map[string]interface{}{"excelHref": url}, nil
+//		}
+//		return map[string]interface{}{"status": -1}, nil
+//
+//	}()
+//	if errMsg != nil {
+//		log.Printf("%s DataExport VipDailySuccess 大会员数据包导出结果查询异常:%s\n", userId, errMsg.Error())
+//	}
+//	this.ServeJson(api.NewResult(rData, errMsg))
+//}

+ 45 - 24
src/jfw/modules/subscribepay/src/service/dataexportPack.go

@@ -7,11 +7,11 @@ import (
 	"entity"
 	"fmt"
 	"github.com/go-xweb/xweb"
-	"jfw/modules/common/src/qfw/util/jy"
 	"log"
 	"pay"
 	qutil "qfw/util"
 	"qfw/util/dataexport"
+	"qfw/util/jy"
 	"qfw/util/redis"
 	"time"
 	"util"
@@ -137,34 +137,34 @@ func (this *DataExportPack) Statistics() {
 		if selectCount > config.ExConf.MsgMaxCount {
 			selectCount = config.ExConf.MsgMaxCount
 		}
-		//获取导出数据信息id列表
-		ids, err := dataexport.GetDataExportIdArrByScdId(util.MQFW, config.Config.Elasticsearch, filterId, selectCount)
-		if err != nil {
-			log.Printf("%s Statistics 查询导出id失败 %v\n", userId, err)
-			return nil, fmt.Errorf("查询去重异常")
-		}
+
 		switch this.GetString("doType") {
 		case "entPack": //企业数据包去重查询
 			entId := this.GetString("entId")
-			deduct, err := entity.JyDataExportPack.DoEntPackRepeatCheck(this.Session(), userId, entId, ids)
+			deduct, err := entity.JyDataExportPack.DoEntPackRepeatCheck(this.Session(), userId, entId, filterId, selectCount)
 			if err != nil {
 				log.Printf("%s Statistics-DoEntPackRepeatCheck 查询导出id失败 %v\n", userId, err)
 				return nil, fmt.Errorf("去重查询失败")
 			}
-			redis.Put("other", fmt.Sprintf("entPack_deduct_%s_%s_%d", entId, filterId, selectCount), deduct, 60*60*1)
 			return map[string]interface{}{
 				"repeat": selectCount - deduct,
 				"deduct": deduct,
 			}, nil
 		case "personalPack": //个人数据包去重查询
 			packType := this.GetString("packType") //senior 高级字段;normal 普通字段包
+			//获取导出数据信息id列表
+			ids, err := dataexport.GetDataExportIdArrByScdId(util.MQFW, config.Config.Elasticsearch, filterId, selectCount)
+			if err != nil {
+				log.Printf("%s Statistics 查询导出id失败 %v\n", userId, err)
+				return nil, fmt.Errorf("查询去重异常")
+			}
 			repeat, deduct, err := entity.JyDataExportPack.DoPerPackRepeatCheck(userId, packType == "senior", ids)
 			if err != nil {
 				log.Printf("%s Statistics-DoPerPackRepeatCheck 查询导出id失败 %v\n", userId, err)
 				return nil, fmt.Errorf("去重查询失败")
 			}
 			//存入可口数量
-			redis.Put("other", fmt.Sprintf("personalPack_deduct_%s_%s_%d", userId, filterId, selectCount), deduct, 60*60*1)
+			redis.Put("other", fmt.Sprintf("personalPack_deduct_%s_%s_%d_%s", userId, filterId, selectCount, packType), deduct, 60*60*1)
 			return map[string]interface{}{
 				"repeat": repeat,
 				"deduct": deduct,
@@ -203,19 +203,27 @@ func (this *DataExportPack) PackPay() {
 		case "entPack": //企业数据包支付
 			entId := this.GetString("entId")
 			//扣除数量查询
-			deduct := redis.GetInt("other", fmt.Sprintf("entPack_deduct_%s_%s_%d", entId, filterId, selectCount))
-
-			log.Println(deduct)
+			err := entity.JyDataExportPack.UseEntPack(this.Session(), userId, entId, filterId)
+			if err != nil {
+				return nil, err
+			}
 			return "success", nil
 		case "personalPack": //个人数据包支付
 			packType := this.GetString("packType") //senior 高级字段;normal 普通字段包
 			//扣除数量查询
-			deduct := redis.GetInt("other", fmt.Sprintf("personalPack_deduct_%s_%s_%d", userId, filterId, selectCount))
-			//生成excel扣除
-			entity.JyDataExportPack.UsePersonalPack(userId, filterId, packType == "senior", deduct, selectCount)
+			deductInterface := redis.Get("other", fmt.Sprintf("personalPack_deduct_%s_%s_%d_%s", userId, filterId, selectCount, packType))
+			if deductInterface == nil {
+				return nil, fmt.Errorf("导出超时")
+			}
+			deduct := qutil.IntAll(deductInterface)
+			//生成excel扣除,
+			_ = entity.JyDataExportPack.UsePersonalPack(userId, filterId, packType == "senior", deduct, selectCount)
 			return "success", nil
 		case "dailyPack": //每日限量包扣除
-
+			err := entity.JyDataExportPack.UseDailyPack(userId, filterId, selectCount)
+			if err != nil {
+				return nil, err
+			}
 			return "success", nil
 		}
 		return nil, fmt.Errorf("未知请求")
@@ -231,14 +239,27 @@ func (this *DataExportPack) PackPay() {
 // return	filePath 下载文件地址
 func (this *DataExportPack) FileCheck() {
 	userId, _ := this.GetSession("userId").(string)
-	rData, errMsg := func() (interface{}, error) {
-		//查询是否完成
-		return nil, nil
-	}()
-	if errMsg != nil {
-		log.Printf("%s DataExportPack FileCheck 异常:%s\n", userId, errMsg.Error())
+	//查询是否完成
+	filterId := this.GetString("filterId")
+	doType := this.GetString("doType")
+	var redisKey string
+	if doType == "personalPack" {
+		redisKey = fmt.Sprintf("%sResult_%s_%s", doType, userId, filterId)
+	} else {
+		packType := this.GetString("packType") //senior 高级字段;normal 普通字段包
+		redisKey = fmt.Sprintf("%sResult_%s_%s_%v", doType, userId, filterId, qutil.If(packType == "senior", true, false))
+	}
+
+	if data := redis.Get("other", redisKey); data != nil {
+		var returnData Result
+		bb, _ := data.([]byte)
+		json.Unmarshal(bb, &returnData)
+		this.ServeJson(returnData)
+	} else {
+		this.ServeJson(Result{
+			Error_code: 1,
+		})
 	}
-	this.ServeJson(NewResult(rData, errMsg))
 }
 
 // RechargeList 数据流量包充值记录