package com.engine.jucailinkq.attendance.vacation.service.impl ;
import com.engine.jucailinkq.attendance.attendanceplan.service.AttendancePlanService ;
import com.engine.jucailinkq.attendance.attendanceplan.service.impl.AttendancePlanServiceImpl ;
import com.engine.jucailinkq.attendance.vacation.cmd.HandleOverdueVocationCmd ;
import com.engine.jucailinkq.attendance.vacation.cmd.VocationCmd ;
import com.engine.jucailinkq.attendance.vacation.service.VocationService ;
import com.engine.jucailinkq.common.cmd.GetPersonVocationBySuitOrganzation ;
import com.engine.jucailinkq.common.util.CommonUtil ;
import com.engine.jucailinkq.common.util.DateUtil ;
import com.engine.jucailinkq.common.util.DbTools ;
import com.engine.common.util.ServiceUtil ;
import com.engine.core.impl.Service ;
import com.google.common.collect.Lists ;
import com.google.common.collect.Maps ;
import lombok.extern.slf4j.Slf4j ;
import weaver.general.Util ;
import java.util.List ;
import java.util.Map ;
import java.util.stream.Collectors ;
@Slf4j
public class VocationServiceImpl extends Service implements VocationService {
private AttendancePlanService basicsetService = ServiceUtil . getService ( AttendancePlanServiceImpl . class ) ;
@Override
public void generateVocation ( Map < String , Object > params ) {
String modeId = Util . null2String ( params . get ( "modeId" ) ) ;
String empId = "" ;
String leavetype = "" ;
Map < String , Object > param = Maps . newHashMap ( ) ;
param . put ( "modeId" , modeId ) ;
if ( params . containsKey ( "empid" ) ) {
empId = Util . null2String ( params . get ( "empid" ) ) ;
param . put ( "empid" , empId ) ;
}
if ( params . containsKey ( "leavetype" ) )
leavetype = Util . null2String ( params . get ( "leavetype" ) ) ;
Map < String , Object > result = commandExecutor . execute ( new GetPersonVocationBySuitOrganzation ( param ) ) ;
String sql = "select a.id userid,a.*,b.* from hrmresource a left join cus_fielddata b on a.id=b.id where a.status <> '5' and a.status <> '4' and a.status <> '7' and a.id in (" ;
String queryDepartEmployeeSql = "select resourceid ,changedate,type_n from HRMSTATUSHISTORY where changedate is not null" ;
if ( params . containsKey ( "empid" ) ) {
queryDepartEmployeeSql = queryDepartEmployeeSql + " and resourceid = " + empId ;
sql = "select a.id userid,a.*,b.* from hrmresource a left join cus_fielddata b on a.id=b.id where a.status <> '5' and a.status <> '4' and a.status <> '7' and a.id = " + Util . null2String ( params . get ( "empid" ) ) + " and a.id in (" ;
}
//获取离职日期
List < Map < String , Object > > departEmployeeList = DbTools . getSqlToList ( queryDepartEmployeeSql ) ;
Map < String , Object > departEmployeeMap = CommonUtil . getDepartEmployeeMap ( departEmployeeList ) ;
List < String > userIds = Lists . newArrayList ( ) ;
for ( Map . Entry < String , Object > entry : result . entrySet ( ) ) {
userIds . add ( entry . getKey ( ) ) ;
}
List < List < String > > userPartions = Lists . partition ( userIds , 100 ) ;
List < Map < String , Object > > dataList = Lists . newArrayList ( ) ;
for ( List < String > list : userPartions ) {
String querySql = sql + String . join ( "," , list ) + ")" ;
log . debug ( "querySql : [{}]" , querySql ) ;
List < Map < String , Object > > resultList = DbTools . getSqlToListKeySmallLetter ( querySql ) ;
resultList = resultList . stream ( ) . filter ( e - > "HrmCustomFieldByInfoType" . equals ( e . get ( "scope" ) ) | | "" . equals ( Util . null2String ( e . get ( "scope" ) ) ) ) . collect ( Collectors . toList ( ) ) ;
dataList . addAll ( resultList ) ;
}
Map < String , List < Map < String , Object > > > userIdMap = dataList . stream ( ) . collect ( Collectors . groupingBy ( e - > Util . null2String ( e . get ( "userid" ) ) ) ) ;
sql = "select c.id name,b.tablename id from workflow_bill b left join modeinfo c on b.id=c.formid where (c.isDelete is null or c.isDelete <> '1') and tablename in ('uf_jcl_kq_jqerror')" ;
Map < String , Object > errorModeMap = DbTools . getSqlToMapList ( sql ) ;
for ( Map . Entry < String , Object > entry : result . entrySet ( ) ) {
String userId = entry . getKey ( ) ;
Map < String , String > vocationMap = ( Map < String , String > ) entry . getValue ( ) ;
Map < String , Object > paramMap = Maps . newHashMap ( ) ;
paramMap . put ( "startDate" , DateUtil . getCurrentDate ( ) ) ;
paramMap . put ( "endDate" , "" ) ;
paramMap . put ( "resourceId" , userId ) ;
Map < String , Object > dataMap = basicsetService . getAttendanceItemsByPerson ( paramMap ) ;
List < Map < String , Object > > attendanceItems = ( List < Map < String , Object > > ) dataMap . get ( "data" ) ;
Map < String , List < Map < String , Object > > > itemGroup = attendanceItems . stream ( ) . collect ( Collectors . groupingBy ( e - > e . get ( "key" ) . toString ( ) ) ) ;
Map < String , Object > needGenerateVocationMap = Maps . newHashMap ( ) ;
for ( Map < String , Object > map : attendanceItems ) {
if ( leavetype ! = "" & & ! leavetype . equals ( map . get ( "key" ) ) ) continue ;
if ( vocationMap . get ( map . get ( "key" ) ) ! = null ) {
needGenerateVocationMap . put ( Util . null2String ( map . get ( "key" ) ) , vocationMap . get ( map . get ( "key" ) ) ) ;
}
}
log . debug ( "VocationServiceImpl userId:{}, userList:{}" , userId , userIdMap . get ( userId ) ) ;
for ( Map . Entry < String , Object > e : needGenerateVocationMap . entrySet ( ) ) {
if ( e . getValue ( ) ! = null & & ( ( List < Map < String , Object > > ) e . getValue ( ) ) . size ( ) > 0 & & userIdMap . get ( userId ) ! = null ) {
Map < String , Object > vocationParam = Maps . newHashMap ( ) ;
vocationParam . put ( "userId" , userId ) ;
vocationParam . put ( "vocationList" , e . getValue ( ) ) ;
vocationParam . put ( "userList" , userIdMap . get ( userId ) ) ;
vocationParam . put ( "yxyz" , itemGroup . get ( e . getKey ( ) ) . get ( 0 ) . get ( "yxyz" ) ) ;
vocationParam . put ( "releaseDate" , params . get ( "releaseDate" ) ) ;
vocationParam . put ( "cover" , params . get ( "cover" ) ) ;
vocationParam . put ( "leaveDate" , departEmployeeMap . get ( userId ) ) ;
vocationParam . put ( "errorMode" , errorModeMap . get ( "uf_jcl_kq_jqerror" ) ) ;
commandExecutor . execute ( new VocationCmd ( vocationParam ) ) ;
}
}
}
}
@Override
public void handleOverdueVocation ( Map < String , Object > params ) {
commandExecutor . execute ( new HandleOverdueVocationCmd ( params ) ) ;
}
}