package com.jfinal.club._admin.ques;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.model.Doc;
import com.jfinal.club.common.model.Doctype;
import com.jfinal.club.common.model.Questions;
import com.jfinal.club.common.upload.UploadService;
import com.jfinal.club.util.Cons;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;

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

/**
 * @ProjectName: jfinal-club
 * @Package: com.jfinal.club._admin.ques
 * @ClassName: quesAdminController
 * @Author: Jzdsh
 * @Description:
 * @Date: 2020/3/31 14:06
 * @Version: 1.0
 */
public class QuesAdminController extends BaseController {

    @Inject
    QuesAdminService service;

    @Inject
    DocTypeService docTypeService;

    public void index( ) {
        Page< Questions > page = service.page(getInt("p", 1), Cons.ADMIN_PAGE_SIZE, get("keyword"));
        setAttr("quesPage", page);
        render("index.html");
    }

    public void add( ) {
        List< Doctype > byType = docTypeService.getByType(4);
        setAttr("doctypes", byType);
        render("add_edit.html");
    }

    @Before(QuestAdminValidator.class)
    public void save( ) {
        UploadFile file = getFile("filenames", "/temp");
        if (file == null) {
            renderJson(Ret.fail("msg", "请上传图片!"));
            return;
        }
        //0 默认管理员
        Ret ret = UploadService.uploadAvatar(0, file, Questions.UPLOADPATH);
        if (ret.isFail( )) {
            renderJson(ret);
            return;
        }
        boolean save = getBean(Questions.class, "ques").set("quesimg", ret.get("img")).set("addtime", new Date( )).set("usersid", 0).save( );
        if (save) {
            renderJson(Ret.ok("msg", "保存成功！"));
        } else {
            renderJson(Ret.ok("msg", "保存失败！"));
        }
    }

    public void edit( ) {
        Questions questions = new Questions( ).dao( ).findById(getInt("id"));
        setAttr("ques", questions);
        setAttr("doctypes", docTypeService.getByType(4));
        render("add_edit.html");
    }

    @Before(QuestAdminValidator.class)
    public void update( ) {
        UploadFile file = getFile("filenames", "/temp");
        UploadFile bannerpath = getFile("bannerpath","/temp" );
        String banner = null;
        if(bannerpath != null){
            Ret ret = UploadService.uploadAvatar(0, bannerpath, Cons.QUES_BANNER);
            if (ret.isFail( )) {
                renderJson(ret);
                return;
            }
            banner = ret.getStr("img");
        }
        boolean update = getBean(Questions.class, "ques").set("banner",banner).removeNullValueAttrs( ).update( );
        if (update) {
            renderJson(Ret.ok("msg", "修改成功!"));
        } else {
            renderJson(Ret.fail("msg", "修改失败!"));
        }
    }


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


    /**
     * 快捷方式
     * 审核成功
     */
    public void status_t(){
        Integer id = getInt("id");
        new Questions( ).dao( ).findById(id).set("status", 1).removeNullValueAttrs( ).update( );
        index();
    }
    /**
     * 快捷方式
     * 取消审核
     */
    public void status_f(){
        Integer id = getInt("id");
        new Questions( ).dao( ).findById(id).set("status", 0).removeNullValueAttrs( ).update( );
        index();
    }



}
