package com.product.administration.service; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSONObject; import com.product.administration.config.CmnConst; import com.product.administration.util.WorkDayUtil; 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.permission.PermissionService; import com.product.core.service.support.AbstractBaseService; import com.product.core.service.support.QueryFilterService; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.util.BaseUtil; @Component public class WorkAttendanceService extends AbstractBaseService { @Autowired BaseDao baseDao; @Autowired PermissionService permissionService; @Autowired QueryFilterService queryFilterService; /** * 考勤信息列表 * * @param fse * @return */ public DataTableEntity listAttendanceInfo(FieldSetEntity fse) { String queryFilter = null; if (!BaseUtil.dataTableIsEmpty(fse.getSubDataTable("systemSeniorQueryString"))) { queryFilter = queryFilterService.getQueryFilter(fse); } //String dataFilter = permissionService.getDataFilter(CmnConst.ORG_LEVEL_UUID); //周杰 2022-02-17 条件由ORG_LEVEL_UUID改为CREATE_BY String dataFilter = permissionService.getDataFilter(fse.getTableName(),CmnConst.CREATED_BY); if (!StringUtils.isEmpty(dataFilter)) { if (StringUtils.isEmpty(queryFilter)) { queryFilter = dataFilter; } else { queryFilter += " AND " + dataFilter; } } DataTableEntity dt = baseDao.listTable(CmnConst.PRODUCT_OA_ATTENDANCE_INFO, queryFilter, null, null, null, fse.getInteger(CmnConst.PAGESIZE), fse.getInteger(CmnConst.CPAGE)); baseDao.loadPromptData(dt); return dt; } /** * 考勤信息详情 * * @param uuid * @return */ public FieldSetEntity findAttendanceInfo(String uuid) { return baseDao.getFieldSetEntity(CmnConst.PRODUCT_OA_ATTENDANCE_INFO, uuid, false); } /** * 考勤信息保存 * * @param fse * @return */ public String saveAttendanceInfo(FieldSetEntity fse) { if (StringUtils.isEmpty(fse.getUUID())) { return baseDao.add(fse); } else { fse.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); fse.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date()); baseDao.update(fse); return fse.getUUID(); } } /** * 考勤信息删除 * * @param uuid * @return */ public boolean deleteAttendanceInfo(String uuid) { return baseDao.delete(CmnConst.PRODUCT_OA_ATTENDANCE_INFO, "uuid=?", new Object[] { uuid }); } /** * 获取员工工作信息 * @param user_id * @param yearAndMonth * @return */ public JSONObject getStaffWorkInfo(Integer user_id, String yearAndMonth) { //信息汇总 JSONObject jsonWorkInfo=new JSONObject(); //获取年月日 if (yearAndMonth.length() > 10) { yearAndMonth = yearAndMonth.substring(0, 10); } //获取当月工作时长 int totalHours=WorkDayUtil.queryMonthDay(yearAndMonth) * 7; jsonWorkInfo.put("should_arrived_hours", WorkDayUtil.queryMonthDay(yearAndMonth) * 7); //该月应工作小时 //获取请假信息 Integer [] leaveHours = getLeaveHours(user_id, yearAndMonth); jsonWorkInfo.put("casual_leave_hours", leaveHours[1] * 7); //事假时长 jsonWorkInfo.put("sick_leave_hours", leaveHours[2] * 7); //病假时长 jsonWorkInfo.put("paid_leave_hours", leaveHours[3] * 7); //带薪休假时长 jsonWorkInfo.put("have_arrived_hours", totalHours-leaveHours[0] * 7); //该月实工作小时 //获取出差信息 int tripHours = getBusinessTripHours(user_id, yearAndMonth); jsonWorkInfo.put("business_trip_hours", tripHours * 7); //出差时长 return jsonWorkInfo; } /** * 获取请假时长 * @param user_id * @param yearAndMonth * @return */ public Integer[] getLeaveHours(Integer user_id, String yearAndMonth) { StringBuilder sb=new StringBuilder(); sb.append(" SELECT "); sb.append(" SUM(day) t0, "); sb.append(" SUM(CASE WHEN leave_type=1 THEN day ELSE 0 END ) t1, "); sb.append(" SUM(CASE WHEN leave_type=2 THEN day ELSE 0 END ) t2, "); sb.append(" SUM(CASE WHEN leave_type <>1 and leave_type<>2 THEN day ELSE 0 END ) t3 "); sb.append(" FROM product_oa_ask_for_leave "); sb.append(" WHERE flow_flag=2 "); sb.append(" AND DATE_FORMAT(start_time,'%Y-%m') =? "); sb.append(" AND created_by=? "); FieldSetEntity fse=baseDao.getFieldSetEntityBySQL(sb.toString(), new Object[] {yearAndMonth.substring(0, 7), user_id}, false); return new Integer[] {fse.getInteger("t0")==null?0:fse.getInteger("t0"), fse.getInteger("t1")==null?0:fse.getInteger("t1"), fse.getInteger("t2")==null?0:fse.getInteger("t2"), fse.getInteger("t3")==null?0:fse.getInteger("t3")}; } /** * 获取出差时长 * @param user_id * @param yearAndMonth * @return */ public Integer getBusinessTripHours(Integer user_id, String yearAndMonth) { String sql="SELECT SUM(total_days)total FROM product_oa_business_trip WHERE flow_flag=2 AND DATE_FORMAT(business_start_time,'%Y-%m') = ? AND applicant=?"; FieldSetEntity fse=baseDao.getFieldSetEntityBySQL(sql, new Object[] {yearAndMonth.substring(0, 7), user_id}, false); return fse.getInteger("total")==null?0:fse.getInteger("total"); } }