package com.jzdsh.auto.app.controller.admin;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jzdsh.auto.biz.ActivityServce;
import com.jzdsh.auto.biz.BsTypeService;
import com.jzdsh.auto.model.Activity;
import com.jzdsh.auto.model.BsType;

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

public class AdminActivityController extends Controller {

    @Inject
    ActivityServce activityServce;

    @Inject
    BsTypeService bsTypeService;

    public void index( ) {
        //查询所有活动资讯并分页
        Page< Activity > activityPage = activityServce.activityPaginate(getParaToInt("pageNum", 1), 10);
        setAttr("activityPage", activityPage);
        render("index.html");
    }


    /**
     * 添加 跳转
     */
    public void add( ) {
        //查询活动资讯所有类型
        List< BsType > allType = bsTypeService.getByStatus(1);
        setAttr("allType", allType);
        //查询活动资讯所有标签
        List< BsType > allTag = bsTypeService.findAllTag( );
        setAttr("allTag", allTag);

        Activity activity = new Activity( );
        activity.setTypeId(0);
        activity.setTagId(0);

        setAttr("activity", activity);
        render("add_edit.html");
    }

    /**
     * 添加  实现
     */
    public void save( ) {
        Activity bean = getBean(Activity.class);
        bean.setPv(0);
        bean.setReleaseTime(new Date( ));
        boolean save = bean.save( );
        renderJson(Ret.ok("msg", save));
    }


    public void edit( ) {
        Activity activity = activityServce.findById(getParaToInt(0));
        setAttr("activity", activity);
        //查询活动资讯所有类型
        List< BsType > allType = bsTypeService.findAllType( );
        setAttr("allType", allType);
        //查询活动资讯所有标签
        List< BsType > allTag = bsTypeService.findAllTag( );
        setAttr("allTag", allTag);
        setAttr("pageNum", getParaToInt(1));
        render("add_edit.html");
    }

    /**
     * 修改  跳转
     */
    public void update( ) {
        Activity bean = getBean(Activity.class);
        bean.setReleaseTime(new Date( ));
        boolean update = bean.update( );
        if (update) {
            renderJson(Ret.ok("msg", "更新成功！"));
        } else {
            renderJson(Ret.fail("msg", "更新失败!"));
        }

    }

    /**
     * 删除
     */
    public void delete( ) {
        Ret ret = activityServce.delete(getParaToInt(0));
        renderJson(ret);
    }
}
