package dataStatistics

import (
	"context"
	"fmt"
	_ "github.com/bitly/go-simplejson"
	"github.com/xuri/excelize/v2"
	"go.uber.org/zap"
	"log-server/global"
	"log-server/model/dataStatistics"
	"log-server/model/dataStatistics/request"
	"log-server/model/dataStatistics/response"
	//"time"
)

type ServiceWeChatScannerLedger struct {
}

//type WeChatGameInfo struct {
//	GameName        string
//	LcPriceNew      float64
//	LcPriceRetained float64
//	HmPriceNew      float64
//	HmPriceRetained float64
//}

func (s *ServiceWeChatScannerLedger) WeChatScannerLedgerList(ctx context.Context, api dataStatistics.WeChatScannerLedger, info request.PageInfo, order string, desc bool) ([]*response.WeChatScannerLedgerResponse, int64, error) {

	db := global.GVA_DB.Model(&dataStatistics.WeChatScannerLedger{})
	//global.GVA_LOG.Info(api.NewDate)
	if api.NewDate != "" {
		db = db.Where("new_date = ?", api.NewDate)
	}
	if api.Platform != "" {
		db = db.Where("platform = ?", api.Platform)
	}
	var total int64
	err := db.Count(&total).Error
	if err != nil {
		return nil, 0, err
	}
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	//var statisticsLogs []*log.LogComputer
	var apiList []*response.WeChatScannerLedgerResponse
	db = db.Limit(limit).Offset(offset)
	if order != "" {
		var OrderStr string
		// 设置有效排序key 防止sql注入
		// 感谢 Tom4t0 提交漏洞信息
		orderMap := make(map[string]bool, 3)
		orderMap["task_name"] = true
		//orderMap["game_id"] = true
		//orderMap["operator"] = true
		if orderMap[order] {
			if desc {
				OrderStr = order + " desc"
			} else {
				OrderStr = order + " desc"
			}
		} else { // didn't matched any order key in `orderMap`
			global.GVA_LOG.Error("获取失败!", zap.Error(err))
			return apiList, total, err
		}
		err = db.Order(OrderStr).Find(&apiList).Error
	} else {
		err = db.Order("task_name").Find(&apiList).Error
	}
	if err != nil {
		return nil, 0, err
	}
	var arrayResponse []*response.WeChatScannerLedgerResponse
	for _, e := range apiList {
		oneResponse := new(response.WeChatScannerLedgerResponse)
		oneResponse.Id = e.Id
		oneResponse.NewDate = e.NewDate
		oneResponse.Platform = e.Platform
		oneResponse.TaskId = e.TaskId
		oneResponse.TaskName = e.TaskName
		oneResponse.Ledger = e.Ledger
		oneResponse.LedgerNew = e.LedgerNew
		oneResponse.LedgerRetained = e.LedgerRetained
		oneResponse.UnitPriceNew = e.UnitPriceNew
		oneResponse.UnitPriceRetained = e.UnitPriceRetained

		arrayResponse = append(arrayResponse, oneResponse)
	}
	return arrayResponse, total, err
}

func (exa *ServiceWeChatScannerLedger) ParseInfoList2Excel(infoList []*response.WeChatScannerLedgerResponse, filePath string) error {
	excel := excelize.NewFile()
	err := excel.SetSheetRow("Sheet1", "A1", &[]string{"日期", "任务ID", "任务名称", "平台", "新增单价", "留存单价", "总消耗", "新增消耗", "留存消耗"})
	if err != nil {
		return err
	}

	for i, one := range infoList {
		axis := fmt.Sprintf("A%d", i+2)
		err = excel.SetSheetRow("Sheet1", axis, &[]interface{}{
			one.NewDate,
			one.TaskId,
			one.TaskName,
			one.Platform,
			one.UnitPriceNew,
			one.UnitPriceRetained,
			one.Ledger,
			one.LedgerNew,
			one.LedgerRetained,
		})
		if err != nil {
			return err
		}
	}
	err = excel.SaveAs(filePath)
	return err
}
