package com.jfinal.club.common.banner;


import com.jfinal.club.common.model.Banners;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;
import java.util.List;

public class BannersService {

    Banners bannersdao = new Banners().dao();


    /**
     * @return com.jfinal.plugin.activerecord.Page<com.jfinal.club.common.model.Banners>
     * @Author Cc
     * @Description //TODO 根据类型查看banners
     * @Date 2020/3/24
     * @Param [type]
     **/
    public Page<Banners> getBannersByType(int pageNum,int type, int size) {
        return bannersdao.paginate(pageNum, size, "select *", "from banners where type = ? order by sort desc ,addtime desc", type);
    }



    public List<Banners> selectAllBanners(){
        return bannersdao.findAll();
    }

    /**
     * @return
     * @Author Cc
     * @Description //TODO  后台查询所有banners
     * @Date 2020/3/25
     * @Param
     **/
    public Page<Banners> getBannersList(int pageNum) {
        return bannersdao.paginate(pageNum, 10, "seletc *", "from banners  order by sort desc ,addtime desc");
    }

    public Banners getBanners(int id) {
        return bannersdao.findById(id);
    }


    public Ret deleteById(int id) {
        boolean b = bannersdao.deleteById(id);
        if (b) {
            return Ret.ok("msg", "删除成功");
        } else {
            return Ret.fail("msg", "删除失败");
        }
    }


    /**
     * @return com.jfinal.kit.Ret
     * @Author Cc
     * @Description //TODO banner修改
     * @Date 2020/3/25
     * @Param [banners]
     **/
    public Ret updateBanner(Banners banners) {
        banners.setAddtime(new Date());
        boolean update = banners.update();
        if (update) {
            return Ret.ok("msg", "修改成功");
        } else {
            return Ret.fail("msg", "修改失败");
        }
    }

    /**
     * @return
     * @Author Cc
     * @Description //TODO
     * @Date 2020/3/25
     * @Param
     **/
    public Ret saveBanner(Banners banners) {
        banners.setAddtime(new Date());
        boolean update = banners.save();
        if (update) {
            return Ret.ok("msg", "修改成功");
        } else {
            return Ret.fail("msg", "修改失败");
        }
    }


}
