getService.go 17 KB

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