cp_pairs_stat.go 6.52 KB
Newer Older
hujiebin's avatar
1  
hujiebin committed
1 2 3 4 5 6 7
package main

import (
	"fmt"
	"git.hilo.cn/hilo-common/script/model"
	"git.hilo.cn/hilo-common/script/mysql"
	"github.com/tealeg/xlsx"
hujiebin's avatar
hujiebin committed
8
	"time"
hujiebin's avatar
1  
hujiebin committed
9 10
)

hujiebin's avatar
hujiebin committed
11 12 13 14 15 16 17 18
// cp等级积分
var CpLevelPoints = map[uint64]uint64{
	0: 0,
	1: 200000,
	2: 800000,
	3: 1000000,
	4: 3000000,
	5: 10000000,
hujiebin's avatar
1  
hujiebin committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
}

func ats26(a interface{}) string {
	return fmt.Sprintf("%v", a)
}

func getAreaByCode2(code string) string {
	sql := "SELECT area FROM res_country c,user u WHERE u.country = c.name AND u.code = ?"
	var area int
	if err := mysql.ProdReadOnlyDB.Raw(sql, code).Scan(&area).Error; err != nil {
		panic(err)
	}
	if area == 1 {
		return "阿语区"
	}
	return "非阿语区"
}

hujiebin's avatar
hujiebin committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
func getChargeMoneyDiamond(uid uint64, sinceTime time.Time) int64 {
	type R struct {
		Money   int64
		Diamond int64
	}
	var money R
	if err := mysql.ProdReadOnlyDB.Table("diamond_account_detail AS d").
		Joins("INNER JOIN pay_order AS p ON d.origin_id = p.id").
		Where("d.user_id in (?) AND operate_type in (?) AND add_reduce = 1",
			uid, []int{int(4), int(42), int(55), int(68)}).
		Where("d.created_time >= ?", sinceTime).
		Order("d.user_id").
		Select("SUM(p.price) as money,SUM(d.num) as diamond").Scan(&money).Error; err != nil {
		return 0
	}
	type R2 struct {
		Price   int64
		Diamond int64
	}
	var money2 R2
	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("d.user_id in (?) AND operate_type = ? AND add_reduce = 1",
			uid, 26).Select("SUM(t.dollar) as price,SUM(d.num) as diamond").
		Where("d.created_time >= ?", sinceTime).
		Order("d.user_id").
		Scan(&money2).Error; err != nil {
		return 0
	}
	return money.Money + money2.Price
}

hujiebin's avatar
1  
hujiebin committed
69
type CpInfo struct {
hujiebin's avatar
hujiebin committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	Id                uint64
	UserId1           uint64
	UserId2           uint64
	Code1             string
	Code2             string
	UserId1Area       string
	UserId2Area       string
	Level             uint64
	Points            uint64
	CreatedTime       time.Time
	Charge            float64
	M1                int
	M2                int
	M3                int
	M4                int
	M5                int
	M6                int
	M7                int
	ModifyAnniversary int
hujiebin's avatar
1  
hujiebin committed
89 90 91
}

type CpLevel struct {
hujiebin's avatar
hujiebin committed
92 93
	CpId        uint64
	Level       uint64
hujiebin's avatar
hujiebin committed
94
	Points      uint64
hujiebin's avatar
hujiebin committed
95
	CreatedTime time.Time
hujiebin's avatar
1  
hujiebin committed
96 97 98
}

func main() {
hujiebin's avatar
hujiebin committed
99
	var cpInfos []*CpInfo
hujiebin's avatar
hujiebin committed
100
	if err := mysql.ProdReadOnlyDB.Table("cp_relation").Select("id,user_id1,user_id2").Find(&cpInfos).Error; err != nil {
hujiebin's avatar
1  
hujiebin committed
101 102 103 104 105 106 107
		panic(err)
	}
	var cpIds []uint64
	for _, v := range cpInfos {
		cpIds = append(cpIds, v.Id)
	}
	var cpLevels []CpLevel
hujiebin's avatar
hujiebin committed
108
	if err := mysql.ProdReadOnlyDB.Table("cp_level").Select("cp_id,level,points,created_time").Where("cp_id in ?", cpIds).Find(&cpLevels).Error; err != nil {
hujiebin's avatar
1  
hujiebin committed
109 110 111 112 113 114 115 116 117 118
		panic(err)
	}
	var userIds []uint64
	var users []model.User
	for i, v := range cpInfos {
		userIds = append(userIds, v.UserId1)
		userIds = append(userIds, v.UserId2)
		for _, v2 := range cpLevels {
			if v.Id == v2.CpId {
				cpInfos[i].Level = v2.Level
hujiebin's avatar
hujiebin committed
119
				cpInfos[i].Points = v2.Points
hujiebin's avatar
hujiebin committed
120
				cpInfos[i].CreatedTime = v2.CreatedTime
hujiebin's avatar
1  
hujiebin committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
				break
			}
		}
	}
	if err := mysql.ProdReadOnlyDB.Table("user").Where("id in ?", userIds).Find(&users).Error; err != nil {
		panic(err)
	}
	uM := make(map[uint64]model.User)
	for i, v := range users {
		uM[v.Id] = users[i]
	}
	for i, v := range cpInfos {
		cpInfos[i].Code1 = uM[v.UserId1].Code
		cpInfos[i].Code2 = uM[v.UserId2].Code
		cpInfos[i].UserId1Area = getAreaByCode2(uM[v.UserId1].Code)
		cpInfos[i].UserId2Area = getAreaByCode2(uM[v.UserId2].Code)
hujiebin's avatar
hujiebin committed
137 138
		cpInfos[i].Points = v.Points + CpLevelPoints[v.Level]
		cpInfos[i].Charge = float64(getChargeMoneyDiamond(v.UserId1, v.CreatedTime)+getChargeMoneyDiamond(v.UserId2, v.CreatedTime)) / 100
hujiebin's avatar
hujiebin committed
139
		cpInfos[i].ModifyAnniversary, cpInfos[i].M1, cpInfos[i].M2, cpInfos[i].M3, cpInfos[i].M4, cpInfos[i].M5, cpInfos[i].M6, cpInfos[i].M7 = modifyAnniversary(v.Id)
hujiebin's avatar
1  
hujiebin committed
140 141 142 143 144 145 146 147
	}
	excelFileName := fmt.Sprintf("./cp数据.xlsx")
	xlFile := xlsx.NewFile()
	sheet, err := xlFile.AddSheet("cp")
	if err != nil {
		panic(err)
	}
	row := sheet.AddRow()
hujiebin's avatar
hujiebin committed
148 149 150 151 152
	c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell()
	c11, c12, c13, c14, c15, c16, c17 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell()
	c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value, c7.Value, c8.Value, c9.Value, c10.Value = "cp ID", "userId1", "userId2", "user1区域", "user2区域", "等级", "积分", "绑定后充值金额 $", "绑定时间", "是否修改纪念日"
	c11.Value, c12.Value, c13.Value, c14.Value, c15.Value, c16.Value, c17.Value = "我们在一起", "XXX的生日", "XXX的生日", "第一次说我爱你", "第一次亲吻", "结婚纪念日", "用户自行编辑"

hujiebin's avatar
1  
hujiebin committed
153 154
	for _, d := range cpInfos {
		row := sheet.AddRow()
hujiebin's avatar
hujiebin committed
155 156 157 158 159
		c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell()
		c11, c12, c13, c14, c15, c16, c17 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell()
		c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value, c7.Value, c8.Value, c9.Value, c10.Value =
			ats26(d.Id), ats26(d.Code1), ats26(d.Code2), ats26(d.UserId1Area), ats26(d.UserId2Area), ats26(d.Level), ats26(d.Points), ats26(d.Charge), ats26(d.CreatedTime.Format("2006-01-02 15:04:05")), ats26(d.ModifyAnniversary)
		c11.Value, c12.Value, c13.Value, c14.Value, c15.Value, c16.Value, c17.Value = ats26(d.M1), ats26(d.M2), ats26(d.M3), ats26(d.M4), ats26(d.M5), ats26(d.M6), ats26(d.M7)
hujiebin's avatar
1  
hujiebin committed
160 161 162 163 164 165
	}
	_ = xlFile.Save(excelFileName)

	//var data []CpPairData

}
hujiebin's avatar
hujiebin committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

type CpAnniversary struct {
	CpId        uint64
	Sort        int
	Type        int
	MsgId       int
	Timestamp   int
	CreatedTime time.Time `gorm:"->"`
	UpdatedTime time.Time `gorm:"->"`
}

func modifyAnniversary(cpId uint64) (t, m1, m2, m3, m4, m5, m6, m7 int) {
	var an []CpAnniversary
	if err := mysql.ProdReadOnlyDB.Model(CpAnniversary{}).Where("cp_id = ? AND timestamp > 0 ", cpId).Find(&an).Error; err != nil {
		panic(err)
	}
	for _, a := range an {
		if a.MsgId == 259 && a.UpdatedTime.Second() != 0 {
			m1 = 1
		}
		if a.MsgId == 260 && a.Type == 2 {
			m2 = 1
		}
		if a.MsgId == 260 && a.Type == 3 {
			m3 = 1
		}
		if a.MsgId == 261 {
			m4 = 1
		}
		if a.MsgId == 262 {
			m5 = 1
		}
		if a.MsgId == 263 {
			m6 = 1
		}
		if a.MsgId == 0 {
			m7++
		}
	}
	if m1 > 0 || m2 > 0 || m3 > 0 || m4 > 0 || m5 > 0 || m6 > 0 || m7 > 0 {
		t = 1
	}
	return
}