angular.module('push') .factory('HandleService', function (UtilService, ConfigService, UserService, $q, ModelService) { /*--同步状态synchrotype 0:已同步,1:新增,2:修改,3:逻辑删除,4:物理删除--*/ var handleFunction = { //--依据对接记录计算本地文件夹下counts computeFolderCounts: function (tempfolderlist, tempconninfolist) { var deferred = $q.defer(); try { //父文件夹、子文件夹分类 var parentlsit = []; var smalllist = []; angular.forEach(tempfolderlist, function (value, index) { if (value.localparentid == null || value.localparentid == "0") { parentlsit.push(value); } else { smalllist.push(value); } }); //计算分类下文件夹总数(剔除已删除文件夹) angular.forEach(parentlsit, function (value, index) { var counts = 0; angular.forEach(smalllist, function (data, ind) { //计算各分类下已发布记录counts if (value.localid == data.localparentid && data.synchrotype != 4) { counts++; } }); parentlsit[index].counts = counts; }); //计算分类下信息总数(剔除已删除信息) angular.forEach(parentlsit, function (value, index) { var counts = 0; angular.forEach(tempconninfolist, function (data, ind) { //计算各分类下已发布记录counts if (value.localid == data.localclaid && data.synchrotype != 4) { counts++; } }); parentlsit[index].counts += counts; }); deferred.resolve(parentlsit); } catch (err) { deferred.reject(err); } return deferred.promise; }, //--处理本地查询的对接信息(所有表查询成功后,组装成同步请求参数) handleRequestData: function (conninfolist, picturelist, cardlist, personlist, themelist, productlist, iur1list, iur2list) { var deferred = $q.defer(); var result = { conninfolist: [], cardlist: [] }; try { angular.forEach(conninfolist, function (value, index) { //图片拼接 var templist1 = []; angular.forEach(picturelist, function (data, ind) { if (value.localid == data.localinfoid) { templist1.push(data); } }); if (!UtilService.isDefined(value.defaultFlag) && value.defaultFlag != 1) { conninfolist[index].defaultFlag = 0; } conninfolist[index].picture = templist1; }); //名片拼接 angular.forEach(cardlist, function (value, index) { //人员拼接 var templist2 = []; angular.forEach(personlist, function (data, ind) { if (value.localid == data.localuid) { templist2.push(data); } }); cardlist[index].personlist = templist2; //主题拼接 var templist3 = []; angular.forEach(themelist, function (data, ind) { if (value.localid == data.localuid) { templist3.push(data); } }); cardlist[index].themelist = templist3; //主题拼接 var templist4 = []; angular.forEach(productlist, function (data, ind) { if (value.localid == data.localuid) { templist4.push(data); } }); cardlist[index].productlist = templist4; //主题拼接 var templist5 = []; angular.forEach(iur1list, function (data, ind) { if (value.localid == data.localuid) { templist5.push(data); } }); cardlist[index].iur1list = templist5; //主题拼接 var templist6 = []; angular.forEach(iur2list, function (data, ind) { if (value.localid == data.localuid) { templist6.push(data); } }); cardlist[index].iur2list = templist6; }); //区分记录状态 result.conninfolist = conninfolist; result.cardlist = cardlist; deferred.resolve(result); } catch (err) { deferred.reject(err); } return deferred.promise; }, //--本地记录列表提取第一张图为列表显示图 extractConninfoPicture: function (conninfolist, picturelist) { var defered = $q.defer(); try { angular.forEach(conninfolist, function (value, index) { var num = 0; conninfolist[index].image = ""; angular.forEach(picturelist, function (data) { if (value.localid == data.localinfoid) { num++; if (num == 1) { conninfolist[index].image = data.photo_name; } } }); }); defered.resolve(conninfolist); } catch (e) { defered.reject(e) } return defered.promise; }, //--处理FirstServer返回数据 handleFirstServerData: function (tempdata) { var deferred = $q.defer(); var response = { conninfofolder: [],//分类 conninfo: [],//对接记录 conninfopicture: [],//图片 conninfounit: [],//名片 conninfoperson: [],//人员 conninfotheme: [],//主题 conninfoproduct: [],//产品 conninfoiur: [],//产学研经历 conninforesearch: []//重大科研项目 }; try { var folderlist = angular.copy(tempdata); //未所有新增文件夹生成Localid angular.forEach(folderlist, function (value, index) { folderlist[index].localid = UtilService.reversalDate(value.createtime) + ""; if (value.parentid == 0) { folderlist[index].localparentid = "0"; } }); //为所有新增文件夹生成父文件夹id (localparentid) angular.forEach(folderlist, function (value, index) { angular.forEach(folderlist, function (data, ind) { if (value.parentid == data.id) { folderlist[index].localparentid = data.localid; } }); }); //分类数据处理(取出所有对接信息组成List) var conninfolist = []; angular.forEach(folderlist, function (value, index) { // var templocalid = UtilService.reversalDate(value.createtime) + ""; // folderlist[index].localid = templocalid; angular.forEach(value.connlist, function (data, ind) { data.localclaid = value.localid; conninfolist.push(data); }); delete value["connlist"]; }); //对接信息、图片处理(取出所有名片) var picturelist = []; var companylist = []; angular.forEach(conninfolist, function (value, index) { var templocalid = UtilService.reversalDate(value.createtime); conninfolist[index].localid = templocalid + ""; angular.forEach(value.picture, function (data, ind) { data.localinfoid = templocalid + ""; data.localid = templocalid + ind + ""; picturelist.push(data); }); delete value["picture"]; angular.forEach(value.unit, function (data, ind) { data.localinfoid = templocalid + ""; companylist.push(data); }); delete value["unit"]; }); //名片、人员、主题处理 var personlist = []; var themelist = []; var productlist = []; var iur1list = []; var iur2list = []; angular.forEach(companylist, function (value, index) { var templocalid = UtilService.reversalDate(value.createtime); companylist[index].localid = templocalid + ""; angular.forEach(value.personlist, function (data, ind) { data.localuid = templocalid + ""; data.localid = templocalid + ind + ""; data.localinfoid = value.localinfoid; personlist.push(data); }); delete value["personlist"]; angular.forEach(value.themelist, function (data, ind) { data.localuid = templocalid + ""; data.localid = templocalid + ind + ""; data.localinfoid = value.localinfoid; themelist.push(data); }); delete value["themelist"]; angular.forEach(value.productlist, function (data, ind) { data.localuid = templocalid + ""; data.localid = templocalid + ind + ""; data.localinfoid = value.localinfoid; productlist.push(data); }); delete value["productlist"]; angular.forEach(value.iur1list, function (data, ind) { data.localuid = templocalid + ""; data.localid = templocalid + ind + ""; data.localinfoid = value.localinfoid; iur1list.push(data); }); delete value["iur1list"]; angular.forEach(value.iur2list, function (data, ind) { data.localuid = templocalid + ""; data.localid = templocalid + ind + ""; data.localinfoid = value.localinfoid; iur2list.push(data); }); delete value["iur2list"]; }); //返回组装数据 response.conninfofolder = folderlist; response.conninfo = conninfolist; response.conninfopicture = picturelist; response.conninfounit = companylist; response.conninfoperson = personlist; response.conninfotheme = themelist; response.conninfoproduct = productlist; response.conninfoiur = iur1list; response.conninforesearch = iur2list; // console.log(response); deferred.resolve(response); } catch (err) { deferred.reject(err); } return deferred.promise; }, //--处理SecondServer返回数据 handleSecondServerData: function (folderlist, recordlist, cardlist) { var defered = $q.defer(); var result = { insertList: { conninfofolder: [], conninfo: [], conninfounit: [] }, updateList: { conninfofolder: [], conninfo: [], conninfounit: [] }, deleteList: { conninfofolder: [], conninfo: [], conninfounit: [] }, conninfopicture: [], conninfoperson: [], conninfotheme: [], conninfoproduct: [],//产品 conninfoiur: [],//产学研经历 conninforesearch: []//重大科研项目 }; try { //未所有新增文件夹生成Localid angular.forEach(folderlist, function (value, index) { if (value.synchrotype == 1) { folderlist[index].localid = UtilService.reversalDate(value.createtime) + ""; if (value.parentid == 0) { folderlist[index].localparentid = "0"; } } }); //为所有新增文件夹生成父文件夹id (localparentid) angular.forEach(folderlist, function (value, index) { if (value.synchrotype == 1) { angular.forEach(folderlist, function (data, ind) { if (value.parentid == data.id) { folderlist[index].localparentid = data.localid; } }); } }); //----分类处理 var tempfolderlist1 = []; var tempfolderlist2 = ModelService.getLocalFolderList(); angular.forEach(folderlist, function (value, index) { //1、已同步数据:回写同步状态 if (value.synchrotype == 0) { var tempobj1 = {}; tempobj1.localid = value.localid; tempobj1.synchrotype = 0; result.updateList.conninfofolder.push(tempobj1); } //2、新增数据:生成localid、回写同步状态 if (value.synchrotype == 1) { value.synchrotype = 0; result.insertList.conninfofolder.push(value); } //3、修改数据:回写信息的数据、回写同步状态 if (value.synchrotype == 2 || value.synchrotype == 3) { value.synchrotype = 0; result.updateList.conninfofolder.push(value); } //4、删除数据:组装localid if (value.synchrotype == 4) { var tempobj2 = {}; tempobj2.localid = value.localid; result.deleteList.conninfofolder.push(tempobj2); } //5、生成localid后组装成folder->list tempfolderlist1.push(value); }); //----对接信息处理 var tempconninfolist = []; angular.forEach(recordlist, function (value, index) { //1、已同步数据:回写同步状态 if (value.synchrotype == 0) { var tempobj3 = {}; tempobj3.localid = value.localid; tempobj3.synchrotype = 0; result.updateList.conninfo.push(tempobj3); } //2、新增数据:生成localid、回写同步状态 if (value.synchrotype == 1) { value.localid = UtilService.reversalDate(value.createtime) + ""; value.synchrotype = 0; //2.1、新增的对接信息插入分类localid(分类新增:遍历请求返回的分类,分类未新增:遍历本地分类) angular.forEach(tempfolderlist1, function (data, ind) { if (value.claid == data.id) { value.localclaid = data.localid; } }); angular.forEach(tempfolderlist2, function (data, ind) { if (value.claid == data.id) { value.localclaid = data.localid; } }); result.insertList.conninfo.push(value); } //3、修改数据:回写信息的数据、回写同步状态 if (value.synchrotype == 2 || value.synchrotype == 3) { value.synchrotype = 0; angular.forEach(tempfolderlist1, function (data, ind) { if (value.claid == data.id) { value.localclaid = data.localid; } }); angular.forEach(tempfolderlist2, function (data, ind) { if (value.claid == data.id) { value.localclaid = data.localid; } }); result.updateList.conninfo.push(value); } //4、删除数据:组装localid if (value.synchrotype == 4) { var tempobj4 = {}; tempobj4.localid = value.localid; result.deleteList.conninfo.push(tempobj4); } //5、图片列表生成 if (angular.isDefined(value.picture)) { var temp = { localinfoid: value.localid, picturelist: [] }; if (value.synchrotype != 4) { angular.forEach(value.picture, function (data, ind) { data.localinfoid = value.localid; data.localid = UtilService.reversalDate(value.createtime) + ind + ""; temp.picturelist.push(data); }); } result.conninfopicture.push(temp); } ///6、生成localid后组装成conninfo->list delete value["picture"]; tempconninfolist.push(value) }); //----名片处理 angular.forEach(cardlist, function (value, index) { //1、已同步数据:回写同步状态 if (value.synchrotype == 0) { var tempobj5 = {}; tempobj5.localid = value.localid; tempobj5.synchrotype = 0; result.updateList.conninfounit.push(tempobj5); } //2、新增数据:生成localid、回写同步状态、插入对接信息localid if (value.synchrotype == 1) { var templocalid = UtilService.reversalDate(value.createtime); value.localid = templocalid + ""; value.synchrotype = 0; angular.forEach(tempconninfolist, function (data, ind) { //新增的对接信息插入分类localid if (value.infoid == data.id) { value.localinfoid = data.localid; } }); result.insertList.conninfounit.push(value); } //3、修改数据:回写信息的数据、回写同步状态 if (value.synchrotype == 2 || value.synchrotype == 3) { value.synchrotype = 0; result.updateList.conninfounit.push(value); } //4、删除数据:组装localid if (value.synchrotype == 4) { result.deleteList.conninfounit.push(value); } //5、人员列表生成 if (angular.isDefined(value.personlist)) { var temp2 = { localuid: value.localid, personlist: [] }; if (value.synchrotype != 4) { angular.forEach(value.personlist, function (data, ind) { data.localuid = value.localid; data.localid = UtilService.reversalDate(value.createtime) + ind + ""; temp2.personlist.push(data); }); } result.conninfoperson.push(temp2); } delete value["personlist"]; //5、主题列表生成 if (angular.isDefined(value.themelist)) { var temp3 = { localuid: value.localid, themelist: [] }; angular.forEach(value.themelist, function (data, ind) { data.localuid = value.localid; data.localid = UtilService.reversalDate(value.createtime) + ind + ""; temp3.themelist.push(data); }); result.conninfotheme.push(temp3); } delete value["themelist"]; //6、产品列表生成 if (angular.isDefined(value.productlist)) { var temp4 = { localuid: value.localid, productlist: [] }; angular.forEach(value.productlist, function (data, ind) { data.localuid = value.localid; data.localid = UtilService.reversalDate(value.createtime) + ind + ""; temp4.productlist.push(data); }); result.conninfoproduct.push(temp4); } delete value["productlist"]; //5、产学研经历列表生成 if (angular.isDefined(value.iur1list)) { var temp5 = { localuid: value.localid, iur1list: [] }; angular.forEach(value.iur1list, function (data, ind) { data.localuid = value.localid; data.localid = UtilService.reversalDate(value.createtime) + ind + ""; temp5.iur1list.push(data); }); result.conninfoiur.push(temp5); } delete value["iur1list"]; //5、主题列表生成 if (angular.isDefined(value.iur2list)) { var temp6 = { localuid: value.localid, iur2list: [] }; angular.forEach(value.iur2list, function (data, ind) { data.localuid = value.localid; data.localid = UtilService.reversalDate(value.createtime) + ind + ""; temp6.iur2list.push(data); }); result.conninforesearch.push(temp6); } delete value["iur2list"]; }); // console.log(result); defered.resolve(result); } catch (e) { defered.reject(e) } return defered.promise; }, //--处理详情数据 handleConninfoDetailData: function (cardlist, ttpersonlist, ttthemelist, tttproductlist, tttiur1list, tttiur2list, tttyearprojectlist) { var deferred = $q.defer(); try { angular.forEach(cardlist, function (value, index) { cardlist[index].personlist = []; angular.forEach(ttpersonlist, function (data, ind) { if (value.localid == data.localuid) { cardlist[index].personlist.push(data); } }); cardlist[index].themelist = []; angular.forEach(ttthemelist, function (data, ind) { if (value.localid == data.localuid) { cardlist[index].themelist.push(data); } }); cardlist[index].productlist = []; angular.forEach(tttproductlist, function (data, ind) { if (value.localid == data.localuid) { cardlist[index].productlist.push(data); } }); cardlist[index].iur1list = []; angular.forEach(tttiur1list, function (data, ind) { if (value.localid == data.localuid) { cardlist[index].iur1list.push(data); } }); cardlist[index].iur2list = []; angular.forEach(tttiur2list, function (data, ind) { if (value.localid == data.localuid) { cardlist[index].iur2list.push(data); } }); cardlist[index].calendarlist = []; angular.forEach(tttyearprojectlist, function (data, ind) { if (value.localid == data.localuid) { cardlist[index].calendarlist.push(data); } }); }); deferred.resolve(cardlist); } catch (err) { deferred.reject(err); } return deferred.promise; }, //--处理FirstServer返回数据 handleByServerData: function (tempconninfolist, tempcardlist) { var deferred = $q.defer(); var response = { conninfo: [],//对接记录 conninfopicture: [],//图片 conninfounit: [],//名片 conninfoperson: [],//人员 conninfotheme: []//主题 }; try { angular.forEach(tempconninfolist, function (value, index) { var tempobj1 = {}; tempobj1.localid = value.localid; response.conninfo.push(tempobj1); var tempobj2 = {}; tempobj2.localinfoid = value.localid; response.conninfopicture.push(tempobj2); }); angular.forEach(tempcardlist, function (value, index) { var tempobj3 = {}; tempobj3.localid = value.localid; response.conninfounit.push(tempobj3); var tempobj4 = {}; tempobj4.localuid = value.localid; response.conninfoperson.push(tempobj4); var tempobj5 = {}; tempobj5.localuid = value.localid; response.conninfotheme.push(tempobj5); }); deferred.resolve(response); } catch (err) { deferred.reject(err); } return deferred.promise; }, /*------------------------------------------数据操作(new)-------------------------------------------------*/ //--拼接本地与云文件夹(按sort、updatetime排序) connectLocalAndCloud: function (templocallist, tempcloudlist) { var deferred = $q.defer(); var response = []; try { var tempcloudlist1 = [];//有置顶列表 var tempcloudlist2 = [];//无置顶列表 angular.forEach(tempcloudlist, function (value) { if (value.sort > 0) { tempcloudlist1.push(value); } else { tempcloudlist2.push(value); } }); tempcloudlist2 = tempcloudlist2.concat(templocallist); tempcloudlist1.sort(function (a, b) { return b.sort - a.sort;//置顶(sort)降序 }); tempcloudlist2.sort(function (a, b) { var tempa = a.updatetime; var tempb = b.updatetime; //IOS 日期格式化 if (device.platform != "Android") { tempa = tempa.replace(/-/g, "/"); tempb = tempb.replace(/-/g, "/"); } return Date.parse(tempb) - Date.parse(tempa);//更新时间降序 }); response = tempcloudlist1.concat(tempcloudlist2);//拼接已排序好的数组 deferred.resolve(response); } catch (err) { // console.log(err); deferred.reject(err); } return deferred.promise; }, //--处理FirstServerResource返回数据 handleFirstServerResourceData: function (tempdata) { var deferred = $q.defer(); var response = { conninfofolder: [],//分类 resource: []//资源 }; try { var folderlist = angular.copy(tempdata); //分类数据处理(取出所有资源信息组成List) var resource = []; angular.forEach(folderlist, function (value, index) { var templocalid1 = UtilService.reversalDate(value.createtime); folderlist[index].localid = templocalid1 + ""; if (value.parentid == 0) { folderlist[index].localparentid = "0"; } angular.forEach(value.reslist, function (data, ind) { data.localclaid = value.localid; resource.push(data); }); delete value["reslist"]; }); //为所有新增文件夹生成父文件夹id (localparentid) angular.forEach(folderlist, function (value, index) { angular.forEach(folderlist, function (data, ind) { if (value.parentid == data.id) { folderlist[index].localparentid = data.localid; } }); }); //资源处理 angular.forEach(resource, function (value, index) { var templocalid2 = UtilService.reversalDate(value.createTime); resource[index].localid = templocalid2 + ""; }); //返回组装数据 response.conninfofolder = folderlist; response.resource = resource; deferred.resolve(response); } catch (err) { deferred.reject(err); } return deferred.promise; }, //--处理SecondServerResource返回数据 handleSecondServerResourceData: function (folderlist, resourcelist, uploadresliblist) { var defered = $q.defer(); var result = { insertList: { conninfofolder: [], resource: [] }, updateList: { conninfofolder: [], resource: [] }, deleteList: { conninfofolder: [], resource: [] } }; try { //未所有新增文件夹生成Localid angular.forEach(folderlist, function (value, index) { if (value.synchrotype == 1) { folderlist[index].localid = UtilService.reversalDate(value.createtime) + ""; if (value.parentid == 0) { folderlist[index].localparentid = "0"; } } }); //为所有新增文件夹生成父文件夹id (localparentid) angular.forEach(folderlist, function (value, index) { if (value.synchrotype == 1) { angular.forEach(folderlist, function (data, ind) { if (value.parentid == data.id) { folderlist[index].localparentid = data.localid; } }); } }); //----分类处理 angular.forEach(folderlist, function (value, index) { //1、已同步数据:回写同步状态 if (value.synchrotype == 0) { var tempobj1 = {}; tempobj1.localid = value.localid; tempobj1.synchrotype = 0; result.updateList.conninfofolder.push(tempobj1); } //2、新增数据:生成localid、回写同步状态 if (value.synchrotype == 1) { value.synchrotype = 0; result.insertList.conninfofolder.push(value); } //3、修改数据:回写信息的数据、回写同步状态 if (value.synchrotype == 2 || value.synchrotype == 3) { value.synchrotype = 0; result.updateList.conninfofolder.push(value); } //4、删除数据:组装localid if (value.synchrotype == 4) { var tempobj2 = {}; tempobj2.localid = value.localid; result.deleteList.conninfofolder.push(tempobj2); } }); //----资源信息处理 var tempresourcelist = []; angular.forEach(resourcelist, function (value, index) { //1、已同步数据:回写同步状态 if (value.synchrotype == 0) { var tempobj3 = {}; tempobj3.localid = value.localid; tempobj3.synchrotype = 0; result.updateList.resource.push(tempobj3); } //2、新增数据:生成localid、回写同步状态 if (value.synchrotype == 1) { value.localid = UtilService.reversalDate(value.createTime) + ""; value.synchrotype = 0; //2.1、新增的资源信息插入分类localid(分类新增:遍历请求返回的分类,分类未新增:遍历本地分类) angular.forEach(folderlist, function (data, ind) { if (value.claid == data.id) { value.localclaid = data.localid; } }); /*angular.forEach(uploadresliblist, function (data, ind) { if (value.claid == data.id) { value.localclaid = data.localid; } });*/ result.insertList.resource.push(value); } //3、修改数据:回写信息的数据、回写同步状态 if (value.synchrotype == 2 || value.synchrotype == 3) { value.synchrotype = 0; angular.forEach(folderlist, function (data, ind) { if (value.claid == data.id) { value.localclaid = data.localid; } }); /*angular.forEach(uploadresliblist, function (data, ind) { if (value.claid == data.id) { value.localclaid = data.localid; } });*/ result.updateList.resource.push(value); } //4、删除数据:组装localid if (value.synchrotype == 4) { var tempobj4 = {}; tempobj4.localid = value.localid; result.deleteList.resource.push(tempobj4); } tempresourcelist.push(value) }); defered.resolve(result); } catch (e) { defered.reject(e) } return defered.promise; } }; return handleFunction; });