package cygx

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

//func init() {
//	GetIndustrialManagementArticleNewPublishData(607)
//}

// 批量修改获取产业关联文章的最新发布时间
func GetIndustrialManagementArticleNewPublishData(industrialManagementId int) (err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
			go alarm_msg.SendAlarmMsg("批量修改获取产业关联文章的最新发布时间GetIndustrialManagementArticleNewPublishData ErrMsg:"+err.Error(), 2)

		}
	}()
	articleNewPublishDataList, e := cygx.GetIndustrialManagementArticleNewPublishData()
	if e != nil {
		err = errors.New("获取客户剩余报名次数失败 GetIndustrialManagementArticleNewPublishData, Err: " + e.Error())
		return
	}
	mapUPdateTime := make(map[int]string)
	var industrialManagementIds []int
	for _, v := range articleNewPublishDataList {
		if v.IndustrialManagementId != industrialManagementId {
			continue
		}
		if v.IndustrialManagementId > 0 {
			industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
		}
		mapUPdateTime[v.IndustrialManagementId] = v.UpdateTime
	}
	if len(industrialManagementIds) == 0 {
		return
	}
	//时间线的更新时间
	listtimelinePublishdate, err := cygx.GetTimeLineReportIndustrialPublishdateList(industrialManagementIds)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	for _, v := range listtimelinePublishdate {
		if v.IndustrialManagementId == industrialManagementId {
			if utils.StrTimeToTime(v.PublishDate).After(utils.StrTimeToTime(mapUPdateTime[v.IndustrialManagementId])) {
				mapUPdateTime[v.IndustrialManagementId] = v.PublishDate
			}
		}
	}
	err = cygx.UpdateIndustrialManagementArticleNewPublishData(mapUPdateTime)
	return
}

// AddUserIndustryFllowByNewId 出现了新的产业，给选择推送方式的用户自动添加关注
func AddUserIndustryFllowByNewId(industrialManagementId int) (err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
			go alarm_msg.SendAlarmMsg("出现了新的产业，给选择推送方式的用户自动添加关注。失败 ErrMsg:"+err.Error()+"industrialManagementId"+strconv.Itoa(industrialManagementId), 2)
		}
	}()
	//获取小助手提交过勾选项的用户
	//var condition string
	//condition = " AND  is_refuse = 0 "
	//chooseSendtList, e := cygx.GetCygxXzsChooseSend(condition)
	//if e != nil {
	//	err = errors.New("GetCygxXzsChooseSend , Err: " + e.Error())
	//	return
	//}
	//var mobiles []string
	//for _, v := range chooseSendtList {
	//	if v.Mobile != "" {
	//		mobiles = append(mobiles, v.Mobile)
	//	}
	//}

	//userList, e := models.GetWxUserListByUserMobileHaveCompany(mobiles)
	userList, e := cygx.GetCygxCompanyUserListByNoLoss() //需求池859 2023-08-29 目前数量大概在 16000条
	if e != nil {
		err = errors.New("GetWxUserListByUserMobileHaveCompany" + e.Error())
		return
	}
	var numk int
	var industryFllowItems []*cygx.CygxIndustryFllow
	for _, vUser := range userList {
		numk++
		item := new(cygx.CygxIndustryFllow)
		item.IndustrialManagementId = industrialManagementId
		item.UserId = vUser.UserId
		item.Email = vUser.Email
		item.Mobile = vUser.Mobile
		item.RealName = vUser.RealName
		item.CompanyId = vUser.CompanyId
		item.CompanyName = vUser.CompanyName
		item.Source = 3 // 通过程序或者管理员手动添加的关注
		item.Type = 1
		item.CreateTime = time.Now()
		item.ModifyTime = time.Now()
		industryFllowItems = append(industryFllowItems, item)
		//一次插入五千条
		if numk%5000 == 0 {
			e = cygx.AddCygxIndustryFllowMulti(industryFllowItems)
			if e != nil {
				err = errors.New("AddCygxIndustryFllowMulti , Err: " + e.Error())
				return
			}
			industryFllowItems = make([]*cygx.CygxIndustryFllow, 0)
			numk = 0
		}
	}
	if len(industryFllowItems) > 0 {
		e = cygx.AddCygxIndustryFllowMulti(industryFllowItems)
		if e != nil {
			err = errors.New("AddCygxIndustryFllowMulti , Err: " + e.Error())
			return
		}
	}

	////更新用户关注产业的标签
	//for _, v := range industryFllowItems {
	//	IndustryFllowUserLabelLogAdd(v.IndustrialManagementId, 0, v.UserId)
	//}
	return
}
