package services

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

// GetCompanyPermission 获取公司对应的权限名称
func GetCompanyPermission(companyId int) (permissionStr string, err error) {
	permissionStr, err = models.GetCompanyPermission(companyId)
	if err != nil {
		return
	}
	permissionStr = strings.Replace(permissionStr, "(主观)", "", -1)
	permissionStr = strings.Replace(permissionStr, "(客观)", "", -1)
	return
}

// GetCompanyPermissionUpgrade 获取公司对应的升级权限名称
func GetCompanyPermissionUpgrade(companyId int) (permissionStr string, err error) {
	permissionStr, err = models.GetCompanyPermissionByUserTrip(companyId)
	if err != nil {
		return
	}
	permissionStr = strings.Replace(permissionStr, "(主观)", "", -1)
	permissionStr = strings.Replace(permissionStr, "(客观)", "", -1)
	return
}

// 获取用户对应的权限申请状态
func GetUserHasPermission(user *models.WxUserItem) (hasPermission int, sellerName, sellerMobile, popupMsg string, err error) {
	//HasPermission     int    `description:"操作方式，1:有该行业权限,正常展示，2：无该行业权限，3：潜在客户，未提交过申请，4：潜在客户,已提交过申请，5：有IFCC、无权益"`
	uid := user.UserId
	applyCount, e := models.GetApplyRecordCount(uid)
	if e != nil {
		err = errors.New("GetCompanyPermissionUpgrade, Err: " + e.Error())
		return
	}
	if user.CompanyId <= 1 {
		if applyCount == 0 {
			hasPermission = 3
		} else {
			hasPermission = 4
		}
	} else {
		companyPermission, e := models.GetCompanyPermission(user.CompanyId)
		if e != nil {
			err = errors.New("GetCompanyPermission, Err: " + e.Error())
			return
		}
		if companyPermission != "" {
			if applyCount > 0 {
				hasPermission = 4
			} else {
				//获取权益销售信息 如果是FICC的客户类型，则默认他申请过
				sellerItemQy, e := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
				if e != nil && e.Error() != utils.ErrNoRow() {
					err = errors.New("GetSellerByCompanyIdCheckFicc, Err: " + e.Error())
					return
				}
				if sellerItemQy != nil {
					hasPermission = 2
					sellerName = sellerItemQy.Mobile
					sellerMobile = sellerItemQy.RealName
				} else {
					hasPermission = 5
				}
			}
		} else {
			sellerItemQy, e := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
			if e != nil && e.Error() != utils.ErrNoRow() {
				err = errors.New("GetSellerByCompanyIdCheckFicc_Qy, Err: " + e.Error())
				return
			}
			if sellerItemQy != nil {
				hasPermission = 2
				sellerMobile = sellerItemQy.Mobile
				sellerName = sellerItemQy.RealName
			} else {
				//获取FICC销售信息
				sellerItemFicc, e := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 1)
				if e != nil && e.Error() != utils.ErrNoRow() {
					err = errors.New("GetSellerByCompanyIdCheckFicc, Err: " + e.Error())
					return
				}
				if sellerItemFicc != nil {
					hasPermission = 3
				} else {
					hasPermission = 5
				}
			}
		}
	}
	popupMsg = "需要升级行业套餐权限才可参与此活动，请联系对口销售"
	return
}

// 获取用户对应的权限申请状态 文章详情
func GetUserHasPermissionArticle(user *models.WxUserItem) (hasPermission int, sellerName, sellerMobile, popupMsg string, err error) {
	//`description:"1:有该行业权限,正常展示，2：无该行业权限，不存在权益客户下，3:无该品类权限，4：潜在客户，未提交过申请，5：潜在客户,已提交过申请"`
	uid := user.UserId
	applyCount, e := models.GetApplyRecordCount(uid)
	if e != nil {
		err = errors.New("GetCompanyPermissionUpgrade, Err: " + e.Error())
		return
	}
	if user.CompanyId <= 1 {
		if applyCount == 0 {
			hasPermission = 4
		} else {
			hasPermission = 5
		}
	} else {
		companyPermission, e := models.GetCompanyPermission(user.CompanyId)
		if e != nil {
			err = errors.New("GetCompanyPermission, Err: " + e.Error())
			return
		}
		if companyPermission != "" {
			if applyCount > 0 {
				hasPermission = 2
			} else {
				hasPermission = 3
				//获取权益销售信息 如果是FICC的客户类型，则默认他申请过
				sellerItemQy, e := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
				if e != nil && e.Error() != utils.ErrNoRow() {
					err = errors.New("GetSellerByCompanyIdCheckFicc, Err: " + e.Error())
					return
				}
				if sellerItemQy != nil {
					sellerName = sellerItemQy.Mobile
					sellerMobile = sellerItemQy.RealName
				}
			}
		} else {
			hasPermission = 3
		}
		hasPermission = 3 //弹窗逻辑变更，状态强制改为3
	}
	popupMsg = "需要升级行业套餐权限才可查看此报告，请联系对口销售"
	return
}

// 获取用户对应的权限申请状态 活动详情
func GetUserHasPermissionActivity(user *models.WxUserItem, activityInfo *models.ActivityDetail) (hasPermission int, sellerName, sellerMobile, popupMsg string, err error) {
	//HasPermission     int    `description:"操作方式，1:有该行业权限,正常展示，2：无该行业权限，3：潜在客户，未提交过申请，4：潜在客户,已提交过申请，5：有IFCC、无权益"`
	uid := user.UserId
	applyCount, e := models.GetApplyRecordCount(uid)
	if e != nil {
		err = errors.New("GetApplyRecordCount, Err: " + e.Error())
		return
	}
	activityPointsByUserAllMap := GetActivityPointsByUserAllMap() // 获取对用户进行研选扣点的活动

	if strings.Contains(activityInfo.ChartPermissionName, utils.CHART_PERMISSION_NAME_YANXUAN) {
		if activityPointsByUserAllMap[activityInfo.ActivityId] {
			popupMsg = "签约研选扣点包才可参与此活动，请联系对口销售"
		} else {
			popupMsg = "暂无<b>买方研选</b>权限<br/>您可申请开通试用"
		}
	} else {
		popupMsg = "您暂无权限参加此活动，若想参加可以申请开通对应的试用权限"
	}
	if user.CompanyId == 1 {
		if applyCount > 0 {
			hasPermission = 4
		} else {
			hasPermission = 3
		}
		return
	}
	companyItem, err := models.GetCompanyDetailById(user.CompanyId)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			if applyCount > 0 {
				hasPermission = 4
			} else {
				//获取FICC销售信息
				sellerItem, e := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 1)
				if e != nil && e.Error() != utils.ErrNoRow() {
					err = e
					return
				}
				if sellerItem != nil {
					hasPermission = 5
				} else {
					hasPermission = 3
				}
			}
			err = nil
			hasPermission = hasPermission
			if strings.Contains(activityInfo.ChartPermissionName, utils.CHART_PERMISSION_NAME_YANXUAN) {
				popupMsg = "暂无<b>买方研选</b>权限<br/>您可申请开通试用"
			} else {
				popupMsg = "您暂无权限参加此活动，若想参加可以申请开通对应的试用权限"
			}

			return
		} else {
			return
		}
	}
	if companyItem.ProductId == 2 {
		hasPermission = 2
		sellerMobile = companyItem.Mobile
		sellerName = companyItem.SellerName
		companyPermission, e := models.GetCompanyPermission(user.CompanyId)
		if e != nil && e.Error() != utils.ErrNoRow() {
			err = errors.New("GetCompanyPermission, Err: " + e.Error())
			return
		}
		if strings.Contains(activityInfo.ChartPermissionName, utils.CHART_PERMISSION_NAME_YANXUAN) {
			//popupMsg = "暂无<b>买方研选</b>权限<br/>点击提交申请，提醒对口销售为你开通试用"
			//if !strings.Contains(companyPermission, utils.CHART_PERMISSION_NAME_YANXUAN) && (activityInfo.ActivityTypeId == 3 || activityInfo.ActivityTypeId == 5) {
			//	popupMsg = "暂无<b>买方研选</b>权限<br/>点击提交申请，提醒对口销售为你开通试用"
			//} else {
			//	popupMsg = "签约研选扣点包才可参与此活动，请联系对口销售"
			//}
			if activityPointsByUserAllMap[activityInfo.ActivityId] {
				popupMsg = "签约研选扣点包才可参与此活动，请联系对口销售"
			} else {
				popupMsg = "<b>暂无权限</b><br/>点击提交申请，提醒对口销售为你开通试用"
			}

		} else {
			if companyPermission == "专家" {
				popupMsg = "您暂无权限参加【" + activityInfo.ActivityTypeName + "】类型活动，若想参加请联系对口销售--" + companyItem.SellerName + "：" + companyItem.Mobile

			} else {
				popupMsg = "您暂无权限参加【" + activityInfo.ChartPermissionName + "】行业活动，若想参加请联系对口销售--" + companyItem.SellerName + "：" + companyItem.Mobile
			}
		}
	} else {
		hasPermission = 5
	}
	return
}

// 根据公司ID获取权益销售名称
func GetSellNameMapByCompanyIds(companyIds []int) (respMap map[int]string) {
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
			go utils.SendAlarmMsg("根据公司ID获取权益销售名称，失败："+err.Error()+fmt.Sprint(companyIds), 2)
		}
	}()
	lenarr := len(companyIds)
	if lenarr == 0 {
		return
	}
	var pars []interface{}
	var condition string
	respMap = make(map[int]string, 0)
	condition = "  AND product_id = 2 "
	list, e := models.GetCompanyProductList(condition, pars)
	if e != nil {
		err = errors.New("GetCompanyProductList, Err: " + e.Error())
		return
	}
	for _, v := range list {
		respMap[v.CompanyId] = v.SellerName
	}
	return
}

// 获取用户对应的权限简单版
func GetUserHasPermissionSimple(user *models.WxUserItem) (hasPermission int, err error) {
	//HasPermission int `description:"1:有该行业权限,正常展示，2：无该行业权限，不存在权益客户下（ficc），3:无该品类权限,已提交过申请，4：无该品类权限,未提交过申请，5：潜在客户，未提交过申请，6：潜在客户,已提交过申请"`
	uid := user.UserId
	applyCount, e := models.GetApplyRecordCount(uid)
	if e != nil {
		err = errors.New("GetCompanyPermissionUpgrade, Err: " + e.Error())
		return
	}
	if user.CompanyId <= 1 {
		if applyCount == 0 {
			hasPermission = 5
		} else {
			hasPermission = 6
		}
	} else {
		companyPermission, e := models.GetCompanyPermission(user.CompanyId)
		if e != nil {
			err = errors.New("GetCompanyPermission, Err: " + e.Error())
			return
		}
		if companyPermission != "" {
			hasPermission = 1
		} else {
			hasPermission = 3
		}
	}
	return
}

// GetPermissionNameMap 权限名称与权限ID的map
func GetPermissionNameMap() (mapItem map[int]string, err error) {
	list, e := models.GetChartPermissionListRai()
	if e != nil {
		err = e
		return
	}
	mapPermissionName := make(map[int]string)
	for _, v := range list {
		mapPermissionName[v.ChartPermissionId] = v.PermissionName
	}
	mapItem = mapPermissionName
	return
}

func GetPermissionNameIcoMap() (mapItem map[int]string, err error) {
	list, e := models.GetChartPermissionListRai()
	if e != nil {
		err = e
		return
	}
	mapPermissionName := make(map[int]string)
	for _, v := range list {
		mapPermissionName[v.ChartPermissionId] = v.ImageUrlM
	}
	mapItem = mapPermissionName
	return
}
