package timetask

import (
	"bytes"
	"fmt"
	"math"
	"net/http"
	qu "qfw/util"
	"strings"
	"time"
	"util"
)

var LuaUserMap map[string]map[string]string
var LuaUserInfoMap map[string]*LuaUserInfo

type UserTextInfo struct {
	Username             string
	FailedTaskCount      int
	FailedTaskOverdueDay int
	HeartErrCount        int
	NoCollectDataDay     int
}

type LuaUserInfo struct {
	Username string
	Mobile   string
}

//
var TitleContentModel = `
	截止目前，爬虫共有未通过任务<font color=\"warning\">%d个</font>，异常心跳爬虫<font color=\"warning\">%d个</font>。请及时处理！\n
`
var UserContentModel = `
     >人员：<font color=\"warning\">%s</font>
     >未通过任务：<font color=\"warning\">%d个</font><font color=\"info\">(最早任务已逾期%d天)</font>
     >异常心跳爬虫：<font color=\"warning\">%d个</font><font color=\"info\">(已有爬虫%d天未采集数据)</font>\n
`
var MarkdownModel = `{
    "msgtype": "markdown",
    "markdown": {
        "content": "%s"
    }
}`
var TextModel = `{
    "msgtype": "text",
    "text": {
        "content": "",
        "mentioned_mobile_list":[%s]
    }
}`

func GetLuaUserInfo() {
	LuaUserInfoMap = map[string]*LuaUserInfo{}
	for eu, info := range LuaUserMap {
		LuaUserInfoMap[eu] = &LuaUserInfo{
			Username: info["username"],
			Mobile:   info["mobile"],
		}
	}
}

// SendInfoToWxWork
func SendInfoToWxWork() {
	defer qu.Catch()
	qu.Debug("企业微信发送提示信息")
	failedTaskCount, heartCodeCount := 0, 0   //总未通过任务个数，总待处理心跳异常爬虫个数
	userTextMap := map[string]*UserTextInfo{} //key:mobile
	for eu, userInfo := range LuaUserInfoMap {
		textInfo := &UserTextInfo{}
		textInfo.Username = userInfo.Username
		//1、未通过任务信息
		list_task, _ := util.MgoE.Find("task",
			map[string]interface{}{"s_modify": eu, "i_state": 5},
			map[string]interface{}{"l_complete": 1},
			map[string]interface{}{"l_complete": 1},
			false, -1, -1)
		taskLen := len(*list_task)
		textInfo.FailedTaskCount = taskLen //个人未通过任务个数赋值
		failedTaskCount += taskLen         //总未通过个数++
		if taskLen > 0 {
			complete := qu.Int64All((*list_task)[0]["l_complete"]) //未通过任务中最迟完成时间最早的任务
			odDay := int(math.Floor(float64(time.Now().Unix()-complete) / float64(86400)))
			textInfo.FailedTaskOverdueDay = odDay //个人未通过任务最早逾期天数赋值
		}
		//2、爬虫心跳信息
		query := map[string]interface{}{
			"modifyuser": eu,
			"del":        false,
			"$or": []interface{}{
				map[string]interface{}{
					"event": map[string]interface{}{
						"$ne": 7500,
					},
					"list": map[string]interface{}{
						"$lte": util.GetTime(0),
					},
				},
				map[string]interface{}{
					"event": 7500,
					"list": map[string]interface{}{
						"$lte": util.GetTime(-1),
					},
				},
			},
		}
		qu.Debug("heart query:", query)
		list_code, _ := util.MgoS.Find("spider_heart",
			query,
			map[string]interface{}{"list": 1},
			map[string]interface{}{"list": 1},
			false, -1, -1)
		codeLen := len(*list_code)
		textInfo.HeartErrCount = codeLen //个人异常心跳爬虫个数赋值
		heartCodeCount += codeLen        //总异常心跳爬虫个数++
		if codeLen > 0 {
			listTime := qu.Int64All((*list_code)[0]["list"]) //未通过任务中最迟完成时间最早的任务
			ncDay := int(math.Floor(float64(time.Now().Unix()-listTime) / float64(86400)))
			textInfo.NoCollectDataDay = ncDay //个人未通过任务最早逾期天数赋值
		}
		userTextMap[userInfo.Mobile] = textInfo
	}
	//拼接content
	resultContent := fmt.Sprintf(TitleContentModel, failedTaskCount, heartCodeCount)
	mobileArr := []string{}
	for mobile, t := range userTextMap {
		mobileArr = append(mobileArr, "\""+mobile+"\"")
		resultContent += fmt.Sprintf(UserContentModel, t.Username, t.FailedTaskCount, t.FailedTaskOverdueDay, t.HeartErrCount, t.NoCollectDataDay)
	}
	msg := fmt.Sprintf(MarkdownModel, resultContent)
	qu.Debug("msg", msg)
	toUserMsg := fmt.Sprintf(TextModel, strings.Join(mobileArr, ","))
	qu.Debug("toUserMsg", toUserMsg)
	resp1, err := http.Post(
		"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=97850772-88d0-4544-a2c3-6201aeddff9e",
		"application/json",
		bytes.NewBuffer([]byte(toUserMsg)),
	)
	defer resp1.Body.Close()
	resp2, err := http.Post(
		"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=97850772-88d0-4544-a2c3-6201aeddff9e",
		"application/json",
		bytes.NewBuffer([]byte(msg)),
	)
	defer resp2.Body.Close()
	if err != nil {
		fmt.Println("request error:", err)
	}

}
