package user import ( "app.yhyue.com/moapp/jybase/common" "context" "errors" "fmt" "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" "jyOrderManager/internal/jyutil" "jyOrderManager/internal/model" "strings" "time" ) func GetUserService(ctx context.Context, param model.UserServiceParams) (interface{}, error) { var ( serviceArrMap []map[string]interface{} userData *map[string]interface{} vipExist bool whereEntTime string entRec gdb.Record ) if param.Phone != "" { //大会员超||级订阅 判断是否有待生效订单 if (strings.Contains(param.ProductType, "大会员") || strings.Contains(param.ProductType, "订阅")) && param.BuySubject == "1" { sqlStr := fmt.Sprintf(`SELECT * FROM jy_order_detail jod INNER JOIN dataexport_order de on jod.order_code = de.order_code 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订阅%", "%大会员%")) if param.OrderCode != "" { sqlStr += fmt.Sprintf(" AND order_code != '%s'", param.OrderCode) } //检验订单是否可以进行传创建 order, _ := g.DB().Query(ctx, sqlStr) if !order.IsEmpty() && order.Len() > 0 { return map[string]interface{}{ "willEffect": true, "userData": userData, "serviceArrMap": nil, }, nil } } //手机号是否注册 userData, _ = jyutil.MG.DB().FindOne("user", map[string]interface{}{ "$or": []map[string]interface{}{ {"s_phone": param.Phone}, {"s_m_phone": param.Phone}, }, }) if userData == nil || len(*userData) == 0 { return map[string]interface{}{ "willEffect": false, //"userData": userData, "serviceArrMap": nil, }, nil } if param.BuySubject == "2" { if param.EntName == "" { return nil, errors.New("企业名称校验异常") } whereEntTime = fmt.Sprintf(` and a.end_time>'%s' `, time.Now().Format("2006-01-02 15:04:05")) entRec, _ = g.DB().GetOne(ctx, fmt.Sprintf(`SELECT a.id,b.id as entUserId FROM entniche_info a inner JOIN entniche_user b on a.id = b.ent_id WHERE b.phone = '%s' and a.name = '%s'`, param.Phone, param.EntName)) } var whereSql string if param.BuySubject == "1" { whereSql = fmt.Sprintf("dor.user_phone = '%s' and dor.buy_subject =1 ", param.Phone) } else { whereSql = fmt.Sprintf("dor.user_phone = '%s' and dor.buy_subject =2 and dor.company_name ='%s' ", param.Phone, param.EntName) } if param.ServiceType != 1 && param.ServiceType != 4 { 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) } orderDetailType, _ := g.DB().Query(ctx, fmt.Sprintf(`SELECT DISTINCT(jod.product_type) FROM jy_order_detail jod INNER JOIN dataexport_order dor on jod.order_code = dor.order_code WHERE %s and jod.status =1 order by dor.create_time desc`, whereSql)) if orderDetailType.IsEmpty() { return map[string]interface{}{ "willEffect": false, //"userData": userData, "serviceArrMap": nil, }, nil } for _, m := range orderDetailType.List() { switch gconv.String(m["product_type"]) { case "大会员": if param.BuySubject == "1" { var ( linkedId, comboId, areaCount, paySubNum, gifUserCount, payUserCount int endTime string orderArr []map[string]interface{} serviceList []int userId = common.InterfaceToStr((*userData)["_id"]) ) startTime := common.Int64All((*userData)["i_member_starttime"]) bigEnd := common.Int64All((*userData)["i_member_endtime"]) 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 INNER JOIN dataexport_order b on a.order_code = b.order_code 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) if !orderData.IsEmpty() { for _, m2 := range orderData.List() { if linkedId == 0 { linkedId = gconv.Int(m2["id"]) } serviceStarttime := gconv.String(m2["service_starttime"]) serviceEndtime := gconv.String(m2["service_endtime"]) if time.Unix(startTime, 0).Format("2006-01-02 15:04:05") <= serviceStarttime && time.Unix(bigEnd, 0).Format("2006-01-02 15:04:05") >= serviceEndtime && gconv.Int(m2["service_type"]) != 2 { var ttt model.JyBigProductFilter if err := gconv.Struct(m2["filter"], &ttt); err != nil { continue } gifUserCount += ttt.GiftAccountCount payUserCount += ttt.BuyAccountCount } orderArr = append(orderArr, map[string]interface{}{ "order_code": m2["order_code"], "create_time": m2["create_time"], "service_type": m2["service_type"], "order_detail_id": m2["id"], }) } } subStatus := common.IntAll((*userData)["i_member_sub_status"]) memberStatus := common.IntAll((*userData)["i_member_status"]) comboId = common.If(memberStatus == 5, 0, memberStatus).(int) mainId := common.ObjToString((*userData)["s_member_mainid"]) if memberJyMap, ok1 := ((*userData)["o_member_jy"]).(map[string]interface{}); ok1 { areaCount = common.IntAll(memberJyMap["i_areacount"]) } if startTime >= time.Now().Unix() { endTime = time.Unix(bigEnd, 0).Format("2006-01-02 15:04:05") vipExist = true } else if (time.Now().Unix() < bigEnd && memberStatus > 0) || startTime >= time.Now().Unix() { vipExist = true endTime = time.Unix(bigEnd, 0).Format("2006-01-02 15:04:05") } if (subStatus == 0 && memberStatus < 0 && mainId != "") || (subStatus == 1 && memberStatus > 0 && mainId != "") { vipExist = true } 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)) if !userServer.IsEmpty() { vipExist = true serMap := make(map[int]bool) 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)) isHUa := count > 0 for _, v := range userServer.List() { if isHUa && v["s_serviceid"] == 26 { continue } var serviceId int if common.IntAll(v["s_smainid"]) == 0 { if common.IntAll(v["s_serviceid"]) == 12 { serviceId = 23 } else { serviceId = common.IntAll(v["s_serviceid"]) } } else { if common.IntAll(v["s_smainid"]) == 12 { serviceId = 23 } else { serviceId = common.IntAll(v["s_smainid"]) } } if !serMap[serviceId] { serviceList = append(serviceList, serviceId) serMap[serviceId] = true } } } if vipExist { paySubNum = common.IntAll((*userData)["i_pay_sub_num"]) + common.IntAll((*userData)["i_free_sub_num"]) + 1 } var productCode string itemMap, _ := g.DB().GetOne(ctx, fmt.Sprintf(`SELECT code FROM jy_product_item WHERE JSON_EXTRACT(price, '$.comboId') = %d;`, comboId)) if !itemMap.IsEmpty() { productCode = gconv.String(itemMap.Map()["code"]) } serviceArrMap = append(serviceArrMap, map[string]interface{}{ "vipExist": vipExist, //当前服务是否在有限期内 "name": gconv.String(m["product_type"]), //产品名称 "productCode": productCode, //产品code "serviceEndTime": endTime, //服务结束时间 "serviceStartTime": startTime, //服务开始时间 "empowerCount": paySubNum, //服务授权数 "gifUserCount": gifUserCount, //赠送授权数 "payUserCount": payUserCount, //收费授权数 "provinceCount": areaCount, //订阅省份 "buySubject": 1, //个体 "comboId": comboId, //套餐id "serviceList": serviceList, //大会员服务id "linkedId": linkedId, //关联id "linkedOrder": orderArr, //关联订单 }) } else { if !entRec.IsEmpty() { uData := userEntService(ctx, gconv.String(entRec.Map()["id"]), whereEntTime, "%大会员%") if len(uData) > 0 { serviceArrMap = append(serviceArrMap, uData...) } } } case "VIP订阅": var ( serviceList []int endTime, startTime string paySubNum, areaCount, linkedId int orderArr []map[string]interface{} ) if param.BuySubject == "1" { var ( newVip, nationwide bool ) orderData, _ := g.DB().Query(ctx, `SELECT a.id,a.service_type,a.order_code,b.create_time FROM jy_order_detail a INNER JOIN dataexport_order b on a.order_code = b.order_code 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) if !orderData.IsEmpty() { for _, m2 := range orderData.List() { if linkedId == 0 { linkedId = gconv.Int(m2["id"]) } orderArr = append(orderArr, map[string]interface{}{ "order_code": m2["order_code"], "create_time": m2["create_time"], "service_type": m2["service_type"], "order_detail_id": m2["id"], }) } } vipExist = gconv.Int((*userData)["i_vip_status"]) > 0 if vipExist { paySubNum = 1 vipInfo := common.ObjToMap((*userData)["o_vipjy"]) buySet := common.ObjToMap((*vipInfo)["o_buyset"]) areaCount = common.IntAll((*buySet)["areacount"]) //判断是新版超级订阅还是老版超级订阅 if common.IntAll((*buySet)["upgrade"]) == 1 { newVip = true } if areaCount == -1 { nationwide = true } endTime = time.Unix(common.Int64All((*userData)["l_vip_endtime"]), 0).Format("2006-01-02 15:04:05") startTime = time.Unix(common.Int64All((*userData)["l_vip_starttime"]), 0).Format("2006-01-02 15:04:05") } productCode := fmt.Sprintf("cjdy%03d", gconv.Int(common.If(areaCount > 15 || areaCount == -1, 16, areaCount))) serviceArrMap = append(serviceArrMap, map[string]interface{}{ "vipExist": vipExist, //当前服务是否在有限期内 "nationwide": nationwide, "name": gconv.String(m["product_type"]), //产品名称 "serviceEndTime": endTime, //服务结束时间 "serviceStartTime": startTime, //服务开始时间 "productCode": productCode, "newVip": newVip, "empowerCount": paySubNum, //服务授权数 "provinceCount": areaCount, //订阅省份 "buySubject": 1, //个体 "serviceList": serviceList, //大会员服务id "linkedId": linkedId, //关联id "linkedOrder": orderArr, //关联订单 }) } else { if !entRec.IsEmpty() { uData := userEntService(ctx, gconv.String(entRec.Map()["id"]), whereEntTime, "%VIP订阅%") if len(uData) > 0 { serviceArrMap = append(serviceArrMap, uData...) } } } default: uData := SpecialService(ctx, whereSql, gconv.String(m["product_type"]), gconv.Int(param.BuySubject)) if uData != nil { serviceArrMap = append(serviceArrMap, uData) } } } } return map[string]interface{}{ "willEffect": false, "userData": userData, "serviceArrMap": serviceArrMap, }, nil } func userEntService(ctx context.Context, entID string, whereEntTime, productType string) (data []map[string]interface{}) { //获取企业下的服务 entService, _ := g.DB().Query(ctx, fmt.Sprintf(`SELECT * FROM entniche_wait_empower a WHERE a.ent_id = '%s' %s and a.product_type like '%s' ORDER BY a.end_time desc`, entID, whereEntTime, productType)) if !entService.IsEmpty() { for _, m := range entService.List() { var ( serviceList []int comboId int gifUserCount, payUserCount int productCode string vipExist bool ) switch { case strings.Contains(productType, "大会员"): //大会员获取下服务最新id serviceId, _ := g.DB().GetOne(ctx, fmt.Sprintf(`SELECT b.filter FROM entniche_order a 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"]))) if !serviceId.IsEmpty() { comboId = gconv.Int(gconv.Map(serviceId.Map()["filter"])["comboId"]) itemMap, _ := g.DB().GetOne(ctx, fmt.Sprintf(`SELECT code FROM jy_product_item WHERE JSON_EXTRACT(price, '$.comboId') = %d;`, comboId)) if !itemMap.IsEmpty() { productCode = gconv.String(itemMap.Map()["code"]) } var ttt model.JyBigProductFilter if err := gconv.Struct(serviceId.Map()["filter"], &ttt); err != nil { continue } for _, id := range ttt.ServiceIds { if id == "12" { serviceList = append(serviceList, 23) } else if id == "19" { serviceList = append(serviceList, 4) } else { serviceList = append(serviceList, gconv.Int(id)) } } } case strings.Contains(productType, "订阅"): 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"])))) } vipExist = jyutil.StrToTime(gconv.String(m["end_time"])).Unix() > time.Now().Unix() var ( orderArr []map[string]interface{} linkedId int ) //获取订单 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 INNER JOIN jy_order_detail b on a.order_detail_id = b.id 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"]))) if !entOrderService.IsEmpty() { for _, m2 := range entOrderService.List() { if linkedId == 0 { linkedId = gconv.Int(m2["id"]) } switch { case strings.Contains(productType, "大会员") && gconv.Int(m2["service_type"]) != 2: var ttt model.JyBigProductFilter if err := gconv.Struct(m2["filter"], &ttt); err != nil { continue } gifUserCount += ttt.GiftAccountCount payUserCount += ttt.BuyAccountCount case strings.Contains(productType, "订阅") && gconv.Int(m2["service_type"]) != 2: var ttt model.VipCycleFilter if err := gconv.Struct(m2["filter"], &ttt); err != nil { continue } gifUserCount += ttt.GiftAccountCount payUserCount += ttt.BuyAccountCount } orderArr = append(orderArr, map[string]interface{}{ "order_code": m2["order_code"], "create_time": m2["create_time"], "service_type": m2["service_type"], "order_detail_id": m2["id"], }) } } data = append(data, map[string]interface{}{ "name": gconv.String(m["product_type"]), //产品名称 "productCode": productCode, //产品code "vipExist": vipExist, //当前服务是否在有限期内 "serviceEndTime": gconv.String(m["end_time"]), //服务结束时间 "serviceStartTime": gconv.String(m["start_time"]), //服务开始时间 "empowerCount": gconv.String(m["empower_count"]), //服务授权数 "gifUserCount": gifUserCount, //免费服务授权数 "payUserCount": payUserCount, //付费服务授权数 "provinceCount": gconv.Int(m["province_count"]), //订阅省份 "buySubject": 2, //个体 "linkedId": linkedId, //关联id "comboId": comboId, //套餐id "serviceList": serviceList, //大会员服务id "linkedOrder": orderArr, //关联订单 }) } } return data } func SpecialService(ctx context.Context, phone, productType string, buySubject int) map[string]interface{} { var ( linkedId int orderArr []map[string]interface{} vipExist bool endTime, startTime, productCode string ) //serviceArrMap = append(serviceArrMap, map[string]interface{}{ // "name": gconv.String(m["product_type"]), //产品名称 // "vipExist": vipExist, //当前服务是否在有限期内 // "serviceEndTime": gconv.String(m["end_time"]), //服务结束时间 // "serviceStartTime": gconv.String(m["start_time"]), //服务开始时间 // "empowerCount": gconv.String(m["empower_count"]), //服务授权数 // "provinceCount": gconv.String(m["province_count"]), //订阅省份 // "buySubject": 2, //个体 // "linkedId": linkedId, //关联id // "comboId": comboId, //套餐id // "serviceList": serviceList, //大会员服务id // "linkedOrder": orderArr, //关联订单 // }) 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 INNER JOIN jy_order_detail jod on dor.order_code = jod.order_code WHERE %s and dor.product_type = '%s' and jod.status =1 ORDER BY dor.create_time desc `, phone, productType)) if !orderData.IsEmpty() { for _, m2 := range orderData.List() { if linkedId == 0 { linkedId = gconv.Int(m2["id"]) } if product_code := gconv.String(m2["product_code"]); product_code != "" { productCode = product_code } if gconv.String(m2["service_endtime"]) > time.Now().String() { vipExist = true } if gconv.String(m2["service_starttime"]) > endTime { endTime = gconv.String(m2["service_starttime"]) } if gconv.String(m2["service_starttime"]) < startTime { startTime = gconv.String(m2["service_starttime"]) } orderArr = append(orderArr, map[string]interface{}{ "order_code": m2["order_code"], "create_time": m2["create_time"], "service_type": m2["service_type"], "order_detail_id": m2["id"], }) } return map[string]interface{}{ "name": fmt.Sprintf("%s标准版", productType), //产品名称 "vipExist": vipExist, //当前服务是否在有限期内 "serviceEndTime": common.If(endTime != "", endTime, nil), //服务结束时间 "productCode": productCode, "serviceStartTime": startTime, //服务开始时间 "buySubject": buySubject, //个体 "empowerCount": common.If(productType == "数据流量包", "1", "-"), //B.产品属性为“资源包”,则为“1”; C.产品属性非“会员服务”且非“资源包”,则展示- "linkedId": linkedId, //关联id "linkedOrder": orderArr, //关联订单 } } return nil }