递归均分实现

总体功能:

假定A下有A1,A2,A3,A4.并且A1到A4的各个节点都有子节点,

当设置A1,A2的分配权重和为R,那么A3,A4会均分剩下的 1-R

同时会递归均分计算A1到A4每个节点的子节点,直至层级关系的最后

package com.teradata.estimateCardManage.dao;

import java.math.BigDecimal;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.teradata.bos.dqc.dao.Dao;

import com.teradata.bos.dqc.dao.DaoImp;

import com.teradata.util.CommonFunc;

import com.teradata.util.Constants;

public class EstimateGroupManageDAO {

public List estimateGroupInitAndQuery(Map<String, Object> paramsMap){

List childrenList=null;

Dao dao = new DaoImp();

try{

StringBuffer sb = new StringBuffer();

sb.append("select folder_id , folder_name,folder_desc,par_folder_id as parId,")

.append("weight,create_oper,start_dt,dis_weight ")

.append("from appdqc.estimate_group ")

.append(" where status='1' and ")

.append(" par_folder_id='"+paramsMap.get("parId")+"'");

String whereClause = "";

if(!StringUtils.isBlank((String)paramsMap.get("groupName"))){

whereClause = whereClause+" and FOLDER_NAME like '%"+paramsMap.get("groupName")+"%'";

}

if(!StringUtils.isBlank((String)paramsMap.get("groupID"))){

whereClause = whereClause+" and FOLDER_ID = '"+paramsMap.get("groupID")+"'" ;

}

sb.append(whereClause);

childrenList=dao.getResultList(sb.toString());

}catch(Exception e){

e.printStackTrace();

}

return childrenList;

}

public List queryCardScore(Map<String, Object> paramsMap){

List childrenList=null;

Dao dao = new DaoImp();

try{

String nodeType=(String)paramsMap.get("nodeType");

//Constants.orgTotal.equals(branchId);

StringBuffer sb = new StringBuffer();

if("1".equals(nodeType)){

//根节点上单击,查询所有评分卡的得分

sb.append("select ec.folder_id , ec.folder_name,ecs.score,")

.append("ecs.maxscore as maxscore, case when ecs.maxscore=0 then 0 else (ecs.score*100/ecs.maxscore) end ||'%'  as scorepct,   rundate  ")

.append(" from appdqc.estimate_card ec ")

.append(" left join estimate_card_score ecs on ec.folder_id = ecs.folder_id")

.append(" where ec.status='1'  ")

.append(" and rundate='").append(paramsMap.get("runDate")).append("'")

.append(" and org_id='").append(paramsMap.get("orgCode")).append("'")

.append(" and ec.par_folder_id='").append(paramsMap.get("nodeId")).append("'");

}else if("2".equals(nodeType)){

//某个评分卡上非叶子节点上的单击

sb.append("select ec.folder_id , ec.folder_name,ecs.score,")

.append("ecs.maxscore as maxScore,case when ecs.maxscore=0 then 0 else (ecs.score*100/ecs.maxscore) end||'%'  as scorepct,   rundate  ")

.append(" from appdqc.estimate_group ec ")

.append(" left join estimate_card_score ecs on ec.folder_id = ecs.folder_id")

.append(" where ec.status='1'  ")

.append(" and rundate='").append(paramsMap.get("runDate")).append("'")

.append(" and org_id='").append(paramsMap.get("orgCode")).append("'")

.append(" and ec.par_folder_id='").append(paramsMap.get("nodeId")).append("'");

}else if("3".equals(nodeType)){

//某个评分卡上叶子节点的单击

sb.append("select ecr.check_id as folder_id , r.rule_name as folder_name,rs.score,")

.append("ecr.weight*100 as maxScore,(rs.score/ecr.weight)||'%'  as scorepct,rs.rundate ,ecr.weight")

.append(" ,r.sys_id,r.table_name,r.check_column_name")

.append(" from appdqc.estimate_checkid_relation ecr ")

.append(" left join rule_score rs on ecr.check_id = rs.check_id")

.append(" left join rule r on ecr.check_id= r.check_id")

.append(" where 1=1  ")

.append(" and rs.rundate='").append(paramsMap.get("runDate")).append("'")

.append(" and rs.org_id='").append(paramsMap.get("orgCode")).append("'")

.append(" and ecr.folder_id='").append(paramsMap.get("nodeId")).append("'");

}

childrenList=dao.getResultList(sb.toString());

}catch(Exception e){

e.printStackTrace();

}

return childrenList;

}

public List getLatestCalDate(){

Dao dao = new DaoImp();

List childrenList=null;

try{

StringBuffer sb = new StringBuffer();

//某个评分卡上叶子节点的单击

sb.append("select max(rundate) as maxRundate ")

.append(" from estimate_card_score ")

;

childrenList=dao.getResultList(sb.toString());

}catch(Exception e){

e.printStackTrace();

}

return childrenList;

}

public List setGroupWeightQuery(Map<String, Object> paramsMap){

List groupList=null;

Dao dao = new DaoImp();

String groupIds[] = ((String)paramsMap.get("groupIds")).split("#");

try{

StringBuffer sb = new StringBuffer();

sb.append("select folder_id , folder_name,folder_desc,par_folder_id as parId,")

.append("weight,create_oper,start_dt,dis_weight ")

.append("from appdqc.estimate_group ")

.append(" where status='1' and ")

.append(" folder_id in "+CommonFunc.getInStr(groupIds));

groupList=dao.getResultList(sb.toString());

}catch(Exception e){

e.printStackTrace();

}

return groupList;

}

public List setRuleWeightQuery(Map<String, Object> paramsMap){

List ruleList=null;

Dao dao = new DaoImp();

String ruleIds[] = ((String)paramsMap.get("ruleIds")).split("#");

try{

StringBuffer sb = new StringBuffer();

sb.append("select er.check_id , er.check_id,sys_id,table_name,check_column_name,")

.append("er.start_dt,er.create_oper,er.weight,er.dis_weight,rg.rule_name ")

.append("from appdqc.estimate_checkid_relation er ")

.append(" left join appdqc.rule rg on rg.check_id = er.check_id ")

.append(" where rg.status='1' and er.status='1' and ")

.append(" er.check_id in "+CommonFunc.getInStr(ruleIds));

ruleList=dao.getResultList(sb.toString());

}catch(Exception e){

e.printStackTrace();

}

return ruleList;

}

public List estimateCard(){

Dao dao = new DaoImp();

try{

StringBuffer sb = new StringBuffer();

sb.append("SELECT FOLDER_ID||'#'||FOLDER_NAME as id , FOLDER_NAME as name from estimate_card where par_folder_id='-1' and status=1 ");

String orderBy = " order by FOLDER_ID ";

sb.append(orderBy);

List cardList = dao.getResultList(sb.toString());

return cardList;

}catch(Exception e){

e.printStackTrace();

}finally{

dao.closeconn();

}

return null;

}

public int estimateGroupAdd(Map<String, Object> paramsMap){

Dao dao = new DaoImp();

try{

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String currDate = df.format(new Date());

StringBuffer sb = new StringBuffer();

sb.append("insert into estimate_group ")

.append("select coalesce(case  when max(zeroifnull(folder_id)) =0 then 10000 ")

.append("             when max(zeroifnull(folder_id))>=1 then max(zeroifnull(folder_id))+1 end,10000),'")

.append(paramsMap.get("parId")).append("','")

.append(paramsMap.get("FOLDER_NAME")).append("','")

.append(paramsMap.get("FOLDER_DESC")).append("','0','1','','','")

.append(paramsMap.get("userId")).append("','0','")

.append(currDate).append("','")

.append("2999-12-31 00:00:00',''")

.append(" from estimate_group ");

int ret = dao.insert(sb.toString());

return ret;

}catch(Exception e){

e.printStackTrace();

}finally{

dao.closeconn();

}

return 0;

}

public int estimateGroupModify(Map<String, Object> paramsMap){

Dao dao = new DaoImp();

try{

StringBuffer updateSQL = new StringBuffer();

updateSQL

.append("update estimate_group set ")

.append(" FOLDER_NAME='").append(paramsMap.get("FOLDER_NAME")).append("',")

.append(" FOLDER_DESC='").append(paramsMap.get("FOLDER_DESC")).append("'")

.append(" where folder_id='").append(paramsMap.get("FOLDER_ID")).append("' and status='1'");

dao.update(updateSQL.toString());

     

}catch(Exception e){

e.printStackTrace();

return 0;

}finally{

dao.closeconn();

}

return 1;

}

public int estimateGroupModify_history(Map<String, Object> paramsMap){

Dao dao = new DaoImp();

try{

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String currDate = df.format(new Date());

StringBuffer updateSQL = new StringBuffer();

updateSQL

.append("update estimate_group set ")

.append(" END_DT='").append(currDate).append("',")

.append(" status='0' ")

.append(" where folder_id='").append(paramsMap.get("FOLDER_ID")).append("' and status='1'");

dao.update(updateSQL.toString());

     

StringBuffer insertSQL = new StringBuffer();

insertSQL

.append(" insert into estimate_group ")

.append(" select distinct  FOLDER_ID,PAR_FOLDER_ID,'")

.append(paramsMap.get("FOLDER_NAME")).append("','")

.append(paramsMap.get("FOLDER_DESC")).append("',")

.append("isleaf,'1',")

.append(" weight,display_order,create_oper,node_level,'")

.append(currDate).append("','2999-12-31 00:00:00'")

.append(" from estimate_group ")

.append(" where folder_id='").append(paramsMap.get("FOLDER_ID")).append("'");

dao.insert(insertSQL.toString());

}catch(Exception e){

e.printStackTrace();

return 0;

}finally{

dao.closeconn();

}

return 1;

}

public int estimateGroupDelete(String []estimateGroupIDs){

Dao dao = new DaoImp();

try{

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String currDate = df.format(new Date());

StringBuffer sb = new StringBuffer();

sb.append(" WITH  recursive temptab(id,name,pid,leveld) as(")

    .append(" select a.folder_id,a.folder_name,a.par_folder_id,0 from estimate_group a where folder_id in"+CommonFunc.getInStr(estimateGroupIDs)+ "union all")

    .append(" select a.folder_id,a.folder_name,a.par_folder_id,c.leveld+1 from estimate_group a ,temptab c where  c.id = a.par_folder_id and status='1' and leveld<7)")

    .append(" select id from temptab");

       

List childrenList = dao.getResultList(sb.toString());

String []allIds = new String[childrenList.size()];

for(int i =0;i<childrenList.size();i++){

allIds[i] = (String)((Map)childrenList.get(i)).get("ID");

}

StringBuffer updateSQL = new StringBuffer();

updateSQL

.append("update  estimate_group set  status='0', ")

.append(" END_DT='").append(currDate).append("'")

.append(" where folder_id in "+CommonFunc.getInStr(allIds));

int ret = dao.update(updateSQL.toString());

return ret;

}catch(Exception e){

e.printStackTrace();

return 0;

}finally{

dao.closeconn();

}

}

public int updateWeight(String []estimateGroupIDs){

Dao dao = new DaoImp();

try{

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String currDate = df.format(new Date());

String id = "";

String weight="";

String disWeight="";

for(int i =0;i<estimateGroupIDs.length;i++){

id = estimateGroupIDs[i].split("@")[0];

disWeight = estimateGroupIDs[i].split("@")[4];

weight = estimateGroupIDs[i].split("@")[5];

StringBuffer updateSQL = new StringBuffer();

updateSQL

.append("update  estimate_group set  weight='"+weight+"',dis_weight='"+disWeight+"'")

.append(" where folder_id='"+id+"'");

dao.update(updateSQL.toString());

}

return 1;

}catch(Exception e){

e.printStackTrace();

return 0;

}finally{

dao.closeconn();

}

}

public int setRuleWeight(Map<String, Object> paramsMap){

Dao dao = new DaoImp();

try{

String []ruleInfo = ((String)paramsMap.get("ruleWeightParams")).split(",");

String currNodeId = (String)paramsMap.get("currNodeId");

//得到当前设置规则的父节点的权重信息

StringBuffer leafNodeWeightSQL = new StringBuffer();

leafNodeWeightSQL

.append("select folder_id,folder_name,weight")

.append(" from estimate_group ")

.append(" where folder_id = '"+currNodeId+"' and status='1' ");

List leafNodeWeight = dao.getResultList(leafNodeWeightSQL.toString());

String leafWeight = (String)((Map)leafNodeWeight.get(0)).get("WEIGHT");

String ruleID = "";

String disWeight="";

BigDecimal disWeightTotal = new BigDecimal(0.0);

List rules = new ArrayList();

for(int i =0;i<ruleInfo.length;i++){

ruleID = ruleInfo[i].split("@")[0];

rules.add(ruleID);

disWeight = ruleInfo[i].split("@")[1];

disWeightTotal = disWeightTotal.add(new BigDecimal(disWeight));

BigDecimal weight = new BigDecimal(disWeight).multiply(new BigDecimal(leafWeight)).setScale(4, BigDecimal.ROUND_HALF_UP);

StringBuffer updateSQL = new StringBuffer();

updateSQL

.append("update  estimate_checkid_relation set  dis_weight='"+disWeight+"',weight='"+weight.toString()+"'")

.append(" where folder_id='"+currNodeId+"' and check_id='"+ruleID+"'");

dao.update(updateSQL.toString());

}

//精度4位,四舍五入,剩余的分配权重

BigDecimal remainDisWeight = new BigDecimal(1-disWeightTotal.doubleValue()).setScale(4, BigDecimal.ROUND_HALF_UP);

//未设置规则权重的其他规则平分该权重

BigDecimal remainActWeight = remainDisWeight.multiply(new BigDecimal(leafWeight));

System.out.println("**************剩余的分配权重:"+remainDisWeight.doubleValue());

System.out.println("**************剩余的实际权重:"+remainActWeight.doubleValue());

StringBuffer queryRemainRule = new StringBuffer();

String checkids = CommonFunc.getInStr(rules);

System.out.println("**************已经设置过的checkId:"+checkids);

queryRemainRule

.append("select count(*) as num from estimate_checkid_relation ")

.append("  where folder_id='"+currNodeId+"' and check_id not in "+checkids);

List count = dao.getResultList(queryRemainRule.toString());

String notSetWeight = (String)((Map)count.get(0)).get("NUM");

//如果规则的权重已经全部分配,就不需要均等分配

if(!"0".equals(notSetWeight)){

BigDecimal oddDis = remainDisWeight.divide(new BigDecimal(notSetWeight),4,BigDecimal.ROUND_HALF_UP);

BigDecimal oddAct = remainActWeight.divide(new BigDecimal(notSetWeight),4,BigDecimal.ROUND_HALF_UP);

StringBuffer updateRemainRule = new StringBuffer();

updateRemainRule

.append("update  estimate_checkid_relation set  dis_weight='"+oddDis.toString()+"',weight='"+oddAct.toString()+"'")

.append(" where folder_id='"+currNodeId+"' and check_id not in "+checkids);

dao.update(updateRemainRule.toString());

}

return 1;

}catch(Exception e){

e.printStackTrace();

return 0;

}finally{

dao.closeconn();

}

}

public int updateRuleWeight(String []ruleIDs){

Dao dao = new DaoImp();

try{

String ruleID = "";

String folderID = ruleIDs[0].split("@")[0];

String disWeight="";

String weight = "";

BigDecimal bg = new BigDecimal(0.0);

List rules = new ArrayList();

for(int i =1;i<ruleIDs.length;i++){

ruleID = ruleIDs[i].split("@")[0];

rules.add(ruleID);

disWeight = ruleIDs[i].split("@")[1];

bg = bg.add(new BigDecimal(disWeight));

weight = ruleIDs[i].split("@")[2];

StringBuffer updateSQL = new StringBuffer();

updateSQL

.append("update  estimate_checkid_relation set  dis_weight='"+disWeight+"',weight='"+weight+"'")

.append(" where folder_id='"+folderID+"' and check_id='"+ruleID+"'");

dao.update(updateSQL.toString());

}

//剩余的分配权重

double rema = 1-bg.doubleValue();

BigDecimal folderActWeight = new BigDecimal(ruleIDs[0].split("@")[1]);

//精度4位,四舍五入,剩余的分配权重

BigDecimal remainDisWeight = new BigDecimal(rema).setScale(4, BigDecimal.ROUND_HALF_UP);

//未设置规则权重的其他规则平分该权重

BigDecimal remainActWeight = remainDisWeight.multiply(folderActWeight);

System.out.println("**************叶子节点的权重:"+ruleIDs[0].split("@")[1]);

System.out.println("**************剩余的分配权重:"+remainDisWeight.doubleValue());

System.out.println("**************剩余的实际权重:"+remainActWeight.doubleValue());

//一个叶子评分组下可能有多个规则,实际设置分配权重时只设置几个

StringBuffer queryRemainRule = new StringBuffer();

String checkids = CommonFunc.getInStr(rules);

System.out.println("**************已经设置过的checkId:"+checkids);

queryRemainRule

.append("select count(*) as num from estimate_checkid_relation ")

.append("  where folder_id='"+folderID+"' and check_id not in "+checkids);

List count = dao.getResultList(queryRemainRule.toString());

String notSetWeight = (String)((Map)count.get(0)).get("NUM");

//如果规则的权重已经全部分配,就不需要均等分配

if(!"0".equals(notSetWeight)){

BigDecimal oddDis = remainDisWeight.divide(new BigDecimal(notSetWeight)).setScale(4, BigDecimal.ROUND_HALF_UP);

BigDecimal oddAct = remainActWeight.divide(new BigDecimal(notSetWeight)).setScale(4, BigDecimal.ROUND_HALF_UP);

StringBuffer updateRemainRule = new StringBuffer();

updateRemainRule

.append("update  estimate_checkid_relation set  dis_weight='"+oddDis.toString()+"',weight='"+oddAct.toString()+"'")

.append(" where folder_id='"+folderID+"' and check_id not in "+checkids);

dao.update(updateRemainRule.toString());

}

return 1;

}catch(Exception e){

e.printStackTrace();

return 0;

}finally{

dao.closeconn();

}

}

public int updateGroupWeight(Map<String, Object> paramsMap){

Dao dao = new DaoImp();

try{

String []groupInfo = ((String)paramsMap.get("groupWeightParams")).split(",");

String currNodeId = (String)paramsMap.get("currNodeId");

String par_folder_id = currNodeId;

String parWeight = "";

if(currNodeId.length()<5){

//当前操作的节点是评分卡,评分卡没有权重和分配权重,要特殊处理

parWeight = "1.0000";

}else{

//得到当前设置节点的父节点的权重信息

StringBuffer parGroupSQL = new StringBuffer();

parGroupSQL

.append("select egp.folder_id,egp.folder_name,egp.weight")

.append(" from estimate_group egs")

.append(" left join estimate_group egp on egs.par_folder_id = egp.folder_id")

.append(" where egs.folder_id = '"+groupInfo[0].split("@")[0]+"' and egs.status='1' ")

.append(" and egp.status='1' ");

List parGroupInfo = dao.getResultList(parGroupSQL.toString());

parWeight = (String)((Map)parGroupInfo.get(0)).get("WEIGHT");

}

String disWeight="";

BigDecimal weight = new BigDecimal(0.0000);

String groupId ="";

//正在设置权重的所有评分组

List groupIds = new ArrayList();

//正在设置权重的所有评分组的分配权重之和

BigDecimal disWeightTotal = new BigDecimal(0.0);

for(int i =0;i<groupInfo.length;i++){

groupId = groupInfo[i].split("@")[0];

groupIds.add(groupId);

disWeight = groupInfo[i].split("@")[1];

disWeightTotal = disWeightTotal.add(new BigDecimal(disWeight));

//计算每一个设置了分配权重的评分组节点的实际权重=父节点实际权重*自身的分配权重

weight = new BigDecimal(disWeight).multiply(new BigDecimal(parWeight)).setScale(4, BigDecimal.ROUND_HALF_UP);

StringBuffer updateSQL = new StringBuffer();

updateSQL

.append("update  estimate_group set  dis_weight='"+disWeight+"',weight='"+weight+"'")

.append(" where folder_id='"+groupId+"'");

dao.update(updateSQL.toString());

oddBelowWeight(groupId,dao);

}

System.out.println("**************已经设置过的groupIds:"+groupIds.toString());

StringBuffer queryRemainGroup = new StringBuffer();

queryRemainGroup

.append("select folder_id from estimate_group ")

.append("  where status='1' and par_folder_id='"+par_folder_id+"' and folder_id not in "+CommonFunc.getInStr(groupIds));

List groupList = dao.getResultList(queryRemainGroup.toString());

if(groupList==null){

groupList = new ArrayList();

}

int notSetWeightNum = groupList.size();

//同一个父节点下的评分组没有全部设置,只设置了部分

if(notSetWeightNum>0){

//精度4位,四舍五入,剩余的分配权重

BigDecimal remainDisWeight = new BigDecimal(1-disWeightTotal.doubleValue()).setScale(4, BigDecimal.ROUND_HALF_UP);

BigDecimal remainActWeight = remainDisWeight.multiply(new BigDecimal(parWeight));

BigDecimal oddDis = remainDisWeight.divide(new BigDecimal(notSetWeightNum),4,BigDecimal.ROUND_HALF_UP);

BigDecimal oddAct = remainActWeight.divide(new BigDecimal(notSetWeightNum),4,BigDecimal.ROUND_HALF_UP);

StringBuffer updateRemainGroup = new StringBuffer();

updateRemainGroup

.append("update  estimate_group set  dis_weight='"+oddDis.toString()+"',weight='"+oddAct.toString()+"'")

.append(" where status='1' and par_folder_id='"+par_folder_id+"' and folder_id not in "+ CommonFunc.getInStr(groupIds));

dao.update(updateRemainGroup.toString());

String tmpId  ="";

for(int i=0;i<groupList.size();i++){

tmpId = (String)((Map)groupList.get(i)).get("FOLDER_ID");

oddBelowWeight(tmpId,dao);

}

}

return 1;

}catch(Exception e){

e.printStackTrace();

return 0;

}finally{

dao.closeconn();

}

}

//均分下级节点所有的分配权重和实际权重

public int oddBelowWeight(String folder_id,Dao dao ) throws Exception{

StringBuffer nodeInfoSQL = new StringBuffer();

nodeInfoSQL

.append("select folder_id,folder_name,isleaf,weight")

.append(" from estimate_group ")

.append(" where folder_id='"+folder_id+"' and status='1' " );

List nodeInfo = dao.getResultList(nodeInfoSQL.toString());

String isLeaf = (String)((Map)nodeInfo.get(0)).get("ISLEAF");

//当前节点的实际权重

String weight = (String)((Map)nodeInfo.get(0)).get("WEIGHT");

if("0".equals(isLeaf)){

//不是叶子节点,需要进行递归计算

StringBuffer belowNodeInfoSQL = new StringBuffer();

belowNodeInfoSQL

.append("select count(folder_id) as NUM from estimate_group")

.append(" where status='1' and par_folder_id='"+folder_id+"'");

//评分树末端必须都是叶子节点,也即都设置了规则,否则这里的count=0,出现除0异常

List childList = dao.getResultList(belowNodeInfoSQL.toString());

String childCount = (String)((Map)childList.get(0)).get("NUM");

if(!"0".equals(childCount)){

BigDecimal oddDis = new BigDecimal(1.0000).divide(new BigDecimal(childCount),4,BigDecimal.ROUND_HALF_UP);

BigDecimal oddAct = new BigDecimal(weight).divide(new BigDecimal(childCount),4,BigDecimal.ROUND_HALF_UP);

//均分当前节点的下级节点信息

StringBuffer updateChildGroup = new StringBuffer();

updateChildGroup

.append("update  estimate_group set  dis_weight='"+oddDis.toString()+"',weight='"+oddAct.toString()+"'")

.append(" where status='1' and  par_folder_id='"+folder_id+"'");

dao.update(updateChildGroup.toString());

//对所有的下级进行递归的权重计算

StringBuffer belowNodeIDSQL = new StringBuffer();

belowNodeIDSQL

.append("select folder_id from estimate_group")

.append(" where status='1' and par_folder_id='"+folder_id+"'");

List folderIds = dao.getResultList(belowNodeIDSQL.toString());

String tmpIds = "";

for(int i=0;i<folderIds.size();i++){

tmpIds = (String)((Map)folderIds.get(i)).get("FOLDER_ID");

oddBelowWeight(tmpIds,dao);

}

}

}else if("1".equals(isLeaf)){

//是叶子节点,只需均分节点下的规则分配权重和实际权重

//对所有的下级进行递归的权重计算

StringBuffer ruleSQL = new StringBuffer();

ruleSQL

.append("select count(check_id)as NUM from estimate_checkId_relation")

.append(" where folder_id='"+folder_id+"' and status='1'");

List count = dao.getResultList(ruleSQL.toString());

String ruleNum = (String)((Map)count.get(0)).get("NUM");

BigDecimal oddDis = new BigDecimal(1.0000).divide(new BigDecimal(ruleNum),4,BigDecimal.ROUND_HALF_UP);

BigDecimal oddAct = new BigDecimal(weight).divide(new BigDecimal(ruleNum),4,BigDecimal.ROUND_HALF_UP);

//均分当前节点的下级节点信息

StringBuffer updateChildRule = new StringBuffer();

updateChildRule

.append("update  estimate_checkId_relation set  dis_weight='"+oddDis.toString()+"',weight='"+oddAct.toString()+"'")

.append(" where folder_id='"+folder_id+"' and status='1'");

dao.update(updateChildRule.toString());

}

return 1;

}

public List getScoreInfo(Map scoreParam,String currId){

List scoreList=null;

Dao dao = new DaoImp();

try{

StringBuffer sb = new StringBuffer();

sb.append("select folder_id, score ")

.append ("from estimate_card_score ")

.append(" where 1=1 ")

.append(" and folder_id='").append(currId).append("'")

.append(" and rundate='").append(scoreParam.get("runDate")).append("'")

.append(" and org_id='").append(scoreParam.get("orgCode")).append("'");

scoreList=dao.getResultList(sb.toString());

}catch(Exception e){

e.printStackTrace();

}

return scoreList;

}

public List getMaxScore(Map scoreParam,String currId){

List scoreList=null;

Dao dao = new DaoImp();

try{

StringBuffer sb = new StringBuffer();

sb.append(" select folder_id, maxscore ")

.append ("from estimate_card_score ")

.append(" where 1=1 ")

.append(" and folder_id='").append(currId).append("'")

.append(" and rundate='").append(scoreParam.get("runDate")).append("'")

.append(" and org_id='").append(scoreParam.get("orgCode")).append("'");

scoreList=dao.getResultList(sb.toString());

}catch(Exception e){

e.printStackTrace();

}

return scoreList;

}

public List getChildren(String id){

List childrenList=null;

Dao dao = new DaoImp();

try{

StringBuffer sb = new StringBuffer();

sb.append("select folder_id, folder_name,par_folder_id,isleaf,dis_weight,weight ")

.append ("from estimate_group ")

.append(" where status ='1' ")

.append(" and par_folder_id='"+id+"'")

//.append(" and Start_Dt<'").append(currDate).append("'")

//.append(" and end_Dt>'").append(currDate).append("'")

.append(" order by start_dt");

childrenList=dao.getResultList(sb.toString());

}catch(Exception e){

e.printStackTrace();

}

return childrenList;

}

public List searchRuleGroupByGroupId(Map<String, Object> paramsMap){

List childrenList=null;

Dao dao = new DaoImp();

try{

StringBuffer sb = new StringBuffer();

sb.append("select er.folder_id , folder_name,er.check_id,sys_id,table_name,check_column_name,")

.append("er.start_dt,er.create_oper,rs.score")

.append(" from appdqc.estimate_checkid_relation er ")

.append(" left join appdqc.estimate_group  eg on er.folder_id = eg.folder_id")

.append(" left join appdqc.rule rg on rg.check_id = er.check_id ")

.append(" left join appdqc.rule_score  rs on er.check_id = rs.check_id")

.append(" where er.status='1' and er.folder_id='").append(paramsMap.get("nodeId")).append("'")

.append(" and rs.rundate= date '").append(paramsMap.get("check_date")).append("'")

.append(" and org_id='").append(paramsMap.get("branchId")).append("'")

/*.append(" and er.start_dt<'").append(currDate)

.append("' and er.end_dt >'").append(currDate).append("'")

.append(" and eg.start_dt<'").append(currDate)

.append("' and eg.end_dt >'").append(currDate).append("'")*/;

childrenList=dao.getResultList(sb.toString());

}catch(Exception e){

e.printStackTrace();

}

return childrenList;

}

public List searchBelowOrgRuleScore(Map<String, Object> paramsMap){

List ruleScoreList=null;

Dao dao = new DaoImp();

String inOrgs = derectBelowOrg((String)paramsMap.get("branchId"), dao);

try{

StringBuffer sb = new StringBuffer();

sb.append("select r.check_id , r.rule_name,r.sys_id,r.table_name,r.check_column_name,")

.append("rs.score,rs.rundate as rundate,branch_id,branch_name,ecr.weight,cr.pass_rate*100||'%' ")

.append(" from appdqc.rule_score rs ")

.append(" left join estimate_checkid_relation ecr on rs.check_id = ecr.check_id and ecr.status='1'")

.append(" left join check_result cr on rs.check_id = cr.check_id and rs.org_id = cr.org_id ")

.append(" left join appdqc.rule  r on rs.check_id = r.check_id")

.append(" left join bohz_appmds.tap_c_branch_all tb on rs.org_id = tb.branch_id ")

.append(" where rs.check_id='").append(paramsMap.get("nodeId")).append("'")

.append(" and cr.rundate= date '").append(paramsMap.get("check_date")).append("'")

.append(" and rs.rundate= date '").append(paramsMap.get("check_date")).append("'")

.append(" and cr.org_id in "+inOrgs)

.append(" and rs.org_id in "+inOrgs)

.append(" order by rs.org_id");

;

ruleScoreList=dao.getResultList(sb.toString());

}catch(Exception e){

e.printStackTrace();

}

return ruleScoreList;

}

public List searchBelowOrgGroupScore(Map<String, Object> paramsMap){

List childrenList=null;

Dao dao = new DaoImp();

String inOrgs = derectBelowOrg((String)paramsMap.get("branchId"), dao);

try{

StringBuffer sb = new StringBuffer();

sb.append("select org_id,branch_name as orgname,rundate,score,er.maxScore,")

.append("case when er.maxscore=0 then 0 else ( er.score*100/er.maxScore) end ||'%'  as scorepct")

.append(" from appdqc.estimate_card_score er ")

.append(" left join bohz_appmds.tap_c_branch_all cb on er.org_id = cb.branch_id")

.append(" left join estimate_group eg on er.folder_id = eg.folder_id and eg.status='1' ")

.append(" where er.folder_id='").append(paramsMap.get("nodeId")).append("'")

.append(" and rundate= date '").append(paramsMap.get("check_date")).append("'")

.append(" and org_id in"+inOrgs);

childrenList=dao.getResultList(sb.toString());

}catch(Exception e){

e.printStackTrace();

}

return childrenList;

}

//当前机构的直接下级

public String derectBelowOrg(String currOrg,Dao dao){

StringBuffer sb = new StringBuffer();

sb.append(" select a.branch_id as id " +

" from  bohz_appmds.tap_c_branch_all " +

" a where par_branch_id = '"+currOrg+"'");

List childOrgList = null;

try {

childOrgList = dao.getResultList(sb.toString());

} catch (Exception e) {

e.printStackTrace();

return "('')";

}

String orgInStr = CommonFunc.getInStr(childOrgList, "ID");

return orgInStr;

}

public List searchRuleWeightByGroupId(Map<String, Object> paramsMap){

List childrenList=null;

Dao dao = new DaoImp();

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String currDate = df.format(new Date());

try{

StringBuffer sb = new StringBuffer();

sb.append("select er.folder_id , folder_name,er.check_id,sys_id,table_name,check_column_name, ")

.append("er.start_dt,er.create_oper,er.weight,er.dis_weight,rg.rule_name ")

.append(" from appdqc.estimate_checkid_relation er ")

.append(" left join appdqc.estimate_group  eg on er.folder_id = eg.folder_id and eg.status='1' ")

.append(" left join appdqc.rule rg on rg.check_id = er.check_id ")

.append(" where er.status='1' and er.folder_id='").append(paramsMap.get("nodeId")).append("'");

childrenList=dao.getResultList(sb.toString());

}catch(Exception e){

e.printStackTrace();

}

return childrenList;

}

public int calCardScore(Map<String, Object> paramsMap){

Dao dao = new DaoImp();

String currDate = "2015-12-12 19:12:10";

try{

//规则组的设置内容太多,和显示树的主表进行了数据分离,处理时要单独进行

//1 先通过规则组的得分和权重,计算叶子评分组的得分

String sql = "(select group_Id from estimate_mapid_relation " +

"where "+currDate+" between start_dt and end_dt group by group_Id)tmp" +

"inner join map_score ms on tmp.map_id = ms.map_id";

List l = dao.getResultList(sql);

for(int i = 0;i<l.size();i++){//对每一个设置了规则组的叶子评分组进行加权计算,这里的评分组可能输来自于不同的评分卡

String groupId = (String)((Map)l.get(i)).get("GROUP_ID");

String groupId_rulegroup_sql = "select map_id,ESTIMATE_WEIGHT,ms.score from estimate_mapid_relation er" +

" inner join map_score ms on er.map_id = ms.map_id  " +

"where group_Id='"+groupId+"' " +

" and er.start_dt<date and er.ent_dt>date";

List ll = dao.getResultList(groupId_rulegroup_sql);

double gs = 0;

for(int ii = 0;ii<ll.size();ii++){//一个叶子评分组下设置的规则组

double s = Double.valueOf((String)((Map)ll.get(ii)).get("SCORE"));

double w = Double.valueOf((String)((Map)ll.get(ii)).get("ESTIMATE_WEIGHT"));

gs += s*w;

}

String insert = "insert into estimate_card_score " +

"select org,date,folder_id,par_folder_id,folder_name,"+gs +

"from estimate_card " +

" where folder_id='"+groupId+"'";

dao.insert(insert);

}

//2 在主表上,通过第一步得到的叶子评分组的score,计算树上每一个节点的score

String pfkSQL = "select folder_id from estimate_card where par_folder_id=1 and status=1 and folder_id=5";//得到所有的评分卡

List ll = dao.getResultList(pfkSQL);

for(int ii = 0;ii<ll.size();ii++){

String curr_folder_id = (String)((Map)ll.get(ii)).get("FOLDER_ID");

calNodeScore(curr_folder_id);

}

}catch(Exception e){

e.printStackTrace();

}finally{

dao.closeconn();

}

return 0;

}

/**

* 递归计算当前节点的得分=子节点的加权得分

* 前提是,当前节点不能即添加了评分组,又添加了规则组

* @param curr_folder_id

* @return

*/

public double calNodeScore(String curr_folder_id){

Dao dao = new DaoImp();

double gs = 0;//当前节点的加权结果,若当前节点包括叶子和非叶子节点

try{

String pfkSQL = "select folder_id,isleaf,weight,score from estimate_card where status=1 and par_folder_id="+curr_folder_id;

List ll = dao.getResultList(pfkSQL);

for(int ii = 0;ii<ll.size();ii++){//当前节点的子节点,包括可能的叶子节点和非叶子节点

String child_folder_id = (String)((Map)ll.get(ii)).get("FOLDER_ID");

String isleaf = (String)((Map)ll.get(ii)).get("ISLEAF");

String weight = (String)((Map)ll.get(ii)).get("WEIGHT");

if("0".equals(isleaf)){

//当前节点的非叶子子节点的计算结果

double ret = calNodeScore(child_folder_id);

//还要和兄弟节点的结果进行加权

gs += (ret*Double.valueOf(weight));

}else{

//当前节点的叶子子节点,得分从计算结果表里面获得

String leafScoreSQL = "select * from estimate_card_score ec inner join estimate_card on folder_id="+child_folder_id

+" and ec.org='340010001' and date='20150520'";

Map score = (Map)dao.getResultList(leafScoreSQL).get(0);//结果集理论只有一条

double s = Double.valueOf((String)(score.get("SCORE")));

double w = Double.valueOf((String)(score.get("WEIGHT")));

gs += s*w;

}

}

//插入当前节点的加权得分

String insert = "insert into estimate_card_score " +

"select org,date,folder_id,par_folder_id,folder_name,"+gs +

"from estimate_card " +

" where folder_id='"+curr_folder_id+"'";

dao.insert(insert);

}catch(Exception e){

e.printStackTrace();

}finally{

dao.closeconn();

}

return gs;

}

/**

* 递归计算当前节点的得分=子节点的加权得分

* 前提是,当前节点不能即添加了评分组,又添加了规则组

* @param curr_folder_id

* @return

*/

public double calNodeScore_bak(String curr_folder_id){

Dao dao = new DaoImp();

double gs = 0;//当前节点的加权结果,若当前节点包括叶子和非叶子节点

try{

String pfkSQL = "select folder_id,isleaf,weight,score from estimate_card where status=1 and par_folder_id="+curr_folder_id;

List ll = dao.getResultList(pfkSQL);

for(int ii = 0;ii<ll.size();ii++){//当前节点的子节点,包括可能的叶子节点和非叶子节点

String child_folder_id = (String)((Map)ll.get(ii)).get("FOLDER_ID");

String isleaf = (String)((Map)ll.get(ii)).get("ISLEAF");

String weight = (String)((Map)ll.get(ii)).get("WEIGHT");

if("0".equals(isleaf)){

//当前节点的非叶子子节点的计算结果

double ret = calNodeScore(child_folder_id);

//还要和兄弟节点的结果进行加权

gs += (ret*Double.valueOf(weight));

}else{

//当前节点的叶子子节点

double s = Double.valueOf((String)((Map)ll.get(ii)).get("SCORE"));

double w = Double.valueOf((String)((Map)ll.get(ii)).get("WEIGHT"));

gs += s*w;

}

}

//更新当前节点的加权得分

String update = "update estimate_card set score="+gs+" where folder_id="+curr_folder_id;

dao.update(update);

}catch(Exception e){

e.printStackTrace();

}finally{

dao.closeconn();

}

return gs;

}

//计算规则组的得分

public void calMapScore(String date,String org){

//map_score

Dao dao = new DaoImp();

try{

//评分卡的得分情况细化到农商行,83家

//得到当前机构的所有下级机构

StringBuffer sb = new StringBuffer();

sb.append(" WITH  recursive temporg(id,name,pid,leveld) as(")

    .append(" select a.branch_id,a.branch_name,a.par_branch_id,0 from  bohz_appmds.tap_c_branch_all  a where branch_id ='"+org+ "' union all")

    .append(" select a.branch_id,a.branch_name,a.par_branch_id,c.leveld+1 from bohz_appmds.tap_c_branch_all a ,temporg c where  c.id = a.par_branch_id and leveld<7)")

    .append(" select id from temporg");

List childOrgList = dao.getResultList(sb.toString());

String orgInStr = CommonFunc.getInStr(childOrgList, "ID");

//当前机构及其下属机构检查涉及的规则得分

String resultSQL = " select map_id ,sum(rett) as map_score from" +

"(select main.map_id,main.check_id, " +

"COALESCE(r.rule_weight,COALESCE(r.checkdimension,'100','0.2','200','0.2')) as weight,score,score*weight as rett " +

"from  " +

"(select map_id,check_id,sum(passcount)/sum(effect_count) as score " +

"from check_result " +

"where rundate='20151219' and org_id in"+orgInStr+" group by map_id,check_id) main" +

"inner join rule r on main.check_id = r.check_id" +

") tt group by map_id" ;

List resultList = dao.getResultList(resultSQL);

//将每一个规则组的得分,按照机构,日期的维度进行存储

for(int i=0;i<resultList.size();i++){//进入map_id

String currMapId = (String)((Map)resultList.get(i)).get("map_id");

String score = (String)((Map)resultList.get(i)).get("map_score");

String insertSQL = "insert into map_score values('20151219','"+currMapId+"','"+org+"',"+score+"')";

dao.insert(insertSQL);

}

}catch(Exception e){

e.printStackTrace();

}

}

}

猜你喜欢

转载自zengshaotao.iteye.com/blog/2327620