package entity import ( "encoding/json" "fmt" "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter" "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/mysql" ) type EntUserInfo struct { AppId string EntUserId int64 EntId int64 Mysql *mysql.Mysql } type User struct { Name string //员工姓名 Mail string //邮箱 Phone string //手机号 Dept_name string //部门名称 EntUserId int64 } //获取员工信息 func (this *EntUserInfo) GetEntUserInfo() (*User, string) { data := this.Mysql.SelectBySql(`SELECT a.name,a.phone,a.mail,c.name AS dept_name,a.id as entUserId FROM entniche_user a INNER JOIN entniche_department_user b ON (a.id=? AND a.id=b.user_id) INNER JOIN entniche_department c ON (b.dept_id=c.id) WHERE a.ent_id=?`, this.EntUserId, this.EntId) if data == nil || len(*data) == 0 { return nil, "暂无数据" } users, _ := common.JsonUnmarshal(data, &[]*User{}).(*[]*User) return (*users)[0], "" } //1企业管理员 2部门管理员 3员工 func (this *EntUserInfo) CheckEntAdmin() (int64, string) { if count := this.Mysql.CountBySql(`select count(1) from entniche_user where id =? and ent_id =?`, this.EntUserId, this.EntId); count <= 0 { return -1, "暂无数据" } if data := this.Mysql.SelectBySql(`select role_id from entniche_user_role where user_id =?`, this.EntUserId); data != nil && len(*data) > 0 { rule := common.Int64All((*data)[0]["role_id"]) return rule, "" } return 3, "" } type EntUserList struct { EntId int64 DeptId int64 Name string Mysql *mysql.Mysql } func (this *EntUserList) GetEntUserList() ([]*usercenter.EntUserListData, string) { sql := `SELECT a.id,a.pid,a.name,c.id AS user_id,c.name AS user_name,c.mail AS user_mail, c.phone AS user_phone ,ed.name AS fdept FROM entniche_department a INNER JOIN entniche_department_user b ON (a.ent_id=? AND a.id=b.dept_id) INNER JOIN entniche_user c ON (b.user_id=c.id) LEFT JOIN entniche_department ed ON (a.pid=ed.id)` if this.Name != "" { sql += `where c.name like "%` + this.Name + `%"` } sql += `ORDER BY a.id,CONVERT(c.name USING gbk) COLLATE gbk_chinese_ci ASC` data := this.Mysql.SelectBySql(sql, this.EntId) listMap := map[int64][]*usercenter.EntUser{} if data != nil { for _, v := range *data { id := common.Int64All(v["id"]) listMap[id] = append(listMap[id], &usercenter.EntUser{ Name: common.ObjToString(v["user_name"]), Phone: common.ObjToString(v["user_phone"]), Mail: common.ObjToString(v["user_mail"]), DeptName: common.ObjToString(v["name"]), EntUserId: common.Int64All(v["user_id"]), }) } } deptData := &[]map[string]interface{}{} //获取部门层级关系 if this.DeptId > 0 { deptData = this.Mysql.SelectBySql(`select a.id,a.name,a.pid from entniche_department a inner join entniche_department_parent b on (a.ent_id=? and b.pid=? and a.id =b.id)`, this.EntId, this.DeptId) } else { deptData = this.Mysql.SelectBySql(`select id,name,pid from entniche_department where ent_id=?`, this.EntId) } deptList := []*usercenter.EntUserListData{} ret := []*usercenter.EntUserListData{} if len(*deptData) > 0 { for _, v := range *deptData { deptList = append(deptList, &usercenter.EntUserListData{ DeptId: common.Int64All(v["id"]), Name: common.ObjToString(v["name"]), PId: common.Int64All(v["pid"]), EntUserList: listMap[common.Int64All(v["id"])], DeptList: []*usercenter.EntUserListData{}, }) } //创建根 root := []*usercenter.EntUserListData{deptList[0]} //获取根上部门 pdept := make([]*usercenter.EntUserListData, 0) for i, _ := range deptList { var a *usercenter.EntUserListData a = deptList[i] pdept = append(pdept, a) } // for _, v := range root { makeTree(pdept, v) data, _ := json.Marshal(&v) fmt.Println(string(data)) ret = append(ret, v) } } if len(ret) == 0 { return ret, "暂无数据" } return ret, "" } func has(v1 usercenter.EntUserListData, vs []*usercenter.EntUserListData) bool { var has bool has = false for _, v2 := range vs { v3 := *v2 if v1.PId == v3.DeptId { has = true break } } return has } func makeTree(vs []*usercenter.EntUserListData, node *usercenter.EntUserListData) { childs := findChild(node, vs) for _, child := range childs { node.DeptList = append(node.DeptList, child) if has(*child, vs) { makeTree(vs, child) } } } func findChild(v *usercenter.EntUserListData, vs []*usercenter.EntUserListData) (ret []*usercenter.EntUserListData) { for _, v2 := range vs { if v.DeptId == v2.PId { ret = append(ret, v2) } } return }