package com.jzdsh.auto.biz;

import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jzdsh.auto.model.Activity;
import lombok.Setter;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j2;

import java.util.List;

/**
 * @ProjectName: auto
 * @Package: com.jzdsh.auto.biz
 * @ClassName: ActivityServce
 * @Author: Jzdsh
 * @Description:
 * @Date: 2020/3/20 16:09
 * @Version: 1.0
 */
@Log4j2
public class ActivityServce {

    public List< Activity > queryAll( ) {
        try {
            return Activity.dao.findAll( );
        } catch (Exception e) {
            log.info(e);
        }
        return null;
    }


     public Page<Activity> activityPaginate(Integer pageNum, Integer pageSize){

        try {
            return Activity.dao.paginate(pageNum, pageSize, "select a.*,b.title type,c.title tag", "from activity a inner join bs_type b ON(a.typeId = b.id and b.isTag= 0) inner join  bs_type  c on (a.tagId = c.id and c.isTag = 1) order by a.id desc");
        }catch (Exception e){
            log.info(e);
        }
        return null;
     }

     public Activity findById(int activityNewsId){
        try {
            Activity activity = Activity.dao.findById(activityNewsId);
            if (activity != null) {
                return activity;
            }else{
                return null;
            }
        }catch (Exception e){
            log.info(e);
        }
        return null;
     }


     public Ret delete(int activityNewsId){
        try {
            Integer countId = Db.queryInt("select id from activity where id = ? limit 1", activityNewsId);
            if (countId != null) {
                Activity.dao.deleteById(activityNewsId);
                return Ret.ok("msg", "删除成功");
            } else {
                return Ret.fail("msg", "删除失败");
            }
        }catch (Exception e){
            log.info(e);
        }
         return Ret.fail("msg", "删除失败");
     }


}
