package com.happy.service.impl;

import com.happy.Model.Admin;
import com.happy.Model.AdminPower;
import com.happy.Until.UUIDUtil;
import com.happy.dao.AdminDao;
import com.happy.dto.IPage;
import com.happy.service.AdminService;
import org.springframework.stereotype.Service;

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

@Service("AdminService")
public class AdminImplService implements AdminService {

    @Resource
    public AdminDao adminDao;

    @Override
    public int insertAdmin(Admin admin) {
        admin.setCreateDate(UUIDUtil.getNewDate());
        return adminDao.insertAdmin(admin);
    }

    @Override
    public int updateAdmin(Admin admin) {
        admin.setModifyDate(UUIDUtil.getNewDate());
        return adminDao.updateAdmin(admin);
    }

    @Override
    public int delAdmin(int id) {
        return adminDao.delAdmin(id);
    }

    @Override
    public Admin getById(int id) {
        return adminDao.getById(id);
    }

    @Override
    public IPage<Admin> queryPage(String sqlx, int page, int rows) {
        IPage<Admin> iPage = new IPage();
        List<Admin> adminList = adminDao.queryPage(sqlx,page,rows);
        int total = adminDao.queryTotal(sqlx);
        iPage.setPageList(adminList);
        iPage.setPage(page);
        iPage.setTotalPage( (int)Math.ceil((double)total/rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public List<Admin> queryList(String sqlx) {
        return adminDao.queryList(sqlx);
    }

    @Override
    public List<AdminPower> queryTree(String sqlx) {
        return adminDao.queryTree(sqlx);
    }

    @Override
    public Admin getOen(String sqlx) {
        List<Admin> list = adminDao.queryList(sqlx);
        if(list!=null && list.size()>0) return list.get(0);
        return null;
    }
}
