getService.go 17 KB

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