package com.product.mobile.core.service; import java.text.SimpleDateFormat; import java.util.Date; import com.product.core.transfer.Transactional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; 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.mobile.core.config.MobileCoreConst; import com.product.module.sys.entity.SystemUser; import com.product.util.BaseUtil; /** * 手机端,获取用户所有应用 * @author Administrator * */ @Component public class SignInService extends AbstractBaseService{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //制定输出格式 @Autowired public BaseDao baseDao; /** * 先查出历史打卡记录 * @return * @throws BaseException */ @Transactional public boolean signIn(FieldSetEntity fse) throws BaseException{ SystemUser user=SpringMVCContextHolder.getCurrentUser(); Date d = new Date(); //获取当前时间打卡记录 FieldSetEntity fs_record=signInList(); //获取打卡设置 FieldSetEntity fs_time = baseDao.getFieldSetByFilter(MobileCoreConst.TABLE_PUNCH_TIME, "punch_site_uuid=?", new Object[]{fse.getString("punch_congfig_uuid")}, false); if (!StringUtils.isEmpty(fs_time.getString("morning_work_off"))&&!StringUtils.isEmpty(fs_time.getString("afternoon_work"))){ //四次打卡保存规则 if (FieldSetEntity.isEmpty(fs_record)){ //未查询到打开记录新建打卡记录 if(user!=null) { fse.setValue(MobileCoreConst.ORG_LEVEL_UUID, user.getOrg_level_uuid()); fse.setValue(MobileCoreConst.DEPT_UUID, user.getDept_uuid()); fse.setValue(MobileCoreConst.CREATED_BY, user.getUser_id()); fse.setValue(MobileCoreConst.CREATED_UTC_DATETIME, d); } fse.setValue(MobileCoreConst.FIELD_PUNCH_TYPE,1);//手机打卡 if (!StringUtils.isEmpty(fse.getDate("punch_time_one"))){ if (dataCompareTo(fse.getDate("punch_time_one"),fs_time.getDate("morning_work_off"))){ fse.setValue("punch_time_one",fse.getDate("punch_time_one")); fse.setValue("punch_range_one",fse.getString("punch_range_one")); }else { fse.setValue("punch_time_three",fse.getDate("punch_time_one")); fse.setValue("punch_range_three",fse.getString("punch_range_one")); fse.setValue("punch_time_one",null); fse.setValue("punch_range_one",null); } } if (!StringUtils.isEmpty(fse.getDate("punch_time_four"))){ if (dataCompareTo(fse.getDate("punch_time_four"),fs_time.getDate("afternoon_work_off"))){ fse.setValue("punch_time_two",fse.getDate("punch_time_four")); fse.setValue("punch_range_two",fse.getString("punch_range_four")); }else { fse.setValue("punch_time_two",null); fse.setValue("punch_range_two",null); fse.setValue("punch_time_four",fse.getDate("punch_time_four")); fse.setValue("punch_range_four",fse.getString("punch_range_four")); } } fse.setTableName(MobileCoreConst.TABLE_PUNCH_RECORD); return baseDao.saveFieldSetEntity(fse); } else { //查询到已有打开记录新建打卡记录 if (!StringUtils.isEmpty(fse.getDate("punch_time_one"))){ //判断上班打卡打卡规则:1、上午上班未打卡 2、未超过上午下班时间 3、上午未打下班卡 则为上午上班卡否则为下午上班卡 if(dataCompareTo(fse.getDate("punch_time_one"),fs_time.getDate("morning_work_off")) &&StringUtils.isEmpty(fs_record.getString("punch_time_one")) &&StringUtils.isEmpty(fs_record.getString("punch_time_two"))){ fs_record.setValue("punch_time_one", fse.getDate("punch_time_one")); fs_record.setValue("punch_range_one", fse.getString("punch_range_one")); }else { fs_record.setValue("punch_time_three", fse.getDate("punch_time_one")); fs_record.setValue("punch_range_three", fse.getString("punch_range_one")); } } //判断下班打卡班打卡规则:1、上午下班未打卡 2、未超过上午下班时间 3、下午午未打上班卡 则为上午下班卡否则为下午下班卡 if (!StringUtils.isEmpty(fse.getDate("punch_time_four"))){ if(dataCompareTo(fse.getDate("punch_time_four"),fs_time.getDate("afternoon_work_off")) &&StringUtils.isEmpty(fs_record.getDate("punch_time_two")) &&StringUtils.isEmpty(fs_record.getDate("punch_time_three"))){ fs_record.setValue("punch_time_two", fse.getDate("punch_time_four")); fs_record.setValue("punch_range_two", fse.getString("punch_range_four")); }else { fs_record.setValue("punch_time_four", fse.getDate("punch_time_four")); fs_record.setValue("punch_range_four", fse.getString("punch_range_four")); } } return baseDao.saveFieldSetEntity(fs_record); } }else { //两次打卡保存规则 if (FieldSetEntity.isEmpty(fs_record)){ //未查询到打开记录新建打卡记录 if(user!=null) { fse.setValue(MobileCoreConst.ORG_LEVEL_UUID, user.getOrg_level_uuid()); fse.setValue(MobileCoreConst.DEPT_UUID, user.getDept_uuid()); fse.setValue(MobileCoreConst.CREATED_UTC_DATETIME, d); fse.setValue(MobileCoreConst.CREATED_BY, user.getUser_id()); } fse.setValue(MobileCoreConst.FIELD_PUNCH_TYPE,1);//手机打卡 fse.setTableName(MobileCoreConst.TABLE_PUNCH_RECORD); return baseDao.saveFieldSetEntity(fse); }else { //查询到已有打开记录新建打卡记录 if (!StringUtils.isEmpty(fse.getString("punch_time_one"))){ fs_record.setValue("punch_time_one",fse.getDate("punch_time_one")); fs_record.setValue("punch_range_one",fse.getString("punch_range_one")); } if (!StringUtils.isEmpty(fse.getString("punch_time_four"))){ fs_record.setValue("punch_time_four",fse.getDate("punch_time_four")); fs_record.setValue("punch_range_four",fse.getString("punch_range_four")); } return baseDao.saveFieldSetEntity(fs_record); } } } /** * 将两个时间得时分秒进行对比true则为前者早 否则就是一样时间或者后者早 * @param a * @param b * @return */ private boolean dataCompareTo(Date a,Date b){ a.setTime(a.getTime() % (24 * 60 * 60 * 1000)); // 设置date2为当天的00:00:00 b.setTime(b.getTime() % (24 * 60 * 60 * 1000)); // 设置date2为当天的00:00:00 if (a.compareTo(b) < 0) { return true; } return false; } /** * 查出指定日期打卡记录 * @param cpage * @param pagesize * @return * @throws BaseException */ public FieldSetEntity signInList() throws BaseException{ SystemUser user=SpringMVCContextHolder.getCurrentUser(); Date d = new Date(); String str = sdf.format(d); //将日期转换为字符串且格式按照之前制定的 FieldSetEntity fs = baseDao.getFieldSetByFilter(MobileCoreConst.TABLE_PUNCH_RECORD, "created_by=? and DATE_FORMAT(created_utc_datetime,'%Y-%m-%d')=?", new Object[]{user.getUser_id(), str}, false); return fs; } /** * 查询公司定义的考勤点 * @param org_level_uuid * @return */ public DataTableEntity signInSites(String org_level_uuid) { if(StringUtils.isEmpty(org_level_uuid)) { return baseDao.listTable(MobileCoreConst.TABLE_PUNCH_SITE); }else { return baseDao.listTable(MobileCoreConst.TABLE_PUNCH_SITE,"org_level_uuid=?",new String[] {org_level_uuid}); } } }