getService.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. package user
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "github.com/gogf/gf/v2/database/gdb"
  8. "github.com/gogf/gf/v2/frame/g"
  9. "github.com/gogf/gf/v2/util/gconv"
  10. "jyOrderManager/internal/jyutil"
  11. "jyOrderManager/internal/model"
  12. "strings"
  13. "time"
  14. )
  15. func GetUserService(ctx context.Context, param model.UserServiceParams) (interface{}, error) {
  16. var (
  17. serviceArrMap []map[string]interface{}
  18. userData *map[string]interface{}
  19. vipExist bool
  20. whereEntTime string
  21. entRec gdb.Record
  22. )
  23. if param.Phone != "" {
  24. //大会员超||级订阅 判断是否有待生效订单
  25. if (strings.Contains(param.ProductType, "大会员") || strings.Contains(param.ProductType, "订阅")) && param.BuySubject == "1" {
  26. sqlStr := fmt.Sprintf(`SELECT * FROM jy_order_detail jod
  27. INNER JOIN dataexport_order de on jod.order_code = de.order_code
  28. WHERE jod.status = 1 and de.order_status not in (-2,-3,-4) and de.del_status != 1 and jod.is_service_open = 0 and de.buy_subject =1 and de.user_phone = '%s' and jod.product_type like '%s'`, param.Phone, common.If(param.ProductType == "VIP订阅", "%VIP订阅%", "%大会员%"))
  29. if param.OrderCode != "" {
  30. sqlStr += fmt.Sprintf(" AND order_code != '%s'", param.OrderCode)
  31. }
  32. //检验订单是否可以进行传创建
  33. order, _ := g.DB().Query(ctx, sqlStr)
  34. if !order.IsEmpty() && order.Len() > 0 {
  35. return map[string]interface{}{
  36. "willEffect": true,
  37. "userData": userData,
  38. "serviceArrMap": nil,
  39. }, nil
  40. }
  41. }
  42. //手机号是否注册
  43. userData, _ = jyutil.MG.DB().FindOne("user", map[string]interface{}{
  44. "$or": []map[string]interface{}{
  45. {"s_phone": param.Phone},
  46. {"s_m_phone": param.Phone},
  47. },
  48. })
  49. if userData == nil || len(*userData) == 0 {
  50. return map[string]interface{}{
  51. "willEffect": false,
  52. //"userData": userData,
  53. "serviceArrMap": nil,
  54. }, nil
  55. }
  56. if param.BuySubject == "2" {
  57. if param.EntName == "" {
  58. return nil, errors.New("企业名称校验异常")
  59. }
  60. if param.ServiceType == 3 {
  61. whereEntTime = fmt.Sprintf(` and a.end_time>'%s' `, time.Now().Format("2006-01-02 15:04:05"))
  62. }
  63. entRec, _ = g.DB().GetOne(ctx, fmt.Sprintf(`SELECT a.id,b.id as entUserId FROM entniche_info a
  64. inner JOIN entniche_user b on a.id = b.ent_id
  65. WHERE b.phone = '%s' and a.name = '%s'`, param.Phone, param.EntName))
  66. }
  67. var whereSql string
  68. if param.BuySubject == "1" {
  69. whereSql = fmt.Sprintf("dor.user_phone = '%s' and dor.buy_subject =1 ", param.Phone)
  70. } else {
  71. whereSql = fmt.Sprintf("dor.user_phone = '%s' and dor.buy_subject =2 and dor.company_name ='%s' ", param.Phone, param.EntName)
  72. }
  73. if param.ServiceType == 3 {
  74. whereSql += fmt.Sprintf(" and jod.service_starttime<'%s' and jod.service_endtime > '%s' and jod.product_type = '%s' ", time.Now().Format("2006-01-02 15:04:05"), time.Now().Format("2006-01-02 15:04:05"), param.ProductType)
  75. }
  76. orderDetailType, _ := g.DB().Query(ctx, fmt.Sprintf(`SELECT DISTINCT(jod.product_type) FROM jy_order_detail jod
  77. INNER JOIN dataexport_order dor on jod.order_code = dor.order_code
  78. INNER JOIN jy_product_item jpi on jod.product_code = jpi.code
  79. WHERE %s and jod.status =1 order by dor.create_time desc`, whereSql))
  80. if orderDetailType.IsEmpty() {
  81. return map[string]interface{}{
  82. "willEffect": false,
  83. //"userData": userData,
  84. "serviceArrMap": nil,
  85. }, nil
  86. }
  87. for _, m := range orderDetailType.List() {
  88. switch gconv.String(m["product_type"]) {
  89. case "大会员":
  90. if param.BuySubject == "1" {
  91. var (
  92. linkedId, comboId, areaCount, paySubNum, gifUserCount, payUserCount int
  93. eTime, sTime, pCode string
  94. orderArr []map[string]interface{}
  95. serviceList []int
  96. )
  97. startTime := common.Int64All((*userData)["i_member_starttime"])
  98. if startTime > 0 {
  99. sTime = time.Unix(startTime, 0).Format("2006-01-02 15:04:05")
  100. }
  101. bigEnd := common.Int64All((*userData)["i_member_endtime"])
  102. if bigEnd > 0 {
  103. eTime = time.Unix(bigEnd, 0).Format("2006-01-02 15:04:05")
  104. }
  105. orderData, _ := g.DB().Query(ctx, `SELECT a.product_code,a.id,a.service_type,a.order_code,b.create_time,a.service_starttime,a.service_endtime,a.filter FROM jy_order_detail a
  106. INNER JOIN dataexport_order b on a.order_code = b.order_code
  107. WHERE a.status = 1 and a.is_service_open=1 and b.user_phone = ? and b.buy_subject = 1 and a.product_type like '%大会员%' ORDER BY b.create_time desc `, param.Phone)
  108. if !orderData.IsEmpty() {
  109. for _, m2 := range orderData.List() {
  110. var ttt model.JyBigProductFilter
  111. if err := gconv.Struct(m2["filter"], &ttt); err != nil {
  112. continue
  113. }
  114. if linkedId == 0 {
  115. linkedId = gconv.Int(m2["id"])
  116. pCode = gconv.String(m2["product_code"])
  117. paySubNum = ttt.FinalAccountCount
  118. if len(ttt.ServiceIds) == 0 && ttt.ComboId > 0 {
  119. combo, _ := g.DB().Ctx(ctx).GetOne(ctx, fmt.Sprintf(`SELECT * FROM bigmember_combo WHERE id = %d`, ttt.ComboId))
  120. ttt.ServiceIds = strings.Split(gconv.String(combo.Map()["s_servers"]), ",")
  121. }
  122. for _, id := range ttt.ServiceIds {
  123. serviceList = append(serviceList, gconv.Int(id))
  124. }
  125. }
  126. serviceStarttime := gconv.String(m2["service_starttime"])
  127. serviceEndtime := gconv.String(m2["service_endtime"])
  128. if time.Unix(startTime, 0).Format("2006-01-02 15:04:05") <= serviceStarttime &&
  129. time.Unix(bigEnd, 0).Format("2006-01-02 15:04:05") >= serviceEndtime && gconv.Int(m2["service_type"]) != 2 {
  130. gifUserCount += ttt.GiftAccountCount
  131. payUserCount += ttt.BuyAccountCount
  132. }
  133. orderArr = append(orderArr, map[string]interface{}{
  134. "order_code": m2["order_code"],
  135. "create_time": m2["create_time"],
  136. "service_type": m2["service_type"],
  137. "order_detail_id": m2["id"],
  138. })
  139. }
  140. }
  141. if pCode != "" {
  142. itemMap, _ := g.DB().GetOne(ctx, fmt.Sprintf(`SELECT JSON_EXTRACT(price, '$.comboId') as comboId FROM jy_product_item WHERE code = '%s';`, pCode))
  143. if !itemMap.IsEmpty() {
  144. comboId = gconv.Int(itemMap.Map()["comboId"])
  145. }
  146. }
  147. subStatus := common.IntAll((*userData)["i_member_sub_status"])
  148. memberStatus := common.IntAll((*userData)["i_member_status"])
  149. //comboId = common.If(memberStatus == 5, 0, memberStatus).(int)
  150. mainId := common.ObjToString((*userData)["s_member_mainid"])
  151. if memberJyMap, ok1 := ((*userData)["o_member_jy"]).(map[string]interface{}); ok1 {
  152. areaCount = common.IntAll(memberJyMap["i_areacount"])
  153. }
  154. if startTime >= time.Now().Unix() {
  155. vipExist = true
  156. } else if (time.Now().Unix() < bigEnd && memberStatus > 0) || startTime >= time.Now().Unix() {
  157. vipExist = true
  158. }
  159. if (subStatus == 0 && memberStatus < 0 && mainId != "") || (subStatus == 1 && memberStatus > 0 && mainId != "") {
  160. vipExist = true
  161. }
  162. if vipExist {
  163. paySubNum = common.IntAll((*userData)["i_pay_sub_num"]) + common.IntAll((*userData)["i_free_sub_num"]) + 1
  164. }
  165. var productCode string
  166. itemMap, _ := g.DB().GetOne(ctx, fmt.Sprintf(`SELECT code FROM jy_product_item WHERE JSON_EXTRACT(price, '$.comboId') = %d;`, comboId))
  167. if !itemMap.IsEmpty() {
  168. productCode = gconv.String(itemMap.Map()["code"])
  169. }
  170. serviceArrMap = append(serviceArrMap, map[string]interface{}{
  171. "vipExist": vipExist, //当前服务是否在有限期内
  172. "name": gconv.String(m["product_type"]), //产品名称
  173. "productCode": productCode, //产品code
  174. "serviceEndTime": eTime, //服务结束时间
  175. "serviceStartTime": sTime, //服务开始时间
  176. "empowerCount": paySubNum, //服务授权数
  177. "gifUserCount": gifUserCount, //赠送授权数
  178. "payUserCount": payUserCount, //收费授权数
  179. "provinceCount": areaCount, //订阅省份
  180. "buySubject": 1, //个体
  181. "comboId": comboId, //套餐id
  182. "serviceList": serviceList, //大会员服务id
  183. "linkedId": linkedId, //关联id
  184. "linkedOrder": orderArr, //关联订单
  185. })
  186. } else {
  187. if !entRec.IsEmpty() {
  188. uData := userEntService(ctx, gconv.String(entRec.Map()["id"]), whereEntTime, "%大会员%")
  189. if len(uData) > 0 {
  190. serviceArrMap = append(serviceArrMap, uData...)
  191. }
  192. }
  193. }
  194. case "VIP订阅":
  195. var (
  196. serviceList []int
  197. endTime, startTime string
  198. areaCount, linkedId int
  199. orderArr []map[string]interface{}
  200. )
  201. if param.BuySubject == "1" {
  202. var (
  203. productCode string
  204. newVip, nationwide bool
  205. )
  206. orderData, _ := g.DB().Query(ctx, `SELECT a.id,a.service_type,a.order_code,b.create_time,a.product_code FROM jy_order_detail a
  207. INNER JOIN dataexport_order b on a.order_code = b.order_code
  208. WHERE a.status = 1 and b.user_phone = ? and a.is_service_open=1 and b.buy_subject = 1 and a.product_type like '%VIP订阅%' ORDER BY b.create_time desc `, param.Phone)
  209. if !orderData.IsEmpty() {
  210. for _, m2 := range orderData.List() {
  211. if linkedId == 0 {
  212. productCode = gconv.String(m2["product_code"])
  213. linkedId = gconv.Int(m2["id"])
  214. }
  215. orderArr = append(orderArr, map[string]interface{}{
  216. "order_code": m2["order_code"],
  217. "create_time": m2["create_time"],
  218. "service_type": m2["service_type"],
  219. "order_detail_id": m2["id"],
  220. })
  221. }
  222. }
  223. vipExist = gconv.Int((*userData)["i_vip_status"]) > 0
  224. l_vip_endtime := common.Int64All((*userData)["l_vip_endtime"])
  225. if l_vip_endtime > 0 {
  226. endTime = time.Unix(l_vip_endtime, 0).Format("2006-01-02 15:04:05")
  227. }
  228. l_vip_starttime := common.Int64All((*userData)["l_vip_starttime"])
  229. if l_vip_endtime > 0 {
  230. startTime = time.Unix(l_vip_starttime, 0).Format("2006-01-02 15:04:05")
  231. }
  232. if vipExist {
  233. vipInfo := common.ObjToMap((*userData)["o_vipjy"])
  234. buySet := common.ObjToMap((*vipInfo)["o_buyset"])
  235. areaCount = common.IntAll((*buySet)["areacount"])
  236. //判断是新版超级订阅还是老版超级订阅
  237. if common.IntAll((*buySet)["upgrade"]) == 1 {
  238. newVip = true
  239. }
  240. if areaCount == -1 {
  241. nationwide = true
  242. }
  243. }
  244. //productCode := fmt.Sprintf("cjdy%03d", gconv.Int(common.If(areaCount > 15 || areaCount == -1, 16, areaCount)))
  245. serviceArrMap = append(serviceArrMap, map[string]interface{}{
  246. "vipExist": vipExist, //当前服务是否在有限期内
  247. "nationwide": nationwide,
  248. "name": gconv.String(m["product_type"]), //产品名称
  249. "serviceEndTime": endTime, //服务结束时间
  250. "serviceStartTime": startTime, //服务开始时间
  251. "productCode": productCode,
  252. "newVip": newVip,
  253. "empowerCount": 1, //服务授权数
  254. "provinceCount": areaCount, //订阅省份
  255. "buySubject": 1, //个体
  256. "serviceList": serviceList, //大会员服务id
  257. "linkedId": linkedId, //关联id
  258. "linkedOrder": orderArr, //关联订单
  259. })
  260. } else {
  261. if !entRec.IsEmpty() {
  262. uData := userEntService(ctx, gconv.String(entRec.Map()["id"]), whereEntTime, "%VIP订阅%")
  263. if len(uData) > 0 {
  264. serviceArrMap = append(serviceArrMap, uData...)
  265. }
  266. }
  267. }
  268. default:
  269. uData := SpecialService(ctx, whereSql, gconv.String(m["product_type"]), gconv.Int(param.BuySubject))
  270. if uData != nil {
  271. uData["linkedOrder"] = uData["orderArr"]
  272. delete(uData, "orderArr")
  273. serviceArrMap = append(serviceArrMap, uData)
  274. }
  275. }
  276. }
  277. }
  278. return map[string]interface{}{
  279. "willEffect": false,
  280. "userData": userData,
  281. "serviceArrMap": serviceArrMap,
  282. }, nil
  283. }
  284. func userEntService(ctx context.Context, entID string, whereEntTime, productType string) (data []map[string]interface{}) {
  285. //获取企业下的服务
  286. entService, _ := g.DB().Query(ctx, fmt.Sprintf(`SELECT * FROM entniche_wait_empower a
  287. WHERE a.ent_id = '%s' %s and a.product_type like '%s' ORDER BY a.end_time desc`, entID, whereEntTime, productType))
  288. if !entService.IsEmpty() {
  289. for _, m := range entService.List() {
  290. var (
  291. serviceList []int
  292. comboId int
  293. gifUserCount, payUserCount int
  294. productCode string
  295. vipExist bool
  296. )
  297. switch {
  298. case strings.Contains(productType, "大会员"):
  299. //大会员获取下服务最新id
  300. serviceId, _ := g.DB().GetOne(ctx, fmt.Sprintf(`SELECT b.filter FROM entniche_order a
  301. INNER JOIN jy_order_detail b on a.order_detail_id = b.id WHERE a.wait_empower_id = %d and b.status =1 ORDER BY a.create_time desc LIMIT 1`, gconv.Int(m["id"])))
  302. if !serviceId.IsEmpty() {
  303. comboId = gconv.Int(gconv.Map(serviceId.Map()["filter"])["comboId"])
  304. itemMap, _ := g.DB().GetOne(ctx, fmt.Sprintf(`SELECT code FROM jy_product_item WHERE JSON_EXTRACT(price, '$.comboId') = %d;`, comboId))
  305. if !itemMap.IsEmpty() {
  306. productCode = gconv.String(itemMap.Map()["code"])
  307. }
  308. var ttt model.JyBigProductFilter
  309. if err := gconv.Struct(serviceId.Map()["filter"], &ttt); err != nil {
  310. continue
  311. }
  312. if len(ttt.ServiceIds) == 0 && ttt.ComboId > 0 {
  313. combo, _ := g.DB().Ctx(ctx).GetOne(ctx, fmt.Sprintf(`SELECT * FROM bigmember_combo WHERE id = %d`, ttt.ComboId))
  314. if !combo.IsEmpty() {
  315. ttt.ServiceIds = strings.Split(gconv.String(combo.Map()["s_servers"]), ",")
  316. }
  317. }
  318. for _, id := range ttt.ServiceIds {
  319. if id == "12" {
  320. serviceList = append(serviceList, 23)
  321. } else if id == "19" {
  322. serviceList = append(serviceList, 4)
  323. } else {
  324. serviceList = append(serviceList, gconv.Int(id))
  325. }
  326. }
  327. }
  328. case strings.Contains(productType, "订阅"):
  329. productCode = fmt.Sprintf("cjdy%03d", gconv.Int(common.If(gconv.Int(m["province_count"]) > 15 || gconv.Int(m["province_count"]) == -1, 16, gconv.Int(m["province_count"]))))
  330. }
  331. vipExist = jyutil.StrToTime(gconv.String(m["end_time"])).Unix() > time.Now().Unix()
  332. var (
  333. orderArr []map[string]interface{}
  334. linkedId int
  335. )
  336. //获取订单
  337. entOrderService, _ := g.DB().Query(ctx, fmt.Sprintf(`SELECT b.id,b.service_type,b.order_code,c.create_time,b.service_starttime,b.service_endtime,b.filter FROM entniche_order a
  338. INNER JOIN jy_order_detail b on a.order_detail_id = b.id
  339. LEFT JOIN dataexport_order c on b.order_code = c.order_code WHERE a.wait_empower_id = %d and b.status =1 order by b.id desc`, gconv.Int(m["id"])))
  340. if !entOrderService.IsEmpty() {
  341. for _, m2 := range entOrderService.List() {
  342. if linkedId == 0 {
  343. linkedId = gconv.Int(m2["id"])
  344. }
  345. switch {
  346. case strings.Contains(productType, "大会员") && gconv.Int(m2["service_type"]) != 2:
  347. var ttt model.JyBigProductFilter
  348. if err := gconv.Struct(m2["filter"], &ttt); err != nil {
  349. continue
  350. }
  351. gifUserCount += ttt.GiftAccountCount
  352. payUserCount += ttt.BuyAccountCount
  353. case strings.Contains(productType, "订阅") && gconv.Int(m2["service_type"]) != 2:
  354. var ttt model.VipCycleFilter
  355. if err := gconv.Struct(m2["filter"], &ttt); err != nil {
  356. continue
  357. }
  358. gifUserCount += ttt.GiftAccountCount
  359. payUserCount += ttt.BuyAccountCount
  360. }
  361. orderArr = append(orderArr, map[string]interface{}{
  362. "order_code": m2["order_code"],
  363. "create_time": m2["create_time"],
  364. "service_type": m2["service_type"],
  365. "order_detail_id": m2["id"],
  366. })
  367. }
  368. }
  369. data = append(data, map[string]interface{}{
  370. "name": gconv.String(m["product_type"]), //产品名称
  371. "productCode": productCode, //产品code
  372. "vipExist": vipExist, //当前服务是否在有限期内
  373. "serviceEndTime": gconv.String(m["end_time"]), //服务结束时间
  374. "serviceStartTime": gconv.String(m["start_time"]), //服务开始时间
  375. "empowerCount": gconv.String(m["empower_count"]), //服务授权数
  376. "gifUserCount": gifUserCount, //免费服务授权数
  377. "payUserCount": payUserCount, //付费服务授权数
  378. "provinceCount": gconv.Int(m["province_count"]), //订阅省份
  379. "buySubject": 2, //个体
  380. "linkedId": linkedId, //关联id
  381. "comboId": comboId, //套餐id
  382. "serviceList": serviceList, //大会员服务id
  383. "linkedOrder": orderArr, //关联订单
  384. })
  385. }
  386. }
  387. return data
  388. }
  389. func SpecialService(ctx context.Context, phone, productType string, buySubject int) map[string]interface{} {
  390. var (
  391. linkedId int
  392. orderArr []map[string]interface{}
  393. vipExist bool
  394. endTime, startTime, productCode string
  395. )
  396. //serviceArrMap = append(serviceArrMap, map[string]interface{}{
  397. // "name": gconv.String(m["product_type"]), //产品名称
  398. // "vipExist": vipExist, //当前服务是否在有限期内
  399. // "serviceEndTime": gconv.String(m["end_time"]), //服务结束时间
  400. // "serviceStartTime": gconv.String(m["start_time"]), //服务开始时间
  401. // "empowerCount": gconv.String(m["empower_count"]), //服务授权数
  402. // "provinceCount": gconv.String(m["province_count"]), //订阅省份
  403. // "buySubject": 2, //个体
  404. // "linkedId": linkedId, //关联id
  405. // "comboId": comboId, //套餐id
  406. // "serviceList": serviceList, //大会员服务id
  407. // "linkedOrder": orderArr, //关联订单
  408. // })
  409. orderData, _ := g.DB().Query(ctx, fmt.Sprintf(`SELECT jod.product_code,jod.service_endtime,jod.id,jod.service_starttime,jod.service_type,jod.order_code,dor.create_time FROM dataexport_order dor
  410. INNER JOIN jy_order_detail jod on dor.order_code = jod.order_code
  411. WHERE %s and jod.product_type = '%s' and jod.status =1 ORDER BY dor.create_time desc `, phone, productType))
  412. if !orderData.IsEmpty() {
  413. for _, m2 := range orderData.List() {
  414. if linkedId == 0 {
  415. linkedId = gconv.Int(m2["id"])
  416. }
  417. if product_code := gconv.String(m2["product_code"]); product_code != "" {
  418. productCode = product_code
  419. }
  420. if gconv.String(m2["service_endtime"]) > time.Now().String() {
  421. vipExist = true
  422. }
  423. if gconv.String(m2["service_endtime"]) > endTime {
  424. endTime = gconv.String(m2["service_endtime"])
  425. }
  426. if gconv.String(m2["service_starttime"]) < startTime {
  427. startTime = gconv.String(m2["service_starttime"])
  428. }
  429. orderArr = append(orderArr, map[string]interface{}{
  430. "order_code": m2["order_code"],
  431. "create_time": m2["create_time"],
  432. "service_type": m2["service_type"],
  433. "order_detail_id": m2["id"],
  434. })
  435. }
  436. return map[string]interface{}{
  437. "name": productType, //产品名称
  438. "vipExist": vipExist, //当前服务是否在有限期内
  439. "serviceEndTime": common.If(endTime != "", endTime, nil), //服务结束时间
  440. "productCode": productCode,
  441. "serviceStartTime": startTime, //服务开始时间
  442. "buySubject": buySubject, //个体
  443. "empowerCount": common.If(productType == "数据流量包", "1", "-"), //B.产品属性为“资源包”,则为“1”; C.产品属性非“会员服务”且非“资源包”,则展示-
  444. "linkedId": linkedId, //关联id
  445. "orderArr": orderArr, //关联订单
  446. }
  447. }
  448. return nil
  449. }