dataExportLogService.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. package customerService
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/mongodb"
  5. "app.yhyue.com/moapp/jybase/redis"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "io/ioutil"
  10. "log"
  11. "net/http"
  12. "net/url"
  13. "qmx_admin/src/config"
  14. "strings"
  15. "time"
  16. )
  17. const (
  18. BigmemberUserPowerTable = "bigmember_service_user"
  19. PACKNAME = "数据流包"
  20. PowerCacheDb = "other"
  21. DataPacketUsedNum = "dataPacketUsed_%s_%d"
  22. RedisCacheDb = "other"
  23. )
  24. func FindOnePartner(uid string, id int) (res map[string]interface{}) {
  25. log.Println("service加载中。。。。。。。")
  26. return nil
  27. }
  28. // 查询商机管理企业账户余额
  29. func getCurrEntCount(entName, entPhone string) int {
  30. current, ok := config.JyqyfwMgo.FindOne("user", map[string]interface{}{"phone": entPhone, "username": entName})
  31. if current == nil || !ok {
  32. return 0
  33. }
  34. plan, planOk := (*current)["plan"].(map[string]interface{})
  35. if !planOk {
  36. return 0
  37. }
  38. return common.IntAll(plan["current"])
  39. }
  40. func FindExportLogList(phoneIn, userNameIn, entNameIn string, startTimeIn, endTimeIn, pageNum, pageSize int) (returnList []map[string]interface{}, totalCount int) {
  41. // 根据用户昵称模糊查询出用户mongoid 及手机号
  42. // 根据用户id查询 datapack_record
  43. nick1List := []string{}
  44. nick2List := []string{}
  45. nickPhoneList := []interface{}{}
  46. nickIdList := []interface{}{}
  47. masterIdList := []interface{}{}
  48. masterPreList := []string{}
  49. //entNameIdList := []interface{}{}
  50. if userNameIn != "" {
  51. // 模糊查询出用户列表
  52. userTmpRs, ok := config.MQFW.Find("user", map[string]interface{}{"s_nickname": map[string]interface{}{
  53. "$regex": userNameIn,
  54. }}, `{"createtime":-1}`, nil, false, -1, -1)
  55. if ok && userTmpRs != nil && len(*userTmpRs) > 0 {
  56. for _, v := range *userTmpRs {
  57. if common.ObjToString(v["s_phone"]) != "" {
  58. nick1List = append(nick1List, "?")
  59. nickPhoneList = append(nickPhoneList, common.ObjToString(v["s_phone"]))
  60. } else if common.ObjToString(v["s_m_phone"]) != "" {
  61. nick1List = append(nick1List, "?")
  62. nickPhoneList = append(nickPhoneList, common.ObjToString(v["s_m_phone"]))
  63. }
  64. nick2List = append(nick2List, "?")
  65. nickIdList = append(nickIdList, mongodb.BsonIdToSId(v["_id"]))
  66. }
  67. } else {
  68. log.Println("根据昵称没有查询到用户信息")
  69. return
  70. }
  71. }
  72. if entNameIn != "" {
  73. // 根据企业名称模糊查询出主账户id 列表
  74. // 处理查询条件
  75. masterTmpRs, ok := config.MQFW.Find("member", map[string]interface{}{"entname": map[string]interface{}{
  76. "$regex": entNameIn,
  77. }}, `{"createtime":-1}`, nil, false, -1, -1)
  78. if ok && masterTmpRs != nil && len(*masterTmpRs) > 0 {
  79. for _, tmp := range *masterTmpRs {
  80. masterIdList = append(masterIdList, tmp["userid"])
  81. masterPreList = append(masterPreList, "?")
  82. }
  83. }
  84. }
  85. searchStrList := []string{}
  86. searchValueList := []interface{}{}
  87. datapackSql := ""
  88. entnicheSql := ""
  89. // 查询数据 处理sql
  90. // 由于单日限量包和数据流量包线上没有企业 所以查询条件有企业时不用再查datapacket_record了
  91. // 单日限量包和数据流量包线上
  92. // 企业名称为空时不用走这段,企业名称不为空时查询到了master_id 才走这段
  93. if entNameIn == "" || (entNameIn != "" && len(masterIdList) > 0) {
  94. // 处理手机号或者昵称条件匹配到的用户id
  95. // 查询参数包含手机号时
  96. // 根据手机号查询出mongo库用户id 用于查询 datapacket_record
  97. tmpFilterList := []string{}
  98. if len(masterIdList) > 0 {
  99. // 拼接master_id 占位符
  100. tmpFilterList = append(tmpFilterList, "type = 1")
  101. tmpFilterList = append(tmpFilterList, fmt.Sprintf("master_id in (%s)", strings.Join(masterPreList, ",")))
  102. // 拼接 master_id 值
  103. searchValueList = append(searchValueList, masterIdList...)
  104. }
  105. if phoneIn != "" || userNameIn != "" {
  106. searchMgId := ""
  107. if phoneIn != "" {
  108. userRs, ok := config.MQFW.FindOne("user", map[string]interface{}{"s_phone": phoneIn})
  109. if ok && userRs != nil && len(*userRs) > 0 {
  110. searchMgId = mongodb.BsonIdToSId((*userRs)["_id"])
  111. nick2List = append(nick2List, "?")
  112. nickIdList = append(nickIdList, searchMgId)
  113. } else {
  114. userRs, oks := config.MQFW.FindOne("user", map[string]interface{}{"s_m_phone": phoneIn})
  115. if oks && userRs != nil && len(*userRs) > 0 {
  116. searchMgId = mongodb.BsonIdToSId((*userRs)["_id"])
  117. nick2List = append(nick2List, "?")
  118. nickIdList = append(nickIdList, searchMgId)
  119. } else {
  120. log.Println("未找到对应信息1")
  121. }
  122. }
  123. }
  124. preStr := strings.Join(nick2List, ",")
  125. // 根据手机号或者用户昵称查询到了用户信息 才会拼接其他条件
  126. if len(nickIdList) >= 0 {
  127. // 处理时间条件存在
  128. if startTimeIn != 0 {
  129. tmpFilterList = append(tmpFilterList, "date>?")
  130. searchValueList = append(searchValueList, startTimeIn)
  131. }
  132. if endTimeIn != 0 {
  133. tmpFilterList = append(tmpFilterList, "date<?")
  134. searchValueList = append(searchValueList, endTimeIn)
  135. }
  136. tmpFilterList = append(tmpFilterList, fmt.Sprintf("useid in (%s)", preStr))
  137. searchValueList = append(searchValueList, nickIdList...)
  138. // 存在查询条件 进行拼接
  139. tmpFilterSql := ""
  140. if len(tmpFilterList) > 0 {
  141. tmpFilterSql = fmt.Sprintf(" where %s ", strings.Join(tmpFilterList, " and "))
  142. }
  143. datapackSql = fmt.Sprintf("(SELECT id AS log_id,'' AS ent_id,useid AS user_id,master_id AS master_id,date AS exportDate,type AS data_from,export_num AS export_num,'2' AS source,'' AS phone,'' AS ent_name,'' AS admin_phone,isSenior as isSenior FROM datapacket_record %s) ", tmpFilterSql)
  144. searchStrList = append(searchStrList, datapackSql)
  145. }
  146. } else {
  147. // 处理时间条件存在
  148. if startTimeIn != 0 {
  149. tmpFilterList = append(tmpFilterList, "date>?")
  150. searchValueList = append(searchValueList, startTimeIn)
  151. }
  152. if endTimeIn != 0 {
  153. tmpFilterList = append(tmpFilterList, "date<?")
  154. searchValueList = append(searchValueList, endTimeIn)
  155. }
  156. // 存在查询条件 进行拼接
  157. tmpFilterSql := ""
  158. if len(tmpFilterList) > 0 {
  159. tmpFilterSql = fmt.Sprintf(" where %s ", strings.Join(tmpFilterList, " and "))
  160. }
  161. datapackSql = fmt.Sprintf("(SELECT id AS log_id,'' AS ent_id,useid AS user_id,master_id AS master_id,date AS exportDate,type AS data_from,export_num AS export_num,'2' AS source,'' AS phone ,'' AS ent_name,'' as admin_phone,isSenior as isSenior FROM datapacket_record %s) ", tmpFilterSql)
  162. searchStrList = append(searchStrList, datapackSql)
  163. }
  164. }
  165. // 数据流量包线下的条件参数处理
  166. tmpFilterList2 := []string{}
  167. // 处理手机号或者昵称条件匹配到的用户id
  168. // 根据昵称匹配到了用户 拿到了用户手机号才能去查商机管理表
  169. if userNameIn != "" {
  170. if len(nickPhoneList) > 0 {
  171. if startTimeIn != 0 {
  172. tmpFilterList2 = append(tmpFilterList2, "UNIX_TIMESTAMP( a.export_time )>?")
  173. searchValueList = append(searchValueList, startTimeIn)
  174. }
  175. if endTimeIn != 0 {
  176. tmpFilterList2 = append(tmpFilterList2, "UNIX_TIMESTAMP( a.export_time )<?")
  177. searchValueList = append(searchValueList, endTimeIn)
  178. }
  179. // 根据手机号查询商机管理记录表
  180. preStr := strings.Join(nick1List, ",")
  181. tmpFilterList2 = append(tmpFilterList2, fmt.Sprintf("a.phone in (%s)", preStr))
  182. searchValueList = append(searchValueList, nickPhoneList...)
  183. if entNameIn != "" {
  184. tmpFilterList2 = append(tmpFilterList2, fmt.Sprintf("a.ent_id in (select id from entniche_info WHERE name like '%%%s%%')", entNameIn))
  185. //searchValueList = append(searchValueList, entNameIn)
  186. }
  187. // 存在查询条件 进行拼接
  188. tmpFilterSql2 := ""
  189. if len(tmpFilterList2) > 0 {
  190. tmpFilterSql2 = fmt.Sprintf(" and %s ", strings.Join(tmpFilterList2, " and "))
  191. }
  192. // 数据流量包(线下)
  193. entnicheSql = fmt.Sprintf("(SELECT a.id AS log_id, a.ent_id AS ent_id,a.user_id AS user_id,'' AS master_id,UNIX_TIMESTAMP( a.export_time ) AS exportDate,0 AS data_from,a.export_num AS export_num,'1' AS source,a.phone as phone ,b.NAME AS ent_name,b.phone as admin_phone ,'' as isSenior FROM entniche_export_log AS a,entniche_info AS b WHERE a.ent_id = b.id %s)", tmpFilterSql2)
  194. searchStrList = append(searchStrList, entnicheSql)
  195. }
  196. } else {
  197. if startTimeIn != 0 {
  198. tmpFilterList2 = append(tmpFilterList2, "UNIX_TIMESTAMP( a.export_time )>?")
  199. searchValueList = append(searchValueList, startTimeIn)
  200. }
  201. if endTimeIn != 0 {
  202. tmpFilterList2 = append(tmpFilterList2, "UNIX_TIMESTAMP( a.export_time )<?")
  203. searchValueList = append(searchValueList, endTimeIn)
  204. }
  205. // 根据手机号查询商机管理记录表
  206. if phoneIn != "" {
  207. tmpFilterList2 = append(tmpFilterList2, "a.phone =?")
  208. searchValueList = append(searchValueList, phoneIn)
  209. }
  210. if entNameIn != "" {
  211. tmpFilterList2 = append(tmpFilterList2, fmt.Sprintf("a.ent_id in (select id from entniche_info WHERE name like '%%%s%%')", entNameIn))
  212. //searchValueList = append(searchValueList, entNameIn)
  213. }
  214. // 存在查询条件 进行拼接
  215. tmpFilterSql2 := ""
  216. if len(tmpFilterList2) > 0 {
  217. tmpFilterSql2 = fmt.Sprintf(" and %s ", strings.Join(tmpFilterList2, " and "))
  218. }
  219. // 数据流量包(线下)
  220. entnicheSql = fmt.Sprintf("(SELECT a.id AS log_id, a.ent_id AS ent_id,a.user_id AS user_id,'' AS master_id,UNIX_TIMESTAMP( a.export_time ) AS exportDate,0 AS data_from,a.export_num AS export_num,'1' AS source,a.phone as phone,b.NAME AS ent_name,b.phone as admin_phone,'' as isSenior FROM entniche_export_log AS a,entniche_info AS b WHERE a.ent_id = b.id %s)", tmpFilterSql2)
  221. searchStrList = append(searchStrList, entnicheSql)
  222. }
  223. // 拼接sql集合
  224. searchSql := strings.Join(searchStrList, ` UNION ALL`)
  225. b := fmt.Sprintf("SELECT * FROM (%s) AS alldata ORDER BY exportDate DESC LIMIT ?,? ", searchSql)
  226. // 返回totalCount
  227. totalCount = config.JysqlDB.QueryCount(fmt.Sprintf("SELECT count(*) FROM (%s) AS alldata ", searchSql), searchValueList...)
  228. // 追加翻页参数
  229. searchValueList = append(searchValueList, pageNum, pageSize)
  230. // 替换单日限量包id
  231. c := fmt.Sprintf("SELECT log_id,user_id,exportDate,export_num,source,ent_id,ent_name,phone,data_from,admin_phone,master_id,isSenior,"+
  232. "CASE data_from "+
  233. "WHEN 0 THEN '数据流量包(线下充值)'"+
  234. "WHEN 2 THEN '数据流量包(线上充值)'"+
  235. "WHEN 1 THEN (SELECT CASE s_serviceid "+
  236. "WHEN 18 THEN '单日限量包(200条)'"+
  237. "WHEN 17 THEN '单日限量包(100条)'"+
  238. "END "+
  239. "FROM bigmember_service_user "+
  240. "WHERE s_userid = master_id AND ( s_serviceid = 17 OR s_serviceid = 18 ) "+
  241. "AND exportDate <= UNIX_TIMESTAMP( l_endtime ) AND exportDate > UNIX_TIMESTAMP( l_starttime ) LIMIT 0,1 ) "+
  242. "END AS pay_way "+
  243. "FROM (%s) AS finalData", b)
  244. //查询
  245. // 补充字段 支付方式 处理单日限量包 用户微信昵称 单日限量包和数据流量包线上需要补充 用户手机号
  246. resultList := config.JysqlDB.Query(c, searchValueList...)
  247. if resultList == nil || len(*resultList) == 0 {
  248. log.Println("未查询到结果")
  249. return
  250. }
  251. //格式化返回内容
  252. returnList = make([]map[string]interface{}, 0, pageSize)
  253. for _, row := range *resultList {
  254. // 根据类型查询 mongo 库
  255. // 商机管理的根据手机号查询mongo库 获取用户微信昵称 source 0商机管理的数据 1 datapack的数据
  256. if row["source"] == "1" {
  257. userData, ok := config.MQFW.FindOne("user", map[string]interface{}{"s_phone": row["phone"]})
  258. if ok && userData != nil && len(*userData) > 0 {
  259. row["nickname"] = (*userData)["s_nickname"]
  260. } else {
  261. userDatas, oks := config.MQFW.FindOne("user", map[string]interface{}{"s_m_phone": row["phone"]})
  262. if oks && userDatas != nil && len(*userDatas) > 0 {
  263. row["nickname"] = (*userDatas)["s_nickname"]
  264. } else {
  265. log.Println("未找到对应信息2")
  266. }
  267. }
  268. // 商机管理查询用户余额
  269. leftCount := getCurrEntCount(common.ObjToString(row["ent_name"]), common.ObjToString(row["admin_phone"]))
  270. row["leftCount"] = leftCount
  271. } else {
  272. // 单日限量包和数据流量包线上的根据用户id查询mongo库 获取用户微信昵称和手机号
  273. data, ok := config.MQFW.FindOne("user", map[string]interface{}{"_id": mongodb.StringTOBsonId(common.ObjToString(row["user_id"]))})
  274. if ok && data != nil && len(*data) > 0 {
  275. row["nickname"] = (*data)["s_nickname"]
  276. if common.ObjToString((*data)["s_phone"]) == "" {
  277. row["phone"] = (*data)["s_m_phone"]
  278. } else {
  279. row["phone"] = (*data)["s_phone"]
  280. }
  281. row["nickname"] = (*data)["s_nickname"]
  282. }
  283. if row["pay_way"] == nil {
  284. row["pay_way"] = "每日限量包"
  285. }
  286. // 单日限量包 当日限额
  287. if common.IntAll(row["data_from"]) == 1 {
  288. // 查主账号余额
  289. dayUse := redis.GetInt(RedisCacheDb, fmt.Sprintf(DataPacketUsedNum, common.ObjToString(row["master_id"]), time.Now().Day()))
  290. todayLimit := GetDailyNum(common.ObjToString(row["master_id"]))
  291. row["leftCount"] = todayLimit - dayUse
  292. // 补充公司名称 根据master_id 去member表里 mongo 查数据
  293. companyRs, ok := config.MQFW.FindOne("member", map[string]interface{}{
  294. "userid": row["master_id"],
  295. })
  296. if ok && companyRs != nil && len(*companyRs) > 0 {
  297. row["ent_name"] = (*companyRs)["entname"]
  298. } else {
  299. log.Println("未在member 表里查询到数据")
  300. }
  301. } else {
  302. // 数据流量包(线上) 调用中台接口
  303. //balance := 0 //剩余量
  304. resourceType := ""
  305. if common.IntAll(row["isSenior"]) == 1 {
  306. resourceType = "标准字段包"
  307. } else {
  308. resourceType = "高级字段包"
  309. }
  310. //调用资源中台服务获取剩余量
  311. resMap, err := CommonPost(config.SysConfigs.FindBalance, url.Values{
  312. "accountId": []string{common.ObjToString(row["user_id"])}, //账户标识*
  313. })
  314. if err != nil {
  315. return
  316. }
  317. listObj, ok := resMap["data"].([]interface{})
  318. if !ok {
  319. log.Println("packAccountQuery 请求中台返回数据结构异常")
  320. return
  321. }
  322. for _, v := range listObj {
  323. obj, ok := v.(map[string]interface{})
  324. if !ok {
  325. log.Println("packAccountQuery 请求中台返回数据结构异常")
  326. return
  327. }
  328. if common.ObjToString(obj["resourceType"]) == resourceType {
  329. row["leftCount"] = common.IntAll(obj["number"])
  330. break
  331. }
  332. }
  333. }
  334. }
  335. returnList = append(returnList, row)
  336. }
  337. return
  338. }
  339. func GetDailyNum(masterId string) (dailyNum int) {
  340. serviceList := config.JysqlDB.Find(BigmemberUserPowerTable, map[string]interface{}{"s_userid": masterId, "i_status": 0}, "DISTINCT(s_serviceid),i_frequency", "", -1, -1)
  341. if serviceList != nil && len(*serviceList) != 0 {
  342. for _, item := range *serviceList {
  343. serviceid := common.IntAll(item["s_serviceid"])
  344. if serviceid == 17 || serviceid == 18 { //每日数据包
  345. dailyNum = common.IntAll(item["i_frequency"])
  346. }
  347. }
  348. }
  349. return
  350. }
  351. func packAccountQuery(accountId string) (total int, err error) {
  352. resMap, err := CommonPost(config.SysConfigs.FindBalance, url.Values{
  353. "accountId": []string{accountId}, //账户标识*
  354. })
  355. if err != nil {
  356. return
  357. }
  358. listObj, ok := resMap["data"].([]interface{})
  359. if !ok {
  360. return total, fmt.Errorf("packAccountQuery 请求中台返回数据结构异常")
  361. }
  362. for _, obj := range listObj {
  363. packMap := common.ObjToMap(obj)
  364. if packMap == nil || len(*packMap) == 0 {
  365. continue
  366. }
  367. if packType := common.ObjToString((*packMap)["name"]); packType != PACKNAME {
  368. continue
  369. }
  370. total = common.IntAll((*packMap)["number"]) //总量
  371. }
  372. return
  373. }
  374. func CommonPost(path string, param url.Values) (map[string]interface{}, error) {
  375. //param["appId"] = []string{config.JyApiConfig.AppId} //公共appid
  376. res, err := http.PostForm(path, param)
  377. if err != nil {
  378. return nil, err
  379. }
  380. defer res.Body.Close()
  381. bs, _ := ioutil.ReadAll(res.Body)
  382. resMap := map[string]interface{}{}
  383. err = json.Unmarshal(bs, &resMap)
  384. if err != nil {
  385. return nil, fmt.Errorf("%s 请求中台返回内容异常 %s", path, string(bs))
  386. }
  387. if common.IntAll(resMap["code"]) != 1 {
  388. return nil, fmt.Errorf("%s 请求中台请求出错 %v", path, resMap["message"])
  389. }
  390. return resMap, nil
  391. }
  392. func FindExportLog(id, source int) (result ExportLog, err error) {
  393. //result := ExportLog{}
  394. if source == 1 { //查询商机管理数据导出记录表
  395. exportLog := config.JysqlDB.FindOne("entniche_export_log", map[string]interface{}{"id": id}, ``, "")
  396. if exportLog != nil && len(*exportLog) > 0 {
  397. //查询条件处理
  398. str := common.ObjToString((*exportLog)["filter"])
  399. filterMap := map[string]interface{}{}
  400. err := json.Unmarshal([]byte(str), &filterMap)
  401. if err != nil {
  402. return result, err
  403. }
  404. filter := ""
  405. if common.ObjToString(filterMap["FilterId"]) != "" {
  406. //查询搜索条件
  407. queryMap, ok := config.MQFW.FindById("export_search", common.ObjToString(filterMap["FilterId"]), nil)
  408. if queryMap == nil && !ok {
  409. return result, errors.New("查询export_search出错")
  410. }
  411. _filter, _ := json.Marshal(queryMap)
  412. filter = string(_filter)
  413. } else {
  414. queryMap := map[string]interface{}{}
  415. for k, v := range filterMap {
  416. if k == "Area" {
  417. queryMap["area"] = v
  418. }
  419. if k == "Buyerclass" {
  420. queryMap["buyerclass"] = v
  421. }
  422. if k == "Time" {
  423. queryMap["publishtime"] = v
  424. }
  425. }
  426. _filter, _ := json.Marshal(queryMap)
  427. filter = string(_filter)
  428. }
  429. userId := common.Int64All((*exportLog)["user_id"])
  430. //exportTime := common.ObjToString((*exportLog)["export_time"])
  431. phone := common.ObjToString((*exportLog)["phone"])
  432. //查询用户信息
  433. userData, ok := config.MQFW.FindOne("user", map[string]interface{}{"s_phone": phone})
  434. if ok && userData != nil && len(*userData) > 0 {
  435. (*exportLog)["userName"] = (*userData)["s_nickname"]
  436. //(*exportLog)["email"] = (*userData)["s_email"]
  437. } else {
  438. userDatas, oks := config.MQFW.FindOne("user", map[string]interface{}{"s_m_phone": phone})
  439. if oks && userDatas != nil && len(*userDatas) > 0 {
  440. (*exportLog)["userName"] = (*userDatas)["s_nickname"]
  441. //(*exportLog)["email"] = (*userDatas)["s_email"]
  442. }
  443. }
  444. //查询用户每日导出限额
  445. todayLimit, exportNums := 0, 0
  446. limitData := config.JysqlDB.FindOne("entniche_export_limit", map[string]interface{}{"user_id": userId}, ``, "")
  447. if limitData != nil && len(*limitData) > 0 {
  448. //查询企业信息
  449. entData := config.JysqlDB.FindOne("entniche_info", map[string]interface{}{"id": common.IntAll((*exportLog)["ent_id"])}, `name,phone`, "")
  450. if entData != nil && len(*entData) > 0 {
  451. (*exportLog)["entName"] = (*entData)["name"]
  452. entPhone := common.ObjToString((*entData)["phone"])
  453. //根据企业名称和企业手机号查询商机管理企业剩余条数
  454. (*exportLog)["balance"] = getCurrEntCount(common.ObjToString((*entData)["name"]), entPhone)
  455. }
  456. todayLimit = common.IntAll((*limitData)["data_limit"]) //每日限额
  457. exportNums = common.IntAll((*limitData)["export_nums"]) //今日已导出
  458. }
  459. result = ExportLog{
  460. UserName: common.ObjToString((*exportLog)["userName"]),
  461. Email: common.ObjToString((*exportLog)["export_mail"]),
  462. Phone: common.ObjToString((*exportLog)["phone"]),
  463. CompanyName: common.ObjToString((*exportLog)["entName"]),
  464. PayWay: 1,
  465. DataType: "高级字段包",
  466. Balance: common.IntAll((*exportLog)["balance"]),
  467. ExportNum: common.IntAll((*exportLog)["export_num"]),
  468. RepetitionNum: common.IntAll((*exportLog)["export_num"]) - common.IntAll((*exportLog)["deduct_num"]),
  469. DeductNum: common.IntAll((*exportLog)["deduct_num"]),
  470. DayLimit: todayLimit,
  471. DayExportNum: exportNums,
  472. Filter: filter,
  473. DownloadUrl: config.SysConfigs.JyWebDomain + common.ObjToString((*exportLog)["download_url"]),
  474. }
  475. return result, nil
  476. }
  477. } else {
  478. //查询流量包导出记录
  479. packExportLog := config.JysqlDB.FindOne("datapacket_record", map[string]interface{}{"id": id}, ``, "")
  480. if packExportLog != nil && len(*packExportLog) > 0 {
  481. userId := common.ObjToString((*packExportLog)["useid"]) //user表数据id
  482. queryId := common.ObjToString((*packExportLog)["query_id"])
  483. //查询用户信息
  484. userData, oks := config.MQFW.FindById("user", userId, nil)
  485. if userData != nil && len(*userData) > 0 && oks {
  486. (*packExportLog)["userName"] = (*userData)["s_nickname"]
  487. if common.ObjToString((*userData)["s_phone"]) == "" {
  488. (*packExportLog)["phone"] = (*userData)["s_m_phone"]
  489. } else {
  490. (*packExportLog)["phone"] = (*userData)["s_phone"]
  491. }
  492. }
  493. //查询搜索条件
  494. queryData, ok := config.MQFW.FindById("export_search", queryId, nil)
  495. if queryData == nil && !ok {
  496. return result, errors.New("查询export_search出错")
  497. }
  498. _filter, _ := json.Marshal(queryData)
  499. filter := string(_filter)
  500. if common.IntAll((*packExportLog)["type"]) == 1 { //单日限量包
  501. todayLimit := 0
  502. balance := 0
  503. //todayExport := 0
  504. //查询用户单日限量额度
  505. data := config.JysqlDB.SelectBySql("SELECT s.s_count_month,u.i_frequency FROM bigmember_service_user u LEFT JOIN bigmember_service s ON u.s_serviceid = s.id WHERE u.s_userid = ? and (s.id = 17 or s.id = 18)", userId)
  506. if data != nil && len(*data) > 0 {
  507. todayLimit = common.IntAll((*data)[0]["i_frequency"])
  508. //计算今日已导出
  509. //frequency = common.IntAll((*data)[0]["i_frequency"]) //今日剩余数量
  510. //todayExport = todayLimit - frequency //今日已导出数量
  511. }
  512. //今日已导出数量
  513. dayUse := redis.GetInt(RedisCacheDb, fmt.Sprintf(DataPacketUsedNum, common.ObjToString((*packExportLog)["master_id"]), time.Now().Day()))
  514. balance = todayLimit - dayUse
  515. //查询企业名称
  516. companyName := ""
  517. entInfo, ok := config.MQFW.FindOne("member", map[string]interface{}{"userid": userId})
  518. if entInfo != nil && len(*entInfo) > 0 && ok {
  519. companyName = common.ObjToString((*entInfo)["entname"])
  520. }
  521. result = ExportLog{
  522. UserName: common.ObjToString((*packExportLog)["userName"]),
  523. Email: common.ObjToString((*packExportLog)["mail"]),
  524. Phone: common.ObjToString((*packExportLog)["phone"]),
  525. CompanyName: companyName,
  526. PayWay: 2,
  527. //DataType: "高级字段包",
  528. Balance: balance,
  529. ExportNum: common.IntAll((*packExportLog)["export_num"]),
  530. RepetitionNum: common.IntAll((*packExportLog)["export_num"]) - common.IntAll((*packExportLog)["deduct_num"]),
  531. DeductNum: common.IntAll((*packExportLog)["deduct_num"]),
  532. DayLimit: todayLimit,
  533. DayExportNum: dayUse,
  534. DownloadUrl: config.SysConfigs.JyWebDomain + common.ObjToString((*packExportLog)["path"]),
  535. Filter: filter,
  536. }
  537. if common.IntAll((*packExportLog)["isSenior"]) == 1 {
  538. result.DataType = "标准字段包"
  539. } else {
  540. result.DataType = "高级字段包"
  541. }
  542. return result, nil
  543. } else { //数据流量包
  544. balance := 0 //剩余量
  545. resourceType := ""
  546. if common.IntAll((*packExportLog)["isSenior"]) == 1 {
  547. resourceType = "标准字段包"
  548. } else {
  549. resourceType = "高级字段包"
  550. }
  551. //调用资源中台服务获取剩余量
  552. resMap, err := CommonPost(config.SysConfigs.FindBalance, url.Values{
  553. "accountId": []string{userId}, //账户标识*
  554. })
  555. if err != nil {
  556. return result, err
  557. }
  558. listObj, ok := resMap["data"].([]interface{})
  559. if !ok {
  560. return result, fmt.Errorf("packAccountQuery 请求中台返回数据结构异常")
  561. }
  562. for _, v := range listObj {
  563. obj, ok := v.(map[string]interface{})
  564. if !ok {
  565. return result, fmt.Errorf("packAccountQuery 请求中台返回数据结构异常")
  566. }
  567. if common.ObjToString(obj["resourceType"]) == resourceType {
  568. balance = common.IntAll(obj["number"])
  569. break
  570. }
  571. }
  572. result = ExportLog{
  573. UserName: common.ObjToString((*packExportLog)["userName"]),
  574. Email: common.ObjToString((*packExportLog)["mail"]),
  575. Phone: common.ObjToString((*packExportLog)["phone"]),
  576. CompanyName: "",
  577. PayWay: 3,
  578. DataType: resourceType,
  579. Balance: balance,
  580. ExportNum: common.IntAll((*packExportLog)["export_num"]),
  581. RepetitionNum: common.IntAll((*packExportLog)["export_num"]) - common.IntAll((*packExportLog)["deduct_num"]),
  582. DeductNum: common.IntAll((*packExportLog)["deduct_num"]),
  583. DayLimit: 0,
  584. DayExportNum: 0,
  585. DownloadUrl: config.SysConfigs.JyWebDomain + common.ObjToString((*packExportLog)["path"]),
  586. Filter: filter,
  587. }
  588. return result, nil
  589. }
  590. }
  591. }
  592. return result, errors.New("查询数据出错")
  593. }