|
@@ -0,0 +1,303 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "fmt"
|
|
|
+ "github.com/gogf/gf/v2/database/gdb"
|
|
|
+ "github.com/gogf/gf/v2/frame/g"
|
|
|
+ "github.com/gogf/gf/v2/os/gctx"
|
|
|
+ "github.com/gogf/gf/v2/util/gconv"
|
|
|
+ "github.com/google/uuid"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+type (
|
|
|
+ entBuy struct {
|
|
|
+ Id string `json:"id"`
|
|
|
+ UseCount int `json:"use_count"`
|
|
|
+ EmpowerCount int `json:"empower_count"`
|
|
|
+ EndTime string `json:"end_time"`
|
|
|
+ ProductType string `json:"product_type"`
|
|
|
+ }
|
|
|
+ packNum struct {
|
|
|
+ EntNum int
|
|
|
+ BuyNum int
|
|
|
+ MarketNum int
|
|
|
+ }
|
|
|
+ Detailed struct {
|
|
|
+ AccountId string `xorm:"accountId" form:"accountId" json:"accountId"` //账户标识
|
|
|
+ CompanyId int64 `xorm:"companyId" form:"companyId" json:"companyId"` //企业标识
|
|
|
+ ResourceType string `xorm:"resourceType" form:"resourceType" json:"resourceType"` //资源类型
|
|
|
+ Number int64 `xorm:"number" form:"number" json:"number"` //数量
|
|
|
+ CreateTime time.Time `xorm:"createTime" form:"createTime" json:"createTime"` //创建时间
|
|
|
+ UserType int64 `xorm:"userType" form:"userType" json:"userType"` //流水类型0使用1新增
|
|
|
+ Remarks string `xorm:"remarks" form:"remarks" json:"remarks"` //备注
|
|
|
+ UserId string `xorm:"userId" form:"userId" json:"userId"` //用户标识
|
|
|
+ Name string `xorm:"name" form:"name" json:"name"` //资源名称
|
|
|
+ }
|
|
|
+ Balance struct {
|
|
|
+ AccountId string `xorm:"accountId" form:"accountId" json:"accountId"` //账户标识
|
|
|
+ CompanyId int64 `xorm:"companyId" form:"companyId" json:"companyId"` //企业标识
|
|
|
+ DepartmentId int64 `xorm:"departmentId" form:"departmentId" json:"departmentId"` //组织标识
|
|
|
+ Name string `xorm:"name" form:"name" json:"name"` //资源名称
|
|
|
+ ResourceType string `xorm:"resourceType" form:"resourceType" json:"resourceType"` //资源代码
|
|
|
+ Number int64 `xorm:"number" form:"number" json:"number"` //数量
|
|
|
+ Spec string `xorm:"spec" form:"spec" json:"spec"` //规格
|
|
|
+ AppId string `xorm:"appId" form:"appId" json:"appId"` //标识
|
|
|
+ EndTime string `xorm:"endTime" form:"endTime" json:"endTime"` //截止时间
|
|
|
+ SourceType int64 `xorm:"sourceType" form:"sourceType" json:"sourceType"` //1购买2兑换3定期发放
|
|
|
+ }
|
|
|
+)
|
|
|
+
|
|
|
+func flushEnt(ctx context.Context) error {
|
|
|
+ //查询所有企业版大会员
|
|
|
+ entIdList, err := g.DB().Query(ctx, g.Cfg().MustGet(ctx, "entListQuery").String(), flushEntTime)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ for i, m := range entIdList.List() {
|
|
|
+ if i%10 == 0 {
|
|
|
+ g.Log().Infof(ctx, "企业数据进度【%d/%d】", i, len(entIdList.List()))
|
|
|
+ }
|
|
|
+ entId := gconv.String(m["entId"])
|
|
|
+ buyRes, err := g.DB().Query(ctx, g.Cfg().MustGet(ctx, "entBuyQuery").String(), entId, flushEntTime, entId, flushEntTime)
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Errorf(ctx, "entBuyQuery查询异常%s %v", entId, err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ var arr []*entBuy
|
|
|
+ if err := buyRes.Structs(&arr); err != nil {
|
|
|
+ g.Log().Errorf(ctx, "entBuyQuery查询Structs异常%s %v", entId, err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ pNumMap, err := getPackNum(entId, arr)
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Errorf(ctx, "entBuyQuery获取套餐pdf包数量异常异常%s %v", entId, err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ //未分配初始额度
|
|
|
+ for _, m := range arr {
|
|
|
+ pack := pNumMap[m.Id]
|
|
|
+ //无数据包推出
|
|
|
+ if !(pack.EntNum > 0 || pack.BuyNum > 0 || pack.MarketNum > 0) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ userDetail, err := g.DB().Query(ctx, g.Cfg().MustGet(ctx, "entUserDetailQuery").String(), entId, m.Id, flushEntTime)
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Errorf(ctx, "entBuyQuery查询entUserDetailQuery异常%s %s %v", entId, m.Id, err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(userDetail.List()) != m.UseCount || m.UseCount > m.EmpowerCount {
|
|
|
+ g.Log().Errorf(ctx, "entBuyQuery权益分配异%s %s 常 总量%d 已分配%d", entId, m.Id, m.EmpowerCount, m.UseCount)
|
|
|
+ }
|
|
|
+
|
|
|
+ var (
|
|
|
+ phoneArr []string //已分配权益的手机号
|
|
|
+ positionIdArr []string //职位id
|
|
|
+ )
|
|
|
+ if m.UseCount > 0 {
|
|
|
+ for _, m := range userDetail.List() {
|
|
|
+ if p := gconv.String(m["phone"]); p != "" {
|
|
|
+ phoneArr = append(phoneArr, p)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ userInfos, err := g.DB().Query(ctx, fmt.Sprintf(g.Cfg().MustGet(ctx, "queryPosId").String(), strings.Join(phoneArr, "','")), entId)
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Errorf(ctx, "entBuyQuery查询queryPosId异常%s %s %v", entId, m.Id, err)
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, record := range userInfos {
|
|
|
+ positionIdArr = append(positionIdArr, gconv.String(record["positionId"]))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(positionIdArr) != m.UseCount {
|
|
|
+ g.Log().Errorf(ctx, "entBuyQuery查询queryPosId已分配数量和查询到的职位id数量不一致%s %s %v", entId, m.Id, positionIdArr)
|
|
|
+ }
|
|
|
+
|
|
|
+ //未分配数量插入占位职位id
|
|
|
+ for i := len(positionIdArr); i < m.EmpowerCount; i++ {
|
|
|
+ positionIdArr = append(positionIdArr, fmt.Sprintf("wait_%s", uuid.New().String()))
|
|
|
+ }
|
|
|
+
|
|
|
+ eTime := m.EndTime
|
|
|
+ if arr := strings.Split(m.EndTime, " "); len(arr) == 2 {
|
|
|
+ eTime = arr[0]
|
|
|
+ }
|
|
|
+ if err := pack.InsertDb(entId, eTime, positionIdArr...); err != nil {
|
|
|
+ g.Log().Errorf(ctx, "entBuyQuery插入权益包异常%s %s %v %v", entId, m.Id, positionIdArr, err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func getPackNum(entId string, pMap []*entBuy) (map[string]*packNum, error) {
|
|
|
+ var (
|
|
|
+ ctx = context.TODO()
|
|
|
+ returnData = map[string]*packNum{}
|
|
|
+ )
|
|
|
+ for _, buy := range pMap {
|
|
|
+ t, err := time.Parse(time.DateTime, buy.EndTime)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ pn := &packNum{
|
|
|
+ EntNum: 0,
|
|
|
+ BuyNum: 0,
|
|
|
+ MarketNum: 0,
|
|
|
+ }
|
|
|
+ year := gconv.Int(t.Sub(now).Seconds() / (60 * 60 * 24 * 365))
|
|
|
+ if year > 0 {
|
|
|
+ if buy.ProductType == "大会员" || strings.Index(buy.ProductType, "自定义") > -1 {
|
|
|
+ //查询订单
|
|
|
+ res, err := g.DB().GetOne(ctx, "SELECT filter FROM dataexport_order WHERE ent_id=? AND vip_endtime=? AND buy_subject=2 ", entId, buy.EndTime)
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Errorf(ctx, " %s %s 获取自定义订单异常", entId, buy.Id)
|
|
|
+ } else {
|
|
|
+ for _, serviceId := range strings.Split(gconv.String(gconv.Map(res["filter"])["serversId"]), ",") {
|
|
|
+ if serviceId == "26" {
|
|
|
+ pn.MarketNum = year
|
|
|
+ } else if serviceId == "5" {
|
|
|
+ pn.BuyNum = year
|
|
|
+ } else if serviceId == "4" || serviceId == "19" {
|
|
|
+ pn.EntNum = year
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ switch buy.ProductType {
|
|
|
+ case "大会员商机版":
|
|
|
+ pn = &packNum{
|
|
|
+ MarketNum: 0,
|
|
|
+ EntNum: year,
|
|
|
+ BuyNum: year,
|
|
|
+ }
|
|
|
+ case "大会员商机版2.0":
|
|
|
+ pn = &packNum{
|
|
|
+ MarketNum: 0,
|
|
|
+ EntNum: year,
|
|
|
+ BuyNum: year,
|
|
|
+ }
|
|
|
+ case "大会员专家版2.0":
|
|
|
+ pn = &packNum{
|
|
|
+ MarketNum: year,
|
|
|
+ EntNum: year * 3,
|
|
|
+ BuyNum: year * 3,
|
|
|
+ }
|
|
|
+ case "大会员专家版新":
|
|
|
+ pn = &packNum{
|
|
|
+ MarketNum: year,
|
|
|
+ EntNum: year * 3,
|
|
|
+ BuyNum: year * 3,
|
|
|
+ }
|
|
|
+ case "大会员智慧版新":
|
|
|
+ pn = &packNum{
|
|
|
+ MarketNum: 0,
|
|
|
+ EntNum: year,
|
|
|
+ BuyNum: year,
|
|
|
+ }
|
|
|
+ case "大会员商机版+单日限量包(200条)":
|
|
|
+ pn = &packNum{
|
|
|
+ MarketNum: 0,
|
|
|
+ EntNum: year,
|
|
|
+ BuyNum: year,
|
|
|
+ }
|
|
|
+ case "大会员商机版2.0(单省版)":
|
|
|
+ pn = &packNum{
|
|
|
+ MarketNum: 0,
|
|
|
+ EntNum: year,
|
|
|
+ BuyNum: year,
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ g.Log().Errorf(ctx, "%s %s未知会员套餐", entId, buy.ProductType)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ returnData[buy.Id] = pn
|
|
|
+ }
|
|
|
+ return returnData, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (p *packNum) InsertDb(companyId, endTime string, positionId ...string) error {
|
|
|
+ err := g.DB("jy_user_center").Transaction(gctx.New(), func(ctx context.Context, tx gdb.TX) error {
|
|
|
+ // 资源中台操作
|
|
|
+ var insert = func(ResourceType string, num int) error {
|
|
|
+ var (
|
|
|
+ sources []Detailed
|
|
|
+ balance []Balance
|
|
|
+ )
|
|
|
+ for _, pId := range positionId {
|
|
|
+ var (
|
|
|
+ t = Detailed{
|
|
|
+ AccountId: pId,
|
|
|
+ ResourceType: ResourceType,
|
|
|
+ Number: gconv.Int64(num),
|
|
|
+ CreateTime: createTime,
|
|
|
+ UserType: 1,
|
|
|
+ Remarks: gconv.String(g.Map{
|
|
|
+ "source": fmt.Sprintf("大会员产品套餐有%d份“%s”权益", num, ResourceType),
|
|
|
+ "endTime": endTime,
|
|
|
+ }),
|
|
|
+ UserId: pId,
|
|
|
+ Name: "pdf下载包",
|
|
|
+ }
|
|
|
+ t2 = Balance{
|
|
|
+ AccountId: pId,
|
|
|
+ Name: "pdf下载包",
|
|
|
+ ResourceType: ResourceType,
|
|
|
+ Number: gconv.Int64(num),
|
|
|
+ Spec: "个",
|
|
|
+ AppId: "10000",
|
|
|
+ EndTime: endTime,
|
|
|
+ SourceType: 1,
|
|
|
+ }
|
|
|
+ )
|
|
|
+ if companyId != "" {
|
|
|
+ t.CompanyId = gconv.Int64(companyId)
|
|
|
+ t2.CompanyId = gconv.Int64(companyId)
|
|
|
+ }
|
|
|
+ sources = append(sources, t)
|
|
|
+ balance = append(balance, t2)
|
|
|
+ }
|
|
|
+
|
|
|
+ res_1, err_1 := tx.Insert("account_resources_tmp", balance, 10)
|
|
|
+ if err_1 != nil {
|
|
|
+ return err_1
|
|
|
+ }
|
|
|
+ res_2, err_2 := tx.Insert("consume_record_tmp", sources, 10)
|
|
|
+ if err_2 != nil {
|
|
|
+ return err_2
|
|
|
+ }
|
|
|
+ n1, _ := res_1.RowsAffected()
|
|
|
+ n2, _ := res_2.RowsAffected()
|
|
|
+ if n1 != n2 || n1 != gconv.Int64(len(positionId)) {
|
|
|
+ g.Log().Infof(ctx, "InsertDb异常 %s 插入数据量异常%d 期望%d", companyId, n2, len(positionId))
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ if p.BuyNum > 0 {
|
|
|
+ if err := insert("业主采购分析报告下载包", p.BuyNum); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if p.EntNum > 0 {
|
|
|
+ if err := insert("企业中标分析报告下载包", p.BuyNum); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if p.MarketNum > 0 {
|
|
|
+ if err := insert("市场分析定制报告下载包", p.BuyNum); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|