package services

import (
	"errors"
	"fmt"
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/utils"
	"html"
	"strconv"
	"strings"
	"time"
)

// 是否展示限免标签
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)
	}
}

// 添加关于我们浏览记录
func AddCygxAboutUsVideoHistory(user *models.WxUserItem) (err error) {
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg("产品内测用户浏览信息记录失败"+err.Error(), 2)
		}
	}()
	historyRecord := new(models.CygxAboutUsVideoHistory)
	historyRecord.UserId = user.UserId
	historyRecord.CreateTime = time.Now()
	historyRecord.Mobile = user.Mobile
	historyRecord.Email = user.Email
	historyRecord.CompanyId = user.CompanyId
	historyRecord.CompanyName = user.CompanyName
	sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	historyRecord.RealName = user.RealName
	historyRecord.RegisterPlatform = utils.REGISTER_PLATFORM
	if sellerItem != nil {
		historyRecord.SellerName = sellerItem.RealName
	}
	_, err = models.AddCygxAboutUsVideoHistory(historyRecord)
	return
}

// 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
}

// FICC研报获取免责声明
func GetConfigCodeDisclaimer() (configValue string) {
	var err error
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg(fmt.Sprint("GetConfigCodeDisclaimer 获取FICC研报获取免责声明 失败, ErrMsg", err.Error()), 2)
		}
	}()
	var configCode string
	//获取配置项里面审核人员的手机号
	configCode = "Disclaimer"
	cnf, e := models.GetConfigByCode(configCode)
	if e != nil {
		err = errors.New("GetConfigByCode, Err: " + e.Error() + configCode)
		return
	}
	configValue = html.UnescapeString(cnf.ConfigValue)
	return
}
