|
|
|
@ -22,10 +22,13 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
|
|
import com.elink.esua.epdc.commons.mybatis.service.impl.BaseServiceImpl; |
|
|
|
import com.elink.esua.epdc.commons.tools.constant.NumConstant; |
|
|
|
import com.elink.esua.epdc.commons.tools.constant.OrganizationTypeConstant; |
|
|
|
import com.elink.esua.epdc.commons.tools.enums.YesOrNoEnum; |
|
|
|
import com.elink.esua.epdc.commons.tools.exception.RenException; |
|
|
|
import com.elink.esua.epdc.commons.tools.page.PageData; |
|
|
|
import com.elink.esua.epdc.commons.tools.utils.ConvertUtils; |
|
|
|
import com.elink.esua.epdc.commons.tools.constant.FieldConstant; |
|
|
|
import com.elink.esua.epdc.commons.tools.utils.LocalDateUtils; |
|
|
|
import com.elink.esua.epdc.commons.tools.utils.Result; |
|
|
|
import com.elink.esua.epdc.constant.KpiFieldConstant; |
|
|
|
import com.elink.esua.epdc.dao.*; |
|
|
|
@ -34,21 +37,22 @@ import com.elink.esua.epdc.dto.form.KpiRuleSaveOrUpdateFormDTO; |
|
|
|
import com.elink.esua.epdc.dto.result.KpiRuleQueryResultDTO; |
|
|
|
import com.elink.esua.epdc.entity.*; |
|
|
|
import com.elink.esua.epdc.enums.KpiRuleModeEnum; |
|
|
|
import com.elink.esua.epdc.feign.AdminFeignClient; |
|
|
|
import com.elink.esua.epdc.service.KpiRuleService; |
|
|
|
import com.elink.esua.epdc.utils.DeptUtils; |
|
|
|
import com.google.common.collect.Lists; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.web.bind.annotation.PathVariable; |
|
|
|
|
|
|
|
import javax.script.Invocable; |
|
|
|
import javax.script.ScriptEngine; |
|
|
|
import javax.script.ScriptEngineManager; |
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
import java.time.LocalDate; |
|
|
|
import java.time.YearMonth; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
/** |
|
|
|
@ -60,21 +64,23 @@ import java.util.*; |
|
|
|
@Service |
|
|
|
public class KpiRuleServiceImpl extends BaseServiceImpl<KpiRuleDao, KpiRuleEntity> implements KpiRuleService { |
|
|
|
private Logger logger = LoggerFactory.getLogger(getClass()); |
|
|
|
|
|
|
|
//公式参数表
|
|
|
|
@Autowired |
|
|
|
private KpiRuleParamDao kpiRuleParamDao;//公式参数表
|
|
|
|
|
|
|
|
private KpiRuleParamDao kpiRuleParamDao; |
|
|
|
//绩效考核公式表
|
|
|
|
@Autowired |
|
|
|
private KpiFormulaDao kpiFormulaDao;//绩效考核公式表
|
|
|
|
|
|
|
|
private KpiFormulaDao kpiFormulaDao; |
|
|
|
//考核规则表
|
|
|
|
@Autowired |
|
|
|
private KpiRuleDao kpiRuleDao; //考核规则表
|
|
|
|
private KpiRuleDao kpiRuleDao; |
|
|
|
//绩效考核元数据表
|
|
|
|
@Autowired |
|
|
|
private KpiMetaDataDao kpiMetaDataDao; //绩效考核元数据表
|
|
|
|
private KpiMetaDataDao kpiMetaDataDao; |
|
|
|
//网格考核最终得分表
|
|
|
|
@Autowired |
|
|
|
private AdminFeignClient adminFeignClient; |
|
|
|
private KpiResultGridDao kpiResultGridDao; |
|
|
|
@Autowired |
|
|
|
private KpiResultGridDao kpiResultGridDao; //网格考核最终得分表
|
|
|
|
private DeptUtils deptUtils; |
|
|
|
|
|
|
|
@Override |
|
|
|
public PageData<KpiRuleDTO> page(Map<String, Object> params) { |
|
|
|
@ -278,80 +284,96 @@ public class KpiRuleServiceImpl extends BaseServiceImpl<KpiRuleDao, KpiRuleEntit |
|
|
|
|
|
|
|
@Override |
|
|
|
public Result<List<KpiRuleDTO>> calcByRuleCode(String ruleCode) { |
|
|
|
//每次JOB启动时,根据当前时间,获取上一个月的:月初-月末时间
|
|
|
|
Calendar c=Calendar.getInstance(); |
|
|
|
c.add(Calendar.MONTH, -1); |
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
|
|
int lastMonthMaxDay=c.getActualMaximum(Calendar.DAY_OF_MONTH); // 上月天数
|
|
|
|
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59); |
|
|
|
|
|
|
|
String gtimeEnd = sdf.format(c.getTime()); //按格式输出: 上月最后一天
|
|
|
|
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-01"); |
|
|
|
String gtimeStart = sdf2.format(c.getTime()); //按格式输出: 上月第一天
|
|
|
|
|
|
|
|
//查询考核规则表
|
|
|
|
// 每次JOB启动时,根据当前时间,获取上一个月的:月初-月末时间
|
|
|
|
YearMonth now = YearMonth.now(); |
|
|
|
Date startDate = LocalDateUtils.localDateToDate(now.minusMonths(NumConstant.ONE).atDay(NumConstant.ONE)); |
|
|
|
Date endDate = LocalDateUtils.localDateToDate(now.minusMonths(NumConstant.ONE).atEndOfMonth()); |
|
|
|
|
|
|
|
// 查询考核规则表
|
|
|
|
KpiRuleDTO kpiRuleDTO = kpiRuleDao.queryKpiRuleMetaFormula(ruleCode); |
|
|
|
//根据 考核规则表-外键(公式id),查询绩效考核公式表,待运行公式方法
|
|
|
|
// 根据 考核规则表-外键(公式id),查询绩效考核公式表,待运行公式方法
|
|
|
|
KpiFormulaDTO kpiFormulaDTO = kpiFormulaDao.queryFormula(kpiRuleDTO.getReferenceId()); |
|
|
|
//根据 考核规则表-主键,查询公式参数表,排序:1,2,3,4,5
|
|
|
|
List<KpiRuleParamDTO> kpiRuleParamDTO = kpiRuleParamDao.queryFormulaParam(kpiRuleDTO.getId()); |
|
|
|
|
|
|
|
int j = 0; |
|
|
|
while (true){ |
|
|
|
Result<List<Long>> departmentalList = adminFeignClient.pageDeptIdByTypeKey("grid_party",30,j+1); //得到所有的网格id
|
|
|
|
if (departmentalList.getData().size() > 0){ |
|
|
|
j++; |
|
|
|
for (int n = 0; n < departmentalList.getData().size(); n++) { |
|
|
|
int paramLength = 0; |
|
|
|
BigDecimal[] paramValue = new BigDecimal[kpiFormulaDTO.getParamAmount()]; |
|
|
|
Map<String, Object> map = new HashMap<String, Object>(); |
|
|
|
map.put("startDate",gtimeStart); |
|
|
|
map.put("endDate",gtimeEnd); |
|
|
|
map.put("deptId", departmentalList.getData().get(n).longValue()); |
|
|
|
for (int i = 0; i < kpiRuleParamDTO.size() ; i++) { //按照参数数量遍历
|
|
|
|
map.put("dataCode",kpiRuleParamDTO.get(i).getMetaDataCode()); |
|
|
|
KpiMetaDataDTO dataValue = kpiMetaDataDao.queryFormulaParamValue(map); //查询具体分值
|
|
|
|
if (dataValue != null) { |
|
|
|
paramValue[i] = dataValue.getDataValue(); |
|
|
|
paramLength++; |
|
|
|
} |
|
|
|
} |
|
|
|
if (paramLength == kpiFormulaDTO.getParamAmount()){ //参数准备完全时,执行js运算
|
|
|
|
BigDecimal workScore; |
|
|
|
try{ |
|
|
|
ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");//获得一个javascipt的执行引擎
|
|
|
|
engine.eval(kpiFormulaDTO.getFormula()); //执行js代码:参数为:公式运行方法
|
|
|
|
boolean flag=engine instanceof Invocable;//是否可调用方法
|
|
|
|
if(!flag){ |
|
|
|
return new Result().error("运行方法异常"); |
|
|
|
}else{ |
|
|
|
Invocable in = (Invocable)engine; |
|
|
|
//执行js中的函数 参数:js方法名 + 参数
|
|
|
|
Double result=(Double) in.invokeFunction(kpiFormulaDTO.getFunctionName(), paramValue); |
|
|
|
Double workScoreDoouble = (double) Math.round(result*100)/100; |
|
|
|
workScore = new BigDecimal(workScoreDoouble); |
|
|
|
// 网格考核最终得分 表操作
|
|
|
|
KpiResultGridEntity kpiManualScoreEntity = new KpiResultGridEntity(); |
|
|
|
kpiManualScoreEntity.setWorkScore(workScore); |
|
|
|
kpiManualScoreEntity.setGridId(departmentalList.getData().get(n).longValue()); |
|
|
|
kpiManualScoreEntity.setStartDate(sdf2.parse(gtimeStart)); |
|
|
|
kpiManualScoreEntity.setEndDate(sdf2.parse(gtimeEnd)); |
|
|
|
kpiManualScoreEntity.setKpiCycle(kpiRuleDTO.getKpiCycle()); |
|
|
|
this.saveOrUpdateKpiResult(kpiManualScoreEntity); |
|
|
|
} |
|
|
|
}catch(Exception e){ |
|
|
|
e.printStackTrace(); |
|
|
|
return new Result().error("运行方法异常"+e.getMessage()); |
|
|
|
// 根据 考核规则表-主键,查询公式参数表,排序:1,2,3,4,5
|
|
|
|
List<KpiRuleParamDTO> ruleParamList = kpiRuleParamDao.queryFormulaParam(kpiRuleDTO.getId()); |
|
|
|
List<String> metaDateCodeList = Lists.newArrayList(); |
|
|
|
for (int i = 0; i < ruleParamList.size(); i++) { |
|
|
|
metaDateCodeList.add(ruleParamList.get(i).getMetaDataCode()); |
|
|
|
} |
|
|
|
|
|
|
|
int pageSize = NumConstant.THIRTY; |
|
|
|
int pageIndex = NumConstant.ONE; |
|
|
|
|
|
|
|
List<Long> deptIdList; |
|
|
|
do { |
|
|
|
deptIdList = deptUtils.getDeptIdList(OrganizationTypeConstant.ORG_TYPE_GRID_PARTY, pageSize, pageIndex); |
|
|
|
|
|
|
|
for (Long deptId : deptIdList) { |
|
|
|
|
|
|
|
BigDecimal[] paramValue = selectArrayOfMetaDate(deptId, startDate, endDate, metaDateCodeList); |
|
|
|
|
|
|
|
//参数准备完全时,执行js运算
|
|
|
|
if (paramValue.length == kpiFormulaDTO.getParamAmount()) { |
|
|
|
BigDecimal workScore; |
|
|
|
try { |
|
|
|
//获得一个javascipt的执行引擎
|
|
|
|
ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript"); |
|
|
|
//执行js代码:参数为:公式运行方法
|
|
|
|
engine.eval(kpiFormulaDTO.getFormula()); |
|
|
|
//是否可调用方法
|
|
|
|
boolean flag = engine instanceof Invocable; |
|
|
|
if (!flag) { |
|
|
|
return new Result().error("运行方法异常"); |
|
|
|
} |
|
|
|
Invocable in = (Invocable) engine; |
|
|
|
//执行js中的函数 参数:js方法名 + 参数
|
|
|
|
Double result = (Double) in.invokeFunction(kpiFormulaDTO.getFunctionName(), paramValue); |
|
|
|
Double workScoreDoouble = (double) Math.round(result * 100) / 100; |
|
|
|
workScore = new BigDecimal(workScoreDoouble); |
|
|
|
// 网格考核最终得分 表操作
|
|
|
|
KpiResultGridEntity kpiManualScoreEntity = new KpiResultGridEntity(); |
|
|
|
kpiManualScoreEntity.setWorkScore(workScore); |
|
|
|
kpiManualScoreEntity.setGridId(deptId); |
|
|
|
kpiManualScoreEntity.setStartDate(startDate); |
|
|
|
kpiManualScoreEntity.setEndDate(endDate); |
|
|
|
kpiManualScoreEntity.setKpiCycle(kpiRuleDTO.getKpiCycle()); |
|
|
|
this.saveOrUpdateKpiResult(kpiManualScoreEntity); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return new Result().error("运行方法异常" + e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
}else { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
pageIndex++; |
|
|
|
deptIdList = deptUtils.getDeptIdList(OrganizationTypeConstant.ORG_TYPE_GRID_PARTY, pageSize, pageIndex); |
|
|
|
} while (CollUtil.isNotEmpty(deptIdList)); |
|
|
|
|
|
|
|
return new Result(); |
|
|
|
} |
|
|
|
|
|
|
|
private BigDecimal[] selectArrayOfMetaDate(Long deptId, Date startDate, Date endDate, List<String> metaDateCodeList) { |
|
|
|
|
|
|
|
QueryWrapper<KpiMetaDataEntity> wrapper = new QueryWrapper<>(); |
|
|
|
wrapper.eq(KpiFieldConstant.START_DATE, startDate) |
|
|
|
.eq(KpiFieldConstant.END_DATE, endDate) |
|
|
|
.eq(KpiFieldConstant.DEPT_ID, deptId) |
|
|
|
.in(KpiFieldConstant.META_DATA_CODE, metaDateCodeList); |
|
|
|
|
|
|
|
List<KpiMetaDataEntity> metaDataList = kpiMetaDataDao.selectList(wrapper); |
|
|
|
BigDecimal[] metaDataValueArray = new BigDecimal[metaDataList.size()]; |
|
|
|
|
|
|
|
String metaDataCode; |
|
|
|
for (int i = 0; i < metaDateCodeList.size(); i++) { |
|
|
|
metaDataCode = metaDateCodeList.get(i); |
|
|
|
for (KpiMetaDataEntity kpiMetaDataEntity : metaDataList) { |
|
|
|
if (kpiMetaDataEntity.getDataCode().equals(metaDataCode)) { |
|
|
|
metaDataValueArray[i] = kpiMetaDataEntity.getDataValue(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return metaDataValueArray; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 更新或插入考核结果 |
|
|
|
* |
|
|
|
@ -363,15 +385,16 @@ public class KpiRuleServiceImpl extends BaseServiceImpl<KpiRuleDao, KpiRuleEntit |
|
|
|
private void saveOrUpdateKpiResult(KpiResultGridEntity kpiResultGridEntity) { |
|
|
|
//运算结果存储在 网格考核最终得分表 manual_score字段,:唯一性条件(deptId,scoreStartDate,scoreEndDate)
|
|
|
|
KpiResultGridDTO kpiResultGridDto = getKpiresultGrid(kpiResultGridEntity.getGridId(), kpiResultGridEntity.getStartDate(), kpiResultGridEntity.getEndDate()); |
|
|
|
Result<ParentAndAllDeptDTO> deptInfo = adminFeignClient.getParentAndAllDept(kpiResultGridEntity.getGridId().toString()); //查询部门信息
|
|
|
|
if (kpiResultGridDto != null) { //update
|
|
|
|
if (kpiResultGridDto != null) { |
|
|
|
kpiResultGridEntity.setId(kpiResultGridDto.getId()); |
|
|
|
kpiResultGridDao.updateById(kpiResultGridEntity); |
|
|
|
}else { |
|
|
|
kpiResultGridEntity.setParentDeptIds(deptInfo.getData().getParentDeptIds()); |
|
|
|
kpiResultGridEntity.setParentDeptNames(deptInfo.getData().getParentDeptNames()); |
|
|
|
kpiResultGridEntity.setAllDeptIds(deptInfo.getData().getAllDeptIds()); |
|
|
|
kpiResultGridEntity.setAllDeptNames(deptInfo.getData().getAllDeptNames()); |
|
|
|
} else { |
|
|
|
DeptLevelAndLeaderDTO deptLevelInfo = deptUtils.getDeptLevelInfo(kpiResultGridEntity.getGridId(), YesOrNoEnum.YES); |
|
|
|
kpiResultGridEntity.setParentDeptIds(deptLevelInfo.getParentDeptIds()); |
|
|
|
kpiResultGridEntity.setParentDeptNames(deptLevelInfo.getParentDeptNames()); |
|
|
|
kpiResultGridEntity.setAllDeptIds(deptLevelInfo.getAllDeptIds()); |
|
|
|
kpiResultGridEntity.setAllDeptNames(deptLevelInfo.getAllDeptNames()); |
|
|
|
kpiResultGridEntity.setLeaderName(deptLevelInfo.getLeaderName()); |
|
|
|
kpiResultGridDao.insert(kpiResultGridEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
|