package cygx

import (
	"errors"
	"hongze/hz_crm_api/models/company"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/models/system"
	"hongze/hz_crm_api/utils"
	"strconv"
	"strings"
)

// 获取这个销售所能查看的手机号权限
func GetAdminLookUserMobile(adminInfo *system.Admin) (mapMobile map[string]string, err error) {

	//sellerIds, e := company.GetCompanyProductSellerIdByShareSellerId(adminInfo.AdminId)
	//if e != nil && e.Error() != utils.ErrNoRow() {
	//	err = errors.New("GetCompanyProductSellerIdByShareSellerId 根据共享销售id查被共享的公司销售id, Err: " + e.Error())
	//	return
	//}
	var companyIds []string
	var adminIds []string

	//如果是组长获取所属组员的ID
	if adminInfo.RoleTypeCode == "rai_group" {
		adminList, e := system.GetAdminListByGroupId(adminInfo.GroupId)
		if e != nil && e.Error() != utils.ErrNoRow() {
			err = errors.New("GetAdminListByGroupId, Err: " + e.Error())
			return
		}
		for _, v := range adminList {
			adminIds = append(adminIds, strconv.Itoa(v.AdminId))
		}
	} else {
		adminIds = append(adminIds, strconv.Itoa(adminInfo.AdminId))
	}
	//再获取组员下被共享的公司
	var pars []interface{}
	var condition string
	condition = "  AND  share_seller_id  IN (" + strings.Join(adminIds, ",") + ")  "
	listCompanyProduct, e := company.GetCompanyProductList(condition, pars)
	if e != nil {
		err = errors.New("GetCompanyProductList, Err: " + e.Error())
		return
	}
	for _, v := range listCompanyProduct {
		companyIds = append(companyIds, strconv.Itoa(v.CompanyId))
	}
	companyIds = append(companyIds, "0")
	var conditionMobile string
	//if adminInfo.RoleTypeCode == "rai_group" {
	//
	//	//组长查看本组所有组员
	//	conditionMobile = ` SELECT mobile FROM user_seller_relation WHERE seller_id IN (SELECT  admin_id FROM admin WHERE group_id = (SELECT group_id FROM admin WHERE admin_id = ` + strconv.Itoa(adminInfo.AdminId) + ` )) `
	//} else {
	//	//组员查看自己
	//	if sellerIds != "" {
	//		conditionMobile = ` SELECT mobile FROM user_seller_relation WHERE seller_id  IN ( ` + strconv.Itoa(adminInfo.AdminId) + `,` + sellerIds + ` ) `
	//	} else {
	//		conditionMobile = ` SELECT mobile FROM user_seller_relation WHERE seller_id  IN ( ` + strconv.Itoa(adminInfo.AdminId) + ` ) `
	//	}
	//}
	conditionMobile = ` SELECT mobile FROM user_seller_relation WHERE  1=1  AND product_id = 2   AND (seller_id  IN ( ` + strings.Join(adminIds, ",") + ` )  OR  company_id IN (` + strings.Join(companyIds, ",") + `))   `

	mobileList, e := cygx.GetSellerUserMobile(conditionMobile)
	if e != nil {
		err = errors.New("GetSellerUserMobile 获取这个销售所能查看的手机号权限失败, Err: " + e.Error())
		return
	}
	mapUserMobile := make(map[string]string)
	for _, v := range mobileList {
		mapUserMobile[v.Mobile] = v.Mobile
	}
	mapMobile = mapUserMobile
	return
}

// 获取这个销售所能查看的手机号权限
func GetAdminLookUserUserId(adminInfo *system.Admin) (userIds []int, err error) {
	var conditionMobile string
	if adminInfo.RoleTypeCode == "rai_group" {
		//组长查看本组所有组员
		conditionMobile = ` SELECT user_id FROM user_seller_relation WHERE seller_id IN (SELECT  admin_id FROM admin WHERE group_id = (SELECT group_id FROM admin WHERE admin_id = ` + strconv.Itoa(adminInfo.AdminId) + ` )) `
	} else {
		//组员查看自己
		conditionMobile = ` SELECT user_id FROM user_seller_relation WHERE seller_id  = ` + strconv.Itoa(adminInfo.AdminId)
	}
	conditionMobile += " LIMIT 10"
	userList, e := cygx.GetSellerUserMobile(conditionMobile)
	if e != nil {
		err = errors.New("GetSellerUserMobile 获取这个销售所能查看的手机号权限失败, Err: " + e.Error())
		return
	}
	userIds = make([]int, 0)
	for _, v := range userList {
		userIds = append(userIds, v.UserId)
	}
	return
}

//func init() {
//	sellerId, e := company.GetCompanyProductSellerIdByShareSellerId(163)
//	fmt.Println(e)
//	fmt.Println(sellerId)
//}

// 获取这个销售所能查看的手机号权限
func GetAdminLookUserCompanyIds(sysUser *system.Admin) (companyIds []int, err error) {
	//如果不是管理员,权益管理员那么就对可见权限范围做处理
	adminIds := make([]int, 0)
	if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_RAI_ADMIN {
		if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
			//如果是权益的组长就获取下面的组员
			adminList, e := system.GetAdminListByGroupId(sysUser.GroupId)
			if e != nil {
				err = errors.New("GetAdminListByGroupId , Err: " + e.Error())
				return
			}
			for _, v := range adminList {
				adminIds = append(adminIds, v.AdminId)
			}
		} else {
			adminIds = append(adminIds, sysUser.AdminId)
		}
		//adminIds = append(adminIds, sysUser.AdminId)
		var adminIdGroup string
		for _, v := range adminIds {
			adminIdGroup += strconv.Itoa(v) + ","
		}
		adminIdGroup = strings.TrimRight(adminIdGroup, ",")
		companyProductList, e := company.GetCompanyProductsBySellerId(adminIdGroup)
		if e != nil {
			err = errors.New("GetCompanyProductsBySellerId , Err: " + e.Error())
			return
		}

		for _, v := range companyProductList {
			companyIds = append(companyIds, v.CompanyId)
		}
	}
	companyIds = append(companyIds, 0) //添加潜在客户
	return
}

// 获取这个销售所能查看的公司权限
func GetAdminLookUserCompanyIdsBySelf(sysUser *system.Admin) (companyIds []int, err error) {
	//如果不是管理员,权益管理员那么就对可见权限范围做处理
	adminIds := make([]int, 0)
	if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_RAI_ADMIN && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_FINANCE {
		//if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
		//	//如果是权益的组长就获取下面的组员
		//	adminList, e := system.GetAdminListByGroupId(sysUser.GroupId)
		//	if e != nil {
		//		err = errors.New("GetAdminListByGroupId , Err: " + e.Error())
		//		return
		//	}
		//	for _, v := range adminList {
		//		adminIds = append(adminIds, v.AdminId)
		//	}
		//} else {
		//	adminIds = append(adminIds, sysUser.AdminId)
		//}
		adminIds = append(adminIds, sysUser.AdminId)
		var adminIdGroup string
		for _, v := range adminIds {
			adminIdGroup += strconv.Itoa(v) + ","
		}
		adminIdGroup = strings.TrimRight(adminIdGroup, ",")
		companyProductList, e := company.GetCompanyProductsBySellerId(adminIdGroup)
		if e != nil {
			err = errors.New("GetCompanyProductsBySellerId , Err: " + e.Error())
			return
		}

		for _, v := range companyProductList {
			companyIds = append(companyIds, v.CompanyId)
		}
		companyIds = append(companyIds, 0) //添加潜在客户
	}
	return
}

// 获取这个销售所能查看的公司权限(研究员)
func GetAdminLookUserCompanyIdsByResearcher(sysUser *system.Admin) (companyIds []int, err error) {
	//如果不是管理员,权益管理员那么就对可见权限范围做处理
	adminIds := make([]int, 0)
	if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_RAI_ADMIN && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_RAI_RESEARCHR {

		adminIds = append(adminIds, sysUser.AdminId)
		var adminIdGroup string
		for _, v := range adminIds {
			adminIdGroup += strconv.Itoa(v) + ","
		}
		adminIdGroup = strings.TrimRight(adminIdGroup, ",")
		companyProductList, e := company.GetCompanyProductsBySellerId(adminIdGroup)
		if e != nil {
			err = errors.New("GetCompanyProductsBySellerId , Err: " + e.Error())
			return
		}

		for _, v := range companyProductList {
			companyIds = append(companyIds, v.CompanyId)
		}
		companyIds = append(companyIds, 0) //添加潜在客户
	}
	return
}