case when then

select
  case when sum(case when c.ccus_code = :ccusCode then 1 else 0 end) > 0 then 1
when sum(case when c.ccus_Name = :ccusName then 1 else 0 end)  > 0 then 2
else 0 end rowCount
from cod_customer c
where c.CCUS_DEL_FLAG = 'N'
  <<and c.ccus_Id <> :ccusId>>
  and c.ccus_Org_Id = :ccusOrgId
  and  (c.ccus_Name = :ccusName
  or c.ccus_code = :ccusCode)


--------------------------------------

public CodCustomerModel save(CodCustomerModel model) {

final double checkResult = uniqueCheck(model);
if(checkResult == 0){
return this.dao.save(model);
}else if(checkResult == 1){
throw new ApplicationException("助记码重复");
}else if(checkResult == 2){
throw new ApplicationException("名称重复");
}else{
throw new ApplicationException("助记码或名称重复");
}
}

-----------------------------------------

private double uniqueCheck(CodCustomerModel model){
		
		CodCustomerUniqueCheckQueryCondition con = new CodCustomerUniqueCheckQueryCondition();
		con.setCcusId(model.getCcusId());
		con.setCcusOrgId(model.getCcusOrgId());
		con.setCcusCode(model.getCcusCode());
		con.setCcusName(model.getCcusName());
		
		List<CodCustomerUniqueCheckQueryItem> data = this.dao.query(con, CodCustomerUniqueCheckQueryItem.class);
		if(data.size() == 0){
			return 0;
		}
		return data.get(0).getRowcount();
	}

你可能感兴趣的:(java,oracle)