package com.sv.service.api;

import com.github.pagehelper.PageHelper;
import com.sv.entity.Announcement;
import com.sv.mapper.AnnouncementMapper;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.common.utils.ValidationUtils;
import com.ydd.framework.core.service.impl.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Service - 公告
 *
 * @author lihong
 * @since 2018-08-06
 */
@Service("AnnouncementServiceApi")
@Transactional(readOnly = true)
public class AnnouncementService extends BaseServiceImpl {

	private final Logger logger = LoggerFactory.getLogger(AnnouncementService.class);

	@Resource
	private AnnouncementMapper announcementMapper;

	/**
	 * 创建公告
	 *
	 * @param announcement 公告
	 */
	@Transactional
    public void save(Announcement announcement) {
		if (announcement.getId() != null && announcement.getId() > 0) {
			// 更新
			announcementMapper.update(announcement);
		} else {
			// 新建
			announcementMapper.insert(announcement);
		}
	}

	/**
	 * 更新公告
 	 *
	 * @param announcement 公告
	 */
	@Transactional
    public void update(Announcement announcement) {
		announcementMapper.update(announcement);
	}

	/**
	 * 删除公告
	 *
	 * @param id 编号
   	 * @return 删除数量
	 */
	@Transactional
    public Integer delete(Integer id) {
		if (id == null || id <= 0) {
			return 0;
		}
		return announcementMapper.delete(id);
	}

	/**
	 * 删除公告
	 *
	 * @param ids 编号数组
	 * @return 删除数量
	 */
	@Transactional
    public Integer deleteByIds(Integer[] ids) {
		if (ids == null || ids.length == 0) {
			return 0;
		}
		return announcementMapper.deleteByIds(ids);
	}

	/**
	 * api
	 * 查询公告
	 *
	 * @param id 编号
	 * @return 公告
	 */
	public Announcement findById(Integer id) {
		// 参数校验
		ValidationUtils.assertNotNull(id);
		return announcementMapper.findById(id);
	}

	/**
	 * 查询公告
	 *
	 * @param pagination 分页信息
	 * @return 分页结果
	 */
	public Pagination findPage(Pagination pagination) {
		PageHelper.startPage(pagination.getPage(), pagination.getPageSize());
		PageHelper.orderBy("id desc");
		pagination.setQueryResult(announcementMapper.findAll());
		return pagination;
	}

	/**
	 * api
	 * 查询所有公告
	 */
	public List<Announcement> findAll (){
		return announcementMapper.findAll();
	}
}

