package services

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

func GetAdminMobileMap() (mapItem map[string]string, err error) {
	adminList, e := models.GetRaiAdmin()
	if e != nil {
		err = e
		return
	}
	mapMobile := make(map[string]string)
	for _, v := range adminList {
		mapMobile[v.Mobile] = v.Mobile
	}
	mapItem = mapMobile
	return
}

func GetActivityCcustomerTypeList() (mapItem map[int]string, err error) {
	list, e := models.GetActivityCcustomerTypeList()
	if e != nil {
		err = e
		return
	}
	mapUserType := make(map[int]string)
	for _, v := range list {
		mapUserType[v.CustomerTypeId] = v.PermissionValue
	}
	mapUserType[0] = "0"
	mapItem = mapUserType
	return
}

// GetRaiAdminMobileMap 获取权益内部人员手机号
func GetRaiAdminMobileMap() (mapItem map[string]string) {
	var err error
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg("获取权益内部人员手机号失败 ErrMsg:"+err.Error(), 2)

		}
	}()
	adminList, e := models.GetRaiAdmin()
	if e != nil {
		err = e
		return
	}
	mapMobile := make(map[string]string)
	for _, v := range adminList {
		mapMobile[v.Mobile] = v.Mobile
	}
	mapItem = mapMobile
	return
}

// GetRaiAdminMobileItMap 获取it内部人员手机号
func GetRaiAdminMobileItMap() (mapItem map[string]string) {
	var err error
	defer func() {
		if err != nil {
			go utils.SendAlarmMsg("获取权益内部人员手机号失败 ErrMsg:"+err.Error(), 2)

		}
	}()
	adminList, e := models.GetRaiAdminIt()
	if e != nil {
		err = e
		return
	}
	mapMobile := make(map[string]string)
	for _, v := range adminList {
		mapMobile[v.Mobile] = v.Mobile
	}
	mapItem = mapMobile
	return
}

// 根据手机号判断是否属于权益
func GetBelongingRai(mobile string) (isBelong bool) {
	mapItem := GetRaiAdminMobileMap()
	if mapItem[mobile] != "" {
		isBelong = true
	}
	return
}

// 根据手机号判断是否属于IT人员
func GetBelongingRaiIt(mobile string) (isBelong bool) {
	mapItem := GetRaiAdminMobileItMap()
	if mapItem[mobile] != "" {
		isBelong = true
	}
	return
}

// GetAdminCheckUserMobileMap  获取对应销售所能查看的用户手机号
func GetAdminCheckUserMobileMap(user *models.WxUserItem) (mapItem map[string]string, err error) {
	admin, e := models.GetSysAdminByMobile(user.Mobile)
	if e != nil {
		err = e
		return
	}
	userList, e := models.GetSellerUserMobile(admin.AdminId)
	if e != nil {
		err = e
		return
	}
	mapMobile := make(map[string]string)
	for _, v := range userList {
		mapMobile[v.Mobile] = v.Mobile
	}
	mapItem = mapMobile
	return
}

// 获取本组的销售ID
func GetSelleridWhichGroup(companyId, productId int, mobile string) (adminIds string) {
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
			go utils.SendAlarmMsg("获取本组的销售ID GetSelleridWhichGroup失败 ErrMsg:"+err.Error(), 2)
		}
	}()

	adminIds, e := models.GetSelleridWhichGroup(companyId, productId)
	if e != nil {
		err = errors.New("GetSelleridWhichGroup, Err: " + e.Error())
		return
	}

	if mobile != "" && adminIds != "" {
		adminUser, e := models.GetSellerByAdminMobile(mobile)
		if e != nil && e.Error() != utils.ErrNoRow() {
			err = errors.New("GetSellerByAdminId, Err: " + e.Error())
			return
		}
		if adminUser != nil {
			adminList, e := models.GetAdminListByGroupId(adminUser.GroupId)
			if e != nil && e.Error() != utils.ErrNoRow() {
				err = errors.New("models GetAdminListByGroupId, Err: " + e.Error())
				return
			}
			if len(adminList) > 0 {
				var adminIdArr []string
				for _, v := range adminList {
					adminIdArr = append(adminIdArr, strconv.Itoa(v.AdminId))
				}
				adminIds += "," + strings.Join(adminIdArr, ",")
			}
		}
	}
	return
}

// 根据公司ID获取权益销售、共享销售手机号
func GetCompanySellerAndShareMobileByRai(companyId int) (mobiles []string, err error) {
	productDetail, e := company.GetCompanyProductDetailByCompanyId(companyId, 2)
	if e != nil {
		err = errors.New("GetCompanyProductDetailByCompanyId, Err: " + e.Error())
		return
	}

	var adminIds []int
	adminIds = append(adminIds, productDetail.SellerId)
	if productDetail.ShareSellerId > 0 {
		adminIds = append(adminIds, productDetail.ShareSellerId)
	}

	listAdmin, e := models.GetAdminByAdminIds(adminIds)
	if e != nil {
		err = errors.New("GetAdminByAdminIds, Err: " + e.Error())
		return
	}

	mobiles = make([]string, 0)
	for _, v := range listAdmin {
		if v.Mobile != "" {
			mobiles = append(mobiles, v.Mobile)
		}
	}

	return
}
