/**
 * Company Name : 中贸促信息技术有限责任公司
 * Project Name:memberManageSys
 * File Name:UserEmailService.java
 * Package Name:ccpit.base.user
 * Date:2015年11月23日上午9:46:33
 * Copyright (c) 2015, dingwei@ccpit.org All Rights Reserved.
 *
*/

package org.ccpit.base.user;

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

import javax.transaction.Transactional;

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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * ClassName:UserEmailService <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2015年11月23日 上午9:46:33 <br/>
 * @author   dingwei
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Transactional
@Service
public class UserEmailService {

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Autowired
	private UserEmailDao userEmailDao;
	
	public boolean addUserEmail(UserEmail userEmail){
		return userEmailDao.save(userEmail);
	}
	
	public boolean UpdateUserEmail(UserEmail userEmail){
		return userEmailDao.update(userEmail);
	}
	
	public UserEmail queryUserEmailById(long id){
		return userEmailDao.queryById(id);
	}
	
	public List<UserEmail> queryUserEmailAll(String hql){
		return userEmailDao.query(hql, null);
	}
	
	public Page<UserEmail> queryPage(PageRequest pr, String hql){
        Page<UserEmail> page = userEmailDao.findPage(pr, hql, new String[]{});
        return page;
    }
	
	public Map<String,Object> convertToMap(UserEmail UserEmail){
        Map<String,Object> map = new HashMap<String, Object>();
        if (UserEmail==null){
        	return map;
        }
        map.put("id",UserEmail.getId());
        map.put("createTime",sdf.format(UserEmail.getCreateTime()));
        map.put("creator", null == UserEmail.getCreator() ? "":UserEmail.getCreator().getUsername());
        map.put("modifyBy", null == UserEmail.getModifyBy() ? "":UserEmail.getModifyBy().getUsername());
        map.put("modifyTime", UserEmail.getModifyTime());
        map.put("serverProtocol",UserEmail.getServerProtocol());  
        map.put("serverHost",UserEmail.getServerHost()); 
        map.put("serverPort",UserEmail.getServerPort()); 
        map.put("emailCount",UserEmail.getEmailCount());  
        map.put("emailPassword", "********");  //会员性质
        return map;
    }

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

