package weaver.interfaces.hostar.action ;
import com.engine.kq.biz.KQFormatBiz ;
import com.engine.kq.wfset.util.SplitActionUtil ;
import org.apache.commons.lang3.StringUtils ;
import weaver.common.DateUtil ;
import weaver.conn.RecordSet ;
import weaver.general.BaseBean ;
import weaver.general.Util ;
import weaver.interfaces.workflow.action.Action ;
import weaver.soa.workflow.request.Property ;
import weaver.soa.workflow.request.RequestInfo ;
import java.util.* ;
public class OutSignSyncAction implements Action {
BaseBean bb = new BaseBean ( ) ;
@Override
public String execute ( RequestInfo requestInfo ) {
String startDate = "" ;
String endDate = "" ;
String resourceid = "" ;
String nbtxr = "" ;
String sjjsrq = "" ;
try {
Property [ ] properties = requestInfo . getMainTableInfo ( ) . getProperty ( ) ;
for ( Property property : properties ) {
if ( "ksrq" . equals ( property . getName ( ) ) ) {
startDate = Util . null2String ( property . getValue ( ) ) ;
}
if ( "yjjsrq" . equals ( property . getName ( ) ) ) {
endDate = Util . null2String ( property . getValue ( ) ) ;
}
if ( "sjccr" . equals ( property . getName ( ) ) ) {
resourceid = Util . null2String ( property . getValue ( ) ) ;
}
if ( "nbtxr" . equals ( property . getName ( ) ) ) {
nbtxr = Util . null2String ( property . getValue ( ) ) ;
}
if ( "sjjsrq" . equals ( property . getName ( ) ) ) {
sjjsrq = Util . null2String ( property . getValue ( ) ) ;
}
}
// 如果实际结束日期不为空,则用实际结束日期
if ( StringUtils . isNotBlank ( sjjsrq ) ) {
endDate = sjjsrq ;
}
if ( StringUtils . isNotBlank ( startDate ) & & StringUtils . isNotBlank ( endDate ) & & StringUtils . isNotBlank ( resourceid ) ) {
// 内部同行人也要考虑
if ( StringUtils . isNotBlank ( nbtxr ) ) {
resourceid = resourceid + "," + nbtxr ;
}
RecordSet rs = new RecordSet ( ) ;
List < String > infos = new ArrayList < > ( ) ;
String acqOutSignSql = "select a.id, c.signinfo " +
"from mobile_sign a " +
"left join uf_outsigntype c " +
"on c.outsignid = a.id " +
"where operate_date > '" + startDate + "' and operate_date < '" + endDate + "' and operate in (?) " ;
rs . executeQuery ( acqOutSignSql , resourceid ) ;
while ( rs . next ( ) ) {
String signinfo = Util . null2String ( rs . getString ( "signinfo" ) ) ;
infos . add ( signinfo ) ;
}
for ( String inf : infos ) {
if ( inf ! = null ) {
Map < String , Object > in = mapStringToMap ( inf ) ;
String userId = Util . null2String ( in . get ( "userId" ) ) ;
String userType = Util . null2String ( in . get ( "userType" ) ) ;
String signType = Util . null2String ( in . get ( "signType" ) ) ;
String signDate = Util . null2String ( in . get ( "signDate" ) ) ;
String signTime = Util . null2String ( in . get ( "signTime" ) ) ;
String clientAddress = Util . null2String ( in . get ( "clientAddress" ) ) ;
String isInCom = Util . null2String ( in . get ( "isInCom" ) ) ;
String timeZone = Util . null2String ( in . get ( "timeZone" ) ) ;
String belongdate = Util . null2String ( in . get ( "belongdate" ) ) ;
String signfrom = Util . null2String ( in . get ( "signfrom" ) ) ;
String longitude = Util . null2String ( in . get ( "longitude" ) ) ;
String latitude = Util . null2String ( in . get ( "latitude" ) ) ;
String address = Util . null2String ( in . get ( "address" ) ) ;
String deviceInfo = Util . null2String ( in . get ( "deviceInfo" ) ) ;
String belongdateIsNull = Util . null2String ( in . get ( "belongdateIsNull" ) ) ;
String punchSql = "insert into HrmScheduleSign(userId,userType,signType,signDate,signTime,clientAddress,isInCom,timeZone,belongdate,signfrom,longitude,latitude,addr,deviceInfo,isdev) " +
" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)" ;
boolean isok = rs . executeUpdate ( punchSql , resourceid , userType , signType , signDate , signTime , clientAddress , isInCom ,
timeZone , belongdate , signfrom , longitude , latitude , address , deviceInfo , "1" ) ;
bb . writeLog ( "isok: " + isok ) ;
//同步更新考勤数据到考勤报表
if ( "true" . equals ( belongdateIsNull ) ) {
//外勤签到没有归属日期,遇到跨天班次打卡可能归属前一天,需要格式化前一天考勤
bb . writeLog ( "PunchOutButtonCmd:userId:" + userId + ":belongdate:" + DateUtil . getYesterday ( ) ) ;
new KQFormatBiz ( ) . formatDate ( "" + userId , DateUtil . getYesterday ( ) ) ;
}
bb . writeLog ( "PunchOutButtonCmd:userId:" + userId + ":belongdate:" + ( belongdate . length ( ) = = 0 ? DateUtil . getCurrentDate ( ) : belongdate ) ) ;
if ( belongdate . length ( ) = = 0 ) {
//外勤签到没有归属日期,遇到跨天班次打卡可能归属前一天,需要格式化前一天考勤
new KQFormatBiz ( ) . formatDate ( "" + userId , DateUtil . getYesterday ( ) ) ;
}
new KQFormatBiz ( ) . formatDate ( "" + userId , ( belongdate . length ( ) = = 0 ? DateUtil . getCurrentDate ( ) : belongdate ) ) ;
//外勤签到转的考勤 处理加班规则
SplitActionUtil . pushOverTimeTasksAll ( belongdate , belongdate , "" + userId ) ;
}
}
}
} catch ( Exception e ) {
bb . writeLog ( "OutSignSyncAction Exception: " + e ) ;
return Action . FAILURE_AND_CONTINUE ;
}
return Action . SUCCESS ;
}
public static Map < String , Object > mapStringToMap ( String str ) {
str = str . substring ( 1 , str . length ( ) - 1 ) ;
String [ ] strs = str . split ( "," ) ;
Map < String , Object > map = new HashMap < > ( ) ;
for ( String string : strs ) {
String key = string . split ( "=" ) [ 0 ] ;
String value = string . split ( "=" ) [ 1 ] ;
// 去掉头部空格
String key1 = key . trim ( ) ;
String value1 = value . trim ( ) ;
map . put ( key1 , value1 ) ;
}
return map ;
}
}