user_list.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. package user_merchant
  2. import (
  3. "context"
  4. "fmt"
  5. "gd_management/apis"
  6. "gd_management/errors"
  7. "strings"
  8. "time"
  9. "gd_management/common.in/utils"
  10. "github.com/astaxie/beego/orm"
  11. "go.uber.org/zap"
  12. )
  13. var PAGESIZE = 10
  14. func getUserMerchantListSql(apiIds string, dataApiId int64) string {
  15. sql := `SELECT
  16. a.name,
  17. a.phone,
  18. a.email,
  19. b.id,
  20. b.merchant_name,
  21. b.social_code,
  22. b.address,
  23. b.contact_name,
  24. b.contact_number,
  25. b.merchant_license,
  26. b.auth_status,
  27. b.app_key,
  28. b.app_secret,
  29. b.app_password,
  30. b.warning_enable,
  31. (
  32. SELECT
  33. count( 1 )
  34. FROM
  35. t_gd_data_api_order
  36. WHERE
  37. merchant_id = b.id
  38. AND combo_id IN (
  39. SELECT
  40. id
  41. FROM
  42. t_gd_data_api_combo
  43. WHERE
  44. query_type_id IN ( SELECT id FROM t_gd_data_api_query_type WHERE data_api_id IN ( SELECT id FROM t_gd_data_api WHERE data_api_type = 0 ) )
  45. )
  46. ) AS datacount,
  47. (
  48. SELECT
  49. count( 1 )
  50. FROM
  51. t_gd_data_api_order
  52. WHERE
  53. merchant_id = b.id
  54. AND combo_id IN (
  55. SELECT
  56. id
  57. FROM
  58. t_gd_data_api_combo
  59. WHERE
  60. query_type_id IN ( SELECT id FROM t_gd_data_api_query_type WHERE data_api_id IN ( SELECT id FROM t_gd_data_api WHERE data_api_type = 1 ) )
  61. )
  62. ) AS h5count,
  63. ( SELECT sum( goods_price ) FROM t_gd_data_api_order WHERE merchant_id = b.id AND STATUS = 2 ) AS totalcost
  64. FROM
  65. t_gd_user AS a
  66. INNER JOIN t_gd_merchants AS b ON a.id = b.user_id AND b.auth_status=1`
  67. if apiIds != "" {
  68. sql = `SELECT
  69. a.name,
  70. a.phone,
  71. a.email,
  72. d.api_id,
  73. b.id,
  74. b.merchant_name,
  75. b.social_code,
  76. b.address,
  77. b.contact_name,
  78. b.contact_number,
  79. b.merchant_license,
  80. b.auth_status,
  81. b.app_key,
  82. b.app_secret,
  83. b.app_password,
  84. b.warning_enable,
  85. (
  86. SELECT
  87. count( 1 )
  88. FROM
  89. t_gd_data_api_order
  90. WHERE
  91. merchant_id = b.id
  92. AND combo_id IN (
  93. SELECT
  94. id
  95. FROM
  96. t_gd_data_api_combo
  97. WHERE
  98. query_type_id IN ( SELECT id FROM t_gd_data_api_query_type WHERE data_api_id IN ( SELECT id FROM t_gd_data_api WHERE data_api_type = 0 ) )
  99. )
  100. ) AS datacount,
  101. (
  102. SELECT
  103. count( 1 )
  104. FROM
  105. t_gd_data_api_order
  106. WHERE
  107. merchant_id = b.id
  108. AND combo_id IN (
  109. SELECT
  110. id
  111. FROM
  112. t_gd_data_api_combo
  113. WHERE
  114. query_type_id IN ( SELECT id FROM t_gd_data_api_query_type WHERE data_api_id IN ( SELECT id FROM t_gd_data_api WHERE data_api_type = 1 ) )
  115. )
  116. ) AS h5count,
  117. ( SELECT sum( goods_price ) FROM t_gd_data_api_order WHERE merchant_id = b.id AND STATUS = 2 ) AS totalcost
  118. FROM
  119. t_gd_user AS a
  120. INNER JOIN t_gd_merchants AS b ON a.id = b.user_id
  121. inner join t_gd_merchant_data_api as c on c.merchant_id = b.id
  122. inner join t_gd_merchant_child_data_api as d on d.merchant_data_api_id=c.id
  123. where d.api_id in(` + apiIds + `)`
  124. return sql
  125. }
  126. if dataApiId > 0 {
  127. sql = `SELECT
  128. a.name,
  129. a.phone,
  130. a.email,
  131. b.id,
  132. b.merchant_name,
  133. b.social_code,
  134. b.address,
  135. b.contact_name,
  136. b.contact_number,
  137. b.merchant_license,
  138. b.auth_status,
  139. b.app_key,
  140. b.app_secret,
  141. b.app_password,
  142. b.warning_enable,
  143. (
  144. SELECT
  145. count( 1 )
  146. FROM
  147. t_gd_data_api_order
  148. WHERE
  149. merchant_id = b.id
  150. AND combo_id IN (
  151. SELECT
  152. id
  153. FROM
  154. t_gd_data_api_combo
  155. WHERE
  156. query_type_id IN ( SELECT id FROM t_gd_data_api_query_type WHERE data_api_id IN ( SELECT id FROM t_gd_data_api WHERE data_api_type = 0 ) )
  157. )
  158. ) AS datacount,
  159. (
  160. SELECT
  161. count( 1 )
  162. FROM
  163. t_gd_data_api_order
  164. WHERE
  165. merchant_id = b.id
  166. AND combo_id IN (
  167. SELECT
  168. id
  169. FROM
  170. t_gd_data_api_combo
  171. WHERE
  172. query_type_id IN ( SELECT id FROM t_gd_data_api_query_type WHERE data_api_id IN ( SELECT id FROM t_gd_data_api WHERE data_api_type = 1 ) )
  173. )
  174. ) AS h5count,
  175. ( SELECT sum( goods_price ) FROM t_gd_data_api_order WHERE merchant_id = b.id AND STATUS = 2 ) AS totalcost
  176. FROM
  177. t_gd_user AS a
  178. INNER JOIN t_gd_merchants AS b ON a.id = b.user_id
  179. inner join t_gd_merchant_data_api as c on c.merchant_id = b.id
  180. inner join t_gd_data_api_query_type as d on d.id = c.query_type_id
  181. where d.data_api_id = ` + fmt.Sprintf("%d", dataApiId) + ``
  182. return sql
  183. }
  184. return sql
  185. }
  186. func isContainApiIds(list []apis.UserMerchantItem, apiIds string) bool {
  187. array := strings.Split(apiIds, ",")
  188. for _, apiId := range array {
  189. exist := false
  190. for _, v := range list {
  191. if fmt.Sprintf("%d", v.ApiId) == apiId {
  192. exist = true
  193. break
  194. }
  195. }
  196. if exist == false {
  197. return false
  198. }
  199. }
  200. return true
  201. }
  202. func clearMerchantList(reply *apis.ManagementGetUserMerchantListReply, apiIds string) {
  203. m := map[int64][]apis.UserMerchantItem{}
  204. newList := []apis.UserMerchantItem{}
  205. for _, v := range reply.MerchantList {
  206. m[v.MerchantId] = append(m[v.MerchantId], v)
  207. }
  208. for _, v := range m {
  209. if isContainApiIds(v, apiIds) {
  210. newList = append(newList, v[0])
  211. }
  212. }
  213. reply.MerchantList = newList
  214. }
  215. func ManagementGetUserMerchantList(ctx context.Context, req *apis.ManagementGetUserMerchantListReq, reply *apis.ManagementGetUserMerchantListReply) (err error) {
  216. defer func() {
  217. if err != nil {
  218. return
  219. }
  220. if req.ApiIds == "" {
  221. return
  222. }
  223. clearMerchantList(reply, req.ApiIds)
  224. }()
  225. if req.PageNumber > 0 {
  226. req.PageNumber -= 1
  227. } else {
  228. req.PageNumber = 0
  229. }
  230. sql := getUserMerchantListSql(req.ApiIds, req.DataApiId)
  231. var maps []orm.Params
  232. o := orm.NewOrm()
  233. if req.IsAll {
  234. _, err = o.Raw(sql).Values(&maps)
  235. if err != nil {
  236. l.Error("func",
  237. zap.String("call", "ManagementGetUserMerchantList"),
  238. zap.String("args", utils.MarshalJsonString(req)),
  239. zap.String("error", err.Error()))
  240. return errors.DataBaseError
  241. }
  242. } else {
  243. sql = sql + " LIMIT ? OFFSET ?"
  244. _, err = o.Raw(sql, PAGESIZE, req.PageNumber*PAGESIZE).Values(&maps)
  245. if err != nil {
  246. l.Error("func",
  247. zap.String("call", "ManagementGetUserMerchantList"),
  248. zap.String("args", utils.MarshalJsonString(req)),
  249. zap.String("error", err.Error()))
  250. return errors.DataBaseError
  251. }
  252. err = o.Raw("select count(1) from t_gd_merchants where auth_status=1").QueryRow(&reply.Total)
  253. if err != nil {
  254. l.Error("func",
  255. zap.String("call", "ManagementGetUserMerchantList"),
  256. zap.String("args", utils.MarshalJsonString(req)),
  257. zap.String("error", err.Error()))
  258. return errors.DataBaseError
  259. }
  260. reply.PageSize = PAGESIZE
  261. reply.PageNumber = req.PageNumber + 1
  262. }
  263. for _, v := range maps {
  264. iterm := apis.UserMerchantItem{}
  265. iterm.DataApiOrderCount = int(ormStringToInt(v, "datacount"))
  266. iterm.H5ApiOrderCount = int(ormStringToInt(v, "h5count"))
  267. iterm.TotalCost = ormStringToFloat(v, "totalcost")
  268. iterm.User.Name, _ = v["name"].(string)
  269. iterm.User.Phone, _ = v["phone"].(string)
  270. iterm.User.Email, _ = v["email"].(string)
  271. iterm.Merchant.MerchantName, _ = v["merchant_name"].(string)
  272. iterm.Merchant.SocialCode, _ = v["social_code"].(string)
  273. iterm.Merchant.Address, _ = v["address"].(string)
  274. iterm.Merchant.ContactName, _ = v["contact_name"].(string)
  275. iterm.Merchant.ContactNumber, _ = v["contact_number"].(string)
  276. iterm.Merchant.AuthStatus = int(ormStringToInt(v, "auth_status"))
  277. iterm.Merchant.MerchantLicense, _ = v["merchant_license"].(string)
  278. iterm.Merchant.AppKey, _ = v["app_key"].(string)
  279. iterm.Merchant.AppSecret, _ = v["app_secret"].(string)
  280. iterm.Merchant.AppPassword, _ = v["app_password"].(string)
  281. iterm.Merchant.AppPassword = decAppPassword(iterm.Merchant.AppPassword)
  282. iterm.MerchantId = ormStringToInt(v, "id")
  283. iterm.ApiId = ormStringToInt(v, "api_id")
  284. if v["warning_enable"] == "1" {
  285. iterm.WarningEnable = true
  286. }
  287. reply.MerchantList = append(reply.MerchantList, iterm)
  288. }
  289. l.Debug(utils.MarshalJsonString(req, reply))
  290. return
  291. }
  292. func GetStatus(merchantType int, arrearage, balance float64) string {
  293. if merchantType == 2 {
  294. if arrearage+balance <= 10 {
  295. return "余额不足"
  296. } else {
  297. return "正常"
  298. }
  299. } else if merchantType == 3 {
  300. if arrearage != 0 {
  301. if arrearage+balance <= 10 {
  302. return "余额不足"
  303. } else {
  304. return "正常"
  305. }
  306. } else {
  307. return "正常"
  308. }
  309. }
  310. return ""
  311. }
  312. func ManagementGetUserMerchantListNew(ctx context.Context, req *apis.ManagementGetUserMerchantListNewReq, reply *apis.ManagementGetUserMerchantListNewReply) (err error) {
  313. if req.PageNumber > 0 {
  314. req.PageNumber -= 1
  315. } else {
  316. req.PageNumber = 0
  317. }
  318. if req.Status == "" && req.StatusCode > 0 {
  319. for k, v := range StatusMap {
  320. if req.StatusCode == v {
  321. req.Status = k
  322. break
  323. }
  324. }
  325. }
  326. sql := `SELECT
  327. b.id,
  328. a.name,
  329. a.phone,
  330. a.email,
  331. b.merchant_name,
  332. b.email as memail,
  333. b.company_name,
  334. b.social_code,
  335. b.address,
  336. b.contact_name,
  337. b.contact_number,
  338. b.merchant_license,
  339. b.ip_whitelist,
  340. b.auth_status,
  341. b.app_key,
  342. b.app_secret,
  343. b.app_password,
  344. b.warning_enable,
  345. b.is_http_code,
  346. b.merchant_type,
  347. b.balance,
  348. b.arrearage
  349. FROM
  350. t_gd_user AS a
  351. INNER JOIN t_gd_merchants AS b ON a.id = b.user_id where b.auth_status=1`
  352. if req.MerchantId > 0 {
  353. sql = fmt.Sprintf("%s and b.id=%d", sql, req.MerchantId)
  354. }
  355. if req.MerchantType > 0 {
  356. sql = fmt.Sprintf("%s and b.merchant_type=%d", sql, req.MerchantType)
  357. }
  358. sql = fmt.Sprintf("%s order by b.id desc", sql)
  359. o := orm.NewOrm()
  360. if req.IsAll {
  361. _, err = o.Raw(sql).QueryRows(&reply.MerchantList)
  362. if err != nil {
  363. l.Error("func",
  364. zap.String("call", "ManagementGetUserMerchantList"),
  365. zap.String("args", utils.MarshalJsonString(req)),
  366. zap.String("error", err.Error()))
  367. return errors.DataBaseError
  368. }
  369. } else {
  370. if req.Status == "" && req.DataApiId == "" {
  371. sql = sql + " LIMIT ? OFFSET ?"
  372. countSql := `select count(1) from t_gd_merchants where auth_status=1`
  373. if req.MerchantId > 0 {
  374. countSql = fmt.Sprintf("%s and id=%d", countSql, req.MerchantId)
  375. }
  376. if req.MerchantType > 0 {
  377. countSql = fmt.Sprintf("%s and merchant_type=%d", countSql, req.MerchantType)
  378. }
  379. err = o.Raw(countSql).QueryRow(&reply.Total)
  380. if err != nil {
  381. l.Error("func",
  382. zap.String("call", "ManagementGetUserMerchantList"),
  383. zap.String("args", utils.MarshalJsonString(req)),
  384. zap.String("error", err.Error()))
  385. return errors.DataBaseError
  386. }
  387. _, err = o.Raw(sql, PAGESIZE, req.PageNumber*PAGESIZE).QueryRows(&reply.MerchantList)
  388. if err != nil {
  389. l.Error("func",
  390. zap.String("call", "ManagementGetUserMerchantList"),
  391. zap.String("args", utils.MarshalJsonString(req)),
  392. zap.String("error", err.Error()))
  393. return errors.DataBaseError
  394. }
  395. } else {
  396. _, err = o.Raw(sql).QueryRows(&reply.MerchantList)
  397. if err != nil {
  398. l.Error("func",
  399. zap.String("call", "ManagementGetUserMerchantList"),
  400. zap.String("args", utils.MarshalJsonString(req)),
  401. zap.String("error", err.Error()))
  402. return errors.DataBaseError
  403. }
  404. }
  405. }
  406. for index, _ := range reply.MerchantList {
  407. reply.MerchantList[index].AppPassword = decAppPassword(reply.MerchantList[index].AppPassword)
  408. sql := `SELECT ( SELECT data_api_name FROM t_gd_data_api WHERE id =
  409. (select data_api_id from t_gd_data_api_query_type where id = a.query_type_id)) AS data_api_name,
  410. ( SELECT id FROM t_gd_data_api WHERE id =
  411. (select data_api_id from t_gd_data_api_query_type where id = a.query_type_id)) AS id,
  412. case a.combo_type when 2 then ( SELECT b.count FROM t_gd_api_access_count AS b WHERE
  413. b.merchant_data_api_id = a.id and create_time = ? limit 1) else ( SELECT IFNULL(b.count,0)
  414. FROM t_gd_api_access_count AS b WHERE b.merchant_data_api_id = a.id and create_time = ? limit 1)
  415. end as use_count,a.combo_type, a.count_per_day as day_count,a.count as total_count,a.end_time,a.query_type_id as query_type_id,a.alias FROM t_gd_merchant_data_api as a where a.merchant_id = ?`
  416. zeroDate := "0000-00-00"
  417. nowDate := time.Now().Format("2006-01-02")
  418. _, err = o.Raw(sql, nowDate, zeroDate, reply.MerchantList[index].Id).QueryRows(&reply.MerchantList[index].DataApiInfo)
  419. if err != nil {
  420. if err == orm.ErrNoRows {
  421. continue
  422. }
  423. l.Error("func",
  424. zap.String("call", "ManagementGetUserMerchantList"),
  425. zap.String("args", utils.MarshalJsonString(req)),
  426. zap.String("error", err.Error()))
  427. return errors.DataBaseError
  428. }
  429. now := time.Now().Unix()
  430. for i, _ := range reply.MerchantList[index].DataApiInfo {
  431. if reply.MerchantList[index].DataApiInfo[i].ComboType == 2 {
  432. if reply.MerchantList[index].DataApiInfo[i].EndTime > int64(now) {
  433. if (reply.MerchantList[index].DataApiInfo[i].EndTime-int64(now))%(3600*24) == 0 {
  434. reply.MerchantList[index].DataApiInfo[i].LeftDay = int64((reply.MerchantList[index].DataApiInfo[i].EndTime - int64(now)) / (3600 * 24))
  435. } else {
  436. reply.MerchantList[index].DataApiInfo[i].LeftDay = int64((reply.MerchantList[index].DataApiInfo[i].EndTime-int64(now))/(3600*24)) + 1
  437. }
  438. } else {
  439. reply.MerchantList[index].DataApiInfo[i].LeftDay = 0
  440. }
  441. reply.MerchantList[index].DataApiInfo[i].LeftCount = reply.MerchantList[index].DataApiInfo[i].DayCount - reply.MerchantList[index].DataApiInfo[i].UseCount
  442. if reply.MerchantList[index].DataApiInfo[i].LeftDay > 0 && reply.MerchantList[index].DataApiInfo[i].LeftDay <= 1 {
  443. reply.MerchantList[index].DataApiInfo[i].Status = "即将过期"
  444. } else if reply.MerchantList[index].DataApiInfo[i].LeftDay <= 0 {
  445. reply.MerchantList[index].DataApiInfo[i].Status = "过期"
  446. } else if reply.MerchantList[index].DataApiInfo[i].LeftCount <= 0 {
  447. reply.MerchantList[index].DataApiInfo[i].Status = "达到单日上限"
  448. } else if reply.MerchantList[index].DataApiInfo[i].LeftCount > 0 && reply.MerchantList[index].DataApiInfo[i].LeftCount <= 10 {
  449. reply.MerchantList[index].DataApiInfo[i].Status = "即将达到单日上限"
  450. } else {
  451. reply.MerchantList[index].DataApiInfo[i].Status = "正常"
  452. }
  453. } else {
  454. reply.MerchantList[index].DataApiInfo[i].LeftCount = reply.MerchantList[index].DataApiInfo[i].TotalCount - reply.MerchantList[index].DataApiInfo[i].UseCount
  455. if reply.MerchantList[index].DataApiInfo[i].LeftCount <= 0 {
  456. reply.MerchantList[index].DataApiInfo[i].Status = "用量耗尽"
  457. } else if reply.MerchantList[index].DataApiInfo[i].LeftCount > 0 && reply.MerchantList[index].DataApiInfo[i].LeftCount <= 10 {
  458. reply.MerchantList[index].DataApiInfo[i].Status = "用量即将耗尽"
  459. } else {
  460. reply.MerchantList[index].DataApiInfo[i].Status = "正常"
  461. }
  462. if reply.MerchantList[index].DataApiInfo[i].ComboType == 0 {
  463. var is_free int
  464. o.Raw("select is_free from t_gd_data_api_query_type where id = ?", reply.MerchantList[index].DataApiInfo[i].QueryTypeId).QueryRow(&is_free)
  465. if is_free == 1 {
  466. reply.MerchantList[index].DataApiInfo[i].Status = "正常"
  467. }
  468. }
  469. }
  470. if reply.MerchantList[index].DataApiInfo[i].LeftDay < 0 {
  471. reply.MerchantList[index].DataApiInfo[i].LeftDay = 0
  472. }
  473. if reply.MerchantList[index].DataApiInfo[i].LeftCount < 0 {
  474. reply.MerchantList[index].DataApiInfo[i].LeftCount = 0
  475. }
  476. nowStats := GetStatus(reply.MerchantList[index].MerchantType, reply.MerchantList[index].Arrearage, reply.MerchantList[index].Balance)
  477. if nowStats != "" {
  478. reply.MerchantList[index].DataApiInfo[i].Status = nowStats
  479. }
  480. reply.MerchantList[index].DataApiInfo[i].StatusCode, _ = StatusMap[reply.MerchantList[index].DataApiInfo[i].Status]
  481. }
  482. }
  483. if req.DataApiId == "" && req.Status == "" {
  484. reply.PageSize = PAGESIZE
  485. reply.PageNumber = req.PageNumber + 1
  486. return
  487. }
  488. newList := []apis.UserMerchantItemNew{}
  489. total := 0
  490. for _, v := range reply.MerchantList {
  491. if len(v.DataApiInfo) == 0 {
  492. //newList = append(newList, v)
  493. continue
  494. }
  495. items := []apis.DataApiInfo{}
  496. for _, d := range v.DataApiInfo {
  497. if req.DataApiId != "" && req.Status != "" {
  498. if req.DataApiId == fmt.Sprintf("%d", d.Id) && req.Status == d.Status {
  499. items = append(items, d)
  500. }
  501. } else if req.DataApiId != "" && req.DataApiId == fmt.Sprintf("%d", d.Id) {
  502. items = append(items, d)
  503. } else {
  504. if req.Status == d.Status {
  505. items = append(items, d)
  506. }
  507. }
  508. }
  509. if len(items) > 0 {
  510. if total >= req.PageNumber*PAGESIZE && total < (req.PageNumber+1)*PAGESIZE {
  511. v.DataApiInfo = items
  512. newList = append(newList, v)
  513. }
  514. total++
  515. }
  516. }
  517. reply.Total = total
  518. reply.MerchantList = newList
  519. reply.PageSize = PAGESIZE
  520. reply.PageNumber = req.PageNumber + 1
  521. l.Debug(utils.MarshalJsonString(req, reply))
  522. return
  523. }