Skip to content

Instantly share code, notes, and snippets.

@kuznetsovandrey76
Last active October 25, 2018 11:28
Show Gist options
  • Save kuznetsovandrey76/b0dd816c1e33ff466be7b4b708319478 to your computer and use it in GitHub Desktop.
Save kuznetsovandrey76/b0dd816c1e33ff466be7b4b708319478 to your computer and use it in GitHub Desktop.
XML Data
alert('* Агент Запущен | Потребуется 1-2 мин...');
// Файл расположен на сервере в папке wt\web
var url = AppDirectoryPath() + "\\wt\\web\\Сотрудники1.xml";
try {
function create_table(url) {
stringXML = LoadUrlData(FilePathToUrl(url));
var update_fields = {
"СписокСотрудников": "list_collaborators",
"Сотрудник": "collaborator",
"НаименованиеСокращенное": "name_short",
"НаименованиеПолное": "name_full",
"Наименование": "name",
"Код": "code",
"ВышестоящееПодразделение": "parent_division",
"Подразделение": "division",
"Должность": "position",
"ГоловнаяОрганизация": "head_org",
"Организация": "org",
"ИНН": "inn",
"КПП": "kpp",
"ЮридическоеФизическоеЛицо": "legal_person",
"КорпПочта": "email",
"ДатаПриема": "hire_date",
"ДатаРождения": "birth_date",
"<Пол>": "<sex>",
"</Пол>": "</sex>"
};
for (key in update_fields) {
stringXML = StrReplace(stringXML, key, update_fields[key]);
}
return OpenDocFromStr(stringXML).TopElem;
}
var table = create_table(url);
function count_collaborator(table) {
var count = 0;
for(elem in table) {
if (elem.Name == "collaborator") {
count += 1;
}
}
return count;
}
var number_of_collaborator = count_collaborator(ArrayDirect(table));
var org_uids = [];
var org_from_uid_to_id = {}; // "uid" : "id"
function create_org(data) {
if (org_uids.indexOf(data.org.uid) == -1) {
org_uids.push(data.org.uid)
docPOL = OpenNewDoc('x-local://wtv/wtv_org.xmd');
var org = docPOL.TopElem;
org.custom_elems.ObtainChildByKey("f_org_uid").value = data.org.uid;
org.name = data.org.name;
org.disp_name = data.org.name;
var ess = org.essentials.Add();
ess.inn = Int(data.org.inn);
ess.kpp = Int(data.org.kpp);
// Проверка есть ли org в базе
var request = "for $elem in orgs where doc-contains($elem/id,'wt_data','[f_org_uid contains " + String(data.org.uid) + "~string]') return $elem";
org_in_db = ArrayCount(XQuery(request));
if(org_in_db) {
org_from_uid_to_id[String(data.org.uid)] = Int(RValue(ArrayOptFirstElem(XQuery(request)).id));
} else {
docPOL.BindToDb(DefaultDb);
var org_id = docPOL.DocID
org_from_uid_to_id[String(data.org.uid)] = Int(org_id);
docPOL.Save();
}
}
}
function update_org(data) {
var request = "for $elem in orgs where doc-contains($elem/id,'wt_data','[f_org_uid contains " + String(data.org.uid) + "~string]') return $elem";
org_in_db = ArrayCount(XQuery(request));
if(org_in_db) {
// Проверка полей
org_from_uid_to_id[String(data.org.uid)] = Int(RValue(ArrayOptFirstElem(XQuery(request)).id));
} else {
create_org(data);
}
}
var division_uids = [];
var division_from_uid_to_id = {}; // "uid" : "id"
function create_div(data) {
if (division_uids.indexOf(data.div.uid) == -1) {
division_uids.push(data.div.uid)
docPOL = OpenNewDoc('x-local://wtv/wtv_subdivision.xmd');
var division = docPOL.TopElem;
division.code = data.div.code;
division.name = data.div.name;
division.org_id = (data.org.uid ? org_from_uid_to_id[String(data.org.uid)] : '');
division.custom_elems.ObtainChildByKey("f_div_uid").value = data.div.uid;
var request = "for $elem in subdivisions where doc-contains($elem/id,'wt_data','[f_div_uid contains " + String(data.div.uid) + "~string]') return $elem";
// start parent
if (division_uids.indexOf(data.div.parent_uid) == -1 && data.div.exist_parent) {
division_uids.push(data.div.parent_uid)
docParent = OpenNewDoc('x-local://wtv/wtv_subdivision.xmd');
var div_parent = docParent.TopElem;
div_parent.code = data.div.parent_code;
div_parent.name = data.div.parent_name;
div_parent.org_id = (data.org.uid ? org_from_uid_to_id[String(data.org.uid)] : '');
div_parent.custom_elems.ObtainChildByKey("f_div_uid").value = data.div.parent_uid;
// Проверка есть ли div_parent в базе
div_parent_in_db = ArrayCount(XQuery(request));
if(div_parent_in_db) {
division_from_uid_to_id[String(data.div.parent_uid)] = Int(RValue(ArrayOptFirstElem(XQuery(request)).id));
} else {
docParent.BindToDb(DefaultDb);
var div_parent_id = docParent.DocID
division_from_uid_to_id[String(data.div.parent_uid)] = Int(div_parent_id);
docParent.Save();
}
}
division.parent_object_id = (data.div.parent_uid ? division_from_uid_to_id[String(data.div.parent_uid)] : '');
// end
// Проверка есть ли div в базе
div_in_db = ArrayCount(XQuery(request));
if(div_in_db) {
division_from_uid_to_id[String(data.div.uid)] = Int(RValue(ArrayOptFirstElem(XQuery(request)).id));
} else {
docPOL.BindToDb(DefaultDb);
var division_id = docPOL.DocID
division_from_uid_to_id[String(data.div.uid)] = Int(division_id);
docPOL.Save();
}
}
}
function update_div(data) {
var request = "for $elem in subdivisions where doc-contains($elem/id,'wt_data','[f_div_uid contains " + String(data.div.uid) + "~string]') return $elem";
div_in_db = ArrayCount(XQuery(request));
if(div_in_db) {
// Проверка полей
div_from_uid_to_id[String(data.div.uid)] = Int(RValue(ArrayOptFirstElem(XQuery(request)).id));
} else {
create_div(data);
}
}
var collaborator_from_code_to_id = {}; // "code" : "id"
function create_clb(data) {
fullname = String(data.clb.name).split(' ');
gender = (data.clb.sex == "Мужской" ? "m" : "w");
docPOL = OpenNewDoc('x-local://wtv/wtv_collaborator.xmd');
var clb = docPOL.TopElem;
clb.code = data.clb.code;
clb.custom_elems.ObtainChildByKey("f_clb_uid").value = data.clb.uid;
clb.lastname = fullname[0];
clb.firstname = fullname[1];
fullname.splice(0, 2);
clb.middlename = fullname.join(' ');
clb.email = data.clb.email;
clb.hire_date = (Year(Date(data.clb.hire_date)) > 1990 ? data.clb.hire_date : '');
clb.sex = gender;
clb.birth_date = data.clb.birth_date;
clb.org_id = (data.org.uid ? org_from_uid_to_id[String(data.org.uid)] : '');
docPOL.BindToDb(DefaultDb);
var user_id = docPOL.DocID;
collaborator_from_code_to_id[String(data.clb.code)] = Int(user_id);
docPOL.Save();
}
function update_clb(data) {
fullname = String(data.clb.name).split(' ');
gender = (data.clb.sex == "Мужской" ? "m" : "w");
docPOL = OpenDoc(UrlFromDocID(data.clb.id));
var clb = docPOL.TopElem;
if(clb.code != data.clb.code) clb.code = data.clb.code;
if(clb.custom_elems.ObtainChildByKey("f_clb_uid").value != data.clb.uid) clb.custom_elems.ObtainChildByKey("f_clb_uid").value = data.clb.uid;
if(clb.lastname != fullname[0]) clb.lastname = fullname[0];
if(clb.firstname != fullname[1]) clb.firstname = fullname[1];
fullname.splice(0, 2);
if(clb.middlename != fullname.join(' ')) clb.middlename = fullname.join(' ');
if(clb.email != data.clb.email) clb.email = data.clb.email;
if(clb.hire_date != (Year(Date(data.clb.hire_date)) > 1990 ? data.clb.hire_date : '')) clb.hire_date != (Year(Date(data.clb.hire_date)) > 1990 ? data.clb.hire_date : '');
if(clb.sex != gender) clb.sex = gender;
if(clb.birth_date != data.clb.birth_date) clb.birth_date = data.clb.birth_date ;
// clb.org_id = (data.org.uid ? org_from_uid_to_id[String(data.org.uid)] : '');
docPOL.Save();
}
var position_uids = [];
var position_from_code_to_id = {}; // "code" : "id"
function create_pos(data) {
user_id = XQuery("for $elem in collaborators where $elem/code='" + String(data.clb.code) + "' return $elem");
docPOL = OpenNewDoc('x-local://wtv/wtv_position.xmd');
var position = docPOL.TopElem;
position.code = data.pos.code;
position.name = data.pos.name;
position.basic_collaborator_id = collaborator_from_code_to_id[String(data.clb.code)]
position.org_id = (data.org.uid ? org_from_uid_to_id[String(data.org.uid)] : '');
position.parent_object_id = (data.div.uid ? division_from_uid_to_id[String(data.div.uid)] : '');
position.custom_elems.ObtainChildByKey("f_pos_uid").value = data.pos.uid;
// Проверка есть ли position в базе
var request = "for $elem in positions where doc-contains($elem/id,'wt_data','[f_pos_uid contains " + String(data.pos.uid) + "~string]') return $elem";
pos_in_db = ArrayCount(XQuery(request));
if(pos_in_db) {
docPOL.BindToDb(DefaultDb);
var position_id = docPOL.DocID;
position_from_code_to_id[String(data.pos.code)] = Int(RValue(ArrayOptFirstElem(XQuery(request)).id));
docPOL.Save();
} else {
docPOL.BindToDb(DefaultDb);
var position_id = docPOL.DocID;
position_from_code_to_id[String(data.pos.code)] = Int(position_id);
docPOL.Save();
}
}
function update_pos(data) {
var request = "for $elem in positions where doc-contains($elem/id,'wt_data','[f_pos_uid contains " + String(data.pos.uid) + "~string]') return $elem";
pos_in_db = ArrayCount(XQuery(request));
if(pos_in_db) {
// Проверка полей
position_from_code_to_id[String(data.pos.code)] = Int(RValue(ArrayOptFirstElem(XQuery(request)).id));
} else {
create_pos(data);
}
}
function create_finish(data) {
user_id = ArrayOptFirstElem(XQuery("for $elem in collaborators where doc-contains($elem/id,'wt_data','[f_clb_uid contains " + String(data.clb.uid) + "~string]') return $elem"))
position_id = ArrayOptFirstElem(XQuery("for $elem in positions where $elem/basic_collaborator_id=" + RValue(user_id.id) + " return $elem"));
collaboratorXML = OpenDoc(UrlFromDocID(RValue(user_id.id)))
collaboratorXML.TopElem.position_id = RValue(position_id.id)
collaboratorXML.Save();
}
function update_finish(data) {
// У нас есть id
}
for (i = 0, max = number_of_collaborator; i < max; i++) {
data = {
clb: {
uid: table[i][0], // ГУИД сотрудника;
code: table[i][1], // Табельный номер сотрудника;
name: table[i][2], // ФИО;
email: table[i][6], // E-mail;
hire_date: table[i][7], // Дата вступления в должность;
birth_date: table[i][8], // Дата рождения;
sex: table[i][9], // Пол;
id: null
},
pos: {
uid: table[i][4][0], // ГУИД должности
name: table[i][4][1], // Название должности;
code: table[i][4][2]
},
div: {
exist: false,
exist_parent: false,
uid: '',
parent_uid: ''
},
org: {
exist: false,
uid: ''
}
};
// Проверка существования data.clb.uid
request_clb = "for $elem in collaborators where doc-contains($elem/id,'wt_data','[f_clb_uid contains " + String(data.clb.uid) + "~string]') return $elem";
clb_in_db = ArrayCount(XQuery(request_clb));
if(clb_in_db) { // Exist | Обновить поля
// data.clb.id = RValue(ArrayOptFirstElem(XQuery(request_clb)).id);
// update_org(data)
// update_div(data)
// update_clb(data)
// update_pos(data)
// update_finish(data)
} else { // not Exist
// Проверка заполнения поля Org
if (ArrayCount(table[i][5])) {
data.org.exist = true;
data.org.uid = table[i][5][0];
data.org.name = table[i][5][1]; // Название организации;
data.org.code = table[i][5][2]; // Код организации;
data.org.inn = table[i][5][3];
data.org.kpp = table[i][5][4];
data.org.name_short = table[i][5][5];
data.org.name_full = table[i][5][6];
data.org.legal_person = table[i][5][7];
data.org.head_org = table[i][5][8];
}
if(data.org.exist) {
create_org(data)
}
// Проверка заполнения поля Division
if (ArrayCount(table[i][3])) {
data.div.exist = true;
data.div.uid = table[i][3][0];
data.div.code = table[i][3][1]; // Код подразделения;
data.div.name = table[i][3][2]; // Название подразделения;
// Есть ли Parent Division
if (ArrayCount(table[i][3][3])) {
data.div.exist_parent = true;
data.div.parent_uid = table[i][3][3][0];
data.div.parent_code = table[i][3][3][1]; // Вышестоящее подразделение;
data.div.parent_name = table[i][3][3][2];
}
}
if (data.div.exist) {
create_div(data);
}
create_clb(data)
create_pos(data)
create_finish(data)
}
}
// DeleteDoc(FilePathToUrl(url));
} catch (err) {
alert(err)
}
alert('* Данные обновлены');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment