从中间表取数更新TW销售合同登记数据(含表体)

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.IXshtdjMaintain;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.vo.jych.oavo.OaXshtBVO;
import nc.vo.jych.oavo.OaXshtVO;
import nc.vo.jych.util.SqlBuilder;
import nc.vo.jych.xshtdj.AggXshtdjVO;
import nc.vo.jych.xshtdj.XshtdjBVO;
import nc.vo.jych.xshtdj.XshtdjVO;
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;
/**
 * @desc : 从中间表取数更新TW销售合同登记数据
 * @author hanh
 * @date : 2023-10-09
 */

public class OAXshtdjPlugin implements IBackgroundWorkPlugin{

	@Override
	public PreAlertObject executeTask(BgWorkingContext arg0) throws BusinessException {
		//抽取中间表数据
		generateZjXsht();
		return null;
	}
	
	//抽取中间表数据
	@SuppressWarnings("unchecked")
	private void generateZjXsht() throws BusinessException {
		//查询中间表中未执行的主表数据
		String sql = " select * from jych_zj_xsht where nvl(dr,0) = 0 and nvl(iszx,'N') != 'Y'  ";
		//volist存放中间表中未执行的主表数据
		List<OaXshtVO> volist = (List<OaXshtVO>) getDao().executeQuery(sql.toString(), new BeanListProcessor(OaXshtVO.class));
		if(volist == null || volist.size() < 1){
			return;
		}
		
		//idlist存放id+sjly
		List<String> idlist = new ArrayList<String>(); 
		//zjpklist存放中间表主表主键
		List<String> zjpklist = new ArrayList<String>();
		for (OaXshtVO vo : volist) {//给idlist和zjpklist赋值
			String id = vo.getId()+vo.getSjly();
			idlist.add(id);
			zjpklist.add(vo.getPk_xsht());
		}
		
		//根据中间表id+sjly,查询nc系统中已存在的主表主键
		SqlBuilder pksql = new SqlBuilder(); 
		pksql.append(" select pk_xshtdj from jych_xshtdj where nvl(dr,0) = 0 and ");
		pksql.appendNotIn(" def1||sjly ", idlist.toArray(new String[0]));
		List<String> pklist = (List<String>) getDao().executeQuery(pksql.toString(), new ColumnListProcessor());
		//Map 将已存在的AggVO放入vomap
		Map<String, AggXshtdjVO> vomap = new HashMap<String, AggXshtdjVO>();
		if(pklist != null && pklist.size() > 0){
			//根据已存在的主表主键获取已存在的AggVO
			BillQuery<AggXshtdjVO> billQuery = new BillQuery<AggXshtdjVO>(AggXshtdjVO.class);
			AggXshtdjVO[] aggvos = billQuery.query(pklist.toArray(new String[0]));
			for (AggXshtdjVO aggvo : aggvos) {//将aggvo对应的id和aggvo捆绑
				String wxtbs = aggvo.getParentVO().getDef1();
				String sjly = aggvo.getParentVO().getSjly();
				String id = wxtbs + sjly;
				vomap.put(id, aggvo);
			}
		}
		
		SqlBuilder sqlnameb = new SqlBuilder(); //查询中间表中主表所对应的子表数据
		sqlnameb.append(" select * from jych_zj_xsht_b where nvl(dr,0) = 0 and ");
		sqlnameb.append(" mainid||sjly ", idlist.toArray(new String[0]));
		//bvolist存放已存在主表对应的子表数据
		List<OaXshtBVO> bvolist = (List<OaXshtBVO>) getDao().executeQuery(sqlnameb.toString(), new BeanListProcessor(OaXshtBVO.class));
		//Map 已存在主表对应的子表数据放入bvomap
		Map<String, List<OaXshtBVO>> bvomap = new HashMap<String, List<OaXshtBVO>>();
		if(bvolist != null && bvolist.size() > 0){
			for (OaXshtBVO bvo : bvolist) {//将bvo对应的mainid和bvo捆绑
				String mainid = bvo.getMainid()+bvo.getSjly();
				if(bvomap.containsKey(mainid)){//如果bvomap存在该mainid,直接将该bvo和该mainid捆绑
					List<OaXshtBVO> list = bvomap.get(mainid);
					list.add(bvo);
					bvomap.put(mainid, list);
				} else {//如果bvomap不存在该mainid,则新建一个list,然后再将该bvo和该mainid捆绑
					List<OaXshtBVO> list = new ArrayList<OaXshtBVO>();
					list.add(bvo);
					bvomap.put(mainid, list);
				}
			}
		}
		
		//翻译OaXshtVO
		updateHeadVO(volist.toArray(new OaXshtVO[0])); 
		//insertlist存放执行插入的aggvo
		List<AggXshtdjVO> insertlist = new ArrayList<AggXshtdjVO>();
		//updatelist存放执行更新后的aggvo
		List<AggXshtdjVO> updatelist = new ArrayList<AggXshtdjVO>();
		//orgupdatelist存放执行更新前的aggvo	
		List<AggXshtdjVO> orgupdatelist = new ArrayList<AggXshtdjVO>();		
		for (OaXshtVO vo : volist) {//封装insertlist、updatelist、orgupdatelist
			if(vomap != null && vomap.size() > 0 && vomap.containsKey(vo.getId()+vo.getSjly())){ //修改
				AggXshtdjVO aggvo = vomap.get(vo.getId()+vo.getSjly());
				AggXshtdjVO clone = (AggXshtdjVO) aggvo.clone();
				XshtdjVO headvo = clone.getParentVO();
				headvo.setStatus(VOStatus.UPDATED);
				setHeadvo(headvo, vo);
				updatelist.add(clone);
				orgupdatelist.add(aggvo);
			} else { //新增
				AggXshtdjVO aggvo = new AggXshtdjVO();
				XshtdjVO headvo = new XshtdjVO();
				headvo.setStatus(VOStatus.NEW);
				headvo.setPk_billtype("XSHT");
				headvo.setTranstypecode("XSHT");
				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);
				if(bvomap != null && bvomap.size() > 0 && bvomap.containsKey(vo.getId()+vo.getSjly())){
					XshtdjBVO[] bvos = setbodyvo(bvomap.get(vo.getId()+vo.getSjly()));
					aggvo.setChildrenVO(bvos);
				}
				aggvo.setParentVO(headvo);
				insertlist.add(aggvo);
			}
		}
		if(insertlist != null && insertlist.size() > 0){//调新增接口
			AggXshtdjVO[] insertvos = getService().insert(insertlist.toArray(new AggXshtdjVO[0]), insertlist.toArray(new AggXshtdjVO[0]));
			List<AggXshtdjVO> insertvolist = new ArrayList<AggXshtdjVO>();
			for (AggXshtdjVO insertvo : insertvos) {
				AggXshtdjVO clone = (AggXshtdjVO) insertvo.clone();
				XshtdjVO vo = clone.getParentVO();
				vo.setVbillstatus(1);vo.setApprovedate(new UFDateTime());vo.setApprover(vo.getCreator());
				insertvolist.add(clone);
			}
			getService().approve(insertvolist.toArray(new AggXshtdjVO[0]), insertvos);
		}
		if(updatelist != null && updatelist.size() > 0){//调更新接口
			getService().update(updatelist.toArray(new AggXshtdjVO[0]), orgupdatelist.toArray(new AggXshtdjVO[0]));
		}
		
		SqlBuilder updatesql = new SqlBuilder(); //更新中间表中已执行数据的是否执行标识
		updatesql.append(" update jych_zj_xsht set iszx = 'Y' where ");
		updatesql.append(" pk_xsht ", zjpklist.toArray(new String[0]));
		getDao().executeUpdate(updatesql.toString());
	}
	
	private void updateHeadVO(OaXshtVO[] 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\")",
						//申请人
						"lastname->getColValue2(bd_psndoc,pk_psndoc,name,lastname,dr,0)",
						//申请部门
						"departmentname->getColValue2(org_dept,pk_dept,name,departmentname,dr,0)",
						//甲方
						"jfdwbm->getcolvalue2(bd_customer,pk_customer,name,jfdwbm,dr,0)",
						//乙方
						"partyb->getcolvalue2(bd_supplier,pk_supplier,name,\"北京城建智控科技股份有限公司\",dr,0)",
						//币种
						"bz->getcolvalue2(bd_currtype,pk_currtype,name,bz,dr,0)",
						//营销项目
						"xmbh->getColValue2(zkch_marketpro,pk_marketpro,marketprocode,xmbh,dr,0)",
				});
	}
	
	public void setHeadvo(XshtdjVO headvo, OaXshtVO vo){
		headvo.setDef1(vo.getId());
		//项目编号
		headvo.setPk_marketpro(vo.getXmbh());
		//合同编码
		headvo.setHtcode(vo.getHtbh());
		//合同名称
		headvo.setHtname(vo.getHtmc());
		//合同类型
		headvo.setHttype(vo.getHtlx());
		//投标类型
		headvo.setTbtype(vo.getTblx());
		//甲方
		headvo.setPartya(vo.getJfdwbm());
		headvo.setPartyb(vo.getPartyb());
		//合同金额(含税)
		headvo.setNmny(vo.getHtjkhs());
		//合同金额(不含税)
		headvo.setNtmny(vo.getHtjkbhs());
		//税率(%)
		headvo.setTaxrate(NullValueUtils.getNullStringValue(vo.getSl()));
		//税额
		headvo.setPaid(vo.getSe());
		//累计开票金额
		//				headvo.setLjkpmny(rs.getString(1));
		//累计回款金额
		//				headvo.setLjhkmny(rs.getString(1));
		//累计结算金额
		//				headvo.setLjnjsmny(rs.getString(1));
		//申请日期
		headvo.setSqdate(vo.getSqrq());
		//签约日期
		headvo.setQydate(vo.getQyrq());
		//归档日期
		headvo.setGddate(vo.getSjyyrq());
		//所属机构
		headvo.setSsjg(vo.getSzjg());
		//申请人
		headvo.setSqr(vo.getLastname());
		//申请部门
		headvo.setSqdept(vo.getDepartmentname());
		//是否关联交易方
		headvo.setIsgljyf(vo.getSfgljyf());
		//是否包含自研产品
		headvo.setIszycp(vo.getSfbhzycp());
		//合同开工日期
		headvo.setHtkgdate(vo.getHtkgrq());
		//合同竣工日期
		headvo.setHtjgdate(vo.getHtjgrq());
		//合同工期
		headvo.setHtgq(vo.getHtgq());
		//履约地点
		headvo.setLyadress(vo.getLydd());
		//币种
		headvo.setBz(vo.getBz());
		//汇率
		headvo.setHl(vo.getHl());
		//合同份数
		headvo.setHtfs(vo.getHtfs());
		//是否为公司标椎合同模板
		headvo.setIsgsbzmb(vo.getSfwgsbzhtmb());
		//合同章
		headvo.setHtz(vo.getHtz());
		//公章
		headvo.setGz(vo.getGz());
		//法人章
		headvo.setFrz(vo.getFrz());
		//总经理手签章
		headvo.setZjlsqz(vo.getZjlsqz());
		//福州分公司公章
		headvo.setFzfgsgz(vo.getFzfgsgz());
		//总经理印章
		headvo.setZjlyz(vo.getZjlyz());
		//备注说明
		headvo.setMemo(vo.getBzsm());
		headvo.setSjly(vo.getSjly());
	}
	
	public XshtdjBVO[] setbodyvo(List<OaXshtBVO> bvolist){
		XshtdjBVO[] bvos = new XshtdjBVO[bvolist.size()];
		for (int i = 0; i < bvos.length; i++) {
			OaXshtBVO bvo = bvolist.get(i);
			bvos[i] = new XshtdjBVO();
			bvos[i].setRowno(i+1+"0");
			//税率
			bvos[i].setTaxrate(bvo.getSl());
			//含税金额
			bvos[i].setNmny(bvo.getHsje());
			//不含税金额
			bvos[i].setNtmny(bvo.getBhsje());
			//合计税额
			bvos[i].setPaid(bvo.getSey());
			//合同事项
			bvos[i].setHtsx(bvo.getHtsx());
			bvos[i].setStatus(VOStatus.NEW);
		}
		return bvos;
	}
	
	private IXshtdjMaintain service;
	private IXshtdjMaintain getService() {
		if (service == null) {
			service = NCLocator.getInstance().lookup(IXshtdjMaintain.class);
		}
		return service;
	}
	
	private BaseDAO dao;
	private BaseDAO getDao(){
		if(dao == null){
			dao = new BaseDAO();
		}
		return dao;
	}

}

你可能感兴趣的:(nc积累,数据库,java)