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

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jzdsh.auto.app.controller.admin.mvalidator.AdminBsTypeValidator;
import com.jzdsh.auto.biz.BsTypeService;
import com.jzdsh.auto.model.BsType;

import java.util.Date;

/**
 * @ProjectName: auto
 * @Package: com.jzdsh.auto.app.controller.admin
 * @ClassName: AdminBsTypeController
 * @Author: Jzdsh
 * @Description: 基础类型统一表
 * @Date: 2020/3/26 11:45
 * @Version: 1.0
 */
public class AdminBsTypeController extends Controller {

    @Inject
    BsTypeService bsTypeService;

    public void index( ) {
        Integer status = getInt("status", 1);
        setAttr("bsType", bsTypeService.getByStatus(status));
        setAttr("status", status);
    }

    public void tag(){
        Integer status = getInt("status", 1);
        setAttr("bsType", bsTypeService.findTagByStatus(status));
        setAttr("status", status);
        render("index.html");
    }


    public void add( ) {
        setAttr("type", bsTypeService.findAllType( ));
        render("add_edit.html");
    }

    @Before(AdminBsTypeValidator.class)
    public void save( ) {
        BsType bean = getBean(BsType.class);
        boolean save = bean.set("createDate", new Date( )).save( );
        if (save) {
            renderJson(Ret.ok("status", bean.getStatus()));
        } else {
            renderJson(Ret.fail("msg", "创建失败!"));
        }
    }

    public void edit( ) {
        BsType bsType = BsType.dao.findById(getInt( ));
        setAttr("bsType", bsType);
        setAttr("status", Integer.valueOf(bsType.getStatus( )));
        render("add_edit.html");
    }

    @Before(AdminBsTypeValidator.class)
    public void update(){
        BsType bean = getBean(BsType.class);
        boolean update = bean.update( );
        if (update) {
            renderJson(Ret.ok("status", bean.getStatus()));
        } else {
            renderJson(Ret.fail("msg", "更新失败!"));
        }
    }

    public void delete(){
        boolean b = BsType.dao.deleteById(getInt( ));
        if(b){
            renderJson(Ret.ok("msg","删除成功!"));
        }else{
            renderJson(Ret.fail("msg","删除失败!"));
        }
    }


}
