zm
2024-10-23 4b4eb27dcc051acba3841e7205dc4a593924362d
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package com.product.administration.service;
 
 
 
 
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
 
 
 
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.product.administration.config.CmnConst;
import com.product.administration.config.SystemCode;
import com.product.common.lang.StringUtils;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.module.sys.entity.SystemUser;
 
/**
 *     打卡记录
* Copyright  PRODUCT-BASE
* @Title: PRODUCT-BASE-
* @Project: PunchRecordService
* @Date: 2021年8月9日 上午11:30:38
* @Author: 杜洪波
* @Description:
 */
@Component
public class PunchRecordService extends AbstractBaseService{
 
    @Autowired
    BaseDao baseDao;
 
    /**
     *     考勤打卡列表
     * @param fse
     * @return
     */
    public DataTableEntity listRecordInfo(FieldSetEntity fse) throws ParseException {
        DataTableEntity dt=baseDao.listTable(CmnConst.PRODUCT_OA_PUNCH_RECORD, "created_by=?", new Object[] {SpringMVCContextHolder.getCurrentUser().getUser_id()}, null, CmnConst.CREATED_UTC_DATETIME+" DESC", fse.getInteger(CmnConst.PAGESIZE), fse.getInteger(CmnConst.CPAGE));
        for (int i=0;i<dt.getRows();i++){
            FieldSetEntity fieldSetEntity = dt.getData().get(i);
            String punch_time_one = null;
            if (fieldSetEntity.getString("punch_time_one") != null) {
                punch_time_one=fieldSetEntity.getValue("punch_time_one").toString();
            }else if (fieldSetEntity.getString("punch_time_two") != null) {
                punch_time_one=fieldSetEntity.getValue("punch_time_two").toString();
            }else if (fieldSetEntity.getString("punch_time_three") != null) {
                punch_time_one=fieldSetEntity.getValue("punch_time_three").toString();
            }else if (fieldSetEntity.getString("punch_time_four") != null) {
                punch_time_one=fieldSetEntity.getValue("punch_time_four").toString();
            }
            String sign_date=null;
            if (null!=punch_time_one){
                Date date1=new Date(punch_time_one);
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                 sign_date = dateFormat.format(date1);
                fieldSetEntity.setValue("sign_date",sign_date);
            }
            DateFormat dateFormatGet = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
            //获取早退迟到的分钟数
            List<Object>param1=new ArrayList<>();
            param1.add(fieldSetEntity.getValue("created_by").toString());
            param1.add(fieldSetEntity.getValue("punch_congfig_uuid").toString());
            param1.add(fieldSetEntity.getValue("created_by").toString()+",");
 
            //获取使用到的设置规则
            StringBuilder sbPunchTime=new StringBuilder();
            sbPunchTime.append(" SELECT c.* ");
            sbPunchTime.append(" FROM ( ");
            sbPunchTime.append(" SELECT d.* ");
            sbPunchTime.append(" FROM product_oa_punch_time d ");
            sbPunchTime.append(" left JOIN product_oa_punch_record b ");
            sbPunchTime.append(" on d.punch_site_uuid=b.punch_congfig_uuid and d.created_by=? where d.punch_site_uuid= ? ");
            sbPunchTime.append(" ) c ");
            sbPunchTime.append(" where  c.uuid in ( ");
            sbPunchTime.append(" SELECT DISTINCT uuid ");
            sbPunchTime.append(" FROM product_oa_punch_time ");
            sbPunchTime.append(" where LOCATE(?, concat(attendance_object,','))>0 ");
            sbPunchTime.append(" ) ");
            DataTableEntity dataTableEntity = baseDao.listTable(sbPunchTime.toString(), param1.toArray());
 
 
            //处理补卡的情况
            List<Object>param2=new ArrayList<>();
            param2.add(fieldSetEntity.getValue("created_by").toString());
            param2.add(sign_date);
 
            StringBuilder sbReplenish=new StringBuilder();
            sbReplenish.append(" SELECT T1.id,T1.uuid,T1.created_by,T1.applicant,T1.replenish_date,T1.reason,");
            sbReplenish.append(" SUBSTRING_INDEX( SUBSTRING_INDEX( T1.replacement_card_type, ',', T2.digit + 1 ), ',',- 1 ) AS type ");
            sbReplenish.append(" FROM (select * from product_oa_replenish_punch where flow_flag=2) T1 ");
            sbReplenish.append(" JOIN ( SELECT 0 AS digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 ) T2");
            sbReplenish.append(" ON T2.digit < ( length( T1.replacement_card_type )- length( REPLACE ( T1.replacement_card_type, ',', '' ))+ 1 )");
            sbReplenish.append(" where t1.created_by=? ");
            sbReplenish.append(" and DATE_FORMAT( t1.replenish_date, '%Y-%m-%d' )=? ");
            DataTableEntity dataTableEntity1 = baseDao.listTable(sbReplenish.toString(), param2.toArray());
            if(dataTableEntity1.getRows()!=0){
 
                for(int j=0;j<dataTableEntity1.getRows();j++){
                    String type = dataTableEntity1.getData().get(j).getValue("type").toString();
                    if(type.equals("1")){
                        String morning_work = dataTableEntity.getData().get(0).getValue("morning_work").toString();
                        Date parse = dateFormatGet.parse(morning_work);
                        fieldSetEntity.setValue("punch_time_one", new Timestamp(parse.getTime()));
                    }
 
                    if(type.equals("2")){
                        String morning_work_off = dataTableEntity.getData().get(0).getValue("morning_work_off").toString();
                        Date parse = dateFormatGet.parse(morning_work_off);
                        fieldSetEntity.setValue("punch_time_two", new Timestamp(parse.getTime()));
                    }
 
                    if(type.equals("3")){
                        String afternoon_work = dataTableEntity.getData().get(0).getValue("afternoon_work").toString();
                        Date parse = dateFormatGet.parse(afternoon_work);
                        fieldSetEntity.setValue("punch_time_three", new Timestamp(parse.getTime()));
                    }
 
                    if(type.equals("4")){
                        String afternoon_work_off = dataTableEntity.getData().get(0).getValue("afternoon_work_off").toString();
                        Date parse = dateFormatGet.parse(afternoon_work_off);
                        fieldSetEntity.setValue("punch_time_four", new Timestamp(parse.getTime()));
                    }
                }
            }
 
            long beLate1 = 0;
            long beLate2 = 0;
            long beEarily1 = 0;
            long beEarily2 = 0;
            if(dataTableEntity.getRows()!=0){
 
                String morning_work="";
                String morning_work_off="";
                String afternoon_work="";
                String afternoon_work_off="";
                Date  morning_work_date;
                Date  morning_work_off_date;
                Date  afternoon_work_date;
                Date  afternoon_work_off_date;
 
                DateTime punch_time_one_date;
                DateTime punch_time_two_date;
                DateTime punch_time_three_date;
                DateTime punch_time_four_date;
 
                SimpleDateFormat dateFormatHHmmss = new SimpleDateFormat("HH:mm:ss");
 
                StringBuilder  lateSb=new StringBuilder();
                StringBuilder   earilySb=new StringBuilder();
                StringBuilder supereffective_distance=new StringBuilder();
 
 
                if(!ObjectUtil.isNotEmpty(dataTableEntity.getData().get(0).getValue("morning_work_off")) && !ObjectUtil.isNotEmpty(dataTableEntity.getData().get(0).getValue("afternoon_work"))){
 
                    morning_work = dataTableEntity.getData().get(0).getValue("morning_work").toString();
                    afternoon_work_off = dataTableEntity.getData().get(0).getValue("afternoon_work_off").toString();
                    morning_work_date= DateUtil.parse(morning_work.split(" ")[1]);
                    afternoon_work_off_date= DateUtil.parse(afternoon_work_off.split(" ")[1]);
                    int distance = Integer.parseInt(dataTableEntity.getData().get(0).getValue("distance").toString());
                    if(ObjectUtil.isNotEmpty(fieldSetEntity.getValue("punch_time_one"))){
                        String punch_time_one1 = dateFormatHHmmss.format(fieldSetEntity.getValue("punch_time_one"));
                        punch_time_one_date = DateUtil.parse(punch_time_one1);
                        if(punch_time_one_date.after(morning_work_date)){
                            beLate1 = DateUtil.between(punch_time_one_date, morning_work_date, DateUnit.MINUTE);
                            lateSb.append("迟到:"+beLate1+"分钟");
                            //计算超出的有效距离
                            int punch_range_one = Integer.parseInt(fieldSetEntity.getValue("punch_range_one").toString());
                            if(punch_range_one>distance){
                                int distanceMorning = punch_range_one- distance;
                                fieldSetEntity.setValue("supereffective_distance","上班超有效距离:"+distanceMorning);
                            }
                        }
 
                    }else {
                        lateSb.append("上班未打卡");
                    }
 
                    if(ObjectUtil.isNotEmpty(fieldSetEntity.getValue("punch_time_four"))&&ObjectUtil.isNotEmpty(fieldSetEntity.getValue("punch_range_four"))){
                        String punch_time_four = dateFormatHHmmss.format(fieldSetEntity.getValue("punch_time_four"));
                        punch_time_four_date = DateUtil.parse(punch_time_four);
                        if(punch_time_four_date.before(afternoon_work_off_date)){
                            beEarily2 = DateUtil.between(punch_time_four_date, afternoon_work_off_date, DateUnit.MINUTE);
                            earilySb.append("早退:"+beEarily2+"分钟");
                            int punch_range_four = Integer.parseInt(fieldSetEntity.getValue("punch_range_four").toString());
                            if(punch_range_four>distance){
                                int distanceMorning = punch_range_four- distance;
                                fieldSetEntity.setValue("supereffective_distance","下班超有效距离:"+distanceMorning);
                            }
                        }
 
                    }else {
                        earilySb.append("下班未打卡");
                    }
 
                }else {
 
                    morning_work = dataTableEntity.getData().get(0).getValue("morning_work").toString();
                    morning_work_date= DateUtil.parse(morning_work.split(" ")[1]);
                    morning_work_off = dataTableEntity.getData().get(0).getValue("morning_work_off").toString();
                    morning_work_off_date= DateUtil.parse(morning_work_off.split(" ")[1]);
                    afternoon_work =dataTableEntity.getData().get(0).getValue("afternoon_work").toString();
                    afternoon_work_date= DateUtil.parse(afternoon_work.split(" ")[1]);
                    afternoon_work_off = dataTableEntity.getData().get(0).getValue("afternoon_work_off").toString();
                    afternoon_work_off_date= DateUtil.parse(afternoon_work_off.split(" ")[1]);
 
 
                    int distance = Integer.parseInt(dataTableEntity.getData().get(0).getValue("distance").toString());
 
                    if(ObjectUtil.isNotEmpty(fieldSetEntity.getValue("punch_time_one"))&&ObjectUtil.isNotEmpty(fieldSetEntity.getValue("punch_range_one"))){
                        String punch_time_one1 = dateFormatHHmmss.format(fieldSetEntity.getValue("punch_time_one"));
                        punch_time_one_date = DateUtil.parse(punch_time_one1);
                        if(punch_time_one_date.after(morning_work_date)){
                            beLate1 = DateUtil.between(punch_time_one_date, morning_work_date, DateUnit.MINUTE);
                            lateSb.append("早上班迟到:"+beLate1+" ");
                            int punch_range_one = Integer.parseInt(fieldSetEntity.getValue("punch_range_one").toString());
                            if(punch_range_one>distance){
                                int distanceMorning = punch_range_one- distance;
                                supereffective_distance.append("早上班超有效距离:"+distanceMorning);
                            }
 
                        }
 
                    }else {
                        lateSb.append("早上班未打卡"+" ");
 
                    }
 
                    if(ObjectUtil.isNotEmpty(fieldSetEntity.getValue("punch_time_two"))&&ObjectUtil.isNotEmpty(fieldSetEntity.getValue("punch_range_two"))){
                        String punch_time_two= dateFormatHHmmss.format(fieldSetEntity.getValue("punch_time_two"));
                        punch_time_two_date = DateUtil.parse(punch_time_two);
                        if(punch_time_two_date.before(morning_work_off_date)){
                            beEarily1 = DateUtil.between(punch_time_two_date, morning_work_off_date, DateUnit.MINUTE);
                            earilySb.append("早下班早退:"+beEarily1+" ");
                            int punch_range_two = Integer.parseInt(fieldSetEntity.getValue("punch_range_two").toString());
                            if(punch_range_two>distance){
                                int distanceMorningoff = punch_range_two- distance;
                                supereffective_distance.append("早下班超有效距离:"+distanceMorningoff);
                            }
                        }
 
                    }else {
                        earilySb.append("早下班未打卡"+" ");
                    }
                    if(ObjectUtil.isNotEmpty(fieldSetEntity.getValue("punch_time_three"))&&ObjectUtil.isNotEmpty(fieldSetEntity.getValue("punch_range_three"))){
                        String punch_time_three = dateFormatHHmmss.format(fieldSetEntity.getValue("punch_time_three"));
                        punch_time_three_date = DateUtil.parse(punch_time_three);
                        if(punch_time_three_date.after(afternoon_work_date)){
                            beLate2 = DateUtil.between(punch_time_three_date, afternoon_work_date, DateUnit.MINUTE);
                            lateSb.append("下午上班迟到:"+beLate2+" ");
                            int punch_range_three = Integer.parseInt(fieldSetEntity.getValue("punch_range_three").toString());
                            if(punch_range_three>distance){
                                int distanceAfternoon = punch_range_three- distance;
                                supereffective_distance.append("下午上班超有效距离:"+distanceAfternoon);
                            }
                        }
                    }else {
                        lateSb.append("早上班未打卡"+" ");
                    }
                    if(ObjectUtil.isNotEmpty(fieldSetEntity.getValue("punch_time_four"))&&ObjectUtil.isNotEmpty(fieldSetEntity.getValue("punch_range_four"))){
                        String punch_time_four = dateFormatHHmmss.format(fieldSetEntity.getValue("punch_time_four"));
                        punch_time_four_date = DateUtil.parse(punch_time_four);
                        if(punch_time_four_date.before(afternoon_work_off_date)){
                            beEarily2 = DateUtil.between(punch_time_four_date, afternoon_work_off_date, DateUnit.MINUTE);
                            earilySb.append("下午下班早退:"+beEarily2+" ");
                            int punch_range_four = Integer.parseInt(fieldSetEntity.getValue("punch_range_four").toString());
                            if(punch_range_four>distance){
                                int distanceAfternoonoff = punch_range_four- distance;
                                supereffective_distance.append("下午下班超有效距离:"+distanceAfternoonoff);
                            }
 
                        }
                    }else {
                        earilySb.append("下午下班未打卡"+" ");
                    }
 
                }
                fieldSetEntity.setValue("be_late",lateSb);
                fieldSetEntity.setValue("ealy_leave",earilySb);
                fieldSetEntity.setValue("supereffective_distance",supereffective_distance.toString());
            }
 
        }
        dt.setFieldFormat("punch_time_one", "HH:mm");
        dt.setFieldFormat("punch_time_two", "HH:mm");
        dt.setFieldFormat("punch_time_three", "HH:mm");
        dt.setFieldFormat("punch_time_four", "HH:mm");
        baseDao.loadPromptData(dt);
        return dt;
    }
 
    /**
     *     考勤打卡详情
     * @param uuid
     * @return
     */
    public FieldSetEntity findRecordInfo(String uuid) {
        return baseDao.getFieldSetEntity(CmnConst.PRODUCT_OA_PUNCH_RECORD, uuid, false);
    }
 
    /**
     *     考勤打卡新增
     * @param fse
     * @return
     */
    public String addRecordInfo(FieldSetEntity fse) {
        SimpleDateFormat df = new SimpleDateFormat(CmnConst.YYYYMMDDHHMMSS);
 
        SystemUser currentUser=SpringMVCContextHolder.getCurrentUser();    //获取打卡人
        Date created_datetime=fse.getDate("created_datetime");            //获取打卡时间
//        Date created_datetime=new Date();                                //获取打卡时间
        String string_created_datetime=df.format(created_datetime);        //时间转string
 
        //获取公司打卡时间配置信息
        FieldSetEntity fseCompanyPunch=baseDao.getFieldSetEntityByFilter("product_oa_punch_time", "org_level_uuid=?", new Object[] {currentUser.getOrg_level_uuid()}, false);
        if (fseCompanyPunch==null) {
            throw new BaseException(SystemCode.SYSTEM_CONMAPNY_PUNCH_NO_EXIST.getValue(), SystemCode.SYSTEM_CONMAPNY_PUNCH_NO_EXIST.getText(), this.getClass(), "addRecordInfo");
        }
 
        //获取当天打卡信息
        FieldSetEntity fsePunchInfo=baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_OA_PUNCH_RECORD, "created_by=? and DATE_FORMAT(created_utc_datetime,'%Y-%m-%d')like ?", new Object[] {currentUser.getUser_id(),string_created_datetime.substring(0, 10)+"%"}, false);
        if (fsePunchInfo==null) {
            FieldSetEntity fseNewPunchInfo = new FieldSetEntity();
            fseNewPunchInfo.setTableName(CmnConst.PRODUCT_OA_PUNCH_RECORD);
            fseNewPunchInfo.setValue(CmnConst.ORG_LEVEL_UUID, currentUser.getOrg_level_uuid());
            fseNewPunchInfo.setValue(CmnConst.DEPT_UUID, currentUser.getDept_uuid());
            fseNewPunchInfo.setValue(CmnConst.CREATED_BY, currentUser.getUser_id());
            fseNewPunchInfo.setValue(CmnConst.CREATED_UTC_DATETIME, created_datetime);
            fseNewPunchInfo.setValue("punch_time_one", created_datetime);
            //新增打卡信息
            return baseDao.add(fseNewPunchInfo);
        }else {
 
            //打卡模式(0:双卡  1:四卡)
            int punchMode=0;
            if (StringUtils.isEmpty(fseCompanyPunch.getString("work_time_two"))) {
                punchMode=1;
            }
 
            String punch_time_one=fsePunchInfo.getDate("punch_time_one", CmnConst.YYYYMMDDHHMMSS);
            String punch_time_two=fsePunchInfo.getDate("punch_time_two", CmnConst.YYYYMMDDHHMMSS);
            String punch_time_three=fsePunchInfo.getDate("punch_time_three", CmnConst.YYYYMMDDHHMMSS);
            String punch_time_four=fsePunchInfo.getDate("punch_time_four", CmnConst.YYYYMMDDHHMMSS);
            List<String> list=new ArrayList<>();
            if (punch_time_one!=null) {
                list.add(punch_time_one);
            }
            if (punch_time_two!=null) {
                list.add(punch_time_two);
            }
            if (punch_time_three!=null) {
                list.add(punch_time_three);
            }
            if (punch_time_four!=null) {
                list.add(punch_time_four);
            }
            list.add(string_created_datetime);
            Collections.sort(list);
 
            for (int i = 0; i < list.size(); i++) {
                if (punchMode==1) {
                    if (i==0) {
                        fsePunchInfo.setValue("punch_time_one", list.get(i));
                    }else if (i==1) {
                        fsePunchInfo.setValue("punch_time_four", list.get(i));
                    }
                }else {
                    if (i==0) {
                        fsePunchInfo.setValue("punch_time_one", list.get(i));
                    }else if (i==1) {
                        fsePunchInfo.setValue("punch_time_two", list.get(i));
                    }else if (i==2) {
                        fsePunchInfo.setValue("punch_time_three", list.get(i));
                    }else if (i==3) {
                        fsePunchInfo.setValue("punch_time_four", list.get(i));
                    }
                }
            }
            //修改打卡信息
            baseDao.update(fsePunchInfo);
            return fsePunchInfo.getUUID();
        }
    }
 
}