/**
 * Company Name : 中贸促信息技术有限责任公司
 * Project Name:project
 * File Name:LogInfoService.java
 * Package Name:ccpit.base.logManage
 * Date:2015年10月19日上午10:18:26
 * Copyright (c) 2015, dingwei@ccpit.org All Rights Reserved.
 *
*/

package org.ccpit.base.logManage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.ccpit.base.controller.BaseController;
import org.ccpit.base.controller.Convert;
import org.ccpit.base.controller.Page;
import org.ccpit.base.controller.PageBo;
import org.ccpit.base.controller.PageRequest;
import org.ccpit.base.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * ClassName:LogInfoService <br/>
 * Function: TODO 日志信息管理. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2015年10月19日 上午10:18:26 <br/>
 * @author   dingwei
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Repository
@Transactional
public class LogInfoService {
	
	private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Autowired
	private LogInfoDao logInfoDao;
	/**
	 * 
	 * recordLog:(记录用户操作日志信息). <br/>
	 * TODO(这里描述这个方法适用条件 – 可选).<br/>
	 * TODO(这里描述这个方法的执行流程 – 可选).<br/>
	 * TODO(这里描述这个方法的使用方法 – 可选).<br/>
	 * TODO(这里描述这个方法的注意事项 – 可选).<br/>
	 *
	 * @author dingwei
	 * @param request	 
	 * @param operation  操作名称
	 * @since JDK 1.6
	 */
	public void recordLog(HttpServletRequest request,String operation){
		User user = (User) request.getSession().getAttribute("user_in_session");
			LogInfo logInfo = new LogInfo();
			logInfo.setOperateName(operation);
			logInfo.setOperateTime(new Date());
			logInfo.setOperator(user.getUsername());
			logInfo.setOperatorIp(BaseController.getRequestIp(request));
			logInfoDao.save(logInfo);
	}
	
	public boolean addLogInfo(LogInfo logInfo){
		return logInfoDao.save(logInfo);
	}
	
	public boolean delLogInfoById(long id){
		return logInfoDao.deleteById(id);
	}
	
	public Page<LogInfo> queryLinkInfos(PageRequest pageRequest){
		String hql = "from LogInfo order by operateTime desc";
		return logInfoDao.findPage(pageRequest, hql, null);
	}
	
	 public Map<String,Object> convertToMap(LogInfo LogInfo){
        Map<String,Object> map = new HashMap<String, Object>();
        if (LogInfo==null){
        	return map;
        }
        map.put("id",LogInfo.getId());
        map.put("operateName", LogInfo.getOperateName());
        map.put("operateTime",null == LogInfo.getOperateTime()? "": sdf.format(LogInfo.getOperateTime()));
        map.put("operator", LogInfo.getOperator());
        map.put("operatorIp",LogInfo.getOperatorIp());
        return map;
    }

    public PageBo<LogInfo> convert(Page<LogInfo> page){
        return new PageBo<LogInfo>(page, new Convert<LogInfo>() {
            @Override
            public Map<String, Object> convert(LogInfo obj) {
                return convertToMap(obj);
            }
        });
    }
}

