package model

import (
	"app.yhyue.com/moapp/jybase/common"
	IC "jyBXSubscribe/rpc/init"
	"strings"
)

func AllDistributor(entId, entUserId int) []*User {
	var (
		users []*User
	)
	entInfo := EntInfo(entId, entUserId)
	if entInfo.Role_admin_department {
		users = *GetDisUsers(entId, entInfo.Dept.Id)
	} else if entInfo.Role_admin_system {
		users = *GetEntUsers(entId)
	}
	return users
}
func Distributor(region []string, entId, entUserId int) []*User {
	var (
		ss, users []*User
	)
	entInfo := EntInfo(entId, entUserId)
	if entInfo.Role_admin_department {
		users = *GetDisUsers(entId, entInfo.Dept.Id)
	} else if entInfo.Role_admin_system {
		users = *GetEntUsers(entId)
	}
	//获取所有分配权益的用户
	powerUser := GetEntPowerUsers(entId)
	//判断企业是否为商机管理企业
	isEnt := GetIsEnt(entId)
	regions := []string{}
	for _, v := range region {
		//剔除空与全国
		if v == "" || strings.Contains(v, "全国") {
			continue
		}
		regions = append(regions, v)
	}

	for _, v := range users {
		var _n namePower
		_n.entUserId = common.InterfaceToStr(v.Id)
		_n.power = 1
		n1 := powerUser[_n]
		_n.power = 2
		n2 := powerUser[_n]
		if (isEnt && v.Power == 1) || n1 != 0 || n2 != 0 { //有权益用户 判断是否被设置分配区域
			if len(regions) == 0 { //判断区域
				ss = append(ss, v)
				continue
			}
			var rule_id string
			//有区域限制 过滤筛选
			rules := IC.MainMysql.Find("entniche_user_rule", map[string]interface{}{"user_id": v.Id}, "rule_id,dept_id", "", -1, -1)
			if rules != nil && len(*rules) > 0 {
				if len(*rules) > 1 { //多条规则 查询优先级最高的规则
					ruleIds := make(map[int]string)
					for _, v2 := range *rules {
						ruleIds[common.IntAll(v2["dept_id"])] = common.InterfaceToStr(v2["rule_id"])
					}
					//获取部门优先级
					var ids []int
					departSort(&ids, v.Dept_id)
					ids = append(ids, v.Dept_id)
					//Reverse(&ids)
					for _, v1 := range ids {
						if v2, ok := ruleIds[v1]; ok && v2 != "" {
							rule_id = v2
							break
						}
					}
				} else {
					rule_id = common.InterfaceToStr((*rules)[0]["rule_id"])
				}
				if rule_id != "" {
					data, ok := IC.Mgo.FindById("entniche_distribute", rule_id, "")
					if ok && data != nil && len(*data) > 0 {
						o_area, _ := (*data)["o_area"].(map[string]interface{})
						if regionCheck(o_area, regions) {
							ss = append(ss, v)
						}
						continue //企业设置分发区域
					}
				}
			}
			// 企业未分配规则 商机管理用户查询个人
			data, ok := IC.Mgo.Find("entniche_rule", map[string]interface{}{
				"i_userid": v.Id,
				"i_entid":  entId,
				"i_type":   1, //企业大会员
			}, "", nil, false, -1, 1)
			if !ok || data == nil || len(*data) == 0 {
				data, ok = IC.Mgo.Find("entniche_rule", map[string]interface{}{
					"i_userid": v.Id,
					"i_entid":  entId,
					"i_type":   0, //企业商机管理
				}, "", nil, false, -1, 1)
			}
			if ok && data != nil {
				for _, value := range *data {
					o_entniche := common.ObjToMap(value["o_entniche"])
					o_area, _ := (*o_entniche)["o_area"].(map[string]interface{})
					if regionCheck(o_area, regions) {
						ss = append(ss, v)
						continue //商机管理
					}
				}
			}
		}
	}
	return ss
}

func regionCheck(o_area map[string]interface{}, regions []string) bool {
	if o_area == nil || len(o_area) == 0 {
		return true
	} else {
		regionBool := true
		for _, v1 := range regions {
			if _, ok1 := o_area[v1]; ok1 {
				continue
			} else {
				regionBool = false
				break
			}
		}
		return regionBool
	}
}

func departSort(ids *[]int, deptId int) {
	list := IC.MainMysql.FindOne("entniche_department", map[string]interface{}{"id": deptId}, "id,pid", "")
	if list != nil && len(*list) > 0 {
		pid := common.IntAll((*list)["pid"])
		if pid != 0 {
			departSort(ids, pid)
			*ids = append(*ids, pid)
		}
	}
}

// 数组倒序函数
func Reverse(arr *[]int) {
	var temp int
	length := len(*arr)
	for i := 0; i < length/2; i++ {
		temp = (*arr)[i]
		(*arr)[i] = (*arr)[length-1-i]
		(*arr)[length-1-i] = temp
	}
}
