NC65在日常開發中常用的程式碼寫法
阿新 • • 發佈:2021-01-07
標題 | NC65開發相關程式碼
---|---
版本 | 1.0.1
作者 | walton
說明 | 收集NC在日常開發中常用的程式碼寫法,示例展示
### 1.查詢
1.1 通過BaseDAO查詢結果集並轉換
```java
//通過BaseDAO進行查詢,並將查詢結果轉換為列表
SqlBuilder sql = new SqlBuilder();
sql.append(" select ");
sql.append(" account.code,accasoa.name,accasoa.name2,accasoa.name3,accasoa.name4,accasoa.name5,accasoa.name6,account.balanorient,accasoa.pk_accasoa,accasoa.endflag");
sql.append(" from BD_ACCASOA accasoa");
sql.append(" inner join BD_ACCOUNT account on accasoa.PK_ACCOUNT = account.PK_ACCOUNT ");
if(subjType != null && subjType.equals(1)){// 查詢往來類科目
sql.append(" and account.TRAFFICSIGNS = 'Y' ");
}
sql.append(" inner join BD_ACCCHART accchart on accasoa.PK_ACCCHART = accchart.PK_ACCCHART ");
sql.append(" inner join BD_ACCTYPE acctype on account.PK_ACCTYPE = acctype.PK_ACCTYPE ");
if(subjType != null && subjType.equals(2)){ // 查詢損益類科目
sql.append(" and acctype.TYPE = 1 ");
}
sql.append(" inner join ORG_ACCOUNTINGBOOK book on accchart.PK_ACCCHART = book.PK_CURRACCCHART and book.PK_ACCOUNTINGBOOK =?");
SQLParameter param = new SQLParameter();
param.addParam(pk_accountbook);
// BeanListProcessor 的引數VO不需要元資料,保證Sql查詢出的列名與欄位名一致即可(處理器可在同包下檢視)
List result = (List)this.getBaseDAO().executeQuery(sql.toString(), param, new BeanListProcessor(DiffAccasoaVO.class));
return result;
```
1.2 通過VOQuery查詢單表物件
```java
//查詢條件以and開頭,(String)null引數是order的語句,可為空
SqlBuilder sql = new SqlBuilder();
sql.append(" and ");
sql.append(ICPubMetaNameConst.PK_ORG, pk_org);
sql.append(" and ");
sql.append(ICPubMetaNameConst.CMATERIALVID,cmaterialvids);
VOQuery query = new VOQuery(OnhandDimVO.class);
query.query(sql.toString(), (String)null);
```
1.3 通過EfficientBillQuery查詢聚合物件
```java
SqlBuilder headCond = new SqlBuilder();
headCond.append(" from drugic_priceadjust_h where ");
String pkGroup = (String) bgwc.getKeyMap().get("pk_group");
headCond.append("pk_group", pkGroup);
headCond.append(" and ");
String pkOrg = (String) bgwc.getKeyMap().get("pk_org");
headCond.append("pk_org", pkOrg);
headCond.append(" and ");
headCond.append("approvestatus", BillStatusEnum.APPROVED.valueIndex());
headCond.append(" and ");
headCond.append("dr", 0);
EfficientBillQuery query = new EfficientBillQuery(AggPriceAdjustHeadVO.class);
AggPriceAdjustHeadVO[] vos = (AggPriceAdjustHeadVO[]) query.query(headCond.toString());
```
1.4 通過IUAPQueryBS查詢結果集
```java
IUAPQueryBS iquery = (IUAPQueryBS)NCLocator.getInstance().lookup(IUAPQueryBS.class);
ArrayList list = null;
String sql =" select pk_dept from bd_psnjob where ismainjob ='Y' and pk_psndoc ='"+pk_psndoc+"' and dr = 0 ";
list = (ArrayList) iquery.executeQuery(sql, new ArrayListProcessor());
```
1.5 通過IOnhandQry查詢現存量資訊
```java
OnhandQryCond onhandQryCond = new OnhandQryCond();//構造查詢條件
if(cond.getByBatch()){//可以根據條件來選擇要查詢的現存量維度欄位
onhandQryCond.addAllSelectFields();
}else{
onhandQryCond.addSelectFields(new String[]{"pk_group", "pk_org",
"cwarehouseid", "cmaterialvid", "cmaterialoid", "castunitid",
"clocationid", "vchangerate",
"cvmivenderid", "ctplcustomerid", "cstateid", "cvendorid",
"cprojectid", "casscustid", "cproductorid", "cffileid", "vfree1",
"vfree2", "vfree3", "vfree4", "vfree5", "vfree6", "vfree7",
"vfree8", "vfree9", "vfree10"});
}
onhandQryCond.setISSum(true);//設定現存量是否求和
StringBuilder sbSelFields = new StringBuilder();
sbSelFields.append(OnhandDimVO.ALIASNAME + ".").append(ICPubMetaNameConst.CWAREHOUSEID).append("='")
.append(cond.getInvWh()).append("' AND ");// 倉庫
sbSelFields.append(OnhandDimVO.ALIASNAME + ".").append(ICPubMetaNameConst.PK_ORG).append("='")
.append(cond.getPk_org()).append("' ");// 庫存組織
if (!cond.getZeroOnhand()) {//新增現存量是否需要大於0的查詢條件
sbSelFields.append(" AND (").append(OnhandNumVO.TABLENAME).append(".nonhandnum <> 0 or ")
.append(OnhandNumVO.TABLENAME).append(".nonhandastnum <> 0 ) ");
}
onhandQryCond.setWhere(sbSelFields.toString());
onhandQryCond.setBinnerWhere(true);
try {
OnhandVO[] onhandVOs = NCLocator.getInstance().lookup(IOnhandQry.class).queryOnhand(onhandQryCond);
if (ValueCheckUtil.isNullORZeroLength(onhandVOs)) {
return null;
}
return onhandVOs;//返回查詢到的現存量資訊
} catch (BusinessException e) {
ExceptionUtils.wrappException(e);
}
```
1.6 客戶端Client端查詢(不建議使用)
```java
SqlBuilder whereSql = new SqlBuilder();
whereSql.append(MaintensDrugVO.PK_STOCK,this.m_sWhID);
whereSql.append(" and ");
whereSql.append(MaintensDrugVO.PRODUCTNAME, CollectionUtils.setToArray(set));
MaintensDrugVO[] MaintensDrugVOs = (MaintensDrugVO[])HYPubBO_Client.queryByCondition(MaintensDrugVO.class,whereSql.toString());
```
### 2.更新
2.1 通過VOUpdate更新單表物件
```java
//VO中的status必須設定為更新態 ,可以更新指定欄位
VOUpdate updateOnhand = new VOUpdate();
onhandNum.setStatus(VOStatus.UPDATED);
updateOnhand.update(onhandNumVOs, new String[]{"nnum1"});
```
2.1 通過BillUpdate更新單據(聚合物件)
```java
BillUpdate billUpd = new BillUpdate<>();
billUpd.update(new AggPriceAdjustHeadVO[]{aggVO},new AggPriceAdjustHeadVO[]{originBill});
```
2.1 通過IOnhandUpdate更新現存量
```java
IOnhandUpdate update = NCLocator.getInstance().lookup(IOnhandUpdate.class);
update.modifyOnhandNum(onhandVOs);
```
### 3.介面操作
3.1 根據model獲取當前選中的表頭表體資料
```java
AggPriceAdjustHeadVO aggVO = (AggPriceAdjustHeadVO)this.model.getSelectedData();
PriceAdjustHeadVO parentVO = aggVO.getParentVO();
CircularlyAccessibleValueObject[] childVOs = aggVO.getChildrenVO();
```
3.2 根據model獲取當前選中資料的審批狀態
```java
if(null != getModel().getSelectedData()){
NCObject obj = NCObject.newInstance(getModel().getSelectedData());
if (obj != null) {
Integer status = nc.bs.pubapp.pf.util.ApproveFlowUtil.getBillStatus(obj);
}
}
```
3.3 根據條件控制按鈕是否可操作性
```java
//按鈕的Action中,繼承NCAction並重寫isActionEnable方法是控制按鈕是否可操作的方法,預設返回true(可以操作)
@Override
protected boolean isActionEnable() {return true}
//----------------------------------------------------------------------
//點選新增,自動增行(在新增事件--addhandler實現implements IAppEventHandler)
public void handleAppEvent(AddEvent e) {
BillCardPanel panel = e.getBillForm().getBillCardPanel();
Action action = panel.getBodyPanel().getBillTableAction(0);
if (action instanceof AbstractBodyTableExtendAction) {
AbstractBodyTableExtendAction addAction = (AbstractBodyTableExtendAction)action;
ActionEvent ae = null;
ae = new ActionEvent(panel.getBodyPanel().getTable(), 1234, "AutoLine");
addAction.actionPerformed(ae);
}
}
```
3.4 根據BillCardPanel動態設定表頭表體欄位是否為必輸項(固定則在單據模板中配置)
```java
getBillCardPanel().getHeadItem(DrugBDInfoVO.PRICE_SALE).setNull(true);
getBillCardPanel().getBodyItem("code").setNull(true);
```
3.5 根據BillCardPanel獲取當前頁籤及其billModel
```java
String tableCode = getBillCardPanel().getCurrentBodyTableCode();
BillModel billModel = getBillCardPanel().getBillModel(tableCode);
```
3.6 根據BillCardPanel獲取介面的聚合物件,返回AggregatedValueObject
```java
需要傳入三個VO的名字,返回值型別可強制轉換
AggregatedValueObject bill = e.getBillCardPanel().getBillValueVO(aggClassName, headClassName, bodyClassName);
```
3.7 根據BillListPanel或者BillCardPanel處理介面精度
```java
//列表介面(在介面InitUI中加入)
ListPaneScaleProcessor scaleProcessor = new ListPaneScaleProcessor(AppContext.getInstance().getPkGroup(),this.getBillListPanel());
String[] pricekeys = new String[]{"localdebitamount","localcreditamount","debitamount","creditamount"};
PosEnum pos = PosEnum.head;
scaleProcessor.setPriceCtlInfo(pricekeys, pos, (String)null, "pk_currtype", pos, (String)null);
scaleProcessor.process();
//卡片介面(在介面InitUI中加入)
CardPaneScaleProcessor scaleProcessor = new CardPaneScaleProcessor(AppContext.getInstance().getPkGroup(),this.getBillCardPanel());
String[] pricekeys = new String[]{"amount"};
PosEnum pos = PosEnum.body;
scaleProcessor.setPriceCtlInfo(pricekeys, pos, (String)null, "currtype", pos, (String)null);
scaleProcessor.process();
```
3.8 根據BillCardPanel設定表體資料及載入參照顯示資訊(不載入則顯示主鍵)
```java
DiffAdjustVO headVO = ((AggDiffAdjustVO)agg).getParentVO();
DiffAdjustBodyVO bodyVO = new DiffAdjustBodyVO();
bodyVO.setPk_org(headVO.getPk_org());
bodyVO.setPk_accountbook(headVO.getPk_accountingbook());
bodyVO.setPk_voucher(headVO.getPk_voucher());
bodyVO.setPk_detail(headVO.getPk_detail());
bodyVO.setPk_accasoa(headVO.getPk_accasoa());
int row = getCardPanel().getBillModel().getRowCount()-1;
this.getCardPanel().getBillModel().setBodyRowVO(bodyVO, row);
this.getCardPanel().getBillModel().loadLoadRelationItemValue();
```
3.9 禁止列表表頭排序
```java
//重寫列表介面的初始化方法,移除監聽
nc.ui.pubapp.uif2app.view.ShowUpableBillListView
public void initUI() {
super.initUI();
getBillListPanel().getHeadTable().removeSortListener();
}
```
3.10 根據BillCardPanel控制某個單元格是否可編輯
```java
getBillCardPanel().getBillModel().setCellEditable(int row,String key,Boolean editable);
```
3.11 獲取當前螢幕尺寸
```java
Dimension screen=Toolkit.getDefaultToolkit.getScreenSize();
```
### 4.視窗提示
4.1 介面彈窗(成功、失敗、警告等)
```java
//彈框提示,可以選擇多種,根據自己的需求選擇(提示介面標誌flag:{是:4,否:8,取消:2})
nc.ui.pub.beans.MessageDialog.showWarningDlg(null, "選擇提示", "請選擇單據後再點選執行");
```
4.2 介面底部提示資訊
```java
//在介面底部提示資訊
ShowStatusBarMsgUtil.showStatusBarMsg("執行成功!", getModel().getContext());
//在介面底部彈出異常資訊,中斷當前操作(會出現失效情況,可配合上面提示資訊使用,終止操作)
nc.vo.pubapp.pattern.exception.ExceptionUtils.wrappBusinessException("表體資料為空時,不能儲存。");
```
4.3 自定義檔案編碼和名稱的唯一性校驗
```java
//可以在[基礎資料管控模式]節點,配置規則,如果無法新增規則,可在資料庫中插入資料
select * from bd_uniquerule ;
select * from bd_uniquerule_item;
可以在[基礎資料管控模式]節點中新增一條規則點選儲存後,然後在此方法中進行斷點除錯,獲取需要插入的VO資訊
nc.impl.bd.config.uniquerule.BDUniqueruleManageServiceImpl.insert(BDUniqueruleVO)
```
### 5.單據模板公式
5.1 單據模板根據公式獲取值
```java
//公式可以直接獲取返回值並賦值給下一條語句
pk_onhandim->getcolvalue(IC_ONHANDDIM ,PK_ONHANDDIM ,CMATERIALVID ,code_pd.pk_source );
num1->getcolvalue(IC_ONHANDNUM ,NNUM1 ,PK_ONHANDDIM ,pk_onhandim );//(表名,查詢欄位,條件欄位,條件值)
price->getcolvalue(drugic_priceadjust_b ,price_before ,pk_priceadjust_b ,pk_priceadjust_b );
iif(price>0 ,price ,num1 );
```
### 6.參照
6.1 根據BillCardPanel獲取介面參照型別欄位並設定是否可多選
```java
BillItem item = e.getBillCardPanel().getBodyItem(e.getKey());
if (item == null)
return;
JComponent componen = item.getComponent();
if (componen instanceof UIRefPane){
UIRefPane refPane = (UIRefPane) componen;
refPane.setMultiSelectedEnabled(true);
}
```
6.2 獲取參照的值
```java
UIRefPane measdocRef = (UIRefPane) getBillCardPanel().getHeadItem(MaterialVO.PK_MEASDOC).getComponent();
measdocRef.getRefPK(); //主鍵
measdocRef.getRefName(); //名稱
measdocRef.getRefCode(); //編碼
```
6.3 設定參照過濾條件
```java
//可以新增在編輯前事件中
UIRefPane refPane=(UIRefPane)billForm.getBillCardPanel().getHeadItem("def2").getComponent();
AbstractRefModel refModel=(AbstractRefModel) refPane.getRefModel();
String wherePart=" and def5='1001H2100000004MJ7SV' ";//Sql以and開頭
refModel.addWherePart(wherePart, false);//第二個引數為是否重新整理該資料
refPane.setMultiCorpRef(false);//設定不能跨組織
```
6.4 參照選擇後顯示主鍵或編碼,不顯示名稱
```java
- 進入單據模板初始化節點,選擇對應節點的模板並點選修改
- 進入節點模板,點選要顯示名稱的參照欄位,在左側介面選擇高階屬性
- 選中‘型別設定’行,雙擊值的位置會有一個參照按鈕,點選參照會彈出一個介面
- 在介面中勾選‘焦點離開後顯示名稱’
```
### 7.其他
7.1 查詢多語資訊
```
在NCHOME中找到langlib資料夾,內部有多語jar;
根據所屬模組,在功能註冊節點中找到對應的編碼(如庫存管理為ic);
根據模組編碼找到對應的多語jar;
根據程式碼中的resdir資訊,找到jar中對應的檔案