getService.go 19 KB

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