EmailServiceImpl.java 4.47 KB
package com.cnlive.shenhe.serviceImpl;


import com.alibaba.fastjson.JSON;
import com.cnlive.shenhe.dto.EmailDTO;
import com.cnlive.shenhe.entity.ShyEmail;
import com.cnlive.shenhe.mapper.ShyEmailMapper;
import com.cnlive.shenhe.utils.CommonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: 周伟鹏
 * @Date: 2018/11/29 14:23
 * @Description:
 */
@Service
public class EmailServiceImpl {
	private static Logger logger = LoggerFactory.getLogger(EmailServiceImpl.class);
    @Autowired
    ShyEmailMapper shyEmailMapper;
    
    public PageInfo<ShyEmail> getPage(EmailDTO emailDTO) {
    	logger.info("接受参数列表为:{}", JSON.toJSONString(emailDTO));
        Example example = new Example(ShyEmail.class);
        Example.Criteria criteria = example.createCriteria();
        if (CommonUtils.isNotEmpty(emailDTO.getOrderByClause())) {
            example.setOrderByClause(emailDTO.getOrderByClause());
        }else{
        	example.setOrderByClause("created_at desc");
        }
        if (CommonUtils.isNotEmpty(emailDTO.getRemark())) {
        	criteria.andLike("remark", "%" + emailDTO.getRemark() + "%");
        }
        if (CommonUtils.isNotEmpty(emailDTO.getEmail())) {
        	criteria.andLike("email", "%" + emailDTO.getEmail() + "%");
        }
        criteria.andEqualTo("is_delete",false);
        PageHelper.startPage(emailDTO.getPage(), emailDTO.getPageSize(), true);

        List<ShyEmail> ShyEmailList = shyEmailMapper.selectByExample(example);
        PageInfo<ShyEmail> pageInfo = new PageInfo<ShyEmail>(ShyEmailList);
        return pageInfo;
    }
    
    public ShyEmail get(Integer id) {
        return shyEmailMapper.selectByPrimaryKey(id);
    }

    public ShyEmail select(ShyEmail shyEmail) {
        return shyEmailMapper.selectOne(shyEmail);
    }
    
    public Integer updateState(Integer id, Boolean isEnable,String userId) {
    	ShyEmail shyEmail=get(id);
    	shyEmail.setIs_enable(isEnable);
    	shyEmail.setUpdater(userId);
    	shyEmail.setUpdated_at(CommonUtils.getCurrentTime());
		return shyEmailMapper.updateByPrimaryKeySelective(shyEmail);
	}

	public Integer deleted(Integer id, String userId) {
		ShyEmail shyEmail=get(id);
    	shyEmail.setUpdater(userId);
    	shyEmail.setIs_delete(true);
    	shyEmail.setUpdated_at(CommonUtils.getCurrentTime());
		return shyEmailMapper.updateByPrimaryKeySelective(shyEmail);
	}

	public boolean update(Integer id, String email, String remark, String write, String updater) {

		ShyEmail shyEmail = new ShyEmail();
		if (CommonUtils.isNotNull(id)) {
			shyEmail.setId(id);
			shyEmail = shyEmailMapper.selectOne(shyEmail);
			if (shyEmail != null) {
				shyEmail.setEmail(email);
				shyEmail.setRemark(remark);
				shyEmail.setNotice_type(write);
				shyEmail.setUpdater(updater);
				shyEmail.setUpdated_at(CommonUtils.getCurrentTime());
				int re = shyEmailMapper.updateByPrimaryKeySelective(shyEmail);
				if (re != 1) {
					return false;
				}
			}
		} else {
			shyEmail.setEmail(email);
			shyEmail.setIs_delete(false);
			shyEmail.setIs_enable(true);
			shyEmail.setNotice_type(write);
			shyEmail.setRemark(remark);
			shyEmail.setUpdater(updater);
			shyEmail.setCreated_at(CommonUtils.getCurrentTime());
			shyEmail.setUpdated_at(CommonUtils.getCurrentTime());
			int re = shyEmailMapper.insertSelective(shyEmail);
			if (re != 1) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 通过邮件发送类型,获取需要发送邮件的list
	 * @param emailSendType
	 * @return
	 */
	public List<ShyEmail> getEmailSendList(Integer emailSendType) {
		ShyEmail shyEmail= new ShyEmail();
		shyEmail.setIs_delete(false);
		shyEmail.setIs_enable(true);
		List<ShyEmail> shyEmailList = shyEmailMapper.select(shyEmail);
        List<ShyEmail> emailList = new ArrayList<ShyEmail>();
        for (ShyEmail shyEmail2 : shyEmailList) {
            if (CommonUtils.isNotEmpty(shyEmail2.getNotice_type())) {
                String[] business = shyEmail2.getNotice_type().split(",");
                List<String> asList = Arrays.asList(business);
                if (asList.contains(emailSendType + "")) {
                	emailList.add(shyEmail2);
                }
            }
        }
        return emailList;
		
	}

}