let a = {
"job": [
{
"type": "安全部",
"index": "1",
"job_name": "动态监控员",
"undefined": "on"
},
{
"type": "运营部",
"index": "2",
"job_name": "装卸管理人员",
"undefined": "on"
},
{
"type": "运营部",
"index": "3",
"job_name": "押运员",
"undefined": "on"
},
{
"type": "技术部",
"index": "4",
"job_name": "技术人员",
"undefined": "on"
},
{
"type": "直管后勤部",
"index": "5",
"job_name": "厨子",
"undefined": "on"
},
{
"type": "总经理直管岗位",
"index": "6",
"job_name": "直管xx岗位",
"undefined": "on"
},
{
"type": "安全部",
"index": "7",
"job_name": "专职安全生产管理人员",
"undefined": "on"
},
{
"type": "运营部",
"index": "8",
"job_name": "货运驾驶员",
"undefined": "on"
}
],
"base": {
"isgroup": "1",
"isbranch": "1",
"groupname": "测试集团",
"leadername": "张三",
"leadertype": "总经理",
"companyname": "某集团公司",
"companytype": "子公司",
"isdepartment": "1"
},
"branch": [
{
"type": "分管负责人",
"index": "1",
"undefined": "on",
"branch_name": "分管安全负责人",
"charge_name": "王五"
},
{
"type": "分支机构",
"index": "2",
"undefined": "on",
"branch_name": "吉林市分支机构",
"charge_name": ""
}
],
"department": [
{
"type": "分管安全负责人",
"index": "1",
"undefined": "on",
"charge_name": "赵六",
"department_name": "安全部"
},
{
"type": "吉林市分支机构",
"index": "2",
"undefined": "on",
"charge_name": "杨七",
"department_name": "运营部"
},
{
"type": "吉林市分支机构",
"index": "3",
"undefined": "on",
"charge_name": "宋八",
"department_name": "技术部"
},
{
"type": "总经理直管部门",
"index": "4",
"undefined": "on",
"charge_name": "小张",
"department_name": "直管后勤部"
}
]
};
// console.log(a);
formatData(a);
function havebranch(aaa) {
var result = {};
result.children = [];
for (var i = 0; i < aaa.branch.length; i++) { // 分管/分支
var tempbranch = { "children": [] };
tempbranch.id = aaa.branch[i].charge_name + i;
tempbranch.label = aaa.branch[i].branch_name;
tempbranch.bottomText = aaa.branch[i].charge_name;
tempbranch.origin = aaa.branch[i].branch_name; // 原始本级名
result.children.push(tempbranch);
for (var j = 0; j < aaa.department.length; j++) { // 部门
var temptype = aaa.department[j].type; // 上级
// console.log(aaa.department[2].type);
if (tempbranch.origin == temptype) { // 判断上级是否符合
// console.log(j);
var tempdepartment = { "children": [] };
tempdepartment.id = aaa.department[j].charge_name + j;
var ishave = false; // 是否添加过
for (var m = 0; m < tempbranch.children.length; m++) {
if (tempbranch.children[m].id == tempdepartment.id) {
ishave = true;
}
}
if (!ishave) { // 未添加过
tempdepartment.label = aaa.department[j].department_name;
tempdepartment.bottomText = aaa.department[j].charge_name;
tempdepartment.origin = aaa.department[j].department_name; // 原始本级名
// console.log(tempdepartment.origin);
var tempdepartment = { "children": [] };
tempdepartment.id = aaa.department[j].charge_name + j;
tempdepartment.label = aaa.department[j].department_name;
tempdepartment.bottomText = aaa.department[j].charge_name;
tempdepartment.origin = aaa.department[j].department_name; // 原始本级名
// console.log(tempdepartment.origin);
tempbranch.children.push(tempdepartment);
// aaa.department.splice(j, 1); // 删除防重
}
} else if (temptype.includes("直管部门")) {
var tempdepartment = { "children": [] };
tempdepartment.id = aaa.department[j].charge_name + j;
var ishave = false; // 是否添加过
for (var m = 0; m < result.children.length; m++) {
if (result.children[m].id == tempdepartment.id) {
ishave = true;
}
}
if (!ishave) { // 未添加过
tempdepartment.label = aaa.department[j].department_name;
tempdepartment.bottomText = aaa.department[j].charge_name;
tempdepartment.origin = aaa.department[j].department_name;
result.children.push(tempdepartment);
aaa.department.splice(j, 1); // 删除防重
// j = j - 1;
}
}
for (var k = 0; k < aaa.job.length; k++) { // 岗位
var temptype = aaa.job[k].type; // 上级
if (tempdepartment.origin == temptype) { // 判断上级是否符合
var tempjob = { "children": [] };
tempjob.id = aaa.job[k].job_name + k;
var ishave = false; // 是否添加过
for (var m = 0; m < tempdepartment.children.length; m++) {
if (tempdepartment.children[m].id == tempjob.id) {
ishave = true;
}
}
if (!ishave) { // 未添加过
tempjob.label = aaa.job[k].job_name;
tempdepartment.children.push(tempjob);
// aaa.job.splice(k, 1); // 删除防重
}
} else if (temptype.includes("直管岗位")) {
var tempjob = { "children": [] };
tempjob.id = aaa.job[k].job_name + k;
var ishave = false; // 是否添加过
for (var m = 0; m < result.children.length; m++) {
if (result.children[m].id == tempjob.id) {
ishave = true;
}
}
if (!ishave) { // 未添加过
tempjob.label = aaa.job[k].job_name;
tempjob.origin = aaa.job[k].job_name;
result.children.push(tempjob);
aaa.job.splice(k, 1); // 删除防重
// j = k - 1;
}
}
}
}
}
return result;
}
function nothavebranch(aaa) {
var result = {};
for (var i = 0; i < aaa.department.length; i++) { // 分管/分支
var tempdepartment = { "children": [] };
tempdepartment.id = aaa.department[i].charge_name + i;
tempdepartment.label = aaa.department[i].department_name;
tempdepartment.bottomText = aaa.department[i].charge_name;
tempdepartment.origin = aaa.department[i].department_name; // 原始本级名
result.children.push(tempdepartment);
for (var k = 0; k < aaa.job.length; k++) { // 岗位
var temptype = aaa.job[k].type; // 上级
if (tempdepartment.origin == temptype) { // 判断上级是否符合
var tempjob = { "children": [] };
tempjob.id = aaa.job[k].job_name + k;
var ishave = false; // 是否添加过
for (var m = 0; m < tempdepartment.children.length; m++) {
if (tempdepartment.children[m].id == tempjob.id) {
ishave = true;
}
}
if (!ishave) { // 未添加过
tempjob.label = aaa.job[k].job_name;
tempdepartment.children.push(tempjob);
// aaa.job.splice(k, 1); // 删除防重
}
} else if (temptype.includes("直管岗位")) {
var tempjob = { "children": [] };
tempjob.id = aaa.job[k].job_name + k;
var ishave = false; // 是否添加过
for (var m = 0; m < result.children.length; m++) {
if (result.children[m].id == tempjob.id) {
ishave = true;
}
}
if (!ishave) { // 未添加过
tempjob.label = aaa.job[k].job_name;
tempjob.origin = aaa.job[k].job_name;
result.children.push(tempjob);
aaa.job.splice(k, 1); // 删除防重
// j = k - 1;
}
}
}
}
}
function formatData(organizationdetailsformat) {
let aaa = organizationdetailsformat;
var result = {};
if (aaa.base.isgroup == "1") { // 集团
var secondlevel = {}, others = {};
result.id = aaa.base.groupname;
result.label = aaa.base.groupname;
result.children = [];
secondlevel.id = aaa.base.leadertype;
secondlevel.label = aaa.base.leadertype;
secondlevel.bottomText = aaa.base.leadername;
secondlevel.children = [];
result.children.push(secondlevel);
if (aaa.base.isbranch == '1') { // 设立分管/分支 && 存在部门
obj = havebranch(aaa);
Object.assign(result.children, obj);
// result.children = obj;
} else { // 未设立
obj = nothavebranch(aaa);
Object.assign(result.children, obj);
// result.children = obj;
}
// 其他
others.id = '......';
others.label = '......';
result.children.push(others);
} else { // 非集团
result.id = aaa.base.leadertype;
result.label = result.id;
result.bottomText = aaa.base.leadername;
result.children = [];
if (aaa.base.isbranch == '1') { // 设立分管/分支 && 存在部门 && 存在岗位
obj = havebranch(aaa);
Object.assign(result, obj);
} else { // 未设立分管/分支
obj = nothavebranch(aaa);
Object.assign(result, obj);
}
}
console.log(result);
return result;
}
console