getService.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. "vipExist": false,
  34. }, nil
  35. }
  36. if userData != nil && len(*userData) > 0 {
  37. userId := mongodb.BsonIdToSId((*userData)["_id"])
  38. sqlStr := fmt.Sprintf(`SELECT b.* FROM jy_order_detail
  39. 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))
  40. if param.OrderCode != "" {
  41. sqlStr += fmt.Sprintf(" AND order_code != '%s'", param.OrderCode)
  42. }
  43. //检验订单是否可以进行传创建
  44. order, _ := g.DB().Query(ctx, sqlStr)
  45. if !order.IsEmpty() && order.Len() > 0 {
  46. return map[string]interface{}{
  47. "willEffect": true,
  48. "userData": userData,
  49. "vipExist": false,
  50. }, nil
  51. }
  52. var (
  53. serviceList []int
  54. endTime, startTime string
  55. paySubNum, areaCount, linkedId int
  56. orderArr []map[string]interface{}
  57. )
  58. var whereEntTime string
  59. //购买关联
  60. if param.ServiceType != 1 && param.ServiceType != 4 {
  61. whereEntTime = "and a.is_service_open = 1"
  62. } else {
  63. //续费升级关联
  64. whereEntTime = "and b.order_status = 1"
  65. }
  66. orderData, _ := g.DB().Query(ctx, fmt.Sprintf(`SELECT b.id,b.service_type,b.order_code,c.create_time FROM jy_order_detail a
  67. INNER JOIN dataexport_order b on a.order_code = b.order_code
  68. WHERE a.status = 1 %s and a.user_id = '%s' and a.product_type like '%s' ORDER BY b.create_time desc `, whereEntTime, userId, common.If(param.ProductType == "VIP订阅", "%VIP订阅%", "%大会员%")))
  69. if !orderData.IsEmpty() {
  70. for _, m2 := range orderData.List() {
  71. if linkedId == 0 {
  72. linkedId = gconv.Int(m2["id"])
  73. }
  74. orderArr = append(orderArr, map[string]interface{}{
  75. "order_code": m2["order_code"],
  76. "create_time": m2["create_time"],
  77. "service_type": m2["service_type"],
  78. "order_detail_id": m2["id"],
  79. })
  80. }
  81. }
  82. var comboId int
  83. switch param.ProductType {
  84. case "VIP订阅":
  85. vipExist = gconv.Int((*userData)["i_vip_status"]) == 2
  86. if vipExist {
  87. paySubNum = 1
  88. vipInfo := common.ObjToMap((*userData)["o_vipjy"])
  89. buySet := common.ObjToMap((*vipInfo)["o_buyset"])
  90. areaCount = common.IntAll((*buySet)["areacount"])
  91. //判断是新版超级订阅还是老版超级订阅
  92. if common.IntAll((*buySet)["upgrade"]) == 1 {
  93. //newVip = true
  94. }
  95. endTime = time.Unix(common.Int64All((*userData)["l_vip_endtime"]), 0).Format("2006-01-02 15:04:05")
  96. }
  97. case "大会员":
  98. bigStart := common.Int64All((*userData)["i_member_starttime"])
  99. bigEnd := common.Int64All((*userData)["i_member_endtime"])
  100. subStatus := common.IntAll((*userData)["i_member_sub_status"])
  101. memberStatus := common.IntAll((*userData)["i_member_status"])
  102. comboId = common.If(memberStatus == 5, 0, memberStatus).(int)
  103. mainId := common.ObjToString((*userData)["s_member_mainid"])
  104. if memberJyMap, ok1 := ((*userData)["o_member_jy"]).(map[string]interface{}); ok1 {
  105. areaCount = common.IntAll(memberJyMap["i_areacount"])
  106. }
  107. if bigStart >= time.Now().Unix() {
  108. endTime = time.Unix(bigEnd, 0).Format("2006-01-02 15:04:05")
  109. vipExist = true
  110. } else if (time.Now().Unix() < bigEnd && memberStatus > 0) || bigStart >= time.Now().Unix() {
  111. vipExist = true
  112. endTime = time.Unix(bigEnd, 0).Format("2006-01-02 15:04:05")
  113. }
  114. if (subStatus == 0 && memberStatus < 0 && mainId != "") || (subStatus == 1 && memberStatus > 0 && mainId != "") {
  115. vipExist = true
  116. }
  117. 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))
  118. if !userServer.IsEmpty() {
  119. vipExist = true
  120. count, _ := g.DB().GetCount(ctx, fmt.Sprintf(`SELECT count(1) as count FROM bigmember_service_user WHERE s_userid = %s and i_status =0 and (s_serviceid=4 or s_serviceid =19)`, userId))
  121. isHUa := count > 0
  122. for _, v := range userServer.List() {
  123. if isHUa && v["s_serviceid"] == 26 {
  124. continue
  125. }
  126. if common.IntAll(v["s_smainid"]) == 0 {
  127. if common.IntAll(v["s_serviceid"]) == 12 {
  128. serviceList = append(serviceList, 23)
  129. } else {
  130. serviceList = append(serviceList, common.IntAll(v["s_serviceid"]))
  131. }
  132. } else {
  133. if common.IntAll(v["s_smainid"]) == 12 {
  134. serviceList = append(serviceList, 23)
  135. } else {
  136. serviceList = append(serviceList, common.IntAll(v["s_smainid"]))
  137. }
  138. }
  139. }
  140. }
  141. if vipExist {
  142. paySubNum = common.IntAll((*userData)["i_pay_sub_num"]) + common.IntAll((*userData)["i_free_sub_num"]) + 1
  143. }
  144. }
  145. serviceArrMap = append(serviceArrMap, map[string]interface{}{
  146. "vipExist": vipExist, //当前服务是否在有限期内
  147. "name": param.ProductType, //产品名称
  148. "serviceEndTime": endTime, //服务结束时间
  149. "serviceStartTime": startTime, //服务开始时间
  150. "empowerCount": paySubNum, //服务授权数
  151. "provinceCount": areaCount, //订阅省份
  152. "buySubject": 1, //个体
  153. "comboId": comboId, //套餐id
  154. "serviceList": serviceList, //大会员服务id
  155. "linkedId": linkedId, //关联id
  156. "linkedOrder": orderArr, //关联订单
  157. })
  158. }
  159. } else { //企业版本会员状态查询
  160. if param.EntName == "" {
  161. return nil, errors.New("企业名称获取异常")
  162. }
  163. //获取企业信息
  164. vipRes, _ := g.DB().GetOne(ctx, fmt.Sprintf(`SELECT a.id FROM entniche_info a
  165. inner JOIN entniche_user b on a.id = b.ent_id
  166. WHERE b.phone = '%s' and a.name = '%s'`, param.Phone, param.EntName))
  167. if !vipRes.IsEmpty() {
  168. entID := gconv.String(vipRes.Map()["id"])
  169. var whereEntTime string
  170. if param.ServiceType != 1 && param.ServiceType != 4 {
  171. whereEntTime = fmt.Sprintf(` and a.end_time>'%s' `, time.Now().Format("2006-01-02 15:04:05"))
  172. }
  173. //获取企业下的服务
  174. entService, _ := g.DB().Query(ctx, fmt.Sprintf(`SELECT * FROM entniche_wait_empower a
  175. WHERE a.ent_id = '%s' %s and a.product_type like '%s' and ORDER BY a.create_time desc`, entID, whereEntTime, common.If(param.ProductType == "VIP订阅", "%VIP订阅%", `%大会员%`)))
  176. if !entService.IsEmpty() {
  177. for _, m := range entService.List() {
  178. var (
  179. serviceList []int
  180. comboId int
  181. )
  182. if param.ProductType == "大会员" {
  183. //大会员获取下服务最新id
  184. serviceId, _ := g.DB().GetOne(ctx, fmt.Sprintf(`SELECT b.filter FROM entniche_order a
  185. 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"])))
  186. if !serviceId.IsEmpty() {
  187. comboId = gconv.Int(gconv.Map(serviceId.Map()["filter"])["comboId"])
  188. for _, id := range gconv.Ints(gconv.Map(serviceId.Map()["filter"])["serviceIds"]) {
  189. if id == 12 {
  190. serviceList = append(serviceList, 23)
  191. } else if id == 19 {
  192. serviceList = append(serviceList, 4)
  193. } else {
  194. serviceList = append(serviceList, id)
  195. }
  196. }
  197. }
  198. }
  199. vipExist = jyutil.StrToTime(gconv.String(m["end_time"])).Unix() > time.Now().Unix()
  200. var (
  201. orderArr []map[string]interface{}
  202. linkedId int
  203. )
  204. //获取订单
  205. entOrderService, _ := g.DB().Query(ctx, fmt.Sprintf(`SELECT b.id,b.service_type,b.order_code,c.create_time FROM entniche_order a
  206. INNER JOIN jy_order_detail b on a.order_detail_id = b.id
  207. 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"])))
  208. if !entOrderService.IsEmpty() {
  209. for _, m2 := range entOrderService.List() {
  210. if linkedId == 0 {
  211. linkedId = gconv.Int(m2["id"])
  212. }
  213. orderArr = append(orderArr, map[string]interface{}{
  214. "order_code": m2["order_code"],
  215. "create_time": m2["create_time"],
  216. "service_type": m2["service_type"],
  217. "order_detail_id": m2["id"],
  218. })
  219. }
  220. }
  221. serviceArrMap = append(serviceArrMap, map[string]interface{}{
  222. "name": gconv.String(m["product_type"]), //产品名称
  223. "vipExist": vipExist,
  224. "serviceEndTime": gconv.String(m["end_time"]), //服务结束时间
  225. "serviceStartTime": gconv.String(m["start_time"]), //服务开始时间
  226. "empowerCount": gconv.String(m["empower_count"]), //服务授权数
  227. "provinceCount": gconv.String(m["province_count"]), //订阅省份
  228. "buySubject": 2, //个体
  229. "linkedId": linkedId, //关联id
  230. "comboId": comboId, //套餐id
  231. "serviceList": serviceList, //大会员服务id
  232. "linkedOrder": orderArr, //关联订单
  233. })
  234. }
  235. }
  236. }
  237. }
  238. }
  239. return map[string]interface{}{
  240. "willEffect": false,
  241. "userData": userData,
  242. "serviceArrMap": serviceArrMap,
  243. }, nil
  244. }
  245. func GetServerPid() map[int]int {
  246. pidMap := map[int]int{}
  247. datas, _ := g.DB().Query(context.Background(), `SELECT id,i_pid FROM bigmember_service_user WHERE bigmember_service_user =0 `)
  248. if !datas.IsEmpty() {
  249. for _, v := range datas.List() {
  250. if v["i_pid"] != nil {
  251. pidMap[common.IntAll(v["id"])] = common.IntAll(v["i_pid"])
  252. }
  253. }
  254. }
  255. return pidMap
  256. }