-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattend.js
165 lines (145 loc) · 5.25 KB
/
attend.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var util = require('util');
var moment = require("moment");
var mysql = require('mysql');
function handleDisconnect() {
connection = mysql.createConnection({
host : @hostname,
database : @database,
user : @username,
password : @password
});
connection.connect(function(err) {
if(err) {
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); //接続失敗時リトライ
}
});
connection.on('error', function(err) { //エラー受け取るコールバック
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
handleDisconnect(); //再度接続
} else {
throw err;
}
});
}
handleDisconnect();
module.exports = function(robot) {
//connection.connect();
robot.hear(/hello/i, function(msg) {
msg.send('World!');
});
robot.hear(/\[test\] (.*)/i,function(msg){
item = msg.match[0];
msg.send(msg.match[1]);
});
robot.hear(/\[start\] (.*)/i, function(msg){
var user = msg.message.user.name;
connection.query('insert into attend (user, task, end) values ("'+ user +'","WIP:' + msg.match[1] +'", CURRENT_TIMESTAMP);', function(err, result) {
if (err) {
msg.send(err);
}
});
msg.send(user + "が" +msg.match[1]+"を始めました!");
});
robot.hear(/\[end\] (.*)/i, function(msg){
//connection.connect();
var user = msg.message.user.name;
connection.query('select * from attend WHERE (DATE(start) = CURDATE() OR DATE(start) = DATE_SUB(CURDATE(),interval 1 day)) AND user = "'+user+'" AND task = "WIP:' +msg.match[1]+'";', function(err, result) {
if (err) {
msg.send(err);
}
if (util.inspect(result[0]) === "undefined") {
msg.send(msg.match[1]+"は開始されてないよ!");
}else{
connection.query('update attend set task="'+msg.match[1]+'", end = CURRENT_TIMESTAMP WHERE (DATE(start) = CURDATE() OR DATE(start) = DATE_SUB(CURDATE(),interval 1 day)) AND user = "'+user+'" AND task = "WIP:' +msg.match[1]+'";',function(err, result) {
if (err) {
msg.send("ないよ");
}
msg.send(msg.match[1]+"が終わりました!");
});
}
});
});
robot.hear(/\[end2\]/i, function(msg){
var user = msg.message.user.name;
connection.query('select * from attend WHERE (DATE(start) = CURDATE() OR DATE(start) = DATE_SUB(CURDATE(),interval 1 day)) AND user = "'+user+'" AND task LIKE "WIP:%";', function(err, result) {
if (err) {
msg.send(err);
}
if (util.inspect(result[0]) === "undefined") {
msg.send("タスクは開始されてないよ!");
}else{
connection.query('update attend set task="'+msg.match[1]+'", end = CURRENT_TIMESTAMP WHERE (DATE(start) = CURDATE() OR DATE(start) = DATE_SUB(CURDATE(),interval 1 day)) AND user = "'+user+'" AND task = "WIP:' +msg.match[1]+'";',function(err, result) {
if (err) {
msg.send("ないよ");
}
//msg.send(result[0].task.replace(/WIP:/g,"")+"が終わりました!");
});
}
msg.send(result[0].task.replace(/WIP:/g,""));
});
});
robot.hear(/\[sum\]/i, function(msg){
connection.query('select task, timediff(end,start) as time from attend where (DATE(start) = CURDATE() OR (DATE(end) = CURDATE() AND DATE(start) = DATE_SUB(CURDATE(),interval 1 day))) AND user = "'+msg.message.user.name+'";', function(err, result) {
if (err) {
msg.send(err);
}
for(var i in result){
var mTask = moment(result[i].time, "HH:mm:ss");
if(mTask.hours()==0){
var formatTask = mTask.format('m分s秒');
}else{
var formatTask = mTask.format('H時間m分s秒');
}
msg.send(result[i].task+" "+formatTask);
}
connection.query('select sum(time_to_sec(timediff(end,start))) as sumTime from attend where (DATE(start) = CURDATE() OR (DATE(end) = CURDATE() AND DATE(start) = DATE_SUB(CURDATE(),interval 1 day))) AND user = "'+msg.message.user.name+'";', function(err, sum) {
if (err) {
msg.send(err);
}
msg.send("今日の勤務時間は"+toHms(sum[0].sumTime)+"だよ!");
});
});
});
function toHms(t) {
var hms = "";
var h = t / 3600 | 0;
var m = t % 3600 / 60 | 0;
var s = t % 60;
if (h != 0) {
hms = h + "時間" + padZero(m) + "分" + padZero(s) + "秒";
} else if (m != 0) {
hms = m + "分" + padZero(s) + "秒";
} else {
hms = s + "秒";
}
return hms;
function padZero(v) {
if (v < 10) {
return "0" + v;
} else {
return v;
}
}
}
robot.hear(/\[remove\] (.*)/i, function(msg){
var user = msg.message.user.name;
connection.query('delete from attend WHERE (DATE(start) = CURDATE() OR DATE(start) = DATE_SUB(CURDATE(),interval 1 day)) AND user = "'+user+'" AND task = "WIP:' +msg.match[1]+'";', function(err, result) {
if (err) {
msg.send(err);
}
if(result.affectedRows === 1){
msg.send(msg.match[1]+"を取り消しました!");
}else{
msg.send(msg.match[1]+"は開始されてないよ!");
}
});
});
robot.hear(/yo/i, function(msg) {
msg.send('yo!');
});
robot.hear(/休憩/i, function(msg) {
msg.send('お疲れさま!ゆっくり休んでね');
});
}