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
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
package com.cloud.attendance.service.impl;
 
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloud.attendance.dao.AttDayDao;
import com.cloud.attendance.dao.AttRuleDao;
import com.cloud.attendance.model.AttDay;
import com.cloud.attendance.model.AttRule;
import com.cloud.attendance.model.PersonForEs;
import com.cloud.attendance.service.UserService;
import com.cloud.attendance.utils.EnumStr;
import com.cloud.attendance.utils.HttpClient;
import com.cloud.common.utils.RestTemplateUtil;
import com.cloud.model.sys.LoginAppUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
 
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
@Slf4j
@Component
public class EsAndErlangStartLoading {
    @Value("${c.server_ip_port}")
    private String cServerIp = "http://58.118.225.81:11111";  // 58.118.225.82
 
    private String cServerDB = "/findAllDatabase";
@Value("${es.es_net_hosts}")
    private String cServerHosts = "241,242,243,244";
 
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private UserService userService;
    @Autowired
    private AttRuleDao attRuleDao;
    @Autowired
    private EnumStr enumStr;
    /**
     * [{
     *     "uuid": "e92a005e-fc19-56b5-8a23-53570bc270ae", //表uuid
     *     "tableName": "test",                            //表名
     *     "tableType": "person",                          //表类型
     *     "bwType": "1",                                  //黑白名单类型
     *     "start_time": "2018-01-01 01:02:00",            //开始时间
     *     "end_time": "2018-12-31 01:02:00"               //结束时间
     * }]
     */
 
    private  String cServerDBPerson = "/loadFaceFeaData";
    /**
     * [{
     *         "uuid": "00b429d6-3746-57b9-8123-0dbf134731d2",         //UUID
     *         "tableName": "test",                                    //表名
     *         "idcard": "123456789012345678",                         //身份证号
     *         "imgUrl": "http://192.168.1.188/group1/M00/00/01/wKgBvFuOcuCAbYeQAB86k1llhSw841.jpg"
     *        }]
     */
    @Autowired
    private AttDayDao attDayDao;
    @Autowired
    private RestTemplateUtil restTemplateUtil;
    @Autowired
    private  ScheduledDayData scheduledDayData;
 
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private  SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private  SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
    //启动触发   创建 某天 所有人员 的空表数据------- 主要用来 添加 当天数据
    public void reportCurrentByDay(){
 
        System.out.println ("触发创建当天所有人员 的底库数据: The time is now " + sdfTime.format (new Date()));
        // orgid  employeeid  employeename createby  remarks  createdate可null
      //  attDayDao.startInsertEmployData("basic-wp-start",null);    // nickname  正式数据库 人员使用  暂废
 
        ///        调用 人员项目的查询所有人员
        Map<String,JSONObject> persons = userService.queryAllPersonId();
        List<String> existPer = attDayDao.selectPersonByDate(new Date());
        existPer.forEach(userId -> {
            persons.remove(userId);
        });
        // 去除存在人员
        Collection<JSONObject> valueCollection2 = persons.values();
        List<Object> valueList= new ArrayList<Object>(valueCollection2);//map转list
        if (valueList .size() > 0){
            attDayDao.insertMoreData(valueList,null,null,null);
        }
    }
 
 
// 将 今天之前的 某日期 记录 汇总 入考勤库 ------- 主要用来 添加历史数据
    public void getMouthDayRecord() throws ParseException {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
//        c.add(Calendar.DAY_OF_MONTH, 1);
        Date nowTime = c.getTime();
        String nowDate = sdf.format(c.getTime());
        log.info(nowDate+"----===当前时间====----"+nowTime);
//        attDayDao.startInsertEmployData("basic-wp-add",nowTime);        //  今日 人员 添加
 
        JSONArray jsonData = userService.queryAllPersonList();
        if (jsonData.size() > 0 ){
 
        attDayDao.insertMoreData(jsonData,nowTime,null,null);
        }
 
        reportCurrentByCron(sdf.parse(nowDate)); //  "2018-12-19"
        for (int d=1;d <=28;d++){
            c.add(Calendar.DAY_OF_MONTH, -1);
             nowTime = c.getTime();
             nowDate = sdf.format(c.getTime());
            log.info(nowDate+"----===循环当前时间====----"+nowTime);
//            attDayDao.startInsertEmployData("basic-wp-add",nowTime);    //  今日 人员 添加
             jsonData = userService.queryAllPersonList();
             if (jsonData .size() > 0){
 
            attDayDao.insertMoreData(jsonData,nowTime,null,null);
             }
            reportCurrentByCron(sdf.parse(nowDate));
        }
    }
//  废弃 2018
    public void addPersonByDataBase() throws UnsupportedEncodingException {
        System.out.println ("从 底库查询人员 然后加入 数据库addPerson总库: time is now " + sdfTime.format (new Date ()));
        // id; Integer orgId;Integer officeId;
        //    name; gender; type; photos;
        ///        调用 人员项目的查询所有人员
        List<PersonForEs> persons = queryPersonIdFromDataBase();
        log.info(persons.size()+"人员将被加入person_es库中");
        for (PersonForEs person : persons){
            try {
                int i = attDayDao.insertEsPerson(person);
                log.info(i+"将被加入"+person);
            }catch (RuntimeException ex){
                log.info("添加总库人员异常:"+ex.getLocalizedMessage());
            }
        }
    }
 
    /**
     * {
     *     "TableName":"test"  //表名
     * }
     * @return
     */
    /**
     * [{
     *         "uuid": "00b429d6-3746-57b9-8123-0dbf134731d2",         //UUID
     *         "tableName": "test",                                    //表名
     *         "idcard": "123456789012345678",                         //身份证号
     *         "imgUrl": "http://192.168.1.188/group1/M00/00/01/wKgBvFuOcuCAbYeQAB86k1llhSw841.jpg"
     *        }]
     *         废弃 2018
     */
    public  List<PersonForEs> queryPersonIdFromDataBase() throws UnsupportedEncodingException {
        log.info("cServerIp  人员 加载 ip-->"+cServerIp+"cServerDB-->"+cServerDB);
        String post = restTemplateUtil.post(cServerIp + cServerDB, new JSONObject(), MediaType.APPLICATION_JSON_UTF8, false);
        JSONArray dbArray = JSONArray.parseArray(post);
        List<PersonForEs> persons = new ArrayList<>();
        for (Object db : dbArray) {
            JSONObject dataBase  = (JSONObject) db;
            String tableName = dataBase.getString("tableName");
            tableName = new String(tableName.getBytes("ISO8859-1"),"utf-8");
            log.info("从"+cServerIp+"服务器 查询到的底库表:"+tableName);
            if (tableName!=null && tableName.contains("测试")){
                log.info("包含测试 数据 不加入总库库,不做处理。");
                continue;
            }
            JSONObject getPersonParam = new JSONObject();
            getPersonParam.put("TableName",tableName);
            String personPost = restTemplateUtil.post(cServerIp + cServerDBPerson,getPersonParam , MediaType.APPLICATION_JSON_UTF8, false);
            JSONArray personArray = JSONArray.parseArray(new String(personPost.getBytes("ISO8859-1"),"utf-8"));
            log.info(tableName+"库人员数:"+personArray.size());
            for (Object perObj : personArray){
                JSONObject personDb  = (JSONObject) perObj;
                PersonForEs personForEs = new PersonForEs();
                personForEs.setPid(personDb.getString("uuid"));
                personForEs.setOrgId(1);
                personForEs.setOfficeId(1);
                personForEs.setName(personDb.getString("idcard"));
                personForEs.setGender(null);
                personForEs.setType("100");
                personForEs.setPhotos(personDb.getString("imgUrl"));
                personForEs.setCreateBy("listenter-start-add-person");
                persons.add(personForEs);
 
            }
            log.info(tableName+"库人员汇总数:"+persons.size());
        }
        return persons;
    }
 
 
    public void reportCurrentByCron(Date mouthDate){
        // 调用 http es 接口 查询 数据
        ArrayList<AttDay> attDays = new ArrayList<>();
//        signDate,inTime ,outTime ,inDeviceId ,inDeviceName,outDeviceId ,outDeviceName ,isNotSign ,isLate ,isLeaveEarly,
//        isLeave ,isRepair, revJson ,updateBy
 
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        String esJson = HttpClient.getEsJson();
        if (mouthDate != null ){   //  修改 考勤汇总日期
            String from = sdf.format(mouthDate)+" 00:00:00";
            Calendar c = Calendar.getInstance();
            c.setTime(mouthDate);
            c.add(Calendar.DAY_OF_MONTH, 1);
            String to = sdf.format(c.getTime())+" 00:00:00";
            log.info("执行考勤汇总日志from 当天时间:"+from+"至日期to:"+to);
            String replace = esJson.replace("now+8h/d", from);
            esJson = replace.replace("now+1d+8h/d",to);
            log.info("替换后的json:--->"+esJson);
        }
        AttRule attRule = attRuleDao.selectAttRule();   // 获取考勤规则
        if (attRule == null){
            log.info("考勤规则暂无,使用程序给定规则");
            attRule = new AttRule();
            attRule.setEnable(1);
//            throw  new RuntimeException("请先调整考勤规则,然后才会 依据规则 调整考勤数据。");
        }
        //  考勤 规则不启用 则 记录考勤数据 但不进行 迟到早退判定 wp 19-03-12
        if (attRule.getEnable()== null || attRule.getEnable() == 1){
            attRule.setInTime("23:30");attRule.setInTimeMin(10);
            attRule.setOutTime("00:30");attRule.setOutTimeMin(10);
        }
        HttpEntity<String> formEntity = new HttpEntity<String>(esJson, headers); // json 启动 赋值
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity("http://" + enumStr.getEsHOSTNAME()
                        + ":" + enumStr.getEsHTTP_PORT() + "/"+ EnumStr.esIndexField+"/_search",
                formEntity, JSONObject.class);
        if (!responseEntity.getStatusCode().is2xxSuccessful()){
            throw new RuntimeException("es 汇总考勤数据返回失败!~");
        }else {
            JSONObject resp = responseEntity.getBody();
            JSONObject signtimes = ((JSONObject)resp.getJSONObject("aggregations").getJSONObject("date_range")
                            .getJSONArray("buckets").get(0)); // 签到 集合
            JSONArray sources = signtimes.getJSONObject("aggs_bucket").getJSONArray("buckets");   //   buckets 数据集合
            try {
                Date signDate = sdf.parse(signtimes.getString("from_as_string"));  //      考勤日期
                log.info(signDate+"signDate:考勤日期");
 
                Date inTime = scheduledDayData.getInTimeMin(attRule,mouthDate);     //  考勤迟到截止
                Date outTime =  scheduledDayData.getOutTimeMin(attRule,mouthDate); // 考勤早退截止
                LoginAppUser loginAppUser =  null;  // tokenService.currentUser();
                if (loginAppUser==null|| loginAppUser.getUsername()==null){
                    loginAppUser = new LoginAppUser();loginAppUser.setUsername("basic-wp");
                }
                for (Object source : sources) {
                    JSONObject jsonSource = (JSONObject) source;   //
                    AttDay attDay = new AttDay();  // personId
                    attDay.setSignDate(signDate);      // 签到日期   2018-10-30  只是在下方代码判定需要、、是否签到,为null,则迟到早退给定null
                    attDay.preUpdate(loginAppUser);      // 更新日期
                    attDay.setEmployeeId (jsonSource.getString("key"));
                    JSONArray inTimesource = jsonSource.getJSONObject("top_min_hits").getJSONObject("hits").getJSONArray("hits");
                    JSONObject _source = null;
                    if (inTimesource.size()>0){
                        _source =  ((JSONObject) inTimesource.get(0)).getJSONObject("_source");         // hits 中元素  _source
                        attDay.setIsNotSign(0);
                        attDay.setInTime(_source.getDate(EnumStr.picDateField));  // 切记时 getDate  不是 getSqlDate
                        attDay.setInDeviceId(_source.getString(EnumStr.devIdField));
                        attDay.setInDeviceName(_source.getString(EnumStr.devAddressField)); // 11-30 设备名称 修改为 地址
                    }else{ attDay.setSignDate(null);}
                    JSONArray outTimesource = jsonSource.getJSONObject("top_max_hits").getJSONObject("hits").getJSONArray("hits");
                    if (outTimesource.size()>0){
                        _source   =  ((JSONObject) outTimesource.get(0)).getJSONObject("_source");         // hits 中元素  _source
                        attDay.setOutTime(_source.getDate(EnumStr.picDateField));
                        attDay.setOutDeviceId(_source.getString(EnumStr.devIdField));
                        attDay.setOutDeviceName(_source.getString(EnumStr.devAddressField));  // 11-30 devNameField 修改为 devAddressField
                    }else{ attDay.setSignDate(null); }
                    if (attDay.getSignDate() == null){
                        attDay.setSignDate(new Date());
                        attDay.setIsNotSign(1);attDay.setIsLate(null);attDay.setIsLeaveEarly(null);
                    }else{            // 时间 比较 验证 迟到早退
                        attDay.setIsNotSign(0);   //  0 已签  1 未签
                        if (inTime.after(attDay.getInTime())){  //迟到 0否 1是',
                            attDay.setIsLate(0);
                        }else{ attDay.setIsLate(1);}
                        log.info(outTime.toString()+"--》outTime"+new Date());
                        log.info(outTime.before(new Date())+"确认状态信息");
                        if (outTime.before(new Date())){   // 如果 考勤 未到 迟到之前 不做记录
                            if (outTime.before(attDay.getOutTime())){ // 是否早退 0否 1是',
                                attDay.setIsLeaveEarly(0);
                            }else {attDay.setIsLeaveEarly(1); }
                        }
                    }
                    attDays.add(attDay);
                }
                log.info(attDays.size()+"查询将修改人数");
                String ids = "";
                for (AttDay attDay : attDays) {
                    ids += (","+attDay.getEmployeeId());
                }
                int i = 0 ;
                if (attDays.size()>0){
                    i = attDayDao.updateByMoreSourcePerId(attDays);//  修改考勤记录  可 重复
                }
                log.info(i+"修改内容。。。"+"ids:"+ids);
            } catch (ParseException e) {
                e.printStackTrace();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
 
 
 
 
 
}