package service

import (
	util "app.yhyue.com/moapp/jybase/common"
	. "app.yhyue.com/moapp/jybase/mongodb"
	"app.yhyue.com/moapp/jybase/mysql"
	"github.com/zeromicro/go-zero/core/logx"
)

func ClearEnt() {
	Ents = map[int]*Ent{}
	EntUsers = map[int]*EntUser{}
	EntUserDept = map[int]*EntDeptUser{}
	EntDeptUsers = map[int][]*EntDeptUser{}
	EntRuleUsers = map[string][]*EntRuleUser{}
	EntUserRules = map[int][]*EntRuleUser{}
	EntDepts = map[int]*EntDept{}
	EntParentDept = map[int][]*EntDeptParent{}
	EntChildDept = map[int][]*EntDeptParent{}
	EntDeptDis = map[int][]*EntDistribute{}
}

//
func InitEnt(mysql *mysql.Mysql, mgo *MongodbSim, dbName string, entId, entUserId int) {
	InitEnts(mysql, entId)
	InitEntUsers(mysql, entUserId)
	InitEntDeptUsers(mysql, entUserId)
	InitEntRuleUsers(mysql, entUserId)
	InitEntDepts(mysql, entId)
	InitEntDeptParents(mysql, entId)
	InitEntDistribute(mgo, dbName, entId)
}

///////////////////////
var Ents = map[int]*Ent{}

type Ent struct {
	Id         int
	Model      int
	Name       string
	DeptParent map[int][]int
	IsNew      int
}

func InitEnts(Mysql *mysql.Mysql, entId int) {
	list := Mysql.SelectBySql("select id,model,name from entniche_info where id=? and isNew=1 and status=1 and model in (1,2)", entId)
	if list == nil {
		return
	}
	for _, v := range *list {
		id := util.IntAll(v["id"])
		Ents[id] = &Ent{
			Id:    id,
			Model: util.IntAll(v["model"]),
			Name:  util.ObjToString(v["name"]),
		}
	}
	logx.Info("entniche_info加载完成！")
}

var EntDepts = map[int]*EntDept{}

type EntDept struct {
	Id     int
	Pid    int
	Name   string
	Nodiff int
	Subdis int
}

func InitEntDepts(Mysql *mysql.Mysql, entId int) {
	list := Mysql.SelectBySql(`select id,name,pid,subdis,nodiff from entniche_department where ent_id=?`, entId)
	if list == nil {
		return
	}
	for _, v := range *list {
		id := util.IntAll(v["id"])
		EntDepts[id] = &EntDept{
			Id:     id,
			Pid:    util.IntAll(v["pid"]),
			Nodiff: util.IntAll(v["nodiff"]),
			Subdis: util.IntAll(v["subdis"]),
			Name:   util.ObjToString(v["name"]),
		}
	}
	logx.Info("entniche_department加载完成！")
}

var EntUsers = map[int]*EntUser{}

type EntUser struct {
	Id    int
	Name  string
	Mail  string
	Phone string
	Power int
}

func InitEntUsers(Mysql *mysql.Mysql, entUserId int) {
	list := Mysql.SelectBySql(`select id,name,mail,phone,power from entniche_user where id=?`, entUserId)
	if list == nil {
		return
	}
	for _, v := range *list {
		id := util.IntAll(v["id"])
		EntUsers[id] = &EntUser{
			Id:    id,
			Name:  util.ObjToString(v["name"]),
			Mail:  util.ObjToString(v["mail"]),
			Phone: util.ObjToString(v["phone"]),
			Power: util.IntAll(v["power"]),
		}
	}
	logx.Info("entniche_user加载完成！")
}

//我所有的父部门
var EntParentDept = map[int][]*EntDeptParent{}

//我所有的子部门
var EntChildDept = map[int][]*EntDeptParent{}

type EntDeptParent struct {
	Id  int
	Pid int
}

func InitEntDeptParents(Mysql *mysql.Mysql, entId int) {
	list := Mysql.SelectBySql(`select a.id,a.pid from entniche_department_parent a inner join entniche_department b on (a.id=b.id and b.ent_id=?)`, entId)
	if list == nil {
		return
	}
	for _, v := range *list {
		id := util.IntAll(v["id"])
		pid := util.IntAll(v["pid"])
		EntParentDept[id] = append(EntParentDept[id], &EntDeptParent{
			Id:  id,
			Pid: pid,
		})
		EntChildDept[pid] = append(EntChildDept[pid], &EntDeptParent{
			Id:  id,
			Pid: pid,
		})
	}
	logx.Info("entniche_department_parent加载完成！")
}

var EntUserDept = map[int]*EntDeptUser{}
var EntDeptUsers = map[int][]*EntDeptUser{}

type EntDeptUser struct {
	UserId int
	DeptId int
}

func InitEntDeptUsers(Mysql *mysql.Mysql, entUserId int) {
	list := Mysql.SelectBySql(`select dept_id,user_id from entniche_department_user where user_id=?`, entUserId)
	if list == nil {
		return
	}
	for _, v := range *list {
		user_id := util.IntAll(v["user_id"])
		dept_id := util.IntAll(v["dept_id"])
		entDeptUser := &EntDeptUser{
			UserId: user_id,
			DeptId: dept_id,
		}
		EntUserDept[user_id] = entDeptUser
		EntDeptUsers[dept_id] = append(EntDeptUsers[dept_id], entDeptUser)
	}
	logx.Info("entniche_department_user加载完成！")
}

var EntRuleUsers = map[string][]*EntRuleUser{}
var EntUserRules = map[int][]*EntRuleUser{}

type EntRuleUser struct {
	UserId int
	RuleId string
}

func InitEntRuleUsers(Mysql *mysql.Mysql, entUserId int) {
	list := Mysql.SelectBySql(`select user_id,rule_id from entniche_user_rule where user_id=?`, entUserId)
	if list == nil {
		return
	}
	for _, v := range *list {
		ruleId := util.ObjToString(v["rule_id"])
		userId := util.IntAll(v["user_id"])
		entRuleUser := &EntRuleUser{
			UserId: userId,
			RuleId: ruleId,
		}
		EntRuleUsers[ruleId] = append(EntRuleUsers[ruleId], entRuleUser)
		EntUserRules[userId] = append(EntUserRules[userId], entRuleUser)
	}
	logx.Info("entniche_user_rule加载完成！")
}

var EntDeptDis = map[int][]*EntDistribute{}

type EntDistribute struct {
	Id         string
	DeptId     int
	Area       map[string]interface{}
	Buyerclass []interface{}
	Items      []string
}

func InitEntDistribute(mgo *MongodbSim, dbName string, entId int) {
	sess := mgo.GetMgoConn()
	defer mgo.DestoryMongoConn(sess)
	it := sess.DB(dbName).C("entniche_distribute").Find(map[string]interface{}{
		"i_status": map[string]interface{}{"$ne": 1},
		"i_entid":  entId,
	}).Select(map[string]interface{}{
		"_id":          1,
		"i_entid":      1,
		"i_deptid":     1,
		"a_items":      1,
		"o_area":       1,
		"a_buyerclass": 1,
	}).Iter()
	for temp := make(map[string]interface{}); it.Next(&temp); {
		if temp["i_deptid"] == nil {
			continue
		}
		deptId := util.IntAll(temp["i_deptid"])
		a_items, _ := temp["a_items"].([]interface{})
		o_area, _ := temp["o_area"].(map[string]interface{})
		a_buyerclass, _ := temp["a_buyerclass"].([]interface{})
		EntDeptDis[deptId] = append(EntDeptDis[deptId], &EntDistribute{
			Id:         BsonIdToSId(temp["_id"]),
			DeptId:     deptId,
			Area:       o_area,
			Buyerclass: a_buyerclass,
			Items:      util.ObjArrToStringArr(a_items),
		})
		temp = make(map[string]interface{})
	}
}
