package service import ( "encoding/json" "fmt" "log" "strconv" "strings" "time" . "bp.jydev.jianyu360.cn/BaseService/entManageApplication/entity" "bp.jydev.jianyu360.cn/BaseService/entManageApplication/rpc/entmanageapplication" // "github.com/zeromicro/go-zero/core/logx" "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/redis" ) func AddUsePerson(this *entmanageapplication.AddUsePersonReq) *entmanageapplication.AddUsePersonResp { timeStr, ok, id := time.Now().Format("2006-01-02 15:04:05"), false, int64(0) data := JyMysql.FindOne(EntnichePower, map[string]interface{}{"wait_empower_id": this.WaitEmpowerId, "ent_user_id": this.EntUserId}, "", "") if data != nil && len(*data) > 0 { ok = JyMysql.Update(EntnichePower, map[string]interface{}{"wait_empower_id": this.WaitEmpowerId, "ent_user_id": this.EntUserId}, map[string]interface{}{"status": 1, "update_time": timeStr}) } else { id = JyMysql.Insert(EntnichePower, map[string]interface{}{ "wait_empower_id": this.WaitEmpowerId, "ent_id": this.EntId, "ent_user_id": this.EntUserId, "status": 1, "create_time": timeStr, "update_time": timeStr, }) } addPower(this.WaitEmpowerId) status := 0 if ok || id > 0 { status = 1 } return &entmanageapplication.AddUsePersonResp{ ErrorCode: 0, Data: &entmanageapplication.AddUsePerson{Status: int64(status)}, } } func DelUsePerson(this *entmanageapplication.DelUsePersonReq) *entmanageapplication.AddUsePersonResp { timeStr := time.Now().Format("2006-01-02 15:04:05") ok := JyMysql.Update(EntnichePower, map[string]interface{}{"id": this.EntnichePowerId}, map[string]interface{}{"status": -1, "update_time": timeStr}) delPower(this.EntnichePowerId) status := 0 if ok { status = 1 } return &entmanageapplication.AddUsePersonResp{ ErrorCode: 0, Data: &entmanageapplication.AddUsePerson{Status: int64(status)}, } } func UsePersonList(this *entmanageapplication.UsePersonListReq) *entmanageapplication.UsePersonListResp { data := JyMysql.SelectBySql(`select a.id,c.name,c.phone,c.mail,g.name as department,e.name as role from entniche_power a INNER JOIN entniche_user c on (a.ent_user_id=c.id and a.ent_id = c.ent_id) LEFT JOIN entniche_user_role d on (c.id=d.user_id) LEFT JOIN entniche_role e on (d.role_id=e.id) LEFT JOIN entniche_department_user f on (a.ent_user_id=f.user_id) LEFT JOIN entniche_department g on (g.id=f.dept_id) where a.wait_empower_id = ? and a.status = 1 order by convert(c.name using gbk) COLLATE gbk_chinese_ci asc`, this.WaitEmpowerId) arr := []*entmanageapplication.UsePersonList{} if data != nil && len(*data) > 0 { for _, v := range *data { obj := &entmanageapplication.UsePersonList{ EntnichePowerId: common.Int64All(v["id"]), Name: common.ObjToString(v["name"]), Phone: common.ObjToString(v["phone"]), Email: common.ObjToString(v["mail"]), Department: common.ObjToString(v["department"]), Role: common.ObjToString(v["role"]), } arr = append(arr, obj) } } return &entmanageapplication.UsePersonListResp{ ErrorCode: 0, Data: arr, } } func BuyProductList(this *entmanageapplication.BuyProductListReq) *entmanageapplication.BuyProductListResp { data := JyMysql.Find(EntnicheWaitEmpower, map[string]interface{}{"ent_id": this.EntId}, "", "create_time desc", 0, 0) arr := []*entmanageapplication.BuyProductList{} if data != nil && len(*data) > 0 { for _, v := range *data { obj := &entmanageapplication.BuyProductList{ WaitEmpowerId: common.Int64All(v["id"]), ProductType: common.ObjToString(v["product_type"]), UseCount: common.Int64All(v["use_count"]), EmpowerCount: common.Int64All(v["empower_count"]), ProvinceCount: common.Int64All(v["province_count"]), EndTime: common.ObjToString(v["end_time"]), } arr = append(arr, obj) } } return &entmanageapplication.BuyProductListResp{ ErrorCode: 0, Data: arr, } } func addPower(waitEmpowerId int64) { data := JyMysql.FindOne(EntnicheOrder, map[string]interface{}{"wait_empower_id": waitEmpowerId}, "", "") if data != nil && len(*data) > 0 { orderId := common.Int64All((*data)["order_id"]) orderData := JyMysql.FindOne(Order, map[string]interface{}{"id": orderId}, "", "") if orderData != nil && len(*orderData) > 0 { productType := common.ObjToString((*orderData)["product_type"]) if productType == "VIP订阅" || productType == "超级订阅" { openPowerVip(*orderData) } else if productType == "大会员" { openPowerBig(*orderData) } } } } func openPowerBig(orderData map[string]interface{}) bool { filterMap := common.ObjToMap(orderData["filter"]) startTime := common.ObjToString(orderData["vip_starttime"]) endTime := common.ObjToString(orderData["vip_endtime"]) userId := common.ObjToString(orderData["user_id"]) tmp := "2006-01-02 15:04:05" startdate, _ := time.ParseInLocation(tmp, startTime, time.Local) enddate, _ := time.ParseInLocation(tmp, endTime, time.Local) level := common.IntAll((*filterMap)["level"]) comboId := common.IntAll((*filterMap)["comboId"]) payCycle := common.IntAll((*filterMap)["payCycle"]) serversId := common.ObjToString((*filterMap)["serversId"]) serverMap := GetServerPid() dataType := 1 if startdate.Unix() > time.Now().Unix() { dataType = 2 } sets := map[string]interface{}{ "i_member_status": level, "i_member_starttime": startdate.Unix(), "i_member_endtime": enddate.Unix(), "i_mainaccount": 1, "o_member_jy.i_wxpush": 1, } set := map[string]interface{}{ "$set": sets, } ok := Mgo.UpdateById("user", userId, set) if !ok { log.Println("大会员创建订单user表大会员状态更新失败") } if comboId != 0 { datas := JyMysql.FindOne("bigmember_combo", map[string]interface{}{"id": comboId}, "", "") if datas == nil || len(*datas) == 0 { log.Println("未找到此套餐") return false } s_servers := common.ObjToString((*datas)["s_servers"]) for _, s := range strings.Split(s_servers, ",") { serverId, _ := strconv.Atoi(s) mainId := serverId if serverMap[serverId] != 0 { serverId = serverMap[serverId] } serverData := JyMysql.FindOne("bigmember_service", map[string]interface{}{"id": mainId}, "", "") frequency := 0 if serverData != nil && len(*serverData) > 0 { if serverId == 11 || serverId == 15 { if serverId == 15 { if payCycle >= 12 { frequency = (payCycle / 12 * common.IntAll((*serverData)["s_count_year"])) + (payCycle % 12 * common.IntAll((*serverData)["s_count_month"])) } else { frequency = payCycle * common.IntAll((*serverData)["s_count_month"]) } } else { frequency = payCycle * common.IntAll((*serverData)["s_count_month"]) } } else { frequency = common.IntAll((*serverData)["s_count_month"]) } } sqls := "select * from bigmember_service_user where s_serviceid = ? and s_userid = ? and i_status != -1" serverDatas := JyMysql.SelectBySql(sqls, serverId, userId) if serverDatas != nil && len(*serverDatas) > 0 { i_status := common.If(dataType == 1, 0, 1) //TODO sql := "" if serverId == 17 || serverId == 18 { sql = "update bigmember_service_user set i_frequency = ?,l_updatetime = ?,l_starttime = ?,l_endtime = ?,i_status = ? where s_serviceid = ? and s_userid = ?" } else { sql = "update bigmember_service_user set i_frequency = i_frequency + ?,l_updatetime = ?,l_starttime = ?,l_endtime = ?,i_status = ? where s_serviceid = ? and s_userid = ?" } status1 := JyMysql.UpdateOrDeleteBySql(sql, fmt.Sprint(frequency), time.Now().Format(tmp), startdate.Format(tmp), enddate.Format(tmp), fmt.Sprint(i_status), serverId, userId) if status1 < 0 { log.Println("新建服务-更新服务表出错", userId) return false } } else { insert := map[string]interface{}{ "s_smainid": mainId, "s_userid": userId, "s_serviceid": serverId, "i_frequency": frequency, "l_starttime": startdate.Format(tmp), "l_endtime": enddate.Format(tmp), "i_status": common.If(dataType == 1, 0, 1), "l_createtime": time.Now().Format(tmp), "l_updatetime": time.Now().Format(tmp), } order_id := JyMysql.Insert("bigmember_service_user", insert) if order_id > 0 { log.Println("用户服务表插入成功", userId) } else { log.Println("用户服务表插入失败", userId) return false } } } } else { for _, s := range strings.Split(serversId, ",") { serverId, _ := strconv.Atoi(s) mainId := serverId if serverMap[serverId] != 0 { serverId = serverMap[serverId] } serverData := JyMysql.FindOne("bigmember_service", map[string]interface{}{"id": mainId}, "", "") frequency := 0 if serverData != nil && len(*serverData) > 0 { if serverId == 11 || serverId == 15 { if serverId == 15 { if payCycle >= 12 { frequency = (payCycle / 12 * common.IntAll((*serverData)["s_count_year"])) + (payCycle % 12 * common.IntAll((*serverData)["s_count_month"])) } else { frequency = payCycle * common.IntAll((*serverData)["s_count_month"]) } } else { frequency = payCycle * common.IntAll((*serverData)["s_count_month"]) } } else { frequency = common.IntAll((*serverData)["s_count_month"]) } } sqls := "select * from bigmember_service_user where s_serviceid = ? and s_userid = ? and i_status != -1" serverDatas := JyMysql.SelectBySql(sqls, serverId, userId) if serverDatas != nil && len(*serverDatas) > 0 { i_status := common.If(dataType == 1, 0, 1) sql := "update bigmember_service_user set i_frequency = i_frequency + ?,l_updatetime = ?,l_starttime = ?,l_endtime = ?,i_status = ? where s_serviceid = ? and s_userid = ?" status1 := JyMysql.UpdateOrDeleteBySql(sql, fmt.Sprint(frequency), time.Now().Format(tmp), startdate.Format(tmp), enddate.Format(tmp), fmt.Sprint(i_status), serverId, userId) if status1 < 0 { log.Println("新建服务-更新服务表出错") return false } } else { insert := map[string]interface{}{ "s_userid": userId, "s_smainid": mainId, "s_serviceid": serverId, "i_frequency": frequency, "l_starttime": startdate.Format(tmp), "l_endtime": enddate.Format(tmp), "i_status": common.If(dataType == 1, 0, 1), "l_createtime": time.Now().Format(tmp), "l_updatetime": time.Now().Format(tmp), } order_id := JyMysql.Insert("bigmember_service_user", insert) if order_id > 0 { log.Println("用户服务表插入成功", userId) } else { log.Println("用户服务表插入失败") return false } } } RedisDel(userId) ClearBigVipUserPower(userId) } return true } func GetServerPid() map[int]int { pidMap := map[int]int{} datas := JyMysql.Find("bigmember_service", map[string]interface{}{"i_status": 0}, "id,i_pid", "", 0, 0) if datas != nil && len(*datas) > 0 { for _, v := range *datas { if v["i_pid"] != nil { pidMap[common.IntAll(v["id"])] = common.IntAll(v["i_pid"]) } } } return pidMap } func RedisDel(userId string) { redis.Del("newother", BigKey+userId) cacheKey := fmt.Sprintf(PowerCacheKey, userId) baseInfoCacheKey := fmt.Sprintf(IsGetUserBaseInfoRedisKey, userId) redisMenuKeyPC := fmt.Sprintf(RedisMenuKeyPC, userId) redisMenuKeyWX := fmt.Sprintf(RedisMenuKeyWX, userId) redisMenuKeyAPP := fmt.Sprintf(RedisMenuKeyAPP, userId) userPowerRedisKey := fmt.Sprintf(UserPowerRedisKey, time.Now().Day(), userId) redis.Del(NewOther, cacheKey) redis.Del(NewOther, baseInfoCacheKey) redis.Del(NewOther, redisMenuKeyPC) redis.Del(NewOther, redisMenuKeyWX) redis.Del(NewOther, redisMenuKeyAPP) redis.Del(NewOther, userPowerRedisKey) } func openPowerVip(orderData map[string]interface{}) bool { vms := VipSimpleMsg{} json.Unmarshal([]byte(common.ObjToString(orderData["filter"])), &vms) startTime := common.ObjToString(orderData["vip_starttime"]) endTime := common.ObjToString(orderData["vip_endtime"]) Date_Full_Layout := "2006-01-02 15:04:05" user_phone := common.ObjToString(orderData["user_phone"]) userId := common.ObjToString(orderData["user_id"]) startdate, _ := time.ParseInLocation(Date_Full_Layout, startTime, time.Local) enddate, _ := time.ParseInLocation(Date_Full_Layout, endTime, time.Local) dataType := 1 if startdate.Unix() > time.Now().Unix() { dataType = 2 } // newBuySet := SubvipBuySet{ // Upgrade: 1, // AreaCount: areaCount, // NewCitys: []int{}, // BuyerclassCount: -1, // } // vms := VipSimpleMsg{ // Area: &buyArea, // Industry: []string{}, // Cyclecount: param.CycleCount, // Cycleunit: param.CycleUnit, // NewBuyset: &newBuySet, // OrderType: 1, // DisWord: "", // ContractStatus: contractStatus, // Source: "qmx", // Remark: param.Remark, // } //为用户开通超级订阅权限 //是否开通超级订阅 -1 试用到期 -2 正式到期 1 试用 2 开通 isTrial := false if dataType == 2 { isTrial = true } isOk := StartSubVip(user_phone, *vms.Area, *vms.NewBuyset, startdate, enddate, isTrial, userId) return isOk } func StartSubVip(phone string, area map[string]interface{}, newBuyset SubvipBuySet, startTime, endTime time.Time, isTrial bool, userId string) bool { set := map[string]interface{}{ "o_vipjy.i_trial": -1, //已激活试用 "o_vipjy.o_area": area, //设置地区 "o_vipjy.o_buyset": newBuyset, //购买内容 城市、省份、行业数量 "l_vip_starttime": startTime.Unix(), //开始时间 "l_vip_endtime": endTime.Unix(), //结束时间 "i_vip_status": common.If(isTrial, -2, 2), //1试用 2正式 -2 试用到期 "i_vip_expire_tip": 0, //消息提示初始化 "o_vipjy.a_buyerclass": []interface{}{}, //设置行业 } isOk := Mgo.UpdateById("user", userId, map[string]interface{}{ "$set": set, }) go func() { MergeKws(userId) redis.Del(NewOther, "pl_indexMessage_"+userId) ClearBigVipUserPower(userId) }() if isOk { return true } return true } // 初始化vip订阅关键词 func MergeKws(userId string) { if userId == "" { //11-11 取消此操作 return } data, ok := Mgo.FindById("user", userId, `{"o_jy":1,"o_vipjy":1}`) var o_vipjy map[string]interface{} if ok && data != nil && len(*data) > 0 { o_vipjy, _ = (*data)["o_vipjy"].(map[string]interface{}) a_items, _ := o_vipjy["a_items"].([]interface{}) if a_items == nil { //首次 Mgo.UpdateById("user", userId, map[string]interface{}{ "$set": map[string]interface{}{"o_vipjy.i_matchway": 1, "o_vipjy.i_ratemode": 1, "o_vipjy.i_wxpush": 1, "o_vipjy.i_apppush": 1, "o_vipjy.i_projectmatch": 0, "o_vipjy.a_infotype": []string{}, "o_vipjy.a_items": []string{}, "o_vipjy.l_modifydate": time.Now().Unix()}, }) } } } func ClearBigVipUserPower(userId string) { cacheKey := fmt.Sprintf(PowerCacheKey, userId) baseInfoCacheKey := fmt.Sprintf(IsGetUserBaseInfoRedisKey, userId) redisMenuKeyPC := fmt.Sprintf(RedisMenuKeyPC, userId) redisMenuKeyWX := fmt.Sprintf(RedisMenuKeyWX, userId) redisMenuKeyAPP := fmt.Sprintf(RedisMenuKeyAPP, userId) userPowerRedisKey := fmt.Sprintf(UserPowerRedisKey, time.Now().Day(), userId) redis.Del(NewOther, cacheKey) redis.Del(NewOther, baseInfoCacheKey) redis.Del(NewOther, redisMenuKeyPC) redis.Del(NewOther, redisMenuKeyWX) redis.Del(NewOther, redisMenuKeyAPP) redis.Del(NewOther, userPowerRedisKey) } func delPower(entnichePowerId int64) { data := JyMysql.FindOne(EntnichePower, map[string]interface{}{"id": entnichePowerId}, "", "") if data != nil && len(*data) > 0 { waitEmpowerId := common.Int64All((*data)["wait_empower_id"]) eOData := JyMysql.FindOne(EntnicheOrder, map[string]interface{}{"wait_empower_id": waitEmpowerId}, "", "") if eOData != nil && len(*eOData) > 0 { orderId := common.Int64All((*eOData)["order_id"]) orderData := JyMysql.FindOne(Order, map[string]interface{}{"id": orderId}, "", "") if orderData != nil && len(*orderData) > 0 { productType := common.ObjToString((*orderData)["product_type"]) userId := common.ObjToString((*orderData)["user_id"]) if productType == "VIP订阅" || productType == "超级订阅" { Mgo.UpdateById("user", userId, map[string]interface{}{"$set": map[string]interface{}{"i_vip_status": -2}}) ClearBigVipUserPower(userId) } else if productType == "大会员" { Mgo.UpdateById("user", userId, map[string]interface{}{"$set": map[string]interface{}{"i_member_status": -2}}) RedisDel(userId) ClearBigVipUserPower(userId) DelBigPower(userId) } } } } }