123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611 |
- package dataexport
- import (
- "encoding/json"
- "fmt"
- "io/ioutil"
- "log"
- "net/http"
- "os"
- "sort"
- "strconv"
- "strings"
- "sync"
- "time"
- util "app.yhyue.com/moapp/jybase/common"
- . "app.yhyue.com/moapp/jybase/date"
- . "app.yhyue.com/moapp/jybase/encrypt"
- elastic "app.yhyue.com/moapp/jybase/es"
- "app.yhyue.com/moapp/jybase/mongodb"
- mg "app.yhyue.com/moapp/jybase/mongodb"
- "app.yhyue.com/moapp/jybase/mysql"
- . "app.yhyue.com/moapp/jybase/rpc"
- "github.com/tealeg/xlsx"
- "go.mongodb.org/mongo-driver/bson"
- )
- // 作者:一组开发
- type Filters struct {
- FilterId string
- }
- func GetEntDataExportCount(sim, bid mg.MongodbSim, bidMgoDBName, elasticAddress, _id string, entId, entUserId int, isFirst bool, dataPkgRemoveRepeatAddr string, maxCount int) (count, newCount int, data *[]map[string]interface{}) {
- log.Println(fmt.Sprintf("GetEntDataExportCount entId:%v,entUserId:%v", entId, entUserId))
- start := time.Now()
- defer func() {
- util.Catch()
- log.Printf("GetEntDataExportCount entId:%v,entUserId:%v 总耗时:%v\n", entId, entUserId, time.Since(start))
- }()
- count = GetDataExportSearchCountByScdId(sim, bid, bidMgoDBName, elasticAddress, _id)
- log.Printf("耗时: GetEntDataExportCount-GetDataExportSearchCountByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
- if count > maxCount || count == -1 {
- count = maxCount
- }
- log.Println("GetEntDataExportCount-count", entId, entUserId, count)
- dataType := "2"
- //数据导出数据查询
- res, err := GetDataExportSearchResultByScdId(sim, bid, bidMgoDBName, elasticAddress, _id, dataType, count)
- log.Printf("耗时: GetEntDataExportCount-GetDataExportSearchResultByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
- if err != nil {
- log.Println("企业数据导出错误 ", entId, entUserId, err)
- return 0, 0, nil
- }
- infoIdList := []string{}
- postStart := time.Now()
- for _, v := range *res {
- if util.IntAll(v["signendtime"]) != 0 {
- date := v["signendtime"]
- v["signendtime"] = FormatDateWithObj(&date, Date_Short_Layout)
- }
- if util.IntAll(v["bidendtime"]) != 0 {
- date := v["bidendtime"]
- v["bidendtime"] = FormatDateWithObj(&date, Date_Short_Layout)
- }
- id := util.ObjToString(v["_id"])
- infoIdList = append(infoIdList, id)
- if !isFirst {
- delete(v, "_id")
- v["entid"] = entId
- v["userid"] = entUserId
- v["infoid"] = id
- v["createtime"] = time.Now().Unix()
- }
- }
- m, _ := NoRepeatData(dataPkgRemoveRepeatAddr, map[string]interface{}{"entid": entId, "ids": infoIdList})
- newCount = len(m)
- log.Printf("企业数据导出--数据遍历完成:entId:%v,entUserId:%v,newCount:%v,Since:%v \n", entId, entUserId, newCount, time.Since(postStart))
- data = res
- return
- }
- /*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{}) {
- log.Println(fmt.Sprintf("GetEntDataExportCount entId:%v,entUserId:%v", entId, entUserId))
- start := time.Now()
- defer func() {
- util.Catch()
- log.Printf("GetEntDataExportCount entId:%v,entUserId:%v 总耗时:%v\n", entId, entUserId, time.Since(start))
- }()
- var (
- searchsWaitGroup = &sync.WaitGroup{}
- )
- count = GetDataExportSearchCountByScdId(sim, bid, bidMgoDBName, elasticAddress, _id)
- log.Printf("耗时: GetEntDataExportCount-GetDataExportSearchCountByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
- if count > maxCount || count == -1 {
- count = maxCount
- }
- log.Println("GetEntDataExportCount-count", entId, entUserId, count)
- dataType := "2"
- //数据导出数据查询
- res, err := GetDataExportSearchResultByScdId(sim, bid, bidMgoDBName, elasticAddress, _id, dataType, count)
- log.Printf("耗时: GetEntDataExportCount-GetDataExportSearchResultByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
- if err != nil {
- log.Println("企业数据导出错误 ", entId, entUserId, err)
- return 0, 0, nil
- }
- // 20210716 由原来的redis判重改为调用判重中台接口进行判重
- m := map[string]bool{}
- infoIdList := []string{}
- insertFlag := "false"
- if !isFirst {
- insertFlag = "true"
- }
- postStart := time.Now()
- for _, v := range *res {
- if util.IntAll(v["signendtime"]) != 0 {
- date := v["signendtime"]
- v["signendtime"] = FormatDateWithObj(&date, Date_Short_Layout)
- }
- if util.IntAll(v["bidendtime"]) != 0 {
- date := v["bidendtime"]
- v["bidendtime"] = FormatDateWithObj(&date, Date_Short_Layout)
- }
- 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("响应结果:", entId, entUserId, rs)
- if err5 != nil || util.IntAll(rs["code"]) != 0 {
- log.Println("企业订阅数据导出接口判重失败", entId, entUserId, 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("企业订阅数据导出接口判重失败", entId, entUserId, 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"])
- log.Println(newCount, "加之后")
- }
- }
- searchsWaitGroup.Wait()
- log.Printf("企业数据导出--数据遍历完成:entId:%v,entUserId:%v,newCount:%v,Since:%v \n", entId, entUserId, newCount, time.Since(postStart))
- data = res
- return
- }*/
- func GetEntDataExportCountIdArr(sim, bid mg.MongodbSim, bidMgoDBName, elasticAddress, _id string, entId, entUserId int, dataPkgRemoveRepeatAddr string, maxCount int) (count, newCount int, err error) {
- log.Println(fmt.Sprintf("GetEntDataExportCountIdArr entId:%v,entUserId:%v", entId, entUserId))
- start := time.Now()
- defer func() {
- util.Catch()
- log.Printf("GetEntDataExportCountIdArr entId:%v,entUserId:%v 总耗时:%v\n", entId, entUserId, time.Since(start))
- }()
- count = GetDataExportSearchCountByScdId(sim, bid, bidMgoDBName, elasticAddress, _id)
- log.Printf("耗时: GetEntDataExportCountIdArr-GetDataExportSearchCountByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
- if count > maxCount || count == -1 {
- count = maxCount
- }
- log.Println("GetEntDataExportCountIdArr-count", entId, entUserId, count)
- //数据导出数据查询
- ids, err := GetDataExportIdArrByScdId(sim, elasticAddress, _id, count)
- log.Printf("耗时: GetEntDataExportCountIdArr-GetDataExportSearchResultByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
- if err != nil {
- log.Println("GetEntDataExportCountIdArr 企业数据导出错误 ", entId, entUserId, err)
- return 0, 0, fmt.Errorf("企业数据导出错误")
- }
- if m, e := NoRepeatData(dataPkgRemoveRepeatAddr, map[string]interface{}{"entid": entId, "ids": ids}); e == nil {
- newCount = len(m)
- } else {
- newCount = len(ids)
- }
- log.Printf("GetEntDataExportCountIdArr 企业数据导出--数据遍历完成:entId:%v,entUserId:%v,newCount:%v,Since:%v \n", entId, entUserId, newCount)
- return
- }
- /*func GetEntDataExportCountIdArr(sim, bid mg.MongodbSim, bidMgoDBName, elasticAddress, _id string, entId, entUserId int, url string, maxCount int) (count, newCount int, err error) {
- log.Println(fmt.Sprintf("GetEntDataExportCountIdArr entId:%v,entUserId:%v", entId, entUserId))
- start := time.Now()
- defer func() {
- util.Catch()
- log.Printf("GetEntDataExportCountIdArr entId:%v,entUserId:%v 总耗时:%v\n", entId, entUserId, time.Since(start))
- }()
- var (
- searchsWaitGroup = &sync.WaitGroup{}
- )
- count = GetDataExportSearchCountByScdId(sim, bid, bidMgoDBName, elasticAddress, _id)
- log.Printf("耗时: GetEntDataExportCountIdArr-GetDataExportSearchCountByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
- if count > maxCount || count == -1 {
- count = maxCount
- }
- log.Println("GetEntDataExportCountIdArr-count", entId, entUserId, count)
- //数据导出数据查询
- ids, err := GetDataExportIdArrByScdId(sim, elasticAddress, _id, count)
- log.Printf("耗时: GetEntDataExportCountIdArr-GetDataExportSearchResultByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
- if err != nil {
- log.Println("GetEntDataExportCountIdArr 企业数据导出错误 ", entId, entUserId, err)
- return 0, 0, fmt.Errorf("企业数据导出错误")
- }
- // 20210716 由原来的redis判重改为调用判重中台接口进行判重
- m := map[string]bool{}
- infoIdList := []string{}
- insertFlag := "false"
- postStart := time.Now()
- for i := 0; i < len(ids); i++ {
- id := ids[i]
- 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("GetEntDataExportCountIdArr 响应结果:", entId, entUserId, rs)
- if err5 != nil || util.IntAll(rs["code"]) != 0 {
- log.Println("GetEntDataExportCountIdArr 企业订阅数据导出接口判重失败", entId, entUserId, err5, "rs:", rs, " rs[code]:", rs["code"], " ", util.IntAll(rs["code"]), "code是否为0", util.IntAll(rs["code"]) != 0)
- } else {
- log.Println("GetEntDataExportCountIdArr 企业订阅数据导出")
- // 置空
- infoIdList = []string{}
- // 本次数据累计
- returnData := rs["data"].(map[string]interface{})
- log.Println(newCount, "GetEntDataExportCountIdArr 加之前")
- newCount += util.IntAll(returnData["newCount"])
- //newCount += int(returnData["newCount"].(float64))
- log.Println(newCount, "GetEntDataExportCountIdArr 加之后")
- }
- }
- }
- 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("GetEntDataExportCountIdArr 企业订阅数据导出接口判重失败", entId, entUserId, err5, "rs:", rs, " rs[code]:", rs["code"], " ", util.IntAll(rs["code"]), "code是否为0", util.IntAll(rs["code"]) != 0)
- } else {
- log.Println("GetEntDataExportCountIdArr 企业订阅数据导出")
- // 置空
- infoIdList = []string{}
- // 本次数据累计
- returnData := rs["data"].(map[string]interface{})
- log.Println(newCount, "GetEntDataExportCountIdArr 加之前")
- newCount += util.IntAll(returnData["newCount"])
- log.Println(newCount, "GetEntDataExportCountIdArr 加之后")
- }
- }
- searchsWaitGroup.Wait()
- log.Printf("GetEntDataExportCountIdArr 企业数据导出--数据遍历完成:entId:%v,entUserId:%v,newCount:%v,Since:%v \n", entId, entUserId, newCount, time.Since(postStart))
- 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{}
- )
- sort.Slice(*data, func(i, j int) bool {
- time1 := util.Int64All((*data)[i]["publishtime"])
- time2 := util.Int64All((*data)[j]["publishtime"])
- return time1 > time2
- })
- 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"] = FormatDateWithObj(&date, Date_Short_Layout)
- }
- if v["bidopentime"] != nil {
- date := v["bidopentime"]
- v["bidopentime"] = FormatDateWithObj(&date, Date_Short_Layout)
- }
- if util.IntAll(v["signendtime"]) != 0 {
- date := v["signendtime"]
- v["signendtime"] = FormatDateWithObj(&date, Date_Short_Layout)
- }
- if util.IntAll(v["bidendtime"]) != 0 {
- date := v["bidendtime"]
- v["bidendtime"] = FormatDateWithObj(&date, 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["infoid"] != nil {
- v["url"] = webdomain + "/article/content/" + CommonEncodeArticle("content", v["infoid"].(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, filePath string) 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["keyword"])
- row.AddCell().SetValue(v["area"])
- row.AddCell().SetValue(v["city"])
- row.AddCell().SetValue(v["district"])
- 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["signendtime"] != nil {
- row.AddCell().SetValue(v["signendtime"])
- } else {
- row.AddCell()
- }
- if v["bidopentime"] != nil {
- row.AddCell().SetValue(v["bidopentime"])
- } else {
- row.AddCell()
- }
- if v["bidendtime"] != nil {
- row.AddCell().SetValue(v["bidendtime"])
- } else {
- row.AddCell()
- }
- row.AddCell().SetValue(v["buyer"])
- row.AddCell().SetValue(v["buyerperson"])
- row.AddCell().SetValue(v["buyertel"])
- row.AddCell().SetValue(v["buyeraddr"])
- 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 := filePath + "/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"
- download_url := "/entsearchexport/" + entIds + "_" + entUserIds + "_" + t + "/" + fname
- err = xf.Save(dir + fname)
- if err != nil {
- log.Println("xls error", fname)
- return ""
- }
- return download_url
- }
- 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, exportPhone, exportEmail 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": FormatDate(&now, 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,
- "export_phone": exportPhone,
- "export_mail": exportEmail,
- })
- }
- }
- 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
- }
|