|  | @@ -0,0 +1,169 @@
 | 
	
		
			
				|  |  | +package fi.cas.task;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import com.alibaba.druid.support.logging.Log;
 | 
	
		
			
				|  |  | +import com.alibaba.druid.support.logging.LogFactory;
 | 
	
		
			
				|  |  | +import fi.cas.opplugin.PayBillToolUtil;
 | 
	
		
			
				|  |  | +import kd.bos.context.RequestContext;
 | 
	
		
			
				|  |  | +import kd.bos.dataentity.entity.DynamicObject;
 | 
	
		
			
				|  |  | +import kd.bos.dataentity.entity.DynamicObjectCollection;
 | 
	
		
			
				|  |  | +import kd.bos.exception.KDException;
 | 
	
		
			
				|  |  | +import kd.bos.orm.query.QCP;
 | 
	
		
			
				|  |  | +import kd.bos.orm.query.QFilter;
 | 
	
		
			
				|  |  | +import kd.bos.schedule.executor.AbstractTask;
 | 
	
		
			
				|  |  | +import kd.bos.servicehelper.BusinessDataServiceHelper;
 | 
	
		
			
				|  |  | +import kd.bos.servicehelper.operation.SaveServiceHelper;
 | 
	
		
			
				|  |  | +import org.json.JSONArray;
 | 
	
		
			
				|  |  | +import org.json.JSONObject;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import java.util.HashMap;
 | 
	
		
			
				|  |  | +import java.util.Map;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + * 定时任务调用查询表头状态接口。如果是R。查询次数5以内,则查询次数加一。5以上,清空查询次数,改核心状态为支付处理失败D,
 | 
	
		
			
				|  |  | + * 记录处理失败原因。非R,更新表头核心状态,调用查询分录接口,更新分录支付状态,表头状态会改为B部分成功或Q全部成功
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +public class AgentPayQueryStatusTast extends AbstractTask {
 | 
	
		
			
				|  |  | +    private static final Log log = LogFactory.getLog(AgentPayQueryStatusTast.class);
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
 | 
	
		
			
				|  |  | +        //获取要查询的数据。状态为已审批,支付状态为支付中。核心状态为支付信息登记成功
 | 
	
		
			
				|  |  | +        QFilter filter = new QFilter("billstatus", QCP.equals, "C");
 | 
	
		
			
				|  |  | +        filter.and("nckd_paystatus", QCP.equals, "B");
 | 
	
		
			
				|  |  | +        filter.and("nckd_hxzt", QCP.equals, "R");
 | 
	
		
			
				|  |  | +        Map<Object, DynamicObject> payinfos = BusinessDataServiceHelper.loadFromCache("cas_agentpaybill",new QFilter[] {filter});
 | 
	
		
			
				|  |  | +        DynamicObject jkpzxx = BusinessDataServiceHelper.loadSingle("nckd_jkpzxx",new QFilter[]{new QFilter("number","=","paytozfqz")});
 | 
	
		
			
				|  |  | +        String servername =  jkpzxx.getString("nckd_servername");
 | 
	
		
			
				|  |  | +        String port =  jkpzxx.getString("nckd_port");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        for (DynamicObject dataEntity : payinfos.values()) {
 | 
	
		
			
				|  |  | +            //调用接口查询付款状态
 | 
	
		
			
				|  |  | +            String bodyxml = PayBillToolUtil.createSingleAgentPaymentRequest_5(dataEntity);
 | 
	
		
			
				|  |  | +            String billNum = dataEntity.getString("billno");
 | 
	
		
			
				|  |  | +            log.info("单据号:"+billNum+"传入参数"+bodyxml);
 | 
	
		
			
				|  |  | +            JSONObject cbsReturnJson =PayBillToolUtil.socketServiceAgent(servername,port,bodyxml,"ACHRMSGQ");
 | 
	
		
			
				|  |  | +            log.info("单据号:"+billNum+"返回参数"+cbsReturnJson.toString());
 | 
	
		
			
				|  |  | +            //存入日志表
 | 
	
		
			
				|  |  | +            PayBillToolUtil.saveAgentlog(dataEntity,bodyxml,cbsReturnJson.toString(),"6");
 | 
	
		
			
				|  |  | +            if(cbsReturnJson==null||cbsReturnJson.get("Message")==null){
 | 
	
		
			
				|  |  | +            }else{
 | 
	
		
			
				|  |  | +                JSONObject headinfo = cbsReturnJson.getJSONObject("Message").getJSONObject("HeadInfo");
 | 
	
		
			
				|  |  | +                if(headinfo!=null){
 | 
	
		
			
				|  |  | +                    String errcode =headinfo.getString("errorcode");
 | 
	
		
			
				|  |  | +                    if ("000000".equals(errcode)) {//已查询到,不代表支付成功
 | 
	
		
			
				|  |  | +                        JSONObject bodyInfo = cbsReturnJson.getJSONObject("Message").getJSONObject("BodyInfo");
 | 
	
		
			
				|  |  | +                        JSONObject datainfo =null;
 | 
	
		
			
				|  |  | +                        if(bodyInfo.get("dataList") instanceof  JSONObject){
 | 
	
		
			
				|  |  | +                            datainfo = bodyInfo.getJSONObject("dataList");
 | 
	
		
			
				|  |  | +                        }else if(bodyInfo.get("dataList") instanceof  JSONArray){
 | 
	
		
			
				|  |  | +                            datainfo = bodyInfo.getJSONArray("dataList").getJSONObject(0);
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                        //判断是不是R
 | 
	
		
			
				|  |  | +                        String returnstatus = datainfo.getString("Status");
 | 
	
		
			
				|  |  | +                        String payExecFlag = datainfo.getString("PayExecFlag");
 | 
	
		
			
				|  |  | +                        if("R".equals(returnstatus)){
 | 
	
		
			
				|  |  | +                            //判断查询次数
 | 
	
		
			
				|  |  | +                            int nckd_hxztcxcs = dataEntity.getInt("nckd_hxztcxcs");//查询次数
 | 
	
		
			
				|  |  | +                            if(nckd_hxztcxcs<5){//查询次数5以内,则查询次数加一
 | 
	
		
			
				|  |  | +                                dataEntity.set("nckd_hxztcxcs",nckd_hxztcxcs++);
 | 
	
		
			
				|  |  | +                            }else{//5以上,清空查询次数,改核心状态为支付处理失败D
 | 
	
		
			
				|  |  | +                                dataEntity.set("nckd_hxztcxcs",0);
 | 
	
		
			
				|  |  | +                                dataEntity.set("nckd_hxzt","D");
 | 
	
		
			
				|  |  | +                                dataEntity.set("nckd_hxsbyy","执行失败,请联系管理人员查看原因");
 | 
	
		
			
				|  |  | +                            }
 | 
	
		
			
				|  |  | +                            SaveServiceHelper.update(new DynamicObject[]{dataEntity});
 | 
	
		
			
				|  |  | +                        }else{
 | 
	
		
			
				|  |  | +                            if("Q".equals(payExecFlag)){//全部成功,不调用分录查询,直接改为成功
 | 
	
		
			
				|  |  | +                                dataEntity.set("nckd_hxzt","Q");//核心状态
 | 
	
		
			
				|  |  | +                                dataEntity.set("nckd_hxsbyy","支付全部成功");
 | 
	
		
			
				|  |  | +                                DynamicObjectCollection coll = dataEntity.getDynamicObjectCollection("entry");
 | 
	
		
			
				|  |  | +                                for (int i = 0; i <coll.size() ; i++) {
 | 
	
		
			
				|  |  | +                                    coll.get(i).set("e_issuccess",true);//分录是否成功
 | 
	
		
			
				|  |  | +                                    coll.get(i).set("bankreturnmsg","成功");//核心返回信息
 | 
	
		
			
				|  |  | +                                }
 | 
	
		
			
				|  |  | +                                SaveServiceHelper.update(new DynamicObject[]{dataEntity});
 | 
	
		
			
				|  |  | +                            }else if("B".equals(payExecFlag)){//部分成功
 | 
	
		
			
				|  |  | +                                dataEntity.set("nckd_hxzt","B");//核心状态
 | 
	
		
			
				|  |  | +                                int SuccNum = datainfo.getInt("SuccNum");//成功条数
 | 
	
		
			
				|  |  | +                                int FailNum = datainfo.getInt("FailNum");//失败条数
 | 
	
		
			
				|  |  | +                                dataEntity.set("nckd_hxsbyy","支付部分成功,成功"+SuccNum+"条,失败"+FailNum+"条");
 | 
	
		
			
				|  |  | +                                //调用分录查询
 | 
	
		
			
				|  |  | +                                Map<String,String[]> entryqueryMap = new HashMap<>();
 | 
	
		
			
				|  |  | +                                DynamicObject payeracctbankEntity = BusinessDataServiceHelper.loadSingleFromCache(dataEntity.getDynamicObject("payeracctbank").getPkValue(), "am_accountbank");
 | 
	
		
			
				|  |  | +                                String jgh = payeracctbankEntity.getString("nckd_jgh");
 | 
	
		
			
				|  |  | +                                if("".equals(jgh)){
 | 
	
		
			
				|  |  | +                                    jgh = "791100";
 | 
	
		
			
				|  |  | +                                }
 | 
	
		
			
				|  |  | +                                String filename=dataEntity.getString("nckd_filename");
 | 
	
		
			
				|  |  | +                                if("".equals(filename)){
 | 
	
		
			
				|  |  | +                                    filename = dataEntity.getString("billNo");
 | 
	
		
			
				|  |  | +                                }
 | 
	
		
			
				|  |  | +                                filename=jgh+filename;//机构号
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                                String bodyxml_entryquery = PayBillToolUtil.createSingleAgentPaymentRequest_6(dataEntity,filename);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                                log.info("单据号明细分录查询:"+billNum+"传入参数"+bodyxml_entryquery);
 | 
	
		
			
				|  |  | +                                JSONObject cbsReturnJson_entryquery =PayBillToolUtil.socketServiceAgent(servername,port,bodyxml_entryquery,"ACHRDTLQ");
 | 
	
		
			
				|  |  | +                                log.info("单据号明细分录查询:"+billNum+"返回参数"+cbsReturnJson_entryquery.toString());
 | 
	
		
			
				|  |  | +                                //存入日志表
 | 
	
		
			
				|  |  | +                                PayBillToolUtil.saveAgentlog(dataEntity,bodyxml_entryquery,cbsReturnJson_entryquery.toString(),"7");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                                if(cbsReturnJson_entryquery==null||cbsReturnJson_entryquery.get("Message")==null){
 | 
	
		
			
				|  |  | +                                }else{
 | 
	
		
			
				|  |  | +                                    JSONObject headinfo_entryquery = cbsReturnJson_entryquery.getJSONObject("Message").getJSONObject("HeadInfo");
 | 
	
		
			
				|  |  | +                                    if(headinfo_entryquery!=null) {
 | 
	
		
			
				|  |  | +                                        String errcode_entryquery =headinfo_entryquery.getString("errorcode");
 | 
	
		
			
				|  |  | +                                        if ("000000".equals(errcode_entryquery)) {//已查询到,不代表支付成功
 | 
	
		
			
				|  |  | +                                            JSONObject bodyInfo_entryquery = cbsReturnJson_entryquery.getJSONObject("Message").getJSONObject("BodyInfo");
 | 
	
		
			
				|  |  | +                                            JSONArray datainfo_entryqueryArray =null;
 | 
	
		
			
				|  |  | +                                            if( bodyInfo_entryquery.get("dataList") instanceof  JSONObject){
 | 
	
		
			
				|  |  | +                                                datainfo_entryqueryArray = new JSONArray();
 | 
	
		
			
				|  |  | +                                                datainfo_entryqueryArray.put(bodyInfo_entryquery.getJSONObject("dataList"));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                                            }else if(bodyInfo_entryquery.get("dataList") instanceof  JSONArray){
 | 
	
		
			
				|  |  | +                                                datainfo_entryqueryArray = bodyInfo_entryquery.getJSONArray("dataList");
 | 
	
		
			
				|  |  | +                                            }
 | 
	
		
			
				|  |  | +                                            for (int i = 0; i < datainfo_entryqueryArray.length(); i++) {
 | 
	
		
			
				|  |  | +                                                JSONObject datainfo_entryquery = datainfo_entryqueryArray.getJSONObject(i);
 | 
	
		
			
				|  |  | +                                                String inRichNbr = datainfo_entryquery.get("InRichNbr")+"";
 | 
	
		
			
				|  |  | +                                                //判断是不是成功
 | 
	
		
			
				|  |  | +                                                String returnstatus_entryquery = datainfo_entryquery.getString("Status");
 | 
	
		
			
				|  |  | +                                                if("C".equals(returnstatus_entryquery)){//成功
 | 
	
		
			
				|  |  | +                                                    entryqueryMap.put(inRichNbr,new String[]{"true","成功"});
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                                                }else{//失败
 | 
	
		
			
				|  |  | +                                                    String returnstatus_MsgVar = datainfo_entryquery.getString("MsgVar");
 | 
	
		
			
				|  |  | +                                                    if("".equals(returnstatus_MsgVar)){
 | 
	
		
			
				|  |  | +                                                        returnstatus_MsgVar="执行失败";
 | 
	
		
			
				|  |  | +                                                    }
 | 
	
		
			
				|  |  | +                                                    entryqueryMap.put(inRichNbr,new String[]{"false",returnstatus_MsgVar});
 | 
	
		
			
				|  |  | +                                                }
 | 
	
		
			
				|  |  | +                                            }
 | 
	
		
			
				|  |  | +                                        }
 | 
	
		
			
				|  |  | +                                    }
 | 
	
		
			
				|  |  | +                                }
 | 
	
		
			
				|  |  | +                                DynamicObjectCollection coll = dataEntity.getDynamicObjectCollection("entry");
 | 
	
		
			
				|  |  | +                                for (int i = 0; i <coll.size() ; i++) {
 | 
	
		
			
				|  |  | +                                    String inRichNbr=coll.get(i).getString("payeeacctbank");
 | 
	
		
			
				|  |  | +                                    if(!"成功".equals(coll.get(i).getString("bankreturnmsg"))){//失败的
 | 
	
		
			
				|  |  | +                                        if(entryqueryMap.get(inRichNbr)!=null){
 | 
	
		
			
				|  |  | +                                          String[] entryreturnStr =   entryqueryMap.get(inRichNbr);
 | 
	
		
			
				|  |  | +                                          if("true".equals(entryreturnStr[0])){
 | 
	
		
			
				|  |  | +                                              coll.get(i).set("e_issuccess",true);//分录是否成功
 | 
	
		
			
				|  |  | +                                              coll.get(i).set("bankreturnmsg","成功");//核心返回信息
 | 
	
		
			
				|  |  | +                                          }else{
 | 
	
		
			
				|  |  | +                                              coll.get(i).set("e_issuccess",false);//分录是否成功
 | 
	
		
			
				|  |  | +                                              coll.get(i).set("bankreturnmsg",entryreturnStr[1]);//核心返回信息
 | 
	
		
			
				|  |  | +                                          }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                                        }
 | 
	
		
			
				|  |  | +                                    }
 | 
	
		
			
				|  |  | +                                }
 | 
	
		
			
				|  |  | +                                SaveServiceHelper.update(new DynamicObject[]{dataEntity});
 | 
	
		
			
				|  |  | +                            }
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |