entdataexport.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. package dataexport
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "os"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. "time"
  14. util "app.yhyue.com/moapp/jybase/common"
  15. . "app.yhyue.com/moapp/jybase/date"
  16. . "app.yhyue.com/moapp/jybase/encrypt"
  17. elastic "app.yhyue.com/moapp/jybase/es"
  18. "app.yhyue.com/moapp/jybase/mongodb"
  19. mg "app.yhyue.com/moapp/jybase/mongodb"
  20. "app.yhyue.com/moapp/jybase/mysql"
  21. . "app.yhyue.com/moapp/jybase/rpc"
  22. "github.com/tealeg/xlsx"
  23. "go.mongodb.org/mongo-driver/bson"
  24. )
  25. // 作者:一组开发
  26. type Filters struct {
  27. FilterId string
  28. }
  29. 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{}) {
  30. log.Println(fmt.Sprintf("GetEntDataExportCount entId:%v,entUserId:%v", entId, entUserId))
  31. start := time.Now()
  32. defer func() {
  33. util.Catch()
  34. log.Printf("GetEntDataExportCount entId:%v,entUserId:%v 总耗时:%v\n", entId, entUserId, time.Since(start))
  35. }()
  36. count = GetDataExportSearchCountByScdId(sim, bid, bidMgoDBName, elasticAddress, _id)
  37. log.Printf("耗时: GetEntDataExportCount-GetDataExportSearchCountByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
  38. if count > maxCount || count == -1 {
  39. count = maxCount
  40. }
  41. log.Println("GetEntDataExportCount-count", entId, entUserId, count)
  42. dataType := "2"
  43. //数据导出数据查询
  44. res, err := GetDataExportSearchResultByScdId(sim, bid, bidMgoDBName, elasticAddress, _id, dataType, count)
  45. log.Printf("耗时: GetEntDataExportCount-GetDataExportSearchResultByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
  46. if err != nil {
  47. log.Println("企业数据导出错误 ", entId, entUserId, err)
  48. return 0, 0, nil
  49. }
  50. infoIdList := []string{}
  51. postStart := time.Now()
  52. for _, v := range *res {
  53. if util.IntAll(v["signendtime"]) != 0 {
  54. date := v["signendtime"]
  55. v["signendtime"] = FormatDateWithObj(&date, Date_Short_Layout)
  56. }
  57. if util.IntAll(v["bidendtime"]) != 0 {
  58. date := v["bidendtime"]
  59. v["bidendtime"] = FormatDateWithObj(&date, Date_Short_Layout)
  60. }
  61. id := util.ObjToString(v["_id"])
  62. infoIdList = append(infoIdList, id)
  63. if !isFirst {
  64. delete(v, "_id")
  65. v["entid"] = entId
  66. v["userid"] = entUserId
  67. v["infoid"] = id
  68. v["createtime"] = time.Now().Unix()
  69. }
  70. }
  71. m, _ := NoRepeatData(dataPkgRemoveRepeatAddr, map[string]interface{}{"entid": entId, "ids": infoIdList})
  72. newCount = len(m)
  73. log.Printf("企业数据导出--数据遍历完成:entId:%v,entUserId:%v,newCount:%v,Since:%v \n", entId, entUserId, newCount, time.Since(postStart))
  74. data = res
  75. return
  76. }
  77. /*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{}) {
  78. log.Println(fmt.Sprintf("GetEntDataExportCount entId:%v,entUserId:%v", entId, entUserId))
  79. start := time.Now()
  80. defer func() {
  81. util.Catch()
  82. log.Printf("GetEntDataExportCount entId:%v,entUserId:%v 总耗时:%v\n", entId, entUserId, time.Since(start))
  83. }()
  84. var (
  85. searchsWaitGroup = &sync.WaitGroup{}
  86. )
  87. count = GetDataExportSearchCountByScdId(sim, bid, bidMgoDBName, elasticAddress, _id)
  88. log.Printf("耗时: GetEntDataExportCount-GetDataExportSearchCountByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
  89. if count > maxCount || count == -1 {
  90. count = maxCount
  91. }
  92. log.Println("GetEntDataExportCount-count", entId, entUserId, count)
  93. dataType := "2"
  94. //数据导出数据查询
  95. res, err := GetDataExportSearchResultByScdId(sim, bid, bidMgoDBName, elasticAddress, _id, dataType, count)
  96. log.Printf("耗时: GetEntDataExportCount-GetDataExportSearchResultByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
  97. if err != nil {
  98. log.Println("企业数据导出错误 ", entId, entUserId, err)
  99. return 0, 0, nil
  100. }
  101. // 20210716 由原来的redis判重改为调用判重中台接口进行判重
  102. m := map[string]bool{}
  103. infoIdList := []string{}
  104. insertFlag := "false"
  105. if !isFirst {
  106. insertFlag = "true"
  107. }
  108. postStart := time.Now()
  109. for _, v := range *res {
  110. if util.IntAll(v["signendtime"]) != 0 {
  111. date := v["signendtime"]
  112. v["signendtime"] = FormatDateWithObj(&date, Date_Short_Layout)
  113. }
  114. if util.IntAll(v["bidendtime"]) != 0 {
  115. date := v["bidendtime"]
  116. v["bidendtime"] = FormatDateWithObj(&date, Date_Short_Layout)
  117. }
  118. id := util.ObjToString(v["_id"])
  119. if m[id] {
  120. continue
  121. }
  122. m[id] = true
  123. // 20210716 redis判重调整为调用判重中台接口 每一千个调用一次
  124. infoIdList = append(infoIdList, id)
  125. if len(infoIdList) > 1000 {
  126. // 调接口
  127. rs, err5 := Post(url, map[string]string{
  128. "personId": "0", // 没有使用这个参数
  129. "infoId": strings.Join(infoIdList, ","),
  130. "entId": fmt.Sprintf("%d", entId),
  131. "isInsert": insertFlag,
  132. "isEnt": "true",
  133. })
  134. log.Println("响应结果:", entId, entUserId, rs)
  135. if err5 != nil || util.IntAll(rs["code"]) != 0 {
  136. log.Println("企业订阅数据导出接口判重失败", entId, entUserId, err5, "rs:", rs, " rs[code]:", rs["code"], " ", util.IntAll(rs["code"]), "code是否为0", util.IntAll(rs["code"]) != 0)
  137. } else {
  138. log.Println("企业订阅数据导出")
  139. // 置空
  140. infoIdList = []string{}
  141. // 本次数据累计
  142. returnData := rs["data"].(map[string]interface{})
  143. log.Println(newCount, "加之前")
  144. newCount += util.IntAll(returnData["newCount"])
  145. //newCount += int(returnData["newCount"].(float64))
  146. log.Println(newCount, "加之后")
  147. }
  148. }
  149. if !isFirst {
  150. delete(v, "_id")
  151. v["entid"] = entId
  152. v["userid"] = entUserId
  153. v["infoid"] = id
  154. v["createtime"] = time.Now().Unix()
  155. }
  156. }
  157. if len(infoIdList) > 0 {
  158. rs, err5 := Post(url, map[string]string{
  159. "personId": "0", // 没有使用这个参数
  160. "infoId": strings.Join(infoIdList, ","),
  161. "entId": fmt.Sprintf("%d", entId),
  162. "isInsert": insertFlag,
  163. "isEnt": "true",
  164. })
  165. log.Println(rs)
  166. if err5 != nil || util.IntAll(rs["code"]) != 0 {
  167. log.Println("企业订阅数据导出接口判重失败", entId, entUserId, err5, "rs:", rs, " rs[code]:", rs["code"], " ", util.IntAll(rs["code"]), "code是否为0", util.IntAll(rs["code"]) != 0)
  168. } else {
  169. log.Println("企业订阅数据导出")
  170. // 置空
  171. infoIdList = []string{}
  172. // 本次数据累计
  173. returnData := rs["data"].(map[string]interface{})
  174. log.Println(newCount, "加之前")
  175. newCount += util.IntAll(returnData["newCount"])
  176. log.Println(newCount, "加之后")
  177. }
  178. }
  179. searchsWaitGroup.Wait()
  180. log.Printf("企业数据导出--数据遍历完成:entId:%v,entUserId:%v,newCount:%v,Since:%v \n", entId, entUserId, newCount, time.Since(postStart))
  181. data = res
  182. return
  183. }*/
  184. func GetEntDataExportCountIdArr(sim, bid mg.MongodbSim, bidMgoDBName, elasticAddress, _id string, entId, entUserId int, dataPkgRemoveRepeatAddr string, maxCount int) (count, newCount int, err error) {
  185. log.Println(fmt.Sprintf("GetEntDataExportCountIdArr entId:%v,entUserId:%v", entId, entUserId))
  186. start := time.Now()
  187. defer func() {
  188. util.Catch()
  189. log.Printf("GetEntDataExportCountIdArr entId:%v,entUserId:%v 总耗时:%v\n", entId, entUserId, time.Since(start))
  190. }()
  191. count = GetDataExportSearchCountByScdId(sim, bid, bidMgoDBName, elasticAddress, _id)
  192. log.Printf("耗时: GetEntDataExportCountIdArr-GetDataExportSearchCountByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
  193. if count > maxCount || count == -1 {
  194. count = maxCount
  195. }
  196. log.Println("GetEntDataExportCountIdArr-count", entId, entUserId, count)
  197. //数据导出数据查询
  198. ids, err := GetDataExportIdArrByScdId(sim, elasticAddress, _id, count)
  199. log.Printf("耗时: GetEntDataExportCountIdArr-GetDataExportSearchResultByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
  200. if err != nil {
  201. log.Println("GetEntDataExportCountIdArr 企业数据导出错误 ", entId, entUserId, err)
  202. return 0, 0, fmt.Errorf("企业数据导出错误")
  203. }
  204. if m, e := NoRepeatData(dataPkgRemoveRepeatAddr, map[string]interface{}{"entid": entId, "ids": ids}); e == nil {
  205. newCount = len(m)
  206. } else {
  207. newCount = len(ids)
  208. }
  209. log.Printf("GetEntDataExportCountIdArr 企业数据导出--数据遍历完成:entId:%v,entUserId:%v,newCount:%v,Since:%v \n", entId, entUserId, newCount)
  210. return
  211. }
  212. /*func GetEntDataExportCountIdArr(sim, bid mg.MongodbSim, bidMgoDBName, elasticAddress, _id string, entId, entUserId int, url string, maxCount int) (count, newCount int, err error) {
  213. log.Println(fmt.Sprintf("GetEntDataExportCountIdArr entId:%v,entUserId:%v", entId, entUserId))
  214. start := time.Now()
  215. defer func() {
  216. util.Catch()
  217. log.Printf("GetEntDataExportCountIdArr entId:%v,entUserId:%v 总耗时:%v\n", entId, entUserId, time.Since(start))
  218. }()
  219. var (
  220. searchsWaitGroup = &sync.WaitGroup{}
  221. )
  222. count = GetDataExportSearchCountByScdId(sim, bid, bidMgoDBName, elasticAddress, _id)
  223. log.Printf("耗时: GetEntDataExportCountIdArr-GetDataExportSearchCountByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
  224. if count > maxCount || count == -1 {
  225. count = maxCount
  226. }
  227. log.Println("GetEntDataExportCountIdArr-count", entId, entUserId, count)
  228. //数据导出数据查询
  229. ids, err := GetDataExportIdArrByScdId(sim, elasticAddress, _id, count)
  230. log.Printf("耗时: GetEntDataExportCountIdArr-GetDataExportSearchResultByScdId entId:%v,entUserId:%v,Since:%v \n", entId, entUserId, time.Since(start))
  231. if err != nil {
  232. log.Println("GetEntDataExportCountIdArr 企业数据导出错误 ", entId, entUserId, err)
  233. return 0, 0, fmt.Errorf("企业数据导出错误")
  234. }
  235. // 20210716 由原来的redis判重改为调用判重中台接口进行判重
  236. m := map[string]bool{}
  237. infoIdList := []string{}
  238. insertFlag := "false"
  239. postStart := time.Now()
  240. for i := 0; i < len(ids); i++ {
  241. id := ids[i]
  242. if m[id] {
  243. continue
  244. }
  245. m[id] = true
  246. // 20210716 redis判重调整为调用判重中台接口 每一千个调用一次
  247. infoIdList = append(infoIdList, id)
  248. if len(infoIdList) > 1000 {
  249. // 调接口
  250. rs, err5 := Post(url, map[string]string{
  251. "personId": "0", // 没有使用这个参数
  252. "infoId": strings.Join(infoIdList, ","),
  253. "entId": fmt.Sprintf("%d", entId),
  254. "isInsert": insertFlag,
  255. "isEnt": "true",
  256. })
  257. log.Println("GetEntDataExportCountIdArr 响应结果:", entId, entUserId, rs)
  258. if err5 != nil || util.IntAll(rs["code"]) != 0 {
  259. log.Println("GetEntDataExportCountIdArr 企业订阅数据导出接口判重失败", entId, entUserId, err5, "rs:", rs, " rs[code]:", rs["code"], " ", util.IntAll(rs["code"]), "code是否为0", util.IntAll(rs["code"]) != 0)
  260. } else {
  261. log.Println("GetEntDataExportCountIdArr 企业订阅数据导出")
  262. // 置空
  263. infoIdList = []string{}
  264. // 本次数据累计
  265. returnData := rs["data"].(map[string]interface{})
  266. log.Println(newCount, "GetEntDataExportCountIdArr 加之前")
  267. newCount += util.IntAll(returnData["newCount"])
  268. //newCount += int(returnData["newCount"].(float64))
  269. log.Println(newCount, "GetEntDataExportCountIdArr 加之后")
  270. }
  271. }
  272. }
  273. if len(infoIdList) > 0 {
  274. rs, err5 := Post(url, map[string]string{
  275. "personId": "0", // 没有使用这个参数
  276. "infoId": strings.Join(infoIdList, ","),
  277. "entId": fmt.Sprintf("%d", entId),
  278. "isInsert": insertFlag,
  279. "isEnt": "true",
  280. })
  281. log.Println(rs)
  282. if err5 != nil || util.IntAll(rs["code"]) != 0 {
  283. log.Println("GetEntDataExportCountIdArr 企业订阅数据导出接口判重失败", entId, entUserId, err5, "rs:", rs, " rs[code]:", rs["code"], " ", util.IntAll(rs["code"]), "code是否为0", util.IntAll(rs["code"]) != 0)
  284. } else {
  285. log.Println("GetEntDataExportCountIdArr 企业订阅数据导出")
  286. // 置空
  287. infoIdList = []string{}
  288. // 本次数据累计
  289. returnData := rs["data"].(map[string]interface{})
  290. log.Println(newCount, "GetEntDataExportCountIdArr 加之前")
  291. newCount += util.IntAll(returnData["newCount"])
  292. log.Println(newCount, "GetEntDataExportCountIdArr 加之后")
  293. }
  294. }
  295. searchsWaitGroup.Wait()
  296. log.Printf("GetEntDataExportCountIdArr 企业数据导出--数据遍历完成:entId:%v,entUserId:%v,newCount:%v,Since:%v \n", entId, entUserId, newCount, time.Since(postStart))
  297. return
  298. }*/
  299. func FormatExportDatas(Mgo_Ent mongodb.MongodbSim, data *[]map[string]interface{}, webdomain string, dataType string, entId int) *[]map[string]interface{} {
  300. //格式化输出
  301. var (
  302. entexportPool = make(chan bool, 20)
  303. entexportWaitGroup = &sync.WaitGroup{}
  304. )
  305. sort.Slice(*data, func(i, j int) bool {
  306. time1 := util.Int64All((*data)[i]["publishtime"])
  307. time2 := util.Int64All((*data)[j]["publishtime"])
  308. return time1 > time2
  309. })
  310. log.Println("补充信息开始")
  311. for _, v := range *data {
  312. entexportWaitGroup.Add(1)
  313. entexportPool <- true
  314. go func(v map[string]interface{}) {
  315. defer func() {
  316. entexportWaitGroup.Done()
  317. <-entexportPool
  318. }()
  319. //有中标企业 且 高级字段查询
  320. if dataType == "2" {
  321. //查询企业公示 法人 公司电话 公司邮箱地址
  322. s_winner := strings.Split(util.ObjToString(v["s_winner"]), ",")[0]
  323. if entData, ok := Mgo_Ent.Find("winner_enterprise", bson.M{"company_name": s_winner}, nil,
  324. `{"company_name":1,"company_email":1,"legal_person":1,"company_phone":1}`, false, -1, -1); ok {
  325. if entData != nil && *entData != nil && len(*entData) > 0 {
  326. for _, ev := range *entData {
  327. if v["s_winner"] == ev["company_name"] {
  328. legal_person := ""
  329. if ev["legal_person"] != nil {
  330. legal_person = ev["legal_person"].(string)
  331. }
  332. company_phone := ""
  333. if ev["company_phone"] != nil {
  334. company_phone = ev["company_phone"].(string)
  335. }
  336. company_email := ""
  337. if ev["company_email"] != nil && ev["company_email"] != "无" {
  338. company_email = ev["company_email"].(string)
  339. }
  340. v["legal_person"] = legal_person
  341. v["company_phone"] = company_phone
  342. v["company_email"] = company_email
  343. }
  344. }
  345. }
  346. }
  347. }
  348. //====================字段补漏=========================
  349. if v["toptype"] == "结果" && dataType == "2" && !(v["agency"] != nil && v["budget"] != nil && v["buyerperson"] != nil && v["buyertel"] != nil) {
  350. r := elastic.Get("projectset", "projectset", fmt.Sprintf(`{"query":{"term":{"list.infoid":"%s"}},"_source": ["list"]}`, v["_id"]))
  351. if len(*r) > 0 {
  352. MsgList := (*r)[0]["list"]
  353. if MsgList != nil {
  354. list := util.ObjArrToMapArr(MsgList.([]interface{}))
  355. for _, vv := range list {
  356. if vv["subtype"] == "招标" {
  357. if v["agency"] == nil && vv["agency"] != nil {
  358. v["agency"] = vv["agency"]
  359. }
  360. if v["budget"] == nil && vv["budget"] != nil {
  361. v["budget"] = vv["budget"]
  362. }
  363. if v["buyerperson"] == nil && vv["buyerperson"] != nil {
  364. v["buyerperson"] = vv["buyerperson"]
  365. }
  366. if v["buyertel"] == nil && vv["buyertel"] != nil {
  367. v["buyertel"] = vv["buyertel"]
  368. }
  369. break
  370. }
  371. }
  372. }
  373. }
  374. }
  375. if v["area"] == "A" {
  376. v["area"] = "全国"
  377. }
  378. if v["publishtime"] != nil {
  379. date := v["publishtime"]
  380. v["publishtime"] = FormatDateWithObj(&date, Date_Short_Layout)
  381. }
  382. if v["bidopentime"] != nil {
  383. date := v["bidopentime"]
  384. v["bidopentime"] = FormatDateWithObj(&date, Date_Short_Layout)
  385. }
  386. if util.IntAll(v["signendtime"]) != 0 {
  387. date := v["signendtime"]
  388. v["signendtime"] = FormatDateWithObj(&date, Date_Short_Layout)
  389. }
  390. if util.IntAll(v["bidendtime"]) != 0 {
  391. date := v["bidendtime"]
  392. v["bidendtime"] = FormatDateWithObj(&date, Date_Short_Layout)
  393. }
  394. if v["currency"] == "" || v["currency"] == nil {
  395. v["currency"] = "人民币"
  396. }
  397. if v["subtype"] == nil && v["toptype"] != nil {
  398. v["subtype"] = v["toptype"]
  399. }
  400. if v["detail"] != "" && v["detail"] != nil {
  401. str := ClearHtml.ReplaceAllString(v["detail"].(string), "")
  402. str = ClearOther.ReplaceAllString(str, "")
  403. str = strings.Replace(str, " ", "", -1)
  404. v["detail"] = str
  405. }
  406. if v["infoid"] != nil {
  407. v["url"] = webdomain + "/article/content/" + CommonEncodeArticle("content", v["infoid"].(string)) + ".html"
  408. }
  409. }(v)
  410. }
  411. entexportWaitGroup.Wait()
  412. log.Println("补充信息结束")
  413. return data
  414. }
  415. func Post(url string, form map[string]string) (data map[string]interface{}, err error) {
  416. str := ""
  417. for k, v := range form {
  418. str += "&" + k + "=" + v
  419. }
  420. //log.Println(str)
  421. res, err1 := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(str))
  422. log.Println(res)
  423. if err1 != nil {
  424. log.Println("post err:", err1.Error())
  425. return nil, err1
  426. } else if res.Body != nil {
  427. defer res.Body.Close()
  428. bs, _ := ioutil.ReadAll(res.Body)
  429. err2 := json.Unmarshal(bs, &data)
  430. if err2 != nil {
  431. return nil, err2
  432. }
  433. }
  434. return data, nil
  435. }
  436. // 生成xlsx
  437. func GetXlsx(mMap []map[string]interface{}, entId, entUserId int, filePath string) string {
  438. xf, err := xlsx.OpenFile("./web/staticres/fields.xlsx")
  439. if err != nil {
  440. log.Println("fields file not foud", err.Error())
  441. }
  442. sh := xf.Sheets[1]
  443. for _, v := range mMap {
  444. row := sh.AddRow()
  445. row.AddCell().SetValue(v["keyword"])
  446. row.AddCell().SetValue(v["area"])
  447. row.AddCell().SetValue(v["city"])
  448. row.AddCell().SetValue(v["district"])
  449. row.AddCell().SetValue(v["title"])
  450. row.AddCell().SetValue(v["subtype"])
  451. row.AddCell().SetValue(v["detail"])
  452. if v["publishtime"] != nil {
  453. row.AddCell().SetValue(v["publishtime"])
  454. } else {
  455. row.AddCell()
  456. }
  457. row.AddCell().SetValue(v["href"])
  458. row.AddCell().SetValue(v["url"])
  459. row.AddCell().SetValue(v["projectname"])
  460. row.AddCell().SetValue(v["projectcode"])
  461. row.AddCell().SetValue(v["projectscope"])
  462. if v["budget"] != nil {
  463. row.AddCell().SetFloat(util.Float64All(v["budget"]))
  464. } else {
  465. row.AddCell()
  466. }
  467. if v["bidamount"] != nil {
  468. row.AddCell().SetFloat(util.Float64All(v["bidamount"]))
  469. } else {
  470. row.AddCell()
  471. }
  472. if v["signendtime"] != nil {
  473. row.AddCell().SetValue(v["signendtime"])
  474. } else {
  475. row.AddCell()
  476. }
  477. if v["bidopentime"] != nil {
  478. row.AddCell().SetValue(v["bidopentime"])
  479. } else {
  480. row.AddCell()
  481. }
  482. if v["bidendtime"] != nil {
  483. row.AddCell().SetValue(v["bidendtime"])
  484. } else {
  485. row.AddCell()
  486. }
  487. row.AddCell().SetValue(v["buyer"])
  488. row.AddCell().SetValue(v["buyerperson"])
  489. row.AddCell().SetValue(v["buyertel"])
  490. row.AddCell().SetValue(v["buyeraddr"])
  491. row.AddCell().SetValue(v["agency"])
  492. row.AddCell().SetValue(v["s_winner"])
  493. row.AddCell().SetValue(v["winnerperson"])
  494. row.AddCell().SetValue(v["winnertel"])
  495. row.AddCell().SetValue(v["legal_person"])
  496. row.AddCell().SetValue(v["company_phone"])
  497. row.AddCell().SetValue(v["company_email"])
  498. }
  499. xf.Sheets = xf.Sheets[1:2]
  500. xf.Sheets[0].Name = "数据导出"
  501. //生文件
  502. t := strconv.FormatInt(time.Now().Unix(), 10)
  503. entIds := strconv.Itoa(entId)
  504. entUserIds := strconv.Itoa(entUserId)
  505. dir := filePath + "/entsearchexport/" + entIds + "_" + entUserIds + "_" + t + "/"
  506. if b, _ := PathExists(dir); !b {
  507. err1 := os.MkdirAll(dir, os.ModePerm)
  508. if err1 != nil {
  509. log.Println("mkdir err", dir)
  510. }
  511. }
  512. fname := entIds + "_" + entUserIds + "_" + "entdataexport.xlsx"
  513. download_url := "/entsearchexport/" + entIds + "_" + entUserIds + "_" + t + "/" + fname
  514. err = xf.Save(dir + fname)
  515. if err != nil {
  516. log.Println("xls error", fname)
  517. return ""
  518. }
  519. return download_url
  520. }
  521. func PathExists(path string) (bool, error) {
  522. _, err := os.Stat(path)
  523. if err == nil {
  524. return true, nil
  525. }
  526. if os.IsNotExist(err) {
  527. return false, nil
  528. }
  529. return false, err
  530. }
  531. func SaveExportLog(mysqlSess *mysql.Mysql, entId, entUserId, count, newCount, remain_nums, export_nums int, xlsxUrl, types, filterStr string, exportPhone, exportEmail string) {
  532. query := map[string]interface{}{
  533. "id": entUserId,
  534. "ent_id": entId,
  535. }
  536. set := map[string]interface{}{
  537. "remain_nums": remain_nums - newCount,
  538. "export_nums": export_nums + newCount,
  539. }
  540. ok := mysqlSess.Update("entniche_export_limit", map[string]interface{}{"ent_id": entId, "user_id": entUserId}, set)
  541. if !ok {
  542. log.Println("修改导出条数失败", query, remain_nums, newCount)
  543. }
  544. userData := mysqlSess.FindOne("entniche_user", query, "name,phone", "")
  545. if userData != nil {
  546. name := util.ObjToString((*userData)["name"])
  547. phone := util.ObjToString((*userData)["phone"])
  548. now := time.Now()
  549. mysqlSess.Insert("entniche_export_log", map[string]interface{}{
  550. "user_name": name,
  551. "export_time": FormatDate(&now, Date_Full_Layout),
  552. "data_source": "2",
  553. "export_num": count,
  554. "deduct_num": newCount,
  555. "download_url": xlsxUrl,
  556. "ent_id": entId,
  557. "phone": phone,
  558. "user_id": entUserId,
  559. "filter": filterStr,
  560. "export_phone": exportPhone,
  561. "export_mail": exportEmail,
  562. })
  563. }
  564. }
  565. func DeductNum(m *mysql.Mysql, qyfw mongodb.MongodbSim, entId, newCount int) {
  566. query := map[string]interface{}{
  567. "id": entId,
  568. }
  569. userData := m.FindOne("entniche_info", query, "name,phone", "")
  570. if userData != nil {
  571. name := util.ObjToString((*userData)["name"])
  572. phone := util.ObjToString((*userData)["phone"])
  573. qyfw.Update("user", map[string]interface{}{"phone": phone, "username": name}, map[string]interface{}{
  574. "$inc": map[string]interface{}{
  575. "plan.current": -newCount,
  576. },
  577. }, false, false)
  578. }
  579. }
  580. func GetCurrentCount(m *mysql.Mysql, qyfw mongodb.MongodbSim, entId int) int {
  581. count := 0
  582. userData := m.FindOne("entniche_info", map[string]interface{}{"id": entId}, "name,phone", "")
  583. if userData == nil {
  584. return count
  585. }
  586. current, ok := qyfw.FindOne("user", map[string]interface{}{"phone": util.ObjToString((*userData)["phone"]), "username": util.ObjToString((*userData)["name"])})
  587. if current == nil || !ok {
  588. return count
  589. }
  590. plan, _ := (*current)["plan"].(map[string]interface{})
  591. count = util.IntAll(plan["current"])
  592. return count
  593. }