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 persons = userService.queryAllPersonId(); List existPer = attDayDao.selectPersonByDate(new Date()); existPer.forEach(userId -> { persons.remove(userId); }); // 去除存在人员 Collection valueCollection2 = persons.values(); List valueList= new ArrayList(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 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 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 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 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 formEntity = new HttpEntity(esJson, headers); // json 启动 赋值 ResponseEntity 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(); } } } }