430 lines
12 KiB
Go
430 lines
12 KiB
Go
package innerserverser
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"github.com/360EntSecGroup-Skylar/excelize"
|
|
"stat/api/proto"
|
|
"stat/common/log"
|
|
"stat/common/utils"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
var exportTransWaterTitle = []string{"日活跃", "日注册", "视频日充值", "棋牌日充值", "交易笔数"}
|
|
|
|
const sheetName = "Sheet1"
|
|
|
|
// 按字母
|
|
func NewXlslFile(f *excelize.File, list []*proto.InnerDailyRecharge) {
|
|
// 设置表头样式
|
|
headStyleID, _ := f.NewStyle(titlestyle)
|
|
filePath := "/usr/local" + "/bakexel/" + "vvvv.xlsx"
|
|
// 设置列宽
|
|
f.SetColWidth("Sheet1", "A", "L", 23)
|
|
|
|
// 设置表头行高
|
|
//f.SetRowHeight("Sheet1", 1, 25)
|
|
f.SetCellStyle("Sheet1", "A1", "L1", headStyleID)
|
|
idx := f.NewSheet(sheetName)
|
|
f.MergeCell("Sheet1", "A1", "A2")
|
|
f.SetCellValue(sheetName, "A1", "日期")
|
|
f.SetCellStyle(sheetName, "A4", "A50", headStyleID)
|
|
f.SetActiveSheet(idx)
|
|
now := time.Now()
|
|
day := now.Format("2006-01")
|
|
begin, end := utils.MonthDateRange(now)
|
|
beginday := utils.BeginOfMonth(now).Day()
|
|
endday := utils.EndOfMonth(now).Day()
|
|
index := "A"
|
|
for i := beginday; i <= endday; i++ {
|
|
tmp := fmt.Sprintf("%s%d", index, i+2)
|
|
vvvvv := fmt.Sprintf("%s-%d", day, i)
|
|
f.SetCellValue(sheetName, tmp, vvvvv)
|
|
}
|
|
one := 'A'
|
|
begin0 := 'B'
|
|
temp0 := begin0
|
|
pre := ""
|
|
for _, info := range list {
|
|
t0 := temp0
|
|
countt0 := 1
|
|
for i := 1; i < 5; i++ {
|
|
temp0 = t0 + int32(countt0)
|
|
countt0++
|
|
if temp0 > 'Z' {
|
|
one++
|
|
temp0 = 'A'
|
|
t0 = temp0
|
|
countt0 = 1
|
|
}
|
|
}
|
|
//merge := setcellindex + 5 + int32(index*5)
|
|
beginmerge := fmt.Sprintf("%s%s1", pre, string(begin0))
|
|
if one > 'A' {
|
|
pre = string(one - 1)
|
|
}
|
|
endmerge := fmt.Sprintf("%s%s1", pre, string(temp0))
|
|
temp0 += 2
|
|
if temp0 > 'Z' {
|
|
one++
|
|
temp0 = 'A' + temp0 - 'Z' - 1
|
|
if one > 'A' {
|
|
pre = string(one - 1)
|
|
}
|
|
}
|
|
begin0 = temp0
|
|
f.MergeCell("Sheet1", beginmerge, endmerge)
|
|
//tmp := fmt.Sprintf("%s1", string(index0))
|
|
f.SetCellValue(sheetName, beginmerge, info.Name)
|
|
fmt.Println(beginmerge, endmerge)
|
|
}
|
|
err := f.SaveAs(filePath)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
fmt.Println(begin, end)
|
|
}
|
|
|
|
// 按行列索引
|
|
func NewXlslFile0(f *excelize.File, list []*proto.InnerDailyRecharge, titlestyle, cellstyle int) error {
|
|
// 设置列宽
|
|
f.SetColWidth(sheetName, "A", "AAA", 19)
|
|
|
|
// 设置表头行高
|
|
f.SetRowHeight(sheetName, 1, 25)
|
|
f.SetCellStyle(sheetName, "A1", "ZZ1", titlestyle)
|
|
f.SetCellStyle(sheetName, "A2", "ZZ2", titlestyle)
|
|
f.SetCellStyle(sheetName, "A3", "ZZ3", titlestyle)
|
|
|
|
idx := f.NewSheet(sheetName)
|
|
f.SetCellValue(sheetName, "A1", "日期")
|
|
f.SetActiveSheet(idx)
|
|
|
|
now := time.Now()
|
|
dayformat := now.Format("2006-01")
|
|
|
|
//begin, end := utils.MonthDateRange(now)
|
|
beginday := utils.BeginOfMonth(now).Day()
|
|
endday := utils.EndOfMonth(now).Day()
|
|
|
|
//设置日期
|
|
index := "A"
|
|
for i := beginday; i <= endday; i++ {
|
|
tmp := fmt.Sprintf("%s%d", index, i+3)
|
|
rowday := fmt.Sprintf("%s-%02d", dayformat, i)
|
|
|
|
f.SetCellValue(sheetName, tmp, rowday)
|
|
}
|
|
|
|
//新文件设置固定的主题和style
|
|
row := 1
|
|
col := 2
|
|
count := col
|
|
for _, info := range list {
|
|
begincol := count
|
|
count = count + 4
|
|
titleindex := 0
|
|
titlemap := make(map[string]string, 0)
|
|
for i := begincol; i <= count; i++ {
|
|
if titleindex < len(exportTransWaterTitle) {
|
|
titlecell, serr := utils.CoordinatesToCellName(begincol+titleindex, row+2)
|
|
if serr != nil {
|
|
log.Error("NewXlslFile0 CoordinatesToCellName ", log.Any("err", serr),
|
|
log.Any("title", exportTransWaterTitle[titleindex]))
|
|
return serr
|
|
}
|
|
titlemap[titlecell] = exportTransWaterTitle[titleindex]
|
|
}
|
|
titleindex++
|
|
}
|
|
info.BeginCol = begincol
|
|
// 第一行产品
|
|
begincell, serr := utils.CoordinatesToCellName(begincol, row)
|
|
if serr != nil {
|
|
log.Error("NewXlslFile0 CoordinatesToCellName ", log.Any("err", serr),
|
|
log.Any("infoname", info.Name))
|
|
return serr
|
|
}
|
|
endcell, serr := utils.CoordinatesToCellName(count, row)
|
|
if serr != nil {
|
|
log.Error("NewXlslFile0 CoordinatesToCellName ", log.Any("err", serr),
|
|
log.Any("infoname", info.Name))
|
|
return serr
|
|
}
|
|
// 第二行appid
|
|
appidbegincell, serr := utils.CoordinatesToCellName(begincol, row+1)
|
|
if serr != nil {
|
|
log.Error("NewXlslFile0 CoordinatesToCellName ", log.Any("err", serr),
|
|
log.Any("infoappid", info.AppId))
|
|
return serr
|
|
}
|
|
appidendcell, serr := utils.CoordinatesToCellName(count, row+1)
|
|
if serr != nil {
|
|
log.Error("NewXlslFile0 CoordinatesToCellName ", log.Any("err", serr),
|
|
log.Any("infoappid", info.AppId))
|
|
return serr
|
|
}
|
|
f.MergeCell(sheetName, begincell, endcell)
|
|
|
|
f.MergeCell(sheetName, appidbegincell, appidendcell)
|
|
//tmp := fmt.Sprintf("%s1", string(index0))
|
|
/*if info.AppId != 0 {
|
|
if info.AppId == 888 {
|
|
//tmp := fmt.Sprintf("%s1", string(index0))
|
|
f.SetCellValue(sheetName, begincell, info.Name+"(集团)")
|
|
} else {
|
|
f.SetCellValue(sheetName, begincell, info.Name+"(内部)")
|
|
}
|
|
} else {
|
|
*/
|
|
f.SetCellValue(sheetName, begincell, info.Name)
|
|
//}
|
|
f.SetCellValue(sheetName, appidbegincell, info.AppId)
|
|
|
|
//第三行app统计的信息
|
|
for key, value := range titlemap {
|
|
//f.SetCellStyle(sheetName, key, key, cellstyle)
|
|
f.SetCellValue(sheetName, key, value)
|
|
}
|
|
count += 2
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 合并单元格
|
|
func MergeCell(f *excelize.File, sheetname string, col int, list []*proto.InnerDailyRecharge) error {
|
|
row := 1
|
|
count := col + 1
|
|
for _, info := range list {
|
|
begincol := count
|
|
count = count + 4
|
|
titleindex := 0
|
|
titlemap := make(map[string]string, 0)
|
|
for i := begincol; i <= count; i++ {
|
|
if titleindex < len(exportTransWaterTitle) {
|
|
titlecell, serr := utils.CoordinatesToCellName(begincol+titleindex, row+2)
|
|
if serr != nil {
|
|
log.Error("MergeCell CoordinatesToCellName ", log.Any("err", serr),
|
|
log.Any("title", exportTransWaterTitle[titleindex]))
|
|
return serr
|
|
}
|
|
titlemap[titlecell] = exportTransWaterTitle[titleindex]
|
|
}
|
|
titleindex++
|
|
}
|
|
begincell, serr := utils.CoordinatesToCellName(begincol, row)
|
|
if serr != nil {
|
|
log.Error("MergeCell CoordinatesToCellName ", log.Any("err", serr),
|
|
log.Any("infoname", info.Name))
|
|
return serr
|
|
}
|
|
endcell, serr := utils.CoordinatesToCellName(count, row)
|
|
if serr != nil {
|
|
log.Error("MergeCell CoordinatesToCellName ", log.Any("err", serr),
|
|
log.Any("infoname", info.Name))
|
|
return serr
|
|
}
|
|
// 第一行appid
|
|
appidbegincell, serr := utils.CoordinatesToCellName(begincol, row+1)
|
|
if serr != nil {
|
|
log.Error("MergeCell CoordinatesToCellName ", log.Any("err", serr),
|
|
log.Any("appid", info.AppId))
|
|
return serr
|
|
}
|
|
appidendcell, serr := utils.CoordinatesToCellName(count, row+1)
|
|
if serr != nil {
|
|
log.Error("MergeCell CoordinatesToCellName ", log.Any("err", serr),
|
|
log.Any("appid", info.AppId))
|
|
return serr
|
|
}
|
|
info.BeginCol = begincol
|
|
f.MergeCell(sheetname, begincell, endcell)
|
|
/*if info.AppId != 0 {
|
|
if info.AppId == 888 {
|
|
f.SetCellValue(sheetname, begincell, info.Name+"(集团)")
|
|
} else {
|
|
f.SetCellValue(sheetname, begincell, info.Name+"(内部)")
|
|
}
|
|
} else {
|
|
*/
|
|
f.SetCellValue(sheetname, begincell, info.Name)
|
|
//}
|
|
|
|
f.MergeCell(sheetname, appidbegincell, appidendcell)
|
|
//tmp := fmt.Sprintf("%s1", string(index0))
|
|
f.SetCellValue(sheetname, appidbegincell, info.AppId)
|
|
for key, value := range titlemap {
|
|
f.SetCellValue(sheetname, key, value)
|
|
}
|
|
count += 2
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func OpenOldXlsl(f *excelize.File, list []*proto.InnerDailyRecharge) error {
|
|
idx := f.GetSheetIndex(sheetName)
|
|
f.SetActiveSheet(idx)
|
|
|
|
cols := f.GetRows(sheetName)
|
|
begincol := 0
|
|
//查找数据开始单元格,新数据先合并单元格
|
|
/*for rowindex, col := range cols {
|
|
for colindex, rowCell := range col {
|
|
if rowCell == "" {
|
|
continue
|
|
}
|
|
if rowindex == 1 {
|
|
for _, info := range list {
|
|
if rowCell == string(info.AppId) {
|
|
info.BeginRow = rowindex + 1
|
|
info.BeginCol = colindex + 1
|
|
info.IsOld = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
|
|
if len(cols) < 2 || len(cols[1]) < len(list) {
|
|
return errors.New("OpenOldXlsl cols < 0")
|
|
}
|
|
|
|
/*for rowindex := 0; rowindex < len(cols); rowindex++ {
|
|
if cols[rowindex][0] != "" {
|
|
for _, info := range list {
|
|
if cols[rowindex][0] == info.DateTime {
|
|
info.BeginRow = rowindex + 1
|
|
info.IsOld = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
//旧的数据单元格开始信息
|
|
for colindex := 0; colindex < len(cols[1]); colindex++ {
|
|
for _, info := range list {
|
|
appid := strconv.Itoa(int(info.AppId))
|
|
if cols[1][colindex] != "" && cols[1][colindex] == appid {
|
|
info.BeginCol = colindex + 1
|
|
info.IsOld = true
|
|
}
|
|
}
|
|
}
|
|
|
|
//新的单元格从第几列信息
|
|
newlist := make([]*proto.InnerDailyRecharge, 0)
|
|
for _, info := range list {
|
|
if info.IsOld {
|
|
if info.BeginCol > begincol {
|
|
begincol = info.BeginCol
|
|
}
|
|
} else {
|
|
newlist = append(newlist, info)
|
|
}
|
|
}
|
|
if len(newlist) > 0 {
|
|
serr := MergeCell(f, sheetName, begincol+5, newlist)
|
|
if serr != nil {
|
|
log.Error("OpenOldXlsl MergeCell ", log.Any("err", serr))
|
|
return serr
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func NewXlslFile2(f *excelize.File, list []*AppInformation, titlestyle, cellstyle int, now time.Time) error {
|
|
// 设置列宽
|
|
f.SetColWidth(sheetName, "A", "AAA", 19)
|
|
|
|
// 设置表头行高
|
|
f.SetRowHeight(sheetName, 1, 25)
|
|
f.SetCellStyle(sheetName, "A1", "ZZ1", titlestyle)
|
|
f.SetCellStyle(sheetName, "A2", "ZZ2", titlestyle)
|
|
f.SetCellStyle(sheetName, "A3", "ZZ3", titlestyle)
|
|
|
|
idx := f.NewSheet(sheetName)
|
|
f.SetCellValue(sheetName, "A1", "日期")
|
|
f.SetActiveSheet(idx)
|
|
|
|
dayformat := now.Format("2006-01")
|
|
|
|
//begin, end := utils.MonthDateRange(now)
|
|
beginday := utils.BeginOfMonth(now).Day()
|
|
endday := utils.EndOfMonth(now).Day()
|
|
|
|
//设置日期
|
|
index := "A"
|
|
for i := beginday; i <= endday; i++ {
|
|
tmp := fmt.Sprintf("%s%d", index, i+3)
|
|
rowday := fmt.Sprintf("%s-%02d", dayformat, i)
|
|
f.SetCellValue(sheetName, tmp, rowday)
|
|
}
|
|
// 设置汇总
|
|
tmp := fmt.Sprintf("%s%d", index, endday+1+3)
|
|
f.SetCellValue(sheetName, tmp, "汇总")
|
|
|
|
//新文件设置固定的主题和style
|
|
row := 1
|
|
col := 2
|
|
count := col
|
|
for _, info := range list {
|
|
begincol := count
|
|
count = count + 4
|
|
titleindex := 0
|
|
titlemap := make(map[string]string, 0)
|
|
for i := begincol; i <= count; i++ {
|
|
if titleindex < len(exportTransWaterTitle) {
|
|
titlecell, serr := utils.CoordinatesToCellName(begincol+titleindex, row+2)
|
|
if serr != nil {
|
|
log.Error("NewXlslFile0 CoordinatesToCellName ", log.Any("err", serr),
|
|
log.Any("title", exportTransWaterTitle[titleindex]))
|
|
return serr
|
|
}
|
|
titlemap[titlecell] = exportTransWaterTitle[titleindex]
|
|
}
|
|
titleindex++
|
|
}
|
|
info.BeginCol = begincol
|
|
// 第一行产品
|
|
begincell, serr := utils.CoordinatesToCellName(begincol, row)
|
|
if serr != nil {
|
|
log.Error("NewXlslFile0 CoordinatesToCellName ", log.Any("err", serr),
|
|
log.Any("infoname", info.Name))
|
|
return serr
|
|
}
|
|
endcell, serr := utils.CoordinatesToCellName(count, row)
|
|
if serr != nil {
|
|
log.Error("NewXlslFile0 CoordinatesToCellName ", log.Any("err", serr),
|
|
log.Any("infoname", info.Name))
|
|
return serr
|
|
}
|
|
// 第二行appid
|
|
appidbegincell, serr := utils.CoordinatesToCellName(begincol, row+1)
|
|
if serr != nil {
|
|
log.Error("NewXlslFile0 CoordinatesToCellName ", log.Any("err", serr),
|
|
log.Any("infoappid", info.Id))
|
|
return serr
|
|
}
|
|
appidendcell, serr := utils.CoordinatesToCellName(count, row+1)
|
|
if serr != nil {
|
|
log.Error("NewXlslFile0 CoordinatesToCellName ", log.Any("err", serr),
|
|
log.Any("infoappid", info.Id))
|
|
return serr
|
|
}
|
|
f.MergeCell(sheetName, begincell, endcell)
|
|
f.MergeCell(sheetName, appidbegincell, appidendcell)
|
|
f.SetCellValue(sheetName, begincell, info.Name)
|
|
f.SetCellValue(sheetName, appidbegincell, info.Id)
|
|
//第三行app统计的信息
|
|
for key, value := range titlemap {
|
|
f.SetCellValue(sheetName, key, value)
|
|
}
|
|
count += 2
|
|
}
|
|
return nil
|
|
}
|