Commit c2fdaaf3 authored by hujiebin's avatar hujiebin

又多了两个脚本

parent d465d244
......@@ -2,3 +2,7 @@ sync_group_code:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o sync_group_code sync_group_code.go
user_charge:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o user_charge_2 user_charge.go
fruit_machine_charge:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o fruit_machine_charge fruit_machine_charge.go
simple_user_charge:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o simple_user_charge12 simple_user_charge.go
package main
import (
"fmt"
"git.hilo.cn/hilo-common/script/mysql"
"github.com/tealeg/xlsx"
"time"
)
type FruitDayChargeData struct {
Date string // 日期
FruitUserNum int // 水果机参与人数
Recycle float64 // 总回收 M
DayChargeCnt int // 当日充值用户数
DayChargeUserIds []uint64 // 当日充值用户id
DayChargeMoney int64 // 当日充值金额
ChargeRecycle float64 // 充值用户回收 M
}
func ats2(a interface{}) string {
return fmt.Sprintf("%v", a)
}
type UserAndMoney struct {
UserId uint64
Money int64 // 美分
}
// 1月的所有充值用户明细
func main() {
startTime := time.Date(2023, time.January, 1, 0, 0, 0, 0, time.Local)
endTime := time.Date(2023, time.March, 1, 0, 0, 0, 0, time.Local)
var data []FruitDayChargeData
for startTime.Before(endTime) {
date := startTime.Format("2006-01-02")
um1, err := GetDateChargeUserAndMoney(date)
if err != nil {
panic(err)
}
um2, err := GetUserDealerTransferUserAndMoney(date)
if err != nil {
panic(err)
}
userChargeNum := make(map[uint64]struct{})
userChargeMoney := int64(0)
for _, um := range um1 {
userChargeNum[um.UserId] = struct{}{}
userChargeMoney += um.Money
}
for _, um := range um2 {
userChargeNum[um.UserId] = struct{}{}
userChargeMoney += um.Money
}
var userIds []uint64
for userId := range userChargeNum {
userIds = append(userIds, userId)
}
userNum, err := GetFruitUserNum(date)
if err != nil {
panic(err)
}
fruitRecycle, err := GetFruitRecycle(date)
if err != nil {
panic(err)
}
fruitChargeRecycle, err := GetFruitChargeRecycle(date, userIds)
if err != nil {
panic(err)
}
data = append(data, FruitDayChargeData{
Date: date,
FruitUserNum: userNum,
Recycle: float64(fruitRecycle) * 0.05 / 1000000, // 1M
DayChargeCnt: len(userChargeNum),
DayChargeUserIds: userIds,
DayChargeMoney: userChargeMoney,
ChargeRecycle: float64(fruitChargeRecycle) * 0.05 / 1000000, // 1M
})
startTime = startTime.AddDate(0, 0, 1)
}
excelFileName := fmt.Sprintf("./水果机充值数据%s.xlsx", time.Now().Format("20060102"))
xlFile := xlsx.NewFile()
sheet, _ := xlFile.AddSheet("charge")
row := sheet.AddRow()
c1, c2, c3, c4, c5, c6 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell()
c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value = "日期", "参与人数", "总回收 M", "当日充值用户数", "当日充值金额", "充值用户回收 M"
for _, d := range data {
row := sheet.AddRow()
c1, c2, c3, c4, c5, c6 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell()
c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value = d.Date, ats2(d.FruitUserNum), ats2(d.Recycle), ats2(d.DayChargeCnt), ats2(d.DayChargeMoney), ats2(d.ChargeRecycle)
}
_ = xlFile.Save(excelFileName)
}
// 获取充值用户以及金额
// return 充值userIds,充值金额
func GetDateChargeUserAndMoney(date string) ([]UserAndMoney, error) {
var money []UserAndMoney
if err := mysql.ProdReadOnlyDB.Table("diamond_account_detail AS d").
Joins("INNER JOIN pay_order AS p ON d.origin_id = p.id").
Where("DATE(d.created_time) >= ? AND DATE(d.created_time) <= ? AND operate_type in (?) AND add_reduce = 1",
date, date, []int{int(4), int(42), int(55), int(68)}).
Group("d.user_id").
Select("d.user_id,SUM(p.price) as money").Scan(&money).Error; err != nil {
return nil, err
}
return money, nil
}
// 获取代理充值用户以及金额
// return 充值userIds,充值金额
func GetUserDealerTransferUserAndMoney(date string) ([]UserAndMoney, error) {
var money []UserAndMoney
if err := mysql.ProdReadOnlyDB.Table("diamond_account_detail AS d").
Joins("INNER JOIN dealer_transfer_detail AS t ON d.origin_id = t.id").
Where("DATE(d.created_time) >= ? AND DATE(d.created_time) <= ? AND operate_type = ? AND add_reduce = 1",
date, date, 26).Select("d.user_id,SUM(t.dollar) as money").
Group("d.user_id").
Scan(&money).Error; err != nil {
return nil, err
}
return money, nil
}
// 获取参与人数
func GetFruitUserNum(date string) (int, error) {
var stake int
if err := mysql.ProdReadOnlyDB.Table("fruit_machine_stake").Where("`date`= ?", date).Select("count(DISTINCT user_id) as total").Scan(&stake).Error; err != nil {
return stake, err
}
return stake, nil
}
// 总回收
func GetFruitRecycle(date string) (int, error) {
var total int
if err := mysql.ProdReadOnlyDB.Table("fruit_machine").Where("`date`= ?", date).Select("SUM(stake) as total").Scan(&total).Error; err != nil {
return total, err
}
return total, nil
}
// 充值用户回收
func GetFruitChargeRecycle(date string, userIds []uint64) (int, error) {
var total int
if err := mysql.ProdReadOnlyDB.Table("fruit_machine_stake").Where("`date`= ?", date).Where("user_id in ?", userIds).
Select("SUM(stake) as total").Scan(&total).Error; err != nil {
return total, err
}
return total, nil
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment