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;
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' ";
List<OaXshtVO> volist = (List<OaXshtVO>) getDao().executeQuery(sql.toString(), new BeanListProcessor(OaXshtVO.class));
if(volist == null || volist.size() < 1){
return;
}
List<String> idlist = new ArrayList<String>();
List<String> zjpklist = new ArrayList<String>();
for (OaXshtVO vo : volist) {
String id = vo.getId()+vo.getSjly();
idlist.add(id);
zjpklist.add(vo.getPk_xsht());
}
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<String, AggXshtdjVO> vomap = new HashMap<String, AggXshtdjVO>();
if(pklist != null && pklist.size() > 0){
BillQuery<AggXshtdjVO> billQuery = new BillQuery<AggXshtdjVO>(AggXshtdjVO.class);
AggXshtdjVO[] aggvos = billQuery.query(pklist.toArray(new String[0]));
for (AggXshtdjVO aggvo : aggvos) {
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]));
List<OaXshtBVO> bvolist = (List<OaXshtBVO>) getDao().executeQuery(sqlnameb.toString(), new BeanListProcessor(OaXshtBVO.class));
Map<String, List<OaXshtBVO>> bvomap = new HashMap<String, List<OaXshtBVO>>();
if(bvolist != null && bvolist.size() > 0){
for (OaXshtBVO bvo : bvolist) {
String mainid = bvo.getMainid()+bvo.getSjly();
if(bvomap.containsKey(mainid)){
List<OaXshtBVO> list = bvomap.get(mainid);
list.add(bvo);
bvomap.put(mainid, list);
} else {
List<OaXshtBVO> list = new ArrayList<OaXshtBVO>();
list.add(bvo);
bvomap.put(mainid, list);
}
}
}
updateHeadVO(volist.toArray(new OaXshtVO[0]));
List<AggXshtdjVO> insertlist = new ArrayList<AggXshtdjVO>();
List<AggXshtdjVO> updatelist = new ArrayList<AggXshtdjVO>();
List<AggXshtdjVO> orgupdatelist = new ArrayList<AggXshtdjVO>();
for (OaXshtVO vo : volist) {
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.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;
}
}