* modify Condition Item and Detail
* @param request request
* @return IResult
*/
public IResult saveConditionDtl(IRequest request) {
AjaxFormResult result = new AjaxFormResult();
String type = request.get("type");
String divCtNo = request.get("divCtNo");
//Factor DETAIL
String[] ftGridData = request.getParamsAsStringArray("grid[]");
DivCtItm ctItm = conditionMntService.findByDivCtItmNo(divCtNo);
if ("A".equals(type)) {
if (ctItm != null) {
// conditionMnt.0001 條件代碼重覆!
throw new CapMessageException(
CapAppContext.getMessage("condition.0001"), getClass());
}
ctItm = new DivCtItm();
} else {
if (ctItm != null && !ctItm.getOid().equals(request.get("oid"))) {
// conditionMnt.0001 條件代碼重覆!
throw new CapMessageException(
CapAppContext.getMessage("condition.0001"), getClass());
} else if (ctItm == null && !CapString.isEmpty(request.get("oid"))) {
ctItm = conditionMntService.getById(request.get("oid"));
} else if (ctItm == null){
ctItm = new DivCtItm();
}
}
CapBeanUtil.map2Bean(request, ctItm);
if ("A".equals(type)) {
ctItm.setOid(null);
}
List<DivCtDtl> delCtDtl = new ArrayList<DivCtDtl>();
if(ftGridData!=null){
List<DivCtDtl> ctDtls = new LinkedList<DivCtDtl>();
if(ctItm.getDivCtDtls()!=null&&!ctItm.getDivCtDtls().isEmpty()){
delCtDtl = ctItm.getDivCtDtls();
}
for(int i = 0; i<ftGridData.length; i++){
JSONObject jsData = JSONObject.fromObject(ftGridData[i]);
DivCtDtl ctDtl = new DivCtDtl();
// int j = 0;
// boolean haveOld = false;
// if(!CapString.isEmpty(jsData.getString("oid")) && !ctItm.getDivCtDtls().isEmpty()){
// for(DivCtDtl ctDtl2 : ctDtls){
// j++;
// if(ctDtl2.getOid().equals(jsData.opt("oid"))){
// ctDtl = ctDtl2;
// haveOld = true;
// break;
// }
// }
// }
CapBeanUtil.map2Bean(jsData, ctDtl);
ctDtl.setDivCtNo(ctItm.getDivCtNo());
ctDtl.setDivCtSor(new BigDecimal(i+1));
ctDtl.setRangeNo(jsData.optString("rangeNo"));
DivFtDtl ftDtl = factorMntService.findByFactorNoAndRangeNo(
jsData.optString("factorNo"), jsData.optString("rangeNo"));
ctDtl.setDivFtDtl(ftDtl);
ctDtl.setDivCtItm(ctItm);
// if(haveOld){
// ctDtls.set(j, ctDtl);
// }else{
ctDtls.add(ctDtl);
// }
}
ctItm.setDivCtDtls(ctDtls);
}
String userId = CapSecurityContext.getUserId();
if(userId.length()>6)userId = userId.substring(0,6);
ctItm.setUpdater(userId);
ctItm.setUpdateTime(CapDate.getCurrentTimestamp());
ctItm.setDivCtTyp("C");
conditionMntService.saveDivCtItm(ctItm);
if(!delCtDtl.isEmpty())
conditionMntService.deleteCtDtlByList(delCtDtl);
result.set(Constants.AJAX_NOTIFY_MESSAGE, CapAppContext.getMessage("condition.0002"));
return result;
}