liuxiaolong
2019-05-06 a7bed6b4cfecd61ec153818945f982c5bb796b98
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
package com.cloud.attendance.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.cloud.attendance.dao.AttAbnormalRecordDao;
import com.cloud.attendance.dao.AttDayDao;
import com.cloud.attendance.dao.AttRuleDao;
import com.cloud.attendance.model.AttDay;
import com.cloud.attendance.model.AttMouthData;
import com.cloud.attendance.model.AttRule;
import com.cloud.attendance.model.DayStatus;
import com.cloud.attendance.service.AttDayService;
import com.cloud.attendance.service.TokenService;
import com.cloud.attendance.service.UserService;
import com.cloud.attendance.utils.EnumStr;
import com.cloud.model.sys.LoginAppUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
@Slf4j
@Service
public class AttDayServiceImpl implements AttDayService {
    @Autowired
    private AttDayDao attDayDao;
 
    @Autowired
    private AttAbnormalRecordDao attAbnormalRecordDao;
    @Autowired
    private AttRuleDao attRuleDao;
    @Autowired
    private UserService userService;
 
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 
    @Override
    public int updateDayRecord(Long recordId, Date mouthDay, String  leaveOrRepair){
        AttDay attDay = new AttDay();
        return attDayDao.updateByIdSelective(attDay);
    }
 
    @Override
    public Map<String,Object> findPerDayRecord(String mouthDate, String pid){
        AttRule attRule = attRuleDao.selectAttRule();   // 获取考勤规则
            String[] mouthDay = mouthDate.split("-");
            int year = Integer.valueOf(mouthDay[0]);
            int month = Integer.valueOf(mouthDay[1]);
        if (attRule == null){
            log.info("考勤规则暂无,考勤暂时做不了");
            throw  new RuntimeException("请先调整考勤规则,然后才会 依据规则 调整考勤数据。");
        }
        int dayOfMonth = 0;
            Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
       if ((new Date().getMonth()+1)==month){
           String[] days = sdf.format(new Date()).split("-");
           dayOfMonth = Integer.valueOf(days[2]);
       }else{
            c.set(year, month, 0); //输入类型为int类型   //   2  判断 2月
             dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
       }
            log.info(year + "年" + month + "月有" + dayOfMonth + "天");
        HashMap<String, Object> map = new HashMap<>();
        List<AttDay>  attDays =  attDayDao.selectAttDayByUserAndMouth(mouthDate,pid);
        if (attDays == null){      //  新人员 刚加入 会查不到数据 或 未在考勤日期内 的数据 则会出现 null 数据
            attDays = new ArrayList<>();
        }
        Integer attendValue=0,isLeave=0,isRepair=0;
        if (attDays.size()<dayOfMonth){
            isRepair = dayOfMonth - attDays.size();
        }
        String employeeName = null;
        for ( AttDay attDay :attDays ){
            employeeName = attDay.getEmployeeName();
            if(attDay.getIsNotSign() !=null && attDay.getIsNotSign()==0){  // 0已签 1未签到
                attendValue+=1;
            }else{
                if (attDay.getIsLeave() ==null || attDay.getIsLeave()==0){  // 0 是未请假
                    isLeave+=1;
                }
//                if (attDay.getIsRepair() ==null|| attDay.getIsRepair()==0){  // 0 是未补签 目前为 未签到数据
//                    isRepair+=1;
//                }
                isRepair+=1;              // 0 是未补签 目前为 未签到数据
            }
        }
        attDays = completionDate(attDays ,mouthDate+"-01" ,dayOfMonth);
        map.put("dayList",attDays);
 
        map.put("attendValue",attendValue);
        map.put("isLeave", isRepair);  // wp 2018-12-04  暂改为未签到  之前作为 请假字段
        map.put("isRepair",isLeave);
        // 直接 查询数据库
//        Map<String, Object> person = attDayDao.getPersonInfoById( pid);
        // 调用人员接口
        Map<String, Object> person = userService.getPersonInfoById(pid);
 
        if (person != null){
            String name = (String) person.get("name");
            String delFlag = (String) person.get("delFlag");
          if ("1".equals(delFlag)){
              name = name != null?(name+"(已删除)"):"(已删除)";
          }
            map.put("employeeName",name != null?name:"");
            if (EnumStr.TEACHER_SQL.equals(person.get("type"))){
                 map.put("identity","教师");
            }else if (EnumStr.STUDENT_SQL.equals(person.get("type"))){
                map.put("identity","学生");
            }else {
                Map<String, String> personType = userService.queryPersonType();
                String type = personType.get(person.get("type"));
                if (type != null){
                    map.put("identity",type);
                }else {
                    map.put("identity","其他");
                }
            }
        }
        return map;
    }
//  补全当月数据
    private   List<AttDay> completionDate(List<AttDay> list , String begin , int daySub){
        ArrayList<AttDay> dateResult = new ArrayList<>();
        //时间增加一天
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar yesterday = Calendar.getInstance();
        yesterday.add(Calendar.DAY_OF_MONTH, 1);
        String dateStr = sdf.format(yesterday.getTime());
        //日期比较(求差多少天),时间也可以比较
        Calendar now = Calendar.getInstance();
        //字符串转化为时间
        Calendar calendar10 = Calendar.getInstance();
        Calendar calendar5 = Calendar.getInstance();
        //循环处理日期数据,把缺失的日期补全。10是时间段内的天数,5是要处理的日期集合的天数
        try {
            Date date = sdf.parse(begin);
            calendar10.setTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        for(int curr = 0 ; curr < daySub ; curr++){
            boolean dbDataExist = false;
            int index = 0;
            for(int i  = 0 ; i < list.size() ; i++){
                try {
                    //  操作 获取数据
                 AttDay  attDay1 =   list.get(i);
                    Date date2 = attDay1.getSignDate();
                    calendar5.setTime(date2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if(calendar10.compareTo(calendar5) == 0){
                    dbDataExist  = true;
                    index = i;
                    break;
                }
            }
            if(dbDataExist){
                //  操作 获取数据
                AttDay  attDay1 =   list.get(index);
                Date date2 = attDay1.getSignDate();
                dateResult.add(attDay1);
            }else{
                Date time = calendar10.getTime();
                AttDay attDay = new AttDay();
                attDay.setSignDate(time);
                dateResult.add(attDay);
            }
            //还原calendar10
            calendar10.add(Calendar.DAY_OF_MONTH, 1 );
        }
        return dateResult;
    }
 
    @Autowired
    private TokenService tokenService;
 
    // 查询多人某月记录
    @Override
    public List<AttMouthData> findAllDayRecord(String mouthDate, String gradeValue,
                                               String classValue, String identity, Integer days, Boolean delFlag, String orgIds){
 
        AttRule attRule = attRuleDao.selectAttRule();   // 获取考勤规则
 
        if (attRule == null || StringUtils.isBlank(attRule.getDeviceIds())){  //  新加 判断
            return new ArrayList<>();
        }
       /* if (EnumStr.STUDENT.equalsIgnoreCase(identity)){
            identity = EnumStr.STUDENT_SQL;
        }else if (EnumStr.TEACHER.equalsIgnoreCase(identity)){
            identity = EnumStr.TEACHER_SQL;
        }*/
       if (StringUtils.isBlank(identity)){
           identity = null;
       }
        String[] mouthDay = mouthDate.split("-");
        int year = Integer.valueOf(mouthDay[0]);
        int month = Integer.valueOf(mouthDay[1]);
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        c.set(year, month, 0); //输入类型为int类型
      int  dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
        if ((new Date().getMonth()+1)==month){
            String[] dayDate = sdf.format(new Date()).split("-");
            dayOfMonth = Integer.valueOf(dayDate[2]);
        }
        List<String> orgList = null;
        if (!StringUtils.isBlank(orgIds)){
            String[] orgIdStr = orgIds.split(",");
            orgList = Arrays.asList(orgIdStr);
        }
        //  添加权限
        LoginAppUser appUser = tokenService.currentUser();
        Long orgId = appUser.getOrgId();
        if (StringUtils.isBlank(gradeValue)){
            Integer officeId = appUser.getOfficeId();
            gradeValue = officeId.toString();
        }
        // 调用 人员管理 查询人员数据
        List<String> strings = userService.queryPersonByContent(null, gradeValue, classValue, identity, null,delFlag,orgList);
        log.info("查询人员列表:"+strings);
        //  查无人员 则 直接返回
        if (strings == null || strings.size() < 1){
            return new ArrayList<AttMouthData>();
        }
        List<AttMouthData> list = attDayDao.selectAttDayByMouth(mouthDate,strings);
        // 查询 所有人员 的 姓名
        Map<String, JSONObject> persons = userService.queryAllPersonId();
        log.info("查询人员列表:list"+list.size());
 
        for(int i=0;i<list.size();i++){
            AttMouthData attMouthData = list.get(i);
            JSONObject personJson = persons.get(attMouthData.getPerId());
            if (personJson != null){                // 人员信息 可能没有
                String delContent = "";
                if ("1".equals(personJson.getString("delFlag"))){
                    delContent = "(已删除)";
                }
                attMouthData.setPerName(personJson.getString("name")+delContent);  //  人员管理的名字
            }
            List<DayStatus> list1 = attMouthData.getTotalList();
            if(list1.size() < dayOfMonth){      //  list1 排好序  dayOfMonth days
                   int d = 1;
                if (list1.size()>0){
                    Date startDate = list1.get(0).getDayDate();// 第一天记录是否从头开始
                    c.setTime(startDate);
                     d=c.get(Calendar.DATE);
                    if (d != 1){
                        for (int j=1;j<d;++j){
                            c.set(year,month-1,j);
                            DayStatus dayStatus = new DayStatus(c.getTime(),"缺勤");
                            list1.add(j-1,dayStatus);
                        }
                    }
                   }
                for (int k = d;k <= dayOfMonth;++k){
                   if (list1.size()< k){
                       c.set(year,month-1,k);
                       list1.add(k-1,new DayStatus(c.getTime(),"缺勤"));
                   }else{
                        Date dayDate = list1.get(k-1).getDayDate();
                        String[] ymd = sdf.format(dayDate).split("-");
                        if (!Integer.valueOf(ymd[2]).equals(k)){  //  不是当天
                            c.set(year,month-1,k);
                            list1.add(k-1,new DayStatus(c.getTime(),"缺勤"));
                        }
                   }
                }
            }else{
                continue;
            }
        }
        return list;
    }
 
    @Override
    public List<AttDay> queryPersonById(String id, String beginTime, String endTime) {
        AttRule attRule = attRuleDao.selectAttRule();   // 获取考勤规则
        if (attRule == null){
//            log.info("考勤规则暂无,考勤暂时做不了");
//            throw  new RuntimeException("请先调整考勤规则,然后才会 依据规则 调整考勤数据。");
        }
 
        List<AttDay> list = attDayDao.queryPersonById(id,beginTime,endTime);
        return list;
    }
}