package cygx

import (
	"errors"
	"fmt"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/services/alarm_msg"
	"hongze/hz_crm_api/utils"
	"time"
)

var (
	EmptyDateTimeStr     = "0000-00-00 00:00:00"
	EmptyDateTimeStrFmot = "0001-01-01 00:00:00"
)

//func init() {
//	var industryIds []int
//	industryIds = append(industryIds, 607)
//	UpdateIndustryLayoutTime(industryIds, false)
//}

// UpdateIndustryLayoutTime 更新产业布局时间
func UpdateIndustryLayoutTime(industryIds []int, forceUpdate bool) (err error) {
	defer func() {
		fmt.Println(err)
		if err != nil {
			go alarm_msg.SendAlarmMsg("更新产业布局时间失败, ErrMsg: "+err.Error(), 3)
		}
	}()
	idLen := len(industryIds)
	if idLen <= 0 {
		return
	}
	var condition string
	var pars []interface{}
	if idLen == 1 {
		condition = ` AND idy.industrial_management_id = ?`
		pars = append(pars, industryIds[0])
	} else {
		condition = ` AND idy.industrial_management_id IN (` + utils.GetOrmInReplace(idLen) + `)`
		pars = append(pars, industryIds)
	}
	list, e := cygx.GetIndustriesEarliestLayoutTime(condition, pars)
	if e != nil {
		err = errors.New("获取产业最早布局时间失败, Err: " + e.Error())
		return
	}
	mapLayoutTime := make(map[int]time.Time)
	for i := range list {
		// 强制更新
		if !forceUpdate {
			if list[i].LayoutTime != "" && list[i].LayoutTime != EmptyDateTimeStr {
				continue
			}
		}
		if list[i].MinArticleTime != "" && list[i].MinArticleTime != EmptyDateTimeStr {
			mapLayoutTime[list[i].IndustrialManagementId] = list[i].MinArticleTime2
		}

	}
	listMorningMeeting, e := cygx.GetIndustriesEarliestLayoutTimeByMorningMeeting(condition, pars)
	if e != nil {
		err = errors.New("获取产业最早布局时间失败, Err: " + e.Error())
		return
	}

	for i := range listMorningMeeting {
		// 强制更新
		if !forceUpdate {
			if listMorningMeeting[i].LayoutTime != "" && listMorningMeeting[i].LayoutTime != EmptyDateTimeStr {
				continue
			}
		}
		//earliestTime := ""
		if listMorningMeeting[i].MinArticleTime != "" && listMorningMeeting[i].MinArticleTime != EmptyDateTimeStr {
			//earliestTime = list[i].MinArticleTime
			if mapLayoutTime[listMorningMeeting[i].IndustrialManagementId].Format(utils.FormatDateTime) == EmptyDateTimeStrFmot {
				mapLayoutTime[listMorningMeeting[i].IndustrialManagementId] = listMorningMeeting[i].MinArticleTime2
			} else {
				if listMorningMeeting[i].MinArticleTime2.Before(mapLayoutTime[listMorningMeeting[i].IndustrialManagementId]) {
					mapLayoutTime[listMorningMeeting[i].IndustrialManagementId] = listMorningMeeting[i].MinArticleTime2
				}
			}
		}
	}

	for k, v := range mapLayoutTime {
		if e = cygx.UpdateIndustryLayoutTime(k, v.AddDate(0, 0, -10).Format(utils.FormatDateTime)); e != nil {
			err = errors.New("更新产业布局时间失败, Err: " + e.Error())
			return
		}
	}
	return
}

//func init() {
//	UpdateOriginIndustryLayoutTime()
//}

// UpdateOriginIndustryLayoutTime 更新所有产业布局时间
func UpdateOriginIndustryLayoutTime() {
	var condition string
	var pars []interface{}
	industries, e := cygx.GetIndustryListByCondition(condition, pars)
	if e != nil {
		fmt.Println(e.Error())
		return
	}
	industryIds := make([]int, 0)
	for i := range industries {
		industryIds = append(industryIds, industries[i].IndustrialManagementId)
	}
	fmt.Println("开始更新")
	if e = UpdateIndustryLayoutTime(industryIds, true); e != nil {
		fmt.Println(e.Error())
		return
	}
	fmt.Println("更新完毕")
	return
}
