package task

import (
	"fmt"
	"go.uber.org/zap"
	"log-server/global"
	"log-server/model/task"
	"log-server/model/typeManage"
	"log-server/utils"
	"time"
)

type Common struct {
	Person typeManage.ResponsiblePerson
	TConf  task.TaskConf
}

type SendMsg struct {
	MsgType  string `json:"msgtype"`
	Markdown struct {
		Content string `json:"content"`
	} `json:"markdown"`
}

type SendTextMsg struct {
	MsgType string `json:"msgtype"`
	Text    struct {
		MentionedMobileList []string `json:"mentioned_mobile_list"`
	} `json:"text"`
}

type SendTextContent struct {
	MsgType string `json:"msgtype"`
	Text    struct {
		Content             string   `json:"content"`
		MentionedMobileList []string `json:"mentioned_mobile_list"`
	} `json:"text"`
}

func (s *Common) RemindSend(url, content string, mobile []string) {
	var sendTextData SendTextContent
	sendTextData.MsgType = "text"
	sendTextData.Text.MentionedMobileList = mobile
	sendTextData.Text.Content = content
	s.SendMsgData(url, sendTextData)
}

func (s *Common) SendMsgData(url string, params interface{}) {
	_, _ = utils.HttpPost(url, params)
	return
}

func (s *Common) SaveGameAddFee(num, target int, taskName string) {
	fee := new(task.GameAddFee)
	fee.TaskName = taskName
	fee.Num = num
	fee.Target = target
	fee.CreateDate = time.Now()
	global.GVA_DB.Omit("create_time").Create(&fee)
	return
}

// 加付费通知
func (s *Common) FreeSendMsg(num, target, taskId int, taskName, director string) {
	taskConf, err := s.TConf.GetTaskConfByTaskId(taskId)
	if err != nil {
		global.GVA_LOG.Error("FreeSendMsg获取任务配置失败"+director, zap.Error(err))
		return
	}
	if taskConf.AddFeeNotice == 0 {
		taskConf.AddFeeNotice = 10
	}
	hour := time.Now().Hour()
	if hour < 8 || num < taskConf.AddFeeNotice {
		return
	}
	person, err := s.Person.GetUserInfoByName(director)
	if err != nil {
		global.GVA_LOG.Error("FreeSendMsg获取用户数据失败"+director, zap.Error(err))
	}
	if person.PushStatus != 1 {
		return
	}
	personMsg := "# 加付费"
	personMsg += fmt.Sprintf("<font color=\"warning\">%s</font>", time.Now().Format("2006-01-02 15:04:05"))
	personMsg += "\n"
	personMsg += taskName
	personMsg += fmt.Sprintf("<font color=\"warning\"> 加付费 %d, 当前付费目标 %d</font>", num, target)
	url := person.Url
	var sendData SendMsg
	sendData.MsgType = "markdown"
	sendData.Markdown.Content = personMsg
	s.SendMsgData(url, sendData)
}
