getService.go 20 KB

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