package cygx

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

//func init() {
//	AddUserFllowCompanyLossToTryOut(12841)
//}

// 流失客户转试用给下面的用户自动添加产业关注
func AddUserFllowCompanyLossToTryOut(companyId int) {
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
			go alarm_msg.SendAlarmMsg("流失客户转试用给下面的用户自动添加产业关注。失败 ErrMsg:"+err.Error()+"companyId ："+strconv.Itoa(companyId), 2)
		}
	}()

	//获取机构下所有的用户
	userList, e := models.GetWxUserListCompanyId(companyId)
	if e != nil {
		err = errors.New("GetWxUserListByUserMobileHaveCompany" + e.Error())
		return
	}

	if len(userList) == 0 {
		return
	}
	var condition string
	var pars []interface{}
	//所有的产业
	industrNamelist, e := cygx.GetTopOneMonthArtReadNumIndustryAll(condition, pars)
	if e != nil {
		err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
		return
	}

	var userIds []int

	for _, v := range userList {
		userIds = append(userIds, v.UserId)
	}

	condition = " AND user_id IN  (" + utils.GetOrmInReplace(len(userIds)) + ")  "
	pars = append(pars, userIds)
	userFllowList, e := cygx.GetCygxIndustryFllowList(condition, pars, 0, 0)
	if e != nil {
		err = errors.New("GetCygxIndustryFllowList, Err: " + e.Error())
		return
	}
	mapUserFllow := make(map[string]bool) // 用户已经关注了某些产业

	for _, v := range userFllowList {
		mapUserFllow[fmt.Sprint("Uid_", v.UserId, "IndustrialManagementId_", v.IndustrialManagementId)] = true //用户ID与产业ID组成唯一索引Map
	}

	var numk int
	var industryFllowItems []*cygx.CygxIndustryFllow
	var Items []*cygx.CygxIndustryFllow

	for _, vUser := range userList {
		for _, vIndustry := range industrNamelist {
			if mapUserFllow[fmt.Sprint("Uid_", vUser.UserId, "IndustrialManagementId_", vIndustry.IndustrialManagementId)] {
				continue
			}
			item := new(cygx.CygxIndustryFllow)
			item.IndustrialManagementId = vIndustry.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()
			Items = append(Items, item)
		}
	}
	//fmt.Println("Items", len(Items))
	//如果小于五千条就直接插入，大于五千条就批量插入
	if len(Items) < 5000 {
		e = cygx.AddCygxIndustryFllowMulti(Items)
		if e != nil {
			err = errors.New("AddCygxIndustryFllowMulti , Err: " + e.Error())
			return
		}
	} else {
		for _, vUser := range Items {
			numk++
			item := new(cygx.CygxIndustryFllow)
			item.IndustrialManagementId = vUser.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
			}
		}
	}

	//fmt.Println("AddUserFllowCompanyLossToTryOut end")
	return
}
