1. 程式人生 > >NC57 定時任務例子

NC57 定時任務例子

package nc.imp.hn.wl.wl.yundan;

import java.util.ArrayList;
import java.util.HashMap;

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.hn.wl.pub.SuperDMO;
import nc.bs.pub.pf.PfUtilBO;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
import nc.itf.uap.busibean.ISysInitQry;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.vo.bd.b08.CustBasVO;
import nc.vo.hn.hn303007.WAYBILLVO;
import nc.vo.hn.hn303010.AggSlArrivivalVO;
import nc.vo.hn.hn303010.SlArrivivalVO;
import nc.vo.hn.hnh506.ChannelVO;
import nc.vo.hn.wl.wl.yunhao.YunDanHaoVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pub.para.SysInitVO;
/**
 * 運單同步
 * 
 * <p>
 * 建立人: liljb 
 * 建立日期: 2014-9-16
 * @version 1.0
 * @since NC5.7
 */
@SuppressWarnings({ "deprecation", "unchecked" })
public class YunDanSys implements IBackgroundWorkPlugin {
	private StringBuilder rwrz = new StringBuilder();// 任務日誌
	private SuperDMO wldao;
	private BaseDAO dao;

	public String executeTask(BgWorkingContext bgwc) throws BusinessException {
		ChannelVO[] channelDVO = (ChannelVO[]) getDao().retrieveByClause(ChannelVO.class, "ileveltype='2' and iproducttype ='3'").toArray(new ChannelVO[0]);
		if (channelDVO == null || channelDVO.length == 0) {
			return "沒有對應的渠道 YunDanSys 34line";
		}
		ChannelVO channelSDVO = channelDVO[0];

		if (channelSDVO.getPk_correscorp() == null) {
			return "##" + channelSDVO.getVlevelcode() + "##的 pk_correscorp 為空";
		}
		ISysInitQry sysinitQry = NCLocator.getInstance().lookup(ISysInitQry.class);
		SysInitVO sysvo = sysinitQry.queryByParaCode(channelSDVO.getPk_correscorp(), "XHFXWL001");
		if (sysvo == null || sysvo.getValue().equals("N")) {
			return "XHFXWL001 為空";
		}
		// 每次處理的條數
		int forinti = 1;
		// 讀取到貨登記運號資訊表 (物流方寫入)
		String sql = "select * from T_WL_YH where RECEFLAG='0' and sendflag='0'";
		sql += " and ROWNUM<= " + forinti + "";
		// sql += " and yh='008561'";
		ArrayList<YunDanHaoVO> ydhvolist = (ArrayList<YunDanHaoVO>) getWLDao().executeQuery(sql, new BeanListProcessor(YunDanHaoVO.class));
		rwrz.append("讀取到貨登記運號資訊表:" + sql + "####");

		if (ydhvolist.size() > 0) {
			UFDate currenttime = new UFDate();
			StringBuffer strbufferDH = new StringBuffer();
			StringBuffer strbufferXT = new StringBuffer();
			StringBuffer strbf = new StringBuffer();
			//到貨 DH 和銷退 XT 分開 
			for (YunDanHaoVO temp : ydhvolist) {
				if (temp.getDJLX().equals("DH")) {
					strbufferDH.append("'" + temp.getFLOWID_TLDJHZ() + "',");
				}
				if (temp.getDJLX().equals("XT")) {
					strbufferXT.append("'" + temp.getFLOWID_TLDJHZ() + "',");
				}
				strbf.append("'" + temp.getSL_GYS_CODE() + "',");
			}
			String custsql = "select * from bd_cubasdoc where pk_cubasdoc in(" + strbf.toString().substring(0, strbf.length() - 1) + ")";
			ArrayList<CustBasVO> custbasvo = (ArrayList<CustBasVO>) getDao().executeQuery(custsql, new BeanListProcessor(CustBasVO.class));
			if (!(custbasvo.size() > 0)) {
				return rwrz + "sql:" + custsql + "返回結果為空";
			}
			// 客戶PK ,名字  
			HashMap<String, String> custmap = new HashMap<String, String>();
			for (CustBasVO temp : custbasvo) {
				custmap.put(temp.getPk_cubasdoc(), temp.getCustname());
			}
			
			ArrayList<WAYBILLVO> xtlist = new ArrayList<WAYBILLVO>();
			for (YunDanHaoVO temp : ydhvolist) {
				// 構造運單管理表
				WAYBILLVO wayvo = new WAYBILLVO();
				wayvo.setSl_gys_code(temp.getSL_GYS_CODE());
				wayvo.setPk_corp(channelSDVO.getPk_corp());
				wayvo.setVbillcode(temp.getYH());// 運單號
				if (temp.getDJLX().equals("DH")) {
					wayvo.setSendtype(1);//到貨
				} else if (temp.getDJLX().equals("XT")) {
					wayvo.setSendtype(2);// 銷退
				}
				wayvo.setSendname(custmap.get(temp.getSL_GYS_CODE()));// 發貨方名稱
				wayvo.setVbaono(temp.getBJSL());// 包數
				// TODO 件數的取值需要確認
				wayvo.setVjianno(new UFDouble(0));// 件數
				wayvo.setVperson(temp.getSL_YWBM_CODE());// 貨主
				wayvo.setDbilldate(currenttime);// 運單日期
				wayvo.setTlrq(temp.getTLRQ1());// 運單登記日期
				wayvo.setWl_yh_pk(temp.getFLOWID_TLDJHZ().toString());// 物流運單主鍵
				wayvo.setYdlx(temp.getDJLX());
				getDao().insertVO(wayvo);
				rwrz.append("構造運單管理表運號:" + temp.getYH());
				// 銷退到貨單,自動匹配上然後審批發送物流
				if (wayvo.getYdlx().equals("XT")) {
					xtlist.add(wayvo);
				}
			}
			if (strbufferDH.length() != 0) {
				// 讀取完後回寫已經讀取過的物流運單資訊表中的接收標誌RECEFLAG欄位,設定為‘1’
				String wlsql = "update T_WL_YH set RECEFLAG='1' where FLOWID_TLDJHZ in(" + strbufferDH.toString().substring(0, strbufferDH.length() - 1) + ")";
				getWLDao().executeUpdate(wlsql);
				rwrz.append("####回寫T_wl_yh 標記 dh :" + wlsql);
			}
			// 銷退到貨單新需求:自動匹配上然後審批發送物流
			if (xtlist.size() != 0) {
				for (WAYBILLVO vo : xtlist) {
					String vbillno = vo.getVbillcode();// 運單號
					String sl_gys_code = vo.getSl_gys_code();// 供應商基本pk(在這裡是客戶)
					String sql1 = "select * from hn_slarrivival t where t.vbillstatus='8' and nvl(t.dr,0)=0 and t.pk_supplier in(select pk_channel from bd_channel a where a.pk_corrbasdoc='" + sl_gys_code + "' and nvl(a.dr,0)=0) and pk_waybill ='" + vbillno + "'";
					ArrayList<SlArrivivalVO> slvolist = (ArrayList<SlArrivivalVO>) getDao().executeQuery(sql1, new BeanListProcessor(SlArrivivalVO.class));
					rwrz.append("####找銷退到貨單並稽核" + sql1);
					ReCalcuTHLX cal = new ReCalcuTHLX();
					RecalcuThlxByydh call = new RecalcuThlxByydh();
					if (slvolist.size() != 0) {
						// 再次計算退貨流向
						for (SlArrivivalVO slArrivivalVO : slvolist) {
							// cal.calcuxtthlx(slArrivivalVO);
							try {
								call.calcthlx(slArrivivalVO.getPk_waybill(), sl_gys_code);
							} catch (Exception e) {
								throw new BusinessException(e.getMessage());
							}
						}

						for (SlArrivivalVO vo1 : slvolist) {
							AggSlArrivivalVO agg1 = new AggSlArrivivalVO();
							vo1.setVapproveid(vo1.getVoperatorid());
							vo1.setTlrq(vo.getTlrq().getDate());// 運單登記日期
							getDao().updateVO(vo1);
							agg1.setParentVO(vo1);
							try {
								getPfutilbo().processAction("APPROVE", "TN1B", new UFDate().toString(), null, agg1, null);
								String wlsqlXT = "update T_WL_YH set RECEFLAG='1' where FLOWID_TLDJHZ in('" + vo.getWl_yh_pk() + "')";
								getWLDao().executeUpdate(wlsqlXT);
							} catch (Exception e) {
								e.printStackTrace();
								throw new BusinessException(e);
							}
						}
					}
					// 找不到銷退到貨單,更新RECEFLAG=5
					else {
						String wlsqlXT = "update T_WL_YH set RECEFLAG='5' where FLOWID_TLDJHZ in('" + vo.getWl_yh_pk() + "')";
						getWLDao().executeUpdate(wlsqlXT);
						rwrz.append("####找不到銷退到貨單" + wlsqlXT);
					}
				}
			}
			// System.out.println("運單號同步成功" + ydhvolist.size() + "條");
		}
		// TODO 需要找人確認這個 2 是什麼意思 李林君2014年9月16日 19:29:25
		String sql1 = "select * from T_WL_YH where SENDERRFLAG='2'";
		ArrayList<YunDanHaoVO> ydhvolist1 = (ArrayList<YunDanHaoVO>) getWLDao().executeQuery(sql1, new BeanListProcessor(YunDanHaoVO.class));
		if (ydhvolist1.size() > 0) {
			for (YunDanHaoVO delvo : ydhvolist1) {
				String sqldel = "delete from hn_waybill where vbillcode='" + delvo.getYH() + "' and wl_yh_pk='" + delvo.getFLOWID_TLDJHZ() + "'";
				getDao().executeUpdate(sqldel);
				rwrz.append("####end" + sqldel);
			}
		}
		return rwrz.toString();
	}

	private SuperDMO getWLDao() {
		if (wldao == null) {
			wldao = new SuperDMO();
		}
		return wldao;
	}

	private BaseDAO getDao() {
		if (dao == null) {
			dao = new BaseDAO();
		}
		return dao;
	}

	private PfUtilBO pfutilbo;

	private PfUtilBO getPfutilbo() {
		if (pfutilbo == null) {
			pfutilbo = new PfUtilBO();
		}
		return pfutilbo;
	}
}