`

多个对象的某个字段多选的增加删除

 
阅读更多
public void getAddAwardList(){
		
		model.setFisrtRow((model.getPage() - 1) * model.getRows());
		
		model.setLastRow(model.getRows());
		
		try {
			BaseDao dao=getDao();
			
			List<YouthModel> list = dao.selectForList("youth.getAddAwardList", model);
			HashMap<String ,Object> hm = new HashMap<String ,Object>();
			hm.put("total", dao.selectByCount("youth.getListCount", model));
			hm.put("rows", list);
			hm.put("success", true);
			this.doResponseWithJson(hm);
		} catch (Exception e) {
			e.printStackTrace();
			this.setData("操作失败");
			this.setSuccess(false);
			this.send();
		}
		
	}
	
/**
 * 
 * @param sqlString 传进来的是以逗号分隔的字符串如:1,2,3
 * @param modelString 传进来是model中的单个字符串,如:4
 * @return
 */
	private String removeSameValue(String sqlString, String modelString) {
		if (StringUtil.isEmpty(sqlString)) {
			return modelString;
		}
		String[] t = sqlString.split(",");

		TreeSet<String> tSet = new TreeSet<String>();
		for (int i = 0; i < t.length; i++) {
			tSet.add(t[i]);
		}
		tSet.add(modelString);
		String s = "";
		for (Iterator<String> it = tSet.iterator(); it.hasNext();) {

			s += it.next().toString() + ",";
		}
		s = s.substring(0, s.length() - 1);

		return s;
	}
	 

public void addAwardConfig(){
		
		try {
			BaseDao dao=getDao();
			//从界面获得所有需要的青年id
			String id = model.getId();
			//用in 查找出()所有id的信息  select * from ybc_youth where id in ($VALUE$)
			List<YouthModel>  retModel = dao.selectForList("youth.getAddAward", id);
			//实例青年
			YouthModel tModel = new YouthModel();
			//创建青年list
			List<YouthModel> sqlModelList = new ArrayList<YouthModel>();
			//遍历青年,得到去重后的奖项id
			for (int i=0; i<retModel.size(); i++) {
				//将数据库信息赋给实例
				tModel = retModel.get(i);
				//数据库的奖项id和界面的到的奖项id去重
				tModel.setYouthAward(this.removeSameValue(tModel.getYouthAward(), model.getYouthAward()));
				//将得到的奖项id放入list
				sqlModelList.add(tModel);
			}
			//批量更新
			dao.updateSql("youth.upt_youth", sqlModelList);
			
			this.setData("青年奖增加项配置成功");
			this.setSuccess(true);
			this.send();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.setData("青年奖项增加配置失败,错误信息:"+e.getMessage());
			this.setSuccess(false);
			this.send();
		}
	}

//减掉重复项
private String removeList(String sqlString,String modelString){
	
	String[] sqlStr = sqlString.split(",");
	String[] modelStr = modelString.split(",");
	
	List<String> listA= java.util.Arrays.asList(sqlStr);
	List<String> listB= java.util.Arrays.asList(modelStr);
	
	Set setA = new TreeSet(listA);
	setA.removeAll(listB);		
	System.out.println(setA);
	
	String s = "";
	for (Iterator<String> it = setA.iterator(); it.hasNext();) {
		s += it.next().toString() + ",";
	}
	s = s.substring(0, s.length() - 1);
	return s;
}


public void removeAwardConfig(){
	try{
		BaseDao dao=getDao();
		
		String id =model.getId();
		List<YouthModel>  retModel = dao.selectForList("youth.getAddAward", id);
			System.out.println(retModel.size());
		YouthModel tModel = new YouthModel();
		List<YouthModel> sqlModelList = new ArrayList<YouthModel>();
		
		for (int i=0; i<retModel.size(); i++) {
			//将数据库信息赋给实例
			tModel = retModel.get(i);
			//数据库的奖项id和界面的到的奖项id去重
			tModel.setYouthAward(this.removeList(tModel.getYouthAward(), model.getYouthAward())); 
			//将得到的奖项id放入list
			sqlModelList.add(tModel);
		}
		dao.updateSql("youth.upt_youth", sqlModelList);
		
		this.setData("青年奖项删除配置成功");
		this.setSuccess(true);
		this.send();
	}catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		this.setData("青年奖项配置删除失败,错误信息:"+e.getMessage());
		this.setSuccess(false);
		this.send();
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics