'use strict';
var app = angular.module('app', [
'angularUtils.directives.dirPagination'
]);
app.controller("taizhangCtrl", ["$scope", "$http", "$timeout", function ($scope, $http, $timeout) {
var self = this;
var s4 = new SM4Util();
self.userId = $.cookie("GlWorkPlatform-userid");
self.userName = $.cookie("GlWorkPlatform-chineseName");
self.get_AccessToken = localStorage.getItem("GlWorkPlatform-AccessToken");
self.get_lastReturnPageno = sp.getUrlName("lastReturnPageno");
var postCfg = {
headers: {
'Content-Type': 'application/json',
'Authorization': "Bearer " + self.get_AccessToken
}
}
self.loading = false;
self.search = JSON.parse($.cookie("GlWorkPlatform-yzaptz")) || { DATE1: sp.getWeekNumber(new Date()).begin, DATE2: sp.getWeekNumber(new Date()).end };
self.getSORTID = function () {
var data = {
userid: self.userId
}
$http.post(apiurljs.login + "g2app/richang/queryLDSortid", { data: s4.encryptData_CBC(JSON.stringify(data)) }, postCfg)
.success(function (obj) {
var res = strToJson(s4.decryptData_CBC(obj.data));
self.LDSORTID = res.data;
self.getData()
})
}
self.getData = function () {
self.list = [];
self.loading = false;
var data = {
tablename: "SX_YZAPTZ",
colums: "*,TO_CHAR(YZAPDATE,'yyyy-mm-dd')as YZAPDATE1,TO_CHAR(CTIME,'hh24:mi')as CTIME1",
order: "CTIME",
sqlwhere: " and YZAPDATE BETWEEN '" + self.search.DATE1 + " 00:00:00' AND '" + self.search.DATE2 + " 23:59:59' and USERID='" + self.userId + "'"
}
$http.post(apiurljs.login + "g2app/dataabase/queryDataByColStr", { data: s4.encryptData_CBC(JSON.stringify(data)) }, postCfg)
.success(function (obj) {
var res = strToJson(s4.decryptData_CBC(obj.data));
self.loading = false;
self.addFlag = false;
self.editFlag = false;
self.addList = [];
self.list = res.data;
self.list.forEach(function (item) {
item.CNAME = item.CNAME == "null" ? "" : item.CNAME
item.ATTENDANCESCOPE = item.ATTENDANCESCOPE == "null" ? "" : item.ATTENDANCESCOPE
item.ADDR = item.ADDR == "null" ? "" : item.ADDR
item.TIMEPERIOD1 = item.TIMEPERIOD;
item.CTIME2 = item.CTIME1;
item.CNAME1 = item.CNAME;
item.ATTENDANCESCOPE1 = item.ATTENDANCESCOPE;
item.ADDR1 = item.ADDR;
})
console.log(self.list)
})
}
self.getSORTID();
self.openLayer = function () {
sp.layer("#Search")
}
self.doSearch = function () {
sp.layerhide()
if (self.search.DATE1 == "" || self.search.DATE2 == "") {
sp.dialog("时间为可为空!");
return
}
$.cookie("GlWorkPlatform-yzaptz", JSON.stringify(self.search), { path: '/', expires: 1 });
self.getData();
}
self.cancal = function () {
self.search = {
DATE1: sp.getWeekNumber(new Date()).begin,
DATE2: sp.getWeekNumber(new Date()).end
}
$.cookie("GlWorkPlatform-yzaptz", null, { path: '/', expires: 1 });
self.getData();
sp.layerhide()
}
self.refresh = function () {
self.getData();
}
self.openInit = function () {
self.init = {
DATE1: sp.getWeekNumber(new Date()).begin, DATE2: sp.getWeekNumber(new Date()).end
}
sp.layer("#Init")
}
self.doInit = function () {
var data = {
userid: self.userId,
routeinfoid:0,
startdate: self.init.DATE1 + " 00:00:00",
enddate: self.init.DATE2 + " 23:59:59"
}
$http.post(apiurljs.login + "g2app/richang/initYZAPTZList", { data: s4.encryptData_CBC(JSON.stringify(data)) }, postCfg)
.success(function (obj) {
var res = strToJson(s4.decryptData_CBC(obj.data));
sp.layerhide();
if (res.success) {
self.getData();
} else {
sp.dialog(res.message)
}
})
}
self.addItem = function () {
self.addFlag = true;
self.addList.push({
LDSORTID: self.LDSORTID,
YZAPDATE: sp.format(sp.getLocalDate()),
CWEEK: weeks[new Date().getDay()],
TIMEPERIOD: "上午",
CTIME: "09:00",
CNAME: "",
ATTENDANCESCOPE: "",
ADDR: ""
})
$timeout(function () {
$(".table-scroll").scrollTop($(".table-scroll").find("table").height())
}, 1)
}
self.delItem = function (item) {
var dialogHead = "温馨提示×";
var dialogBody = "
删除后不可恢复,您确认要删除吗?
";
var dialogFoot = "";
$('.sp-dialog').html(dialogHead + dialogBody + dialogFoot);
sp.openCenter(".sp-dialog");
///取消按钮
$('.sp-closeDialog').click(function (event) {
sp.closeCenter(".sp-dialog");
event.preventDefault();
event.stopPropagation();
});
//post消息的id
$("#delDataTrue").click(function () {
var data = {
tablename: "SX_YZAPTZ",
id: item.ID
};
$http.post(apiurljs.login + "g2app/dataabase/delDataById", { data: s4.encryptData_CBC(JSON.stringify(data)) }, postCfg)
.success(function (obj) {
var res = strToJson(s4.decryptData_CBC(obj.data));
sp.dialoghide();
if (res.success) {
self.getData();
} else {
sp.dialog("删除失败,请联系管理员!");
}
});
});
}
var weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
self.changeCtime = function (obj, n) {
if (n == 0) {
if (obj.CTIME >= "13:00") {
obj.TIMEPERIOD = "下午"
} else {
obj.TIMEPERIOD = "上午"
}
} else {
if (obj.CTIME2 >= "13:00") {
obj.TIMEPERIOD1 = "下午"
} else {
obj.TIMEPERIOD1 = "上午"
}
}
obj.CWEEK = weeks[new Date(obj.YZAPDATE).getDay()]
}
self.btnCancal = function () {
self.addFlag = false;
self.addList = []
self.editFlag = false;
self.list.forEach(function (item) {
item.TIMEPERIOD1 = item.TIMEPERIOD;
item.CTIME2 = item.CTIME1;
item.CNAME1 = item.CNAME;
item.ATTENDANCESCOPE1 = item.ATTENDANCESCOPE;
item.ADDR1 = item.ADDR;
})
}
self.btnSave = function () {
var n1 = true, n2 = true;
if (self.addList.length > 0) {
n1 = false;
var data1 = {
tablename: "SX_YZAPTZ",
data: []
}
self.addList.forEach(function (item) {
data1.data.push({
LDSORTID: item.LDSORTID,
YZAPDATE: item.YZAPDATE,
TIMEPERIOD: item.TIMEPERIOD,
CWEEK: item.CWEEK,
CTIME: item.YZAPDATE + " " + item.CTIME + ":00",
CNAME: item.CNAME,
ATTENDANCESCOPE: item.ATTENDANCESCOPE,
ADDR: item.ADDR,
USERNAME: self.userName,
USERID: self.userId,
SECTCODE: self.userDpCode,
CDATE: sp.getLocalDate()
})
})
$http.post(apiurljs.login + "g2app/dataabase/insertDataList", { data: s4.encryptData_CBC(JSON.stringify(data1)) }, postCfg)
.success(function (obj) {
var res = strToJson(s4.decryptData_CBC(obj.data));
if (res.success == true) {
n1 = true;
if (self.list.length > 0) {
n2 = false;
var data2 = {
entity: "SX_YZAPTZ",
data: [],
setwhere: []
}
self.list.forEach(function (item) {
data2.data.push({
TIMEPERIOD: item.TIMEPERIOD1,
CTIME: item.YZAPDATE1 + " " + item.CTIME2 + ":00",
CNAME: item.CNAME1,
ATTENDANCESCOPE: item.ATTENDANCESCOPE1,
ADDR: item.ADDR1
})
data2.setwhere.push({ ID: item.ID })
})
$http.post(apiurljs.login + "g2app/Data/UpdataByWhereList", { data: s4.encryptData_CBC(JSON.stringify(data2)) }, postCfg)
.success(function (obj1) {
var res1 = strToJson(s4.decryptData_CBC(obj1.data));
if (res1.success == true) {
n2 = true;
if (n1 == true && n2 == true) {
self.getData();
sp.dialog("保存成功!");
$timeout(function () {
sp.dialoghide();
}, 1500)
}
} else {
sp.dialog(res1.message)
}
})
} else {
if (n1 == true && n2 == true) {
self.getData();
sp.dialog("保存成功!");
$timeout(function () {
sp.dialoghide();
}, 1500)
}
}
} else {
sp.dialog(res.message)
}
})
} else {
n2 = false;
var data1 = {
entity: "SX_YZAPTZ",
data: [],
setwhere: []
}
self.list.forEach(function (item) {
data1.data.push({
TIMEPERIOD: item.TIMEPERIOD1,
CTIME: item.YZAPDATE1 + " " + item.CTIME2 + ":00",
CNAME: item.CNAME1,
ATTENDANCESCOPE: item.ATTENDANCESCOPE1,
ADDR: item.ADDR1
})
data1.setwhere.push({ ID: item.ID })
})
$http.post(apiurljs.login + "g2app/Data/UpdataByWhereList", { data: s4.encryptData_CBC(JSON.stringify(data1)) }, postCfg)
.success(function (obj) {
var res = strToJson(s4.decryptData_CBC(obj.data));
if (res.success == true) {
n2 = true;
if (n1 == true && n2 == true) {
self.getData();
sp.dialog("保存成功!");
$timeout(function () {
sp.dialoghide();
}, 1500)
}
} else {
sp.dialog(res.message)
}
})
}
}
self.printOut = function () {
if (self.canExportExcel) {
$('#table td').attr('t', 's')
var defaultCellStyle = {
font: {
name: 'Times New Roman',
sz: 12,
color: {
rgb: "000000"
},
bold: false, italic: false,
underline: false
},
alignment: {
vertical: "center",
horizontal: "center", indent: 0, wrapText: true
},
border: {
top: {
style: "thin",
color: {
rgb: "000000"
}
},
right: {
style: "thin",
color: {
rgb: "000000"
}
},
bottom: {
style: "thin",
color: {
rgb: "000000"
}
},
left: {
style: "thin",
color: {
rgb: "000000"
}
}
}
};
var cell = { defaultCellStyle: defaultCellStyle };
var wb = XLSX.utils.table_to_book_addtitle(document.querySelector('#table'), cell, "一周安排台账")
//设置表格的样式
var wbout = XLSX.write(wb, {
bookType: 'xlsx',
bookSST: false,
type: 'binary',
cellStyles: true,
defaultCellStyle: defaultCellStyle,
showGridLines: false
});
var s2ab = function (s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i = 0; i !== s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
};
try {
saveAs(new Blob([s2ab(wbout)], { type: 'application/octet-stream' }), '一周安排台账.xlsx')
} catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) }
return wbout
} else {
sp.dialog('数据还未加载完成,请稍等...')
}
}
//头部下滚冻结
$(function () {
//页面高度撑满
setTimeout(function () {
if ($(".ibox-content").height() < $(window).height() - 31) {
$(".ibox-content").css("height", $(window).height() - 31)
}
}, 1)
})
}])
app.directive("repeatFinish", function () {
return {
link: function (scope) {
if (scope.$last == true) {
scope.$parent.ctl.canExportExcel = true;
setTimeout(function () {
$("#table").rowspan(1)
$("#table").rowspan(0)
}, 10);
}
}
};
});
jQuery.fn.rowspan = function (colIdx) { //封装的一个JQuery小插件
return this.each(function () {
var that;
$('tr', this).each(function (row) {
$('td:eq(' + colIdx + ')', this).filter(':visible').each(function (col) {
if (that != null && $(this).html() == $(that).html()) {
var rowspan = $(that).attr("rowSpan");
if (rowspan == undefined) {
$(that).attr("rowSpan", 1);
rowspan = $(that).attr("rowSpan");
}
rowspan = Number(rowspan) + 1;
$(that).attr("rowSpan", rowspan);
$(this).remove();
} else {
that = this;
}
});
});
});
}