package com.ssm.service;

import com.ssm.Utils.PageBean;
import com.ssm.entity.Demo;
import com.ssm.mapper.DemoMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
public class DemoService {

	@Resource
	private DemoMapper demoMapper;

	public PageBean<Demo> getDemoAll(int page) {
		PageBean<Demo> pageBean = new PageBean<>();
		pageBean.setPage(page);
//		设置7个
		int limitPage = 6;
		pageBean.setLimitPage(limitPage);
//		设置一共多少页
		int totlePage = 0;
//		查询一共有多少页
		totlePage = demoMapper.countTotlePage();
		if (Math.ceil(totlePage % limitPage) == 0) {
			totlePage = totlePage / limitPage;
		} else {
			totlePage = totlePage / limitPage + 1;
		}
		pageBean.setTotlePage(totlePage);
		int beginPage = (page - 1) * limitPage;
		List<Demo> list = demoMapper.getDemoAll(beginPage, limitPage);
		pageBean.setList(list);
		return pageBean;

	}
	public Demo getDemoById(Integer id) {
		return demoMapper.findDemoByDid(id);
	}
	
	public void addDemo(Demo demo) {

		demoMapper.addDemo(demo);
	}
	public void updateDemo(Demo demo) {
		demoMapper.updateDemo(demo);
	}
	public void deleteDemoById(Integer id) {
		demoMapper.deleteDemoById(id);
	}
	public List<Demo> searchDemoByCondition(String condition){ return demoMapper.searchDemoByCondition(condition); }
	public List<Demo> findDemoByDId(Integer did){return demoMapper.findDemoByDId(did);}
}
