package nc.bs.backgroundtasks;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.pa.PreAlertObject;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
import nc.impl.pubapp.pattern.data.bill.BillQuery;
import nc.itf.jych.IPaymentorderMaintain;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.vo.jych.oavo.OaPaymentOrderVO;
import nc.vo.jych.paymentorder.AggPaymentOrderVO;
import nc.vo.jych.paymentorder.PaymentOrderVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVOUtil;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.wgbfb.tool.NullValueUtils;
import nc.vo.zkch.utils.SqlBuilder;
public class OAPaymentOrderPlugin implements IBackgroundWorkPlugin{
@Override
public PreAlertObject executeTask(BgWorkingContext arg0) throws BusinessException {
generatePaymentOrder();
return null;
}
@SuppressWarnings("unchecked")
private void generatePaymentOrder() throws BusinessException {
SqlBuilder sqlname = new SqlBuilder();
sqlname.append(" select * from jych_zj_fkd where nvl(dr,0) = 0 and nvl(iszx,'N') != 'Y' ");
List<OaPaymentOrderVO> volist = (List<OaPaymentOrderVO>) getDao().executeQuery(sqlname.toString(), new BeanListProcessor(OaPaymentOrderVO.class));
if(volist == null || volist.size() < 1){
return;
}
List<String> idlist = new ArrayList<String>();
List<String> zjpklist = new ArrayList<String>();
for (OaPaymentOrderVO vo : volist) {
String id = vo.getId() + vo.getSjly() + NullValueUtils.getNullStringValue(vo.getFklx());
idlist.add(id);
zjpklist.add(vo.getPk_fkd());
}
SqlBuilder pksql = new SqlBuilder();
pksql.append(" select pk_paymentorder from jych_paymentorder where nvl(dr,0) = 0 and ");
pksql.append(" wxtbs||sjly||fklx ", idlist.toArray(new String[0]));
List<String> pklist = (List<String>) getDao().executeQuery(pksql.toString(), new ColumnListProcessor());
Map<String, AggPaymentOrderVO> vomap = new HashMap<String, AggPaymentOrderVO>();
if(pklist != null && pklist.size() > 0){
BillQuery<AggPaymentOrderVO> billQuery = new BillQuery<AggPaymentOrderVO>(AggPaymentOrderVO.class);
AggPaymentOrderVO[] vos = billQuery.query(pklist.toArray(new String[0]));
for (AggPaymentOrderVO aggvo : vos) {
String wxtbs = aggvo.getParentVO().getWxtbs();
String sjly = aggvo.getParentVO().getSjly();
String fklx = aggvo.getParentVO().getFklx();
String id = wxtbs + sjly + fklx;
vomap.put(id, aggvo);
}
}
updateHeadVO(volist.toArray(new OaPaymentOrderVO[0]));
List<AggPaymentOrderVO> insertlist = new ArrayList<AggPaymentOrderVO>();
List<AggPaymentOrderVO> updatelist = new ArrayList<AggPaymentOrderVO>();
List<AggPaymentOrderVO> orgupdatelist = new ArrayList<AggPaymentOrderVO>();
for (OaPaymentOrderVO vo : volist) {
String id = vo.getId() + vo.getSjly() + NullValueUtils.getNullStringValue(vo.getFklx());
if(vomap != null && vomap.size() > 0 && vomap.containsKey(id)){
AggPaymentOrderVO aggvo = vomap.get(id);
AggPaymentOrderVO clone = (AggPaymentOrderVO) aggvo.clone();
PaymentOrderVO headvo = clone.getParentVO();
headvo.setStatus(VOStatus.UPDATED);
setHeadvo(headvo, vo);
updatelist.add(clone);
orgupdatelist.add(aggvo);
} else {
AggPaymentOrderVO aggvo = new AggPaymentOrderVO();
PaymentOrderVO headvo = new PaymentOrderVO();
headvo.setStatus(VOStatus.NEW);
headvo.setPk_billtype("XS01");
headvo.setTranstypecode("XS01-Cxx-01");
headvo.setPk_org(vo.getPk_org());
headvo.setPk_org_v(vo.getPk_org_v());
headvo.setPk_group(vo.getPk_group());
headvo.setPk_transtype(vo.getPk_transtype());
headvo.setDbilldate(new UFDate());
headvo.setVbillstatus(-1);
setHeadvo(headvo, vo);
aggvo.setParentVO(headvo);
insertlist.add(aggvo);
}
}
if(insertlist != null && insertlist.size() > 0){
AggPaymentOrderVO[] insertvos = getService().insert(insertlist.toArray(new AggPaymentOrderVO[0]), insertlist.toArray(new AggPaymentOrderVO[0]));
List<AggPaymentOrderVO> insertvolist = new ArrayList<AggPaymentOrderVO>();
for (AggPaymentOrderVO insertvo : insertvos) {
AggPaymentOrderVO clone = (AggPaymentOrderVO) insertvo.clone();
PaymentOrderVO vo = clone.getParentVO();
vo.setVbillstatus(1);vo.setApprovedate(new UFDateTime());vo.setApprover(vo.getCreator());
insertvolist.add(clone);
}
getService().approve(insertvolist.toArray(new AggPaymentOrderVO[0]), insertvos);
}
if(updatelist != null && updatelist.size() > 0){
getService().update(updatelist.toArray(new AggPaymentOrderVO[0]), orgupdatelist.toArray(new AggPaymentOrderVO[0]));
}
SqlBuilder updatesql = new SqlBuilder();
updatesql.append(" update jych_zj_fkd set iszx = 'Y' where ");
updatesql.append(" pk_fkd ", zjpklist.toArray(new String[0]));
getDao().executeUpdate(updatesql.toString());
}
private void updateHeadVO(OaPaymentOrderVO[] headvos){
SuperVOUtil.execFormulaWithVOs(headvos,new String[] {
"pk_org->getcolvalue2(org_orgs,pk_org,code,\"05\",islastversion,\"Y\")",
"pk_org_v->getcolvalue(org_orgs,pk_vid,pk_org,pk_org)",
"pk_group->getcolvalue(org_group,pk_group,code,\"01\")",
"def1->fkxs",
"def2->lastname",
"lastname->getColValue2(bd_psndoc,pk_psndoc,name,lastname,dr,0)",
"def3->htbh",
"htbh->getColValue(view_jych_contract,pk,htbh,htbh)",
"def4->departmentname",
"departmentname->getColValue2(org_dept,pk_dept,name,departmentname,dr,0)",
"def5->xmbh",
"xmbh->getColValue2(zkch_marketpro,pk_marketpro,marketprocode,xmbh,dr,0)"
});
}
public void setHeadvo(PaymentOrderVO headvo, OaPaymentOrderVO vo){
headvo.setWxtbs(vo.getId());
headvo.setSqrq(vo.getSqrq());
headvo.setSsjg(vo.getSzjg());
headvo.setSqr(vo.getLastname());
headvo.setSqbm(vo.getDepartmentname());
headvo.setZfddh(vo.getZfddh());
headvo.setXmbh(vo.getXmbh());
headvo.setHtbh(vo.getHtbh());
headvo.setFksy(vo.getFksy());
headvo.setBcfkmny(vo.getBcfkjey());
headvo.setBcsjzf(vo.getBcsjzf());
headvo.setYingfmny(vo.getYfkje());
headvo.setFkxs(vo.getFkxs());
headvo.setIscxyfk(vo.getSfcxyfk());
headvo.setGddate(vo.getLastoperatedate());
headvo.setCnspdate(vo.getOperatedate());
headvo.setFklx(vo.getFklx());
headvo.setSjly(vo.getSjly());
headvo.setSfhjk(vo.getSfhjk());
headvo.setCxje(vo.getCxje());
headvo.setDef1(vo.getDef1());
headvo.setDef2(vo.getDef2());
headvo.setDef3(vo.getDef3());
headvo.setDef4(vo.getDef4());
headvo.setDef5(vo.getDef5());
}
private IPaymentorderMaintain service;
private IPaymentorderMaintain getService() {
if (service == null) {
service = NCLocator.getInstance().lookup(
IPaymentorderMaintain.class);
}
return service;
}
private BaseDAO dao;
private BaseDAO getDao(){
if(dao == null){
dao = new BaseDAO();
}
return dao;
}
}