查詢時若時間為空,開始時間取今天的零點,結束時間取當前時間

ZHOU_VIP發表於2018-09-29
/**
 * 查詢使用者維護終端列表
 * 
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value="/dev/queryinstalleddevbyuser", method=RequestMethod.GET)
@ResponseBody
public APIResponse queryinstalleddevbyuser(HttpServletRequest request) throws Exception {
	String tokenStr = request.getParameter("token");
	//0表示查詢所有維護操作
	String opflag = request.getParameter("opflag");
	String batch = request.getParameter("batch");
	String offset = request.getParameter("offset");
	batch = (batch == null || batch.trim().equals("")) ? "5000" : batch.trim();
	offset = (offset == null || offset.trim().equals("")) ? "0" : offset.trim();
	opflag = (opflag == null || opflag.trim().equals("")) ? "0" : opflag.trim();
	//開始時間
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
	SimpleDateFormat sdfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
	Date dt = new Date();
	String starttime = request.getParameter("starttime");
	if(starttime ==null || starttime.trim().equals("") ){
		starttime = sdf.format(dt) + " "+"00:00:00";
	}
	String endtime = request.getParameter("endtime");
	if(endtime ==null || endtime.trim().equals("") ){
		endtime = sdfm.format(dt);
	}
	logger.info("querydevcomplete: token="+tokenStr);
	APIResponse apiRsp = this.checkApiToken(tokenStr);
	if(apiRsp.getResult() != APIResponse.RET_CODE_OK){
		return apiRsp;
	}
	User user = (User)(apiRsp.getData());
	boolean hasOpRight = this.extAuthService.checkOpRight(user.getUSERID(), Constants.OPCODE_OSM_DEV_QRY);
	if(hasOpRight == false){
		return APIResponse.error("許可權不足,裝置查詢失敗!");
	}
	try {
		List<OsmDevAndOplog> devList = null;
		if(user != null){
			DevQueryPara para = new DevQueryPara();
			if("1".equals(user.getUSERID())){//admin賬戶不過濾
				para.setUserid("");
			}else{
				para.setUserid(user.getUSERID());
			}
			//0表示查詢所有維護操作,1表示查最新的一條資料
			if("1".equals(opflag)){
				para.setBeginDate(DateUtil.parseDatetime(starttime));
				para.setEndDate(DateUtil.parseDatetime(endtime));
				para.setBatch(Integer.parseInt(batch));
				para.setOffset(Integer.parseInt(offset));
				devList = this.devService.queryOsmDevAndOplogByParaNew(para);
			}else{
				//預設0表示查詢所有維護操作
				para.setBeginDate(DateUtil.parseDatetime(starttime));
				para.setEndDate(DateUtil.parseDatetime(endtime));
				para.setBatch(Integer.parseInt(batch));
				para.setOffset(Integer.parseInt(offset));
				devList = this.devService.queryOsmDevAndOplogByPara(para);
			}
		}
		return APIResponse.ok(devList);
	}catch(Exception ex){
		logger.error("querydevcomplete failed!", ex);
		return APIResponse.error(ex.getMessage());
	}
}

後來改成當前時間的前七天

/**
 * 查詢使用者維護終端列表
 * 
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value="/dev/queryinstalleddevbyuser", method=RequestMethod.GET)
@ResponseBody
public APIResponse queryinstalleddevbyuser(HttpServletRequest request) throws Exception {
	String tokenStr = request.getParameter("token");
	//0表示查詢所有維護操作
	String opflag = request.getParameter("opflag");
	String batch = request.getParameter("batch");
	String offset = request.getParameter("offset");
	batch = (batch == null || batch.trim().equals("")) ? "5000" : batch.trim();
	offset = (offset == null || offset.trim().equals("")) ? "0" : offset.trim();
	opflag = (opflag == null || opflag.trim().equals("")) ? "0" : opflag.trim();
	//開始時間
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
	SimpleDateFormat sdfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
	String starttime = request.getParameter("starttime");
	if(starttime ==null || starttime.trim().equals("") ){
		starttime = sdf.format(new Date(System.currentTimeMillis() - 7*24*3600*1000L)) + " "+"00:00:00";
	}
	String endtime = request.getParameter("endtime");
	if(endtime ==null || endtime.trim().equals("") ){
		endtime = sdfm.format(new Date());
	}
	logger.info("querydevcomplete: token="+tokenStr);
	APIResponse apiRsp = this.checkApiToken(tokenStr);
	if(apiRsp.getResult() != APIResponse.RET_CODE_OK){
		return apiRsp;
	}
	User user = (User)(apiRsp.getData());
	boolean hasOpRight = this.extAuthService.checkOpRight(user.getUSERID(), Constants.OPCODE_OSM_DEV_QRY);
	if(hasOpRight == false){
		return APIResponse.error("許可權不足,裝置查詢失敗!");
	}
	try {
		List<OsmDevAndOplog> devList = null;
		if(user != null){
			DevQueryPara para = new DevQueryPara();
			if("1".equals(user.getUSERID())){//admin賬戶不過濾
				para.setUserid("");
			}else{
				para.setUserid(user.getUSERID());
			}
			//0表示查詢所有維護操作,1表示查最新的一條資料
			if("1".equals(opflag)){
				para.setBeginDate(DateUtil.parseDatetime(starttime));
				para.setEndDate(DateUtil.parseDatetime(endtime));
				para.setBatch(Integer.parseInt(batch));
				para.setOffset(Integer.parseInt(offset));
				devList = this.devService.queryOsmDevAndOplogByParaNew(para);
			}else{
				//預設0表示查詢所有維護操作
				para.setBeginDate(DateUtil.parseDatetime(starttime));
				para.setEndDate(DateUtil.parseDatetime(endtime));
				para.setBatch(Integer.parseInt(batch));
				para.setOffset(Integer.parseInt(offset));
				devList = this.devService.queryOsmDevAndOplogByPara(para);
			}
		}
		return APIResponse.ok(devList);
	}catch(Exception ex){
		logger.error("querydevcomplete failed!", ex);
		return APIResponse.error(ex.getMessage());
	}
}

 

相關文章