getService.go 20 KB

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