package TimeTask

import (
	"ColdVerify_server/lib"
	"ColdVerify_server/lib/wx"
	"ColdVerify_server/logs"
	"ColdVerify_server/models/Account"
	"ColdVerify_server/models/InfoCollection"
	"ColdVerify_server/models/Task"
	"fmt"

	"github.com/robfig/cron/v3"
	"time"
)


func TaskList()  {
	crontab := cron.New(cron.WithSeconds())
	ss := "0 10 * * *"
	_, err := crontab.AddFunc(ss, TaskList_return)
	if err != nil {
		fmt.Printf("err: %v\n", err)
	}
	crontab.Start()
	defer crontab.Stop()
	select {}
}

// 回款
func TaskList_return()  {

	logs.Println("------- 任务回款 定时任务 --------")
	List, _ := Task.Read_Task_List_All()

	for _,T := range List{

		logs.Println("任务ID:",T.Id)

		var InfoCollection_r  InfoCollection.InfoCollection //信息采集
		if len(T.T_InfoCollection_id) == 0 {
			logs.Println("信息采集表获取 为空！！")
			continue;
		}
		var is bool
		InfoCollection_r, is = InfoCollection.Read_InfoCollection(T.T_InfoCollection_id)
		if !is {
			logs.Println("信息采集表获取错误 T_InfoCollection_id ",T.T_InfoCollection_id)
			continue;
		}

		if InfoCollection_r.T_State == 5 {
			logs.Println("已经回款")
			continue;
		}
		if len(T.T_reporting_end_time) == 0{
			logs.Println("正在进行中")
			continue;
		}

		T_collection_time_interval, _ := lib.MinutesDifference(T.T_reporting_end_time, time.Now().Format("2006-01-02 15:04:05") )
		T_collection_time_interval = T_collection_time_interval / 60 / 24
		logs.Println("用时 天：",T_collection_time_interval)
		if T_collection_time_interval <= 30 {
			logs.Println("未超时！！！",)
			continue;
		}
		_, company_r := Account.Read_User_ByT_uuid(T.T_uuid)
		//AdminMap := Account.AdminListToMap(Account.Read_Admin_List_ALL_1())

		//go System.Send_Weichat_News(AdminMap[T.T_scheme], fmt.Sprintf("【项目回款】项目回款已经超过%d天【%s-%s】", int(T_collection_time_interval) ,company_r.T_name, T.T_name), "")
		go wx.WxSend(T.T_scheme, fmt.Sprintf("【项目回款】项目回款已经超过%d天【%s-%s】", int(T_collection_time_interval) ,company_r.T_name, T.T_name))

	}


}
