瀏覽代碼

wip:pdf下载包刷库

wangkaiyue 1 年之前
父節點
當前提交
8812b286eb

+ 29 - 0
run/flushDb/pdfFlushPower/config.yaml

@@ -0,0 +1,29 @@
+database:
+  default:
+    link: "mysql:readuser:jyTi_R202403@tcp(192.168.3.71:4003)/jianyu"
+    debug: true
+  jy_user_center:
+    link: "mysql:root:=PDT49#80Z!RVv52_z@tcp(192.168.3.217:4000)/jy_user_center"
+    debug: true
+
+logger:
+  level: "all"
+  path: "logs" # 日志文件路径。默认为空,表示关闭,仅输出到终端
+  file: "{Y-m-d}.log" # 日志文件格式。默认为"{Y-m-d}.log"
+
+flushEndTime: "2025-08-01"
+flushCreateTime: "2024-08-01 00:00:01"
+#查询企业权限
+#entListQuery: "SELECT DISTINCT(ent_id) as entId FROM entniche_wait_empower WHERE end_time > ? and ent_id =25879"
+entListQuery: "SELECT DISTINCT(ent_id) as entId FROM entniche_wait_empower WHERE end_time > ? and ent_id >0 and product_type like '%大会员%'"
+entBuyQuery: "SELECT a.id,SUM(IF(c.id IS NULL || b.status IS NULL || b.status<0,0,1)) AS use_count,a.empower_count,a.province_count,a.end_time,a.product_type FROM entniche_wait_empower a
+  LEFT JOIN entniche_power b ON (a.ent_id=? AND a.end_time>=?  AND a.id=b.wait_empower_id AND b.status=1)
+  LEFT JOIN entniche_user c ON (b.ent_user_id=c.id)
+  WHERE a.ent_id=? AND a.end_time>=? AND a.product_type like'%大会员%' GROUP BY a.id ORDER BY a.create_time DESC"
+entUserDetailQuery: "select h.product_type,a.id,a.wait_empower_id,c.phone 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_wait_empower h on (h.id=a.wait_empower_id) 
+		where a.status = 1 and a.ent_id = ?  and wait_empower_id= ? and h.end_time >= ?"
+queryPosId: "SELECT b.id as positionId,u.phone  FROM base_service.base_user u INNER JOIN base_service.base_position b on(u.id=b.user_id  AND b.ent_id = ? AND u.phone in ('%s'))"
+
+personalQuery: "SELECT user_id,filter,vip_endtime,order_code FROM dataexport_order WHERE product_type='大会员' AND vip_starttime < '2099-01-01 00:00:00' AND vip_endtime>? AND buy_subject!=2 AND order_status=1 AND filter like '%comboId%'"

+ 303 - 0
run/flushDb/pdfFlushPower/flushEnt.go

@@ -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
+}

+ 117 - 0
run/flushDb/pdfFlushPower/flushPersonal.go

@@ -0,0 +1,117 @@
+package main
+
+import (
+	"context"
+	"fmt"
+	"github.com/gogf/gf/v2/frame/g"
+	"github.com/gogf/gf/v2/util/gconv"
+	"strings"
+	"time"
+)
+
+func flushPersonal(ctx context.Context) error {
+	personalList, err := g.DB().Query(ctx, g.Cfg().MustGet(ctx, "personalQuery").String(), flushEntTime)
+	if err != nil {
+		return err
+	}
+	for i, m := range personalList.List() {
+		if i%10 == 0 {
+			g.Log().Infof(ctx, "个人数据进度【%d/%d】", i, len(personalList.List()))
+		}
+		var (
+			userId      = gconv.String(m["user_id"])
+			filterMap   = gconv.Map(m["filter"])
+			vip_endtime = gconv.String(m["vip_endtime"])
+			order_code  = gconv.String(m["order_code"])
+		)
+		pack, err := getPersonalPackNum(filterMap, vip_endtime)
+		if err != nil {
+			g.Log().Errorf(ctx, "flushPersonal获取套餐pdf包数量异常异常%s %s %v", userId, order_code, err)
+			continue
+		}
+		if !(pack.EntNum > 0 || pack.BuyNum > 0 || pack.MarketNum > 0) {
+			continue
+		}
+		eTime := vip_endtime
+		if arr := strings.Split(vip_endtime, " "); len(arr) == 2 {
+			eTime = arr[0]
+		}
+		if err := pack.InsertDb("", eTime, userId); err != nil {
+			g.Log().Errorf(ctx, "flushPersonal插入权益包异常%s %s %v", userId, order_code, err)
+		}
+	}
+	return nil
+}
+
+func getPersonalPackNum(filter map[string]interface{}, endTime string) (*packNum, error) {
+	var comboId = gconv.Int64(filter["comboId"])
+	t, err := time.Parse(time.DateTime, 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 {
+		switch comboId {
+		case 3:
+			pn = &packNum{
+				MarketNum: 0,
+				EntNum:    year,
+				BuyNum:    year,
+			}
+		case 6:
+			pn = &packNum{
+				MarketNum: 0,
+				EntNum:    year,
+				BuyNum:    year,
+			}
+		case 7:
+			pn = &packNum{
+				MarketNum: year,
+				EntNum:    year * 3,
+				BuyNum:    year * 3,
+			}
+		case 101:
+			pn = &packNum{
+				MarketNum: year,
+				EntNum:    year * 3,
+				BuyNum:    year * 3,
+			}
+		case 102:
+			pn = &packNum{
+				MarketNum: 0,
+				EntNum:    year,
+				BuyNum:    year,
+			}
+		case 103:
+			pn = &packNum{
+				MarketNum: 0,
+				EntNum:    year,
+				BuyNum:    year,
+			}
+		case 30190:
+			pn = &packNum{
+				MarketNum: 0,
+				EntNum:    year,
+				BuyNum:    year,
+			}
+		case 0:
+			for _, serviceId := range strings.Split(gconv.String(filter["serversId"]), ",") {
+				if serviceId == "26" {
+					pn.MarketNum = year
+				} else if serviceId == "5" {
+					pn.BuyNum = year
+				} else if serviceId == "4" || serviceId == "19" {
+					pn.EntNum = year
+				}
+			}
+		default:
+			return nil, fmt.Errorf("未知套餐id %d", comboId)
+		}
+	}
+	return pn, nil
+}

+ 26 - 0
run/flushDb/pdfFlushPower/main.go

@@ -0,0 +1,26 @@
+package main
+
+import (
+	"context"
+	_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
+	"github.com/gogf/gf/v2/frame/g"
+	"time"
+)
+
+var (
+	now          = time.Now()
+	flushEntTime = g.Cfg().MustGet(context.TODO(), "flushEndTime").String()
+
+	createTime, _ = time.Parse(time.DateTime, g.Cfg().MustGet(context.TODO(), "flushCreateTime").String())
+)
+
+func main() {
+	flushEntErr := flushEnt(context.TODO())
+	if flushEntErr != nil {
+		g.Log().Errorf(context.TODO(), "flushEntErr %v", flushEntErr)
+	}
+	flushPersonalErr := flushPersonal(context.TODO())
+	if flushPersonalErr != nil {
+		g.Log().Errorf(context.TODO(), "flushPersonalErr %v", flushPersonalErr)
+	}
+}