package services

import (
	"errors"
	"fmt"
	"hongze/hongze_mfyx/models"
	"hongze/hongze_mfyx/utils"
	"strconv"
	"strings"
)

// 是否展示限免标签
func GetShowSustainable() (isShowSustainable bool) {
	total, err := models.GetShowSustainable()
	if err != nil {
		fmt.Println("GetShowSustainable Err:", err.Error())
		return
	}
	if total > 0 {
		isShowSustainable = true
	}
	return
}

func GetShowSustainableNew() (isShowSustainable bool, err error) {
	total, err := models.GetShowSustainable()
	if err != nil {
		fmt.Println("GetShowSustainable Err:", err.Error())
		return
	}
	if total > 0 {
		isShowSustainable = true
	}
	return
}

// GetIndustryUserFollowMap 获取用户关注的产业
func GetIndustryUserFollowMap(user *models.WxUserItem) (itemMap map[int]bool, err error) {
	condition := ` AND  user_id = ` + strconv.Itoa(user.UserId)
	listIndustryFllow, e := models.GetCygxIndustryFllowListByUserId(condition)
	if e != nil {
		err = errors.New("GetCygxIndustryFllowList  " + e.Error())
		return
	}
	follwMap := make(map[int]bool)
	for _, v := range listIndustryFllow {
		follwMap[v.IndustrialManagementId] = true
	}
	itemMap = follwMap
	return
}

func init1231() {
	var condition string
	var pars []interface{}

	condition += ` AND  t.id < 10000 	 `

	list, err := models.GetCygxActivitySpecialTripListinit(condition, pars)
	if err != nil {
		fmt.Println(err)
		return
	}

	for _, v := range list {
		//流水记录表
		itemBill := new(models.CygxActivitySpecialTripBill)
		itemBill.UserId = v.UserId
		itemBill.ActivityId = v.ActivityId
		itemBill.CreateTime = v.CreateTime
		itemBill.Mobile = v.Mobile
		itemBill.Email = v.Email
		itemBill.CompanyId = v.CompanyId
		itemBill.CompanyName = v.CompanyName
		itemBill.RealName = v.RealName
		itemBill.Source = 1
		itemBill.DoType = 1
		itemBill.BillDetailed = -1 // 流水减一
		itemBill.RegisterPlatform = 1
		itemBill.ChartPermissionId = v.ChartPermissionId
		go models.AddCygxActivitySpecialTripBill(itemBill)
	}
}

// CheckYxSpecialIsApprovalPersonnel 校验手机号是否属于研选专栏的审核人员
func CheckYxSpecialIsApprovalPersonnel(mobile string) (isApprovalPersonnel bool) {
	var err error
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg(fmt.Sprint("CheckYxSpecialIsApprovalPersonnel 校验手机号是否属于研选专栏的审核人员失败, mobile:", mobile, "ErrMsg", err.Error()), 2)
		}
	}()
	var configCode string
	//获取配置项里面审核人员的手机号
	configCode = utils.TPL_MSG_YAN_XUAN_SPECIAL_APPROVAL
	cnf, e := models.GetConfigByCode(configCode)
	if e != nil {
		err = errors.New("GetConfigByCode, Err: " + e.Error() + configCode)
		return
	}
	if strings.Contains(cnf.ConfigValue, mobile) {
		isApprovalPersonnel = true
	}
	return
}