package com.controller;

import com.config.BaseController;
import com.config.ControllerInterface;
import com.jfinal.aop.Before;
import com.model.Cardtype;
import com.model.Jieyue;
import com.service.CardTypeService;
import com.service.JieyueService;
import com.tools.JwtUtils;
import com.validator.CardTypeValidator;
import com.validator.JieyueValidator;

/**
 * 读者卡类型
 * 2018-10-22
 * zj
 */
@ControllerInterface(path="/cardtype")
public class CardTypeController extends BaseController {
    CardTypeService service = CardTypeService.me;
    public void index() {
        render("/view/index/index.html");
    }

    public void list(){
        try{
            Cardtype bean = getBean(Cardtype.class,"bean");
            success(service.paginate(this.getParaToInt("pageNumber"),this.getParaToInt("pageSize"),bean));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }
    public void getById(){
        int id =this.getParaToInt("bean.id", 0);
        if(id == 0){
            fail();
            return;
        }
        success(service.findById(id));
    }

    @Before(CardTypeValidator.class)
    public void addOrUpdate(){
        Cardtype bean = getBean(Cardtype.class,"bean");
        String token = this.getPara("token");
        try {
            int operatorid = JwtUtils.validateJWTForUserId(token);
            if(operatorid == 0){
                fail(3,"token错误");
                return;
            }
            service.addOrUpdate(bean,operatorid);
            success();
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }
    public void delete(){
        int id =this.getParaToInt("bean.id");
        service.deleteById(this.getParaToInt("bean.id", 0));
        success();
    }

}
