package com.controller;

import com.config.BaseController;
import com.config.ControllerInterface;
import com.model.BooktypeAuth;
import com.model.ReaderAuth;
import com.service.ReaderAuthService;
import com.tools.JwtUtils;

@ControllerInterface(path="/readerauth")
public class ReaderAuthController extends BaseController {
    ReaderAuthService service = ReaderAuthService.me;

    public void index() {
        render("/view/reader/readerauth.html");
    }

    public void list(){
        Integer orgid = getParaToInt("bean.orgid");
        success(service.list(orgid));
    }
    public void getById(){
        int id =this.getParaToInt("bean.id", 0);
        if(id == 0){
            fail();
        }
        success(service.findById(id));
    }

    public void addOrUpdate(){
        ReaderAuth bean = getBean(ReaderAuth.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(0,e.getMessage());
        }
    }


    public void delete(){
        Integer id = getParaToInt("id");
        service.deleteById(id);
        success();
    }


    public void booktypeauth(){
        int orgid=getParaToInt("orgid",0);
        success(service.findBooktypeAuth(orgid));
    }


    public void saveOrUpdateBookTypeAuth(){
        BooktypeAuth booktypeAuth=getBean(BooktypeAuth.class,"bean");
        if (booktypeAuth.getId()==null){
            booktypeAuth.save();
        }else {
            booktypeAuth.update();
        }
        success();
    }

    public void findBookTypeAuthByIDS(){
        String booktypeauthids=getPara("booktypeauthid");
        if (booktypeauthids==""){
            fail();
            return;
        }
        success(service.findBooktypeAuthByIds(booktypeauthids));
    }
}
