package com.jfinal.club.my.ques;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.interceptor.FrontAuthInterceptor;
import com.jfinal.club.common.model.Doc;
import com.jfinal.club.common.model.Quesanswer;
import com.jfinal.club.common.model.Questions;
import com.jfinal.club.common.model.Userct;
import com.jfinal.club.common.upload.UploadService;
import com.jfinal.club.my.ct.UserCtService;
import com.jfinal.club.my.doc.TypeIntreceptor;
import com.jfinal.club.my.doc.UsersDocService;
import com.jfinal.club.my.newsfeed.UsersInfoIntreceptor;
import com.jfinal.club.util.Cons;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;

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

/**
 * @ClassName QuestionsContrller
 * @Description TODO
 * @Author caolong
 * @Date 2020/3/21 18:06
 */
@Before(TypeIntreceptor.class)
public class QuestionsContrller extends BaseController {

    @Inject
    UserCtService userCtService;

    @Inject
    QuestionsService questionsService;

    @Inject
    UsersDocService usersDocService;


    /**
     * @return void
     * @Author Cc
     * @Description //TODO 文档首页
     * @Date 2020/3/21
     * @Param []
     **/
    public void index( ) {
        List< Questions > questionsListByTop = questionsService.questionsListByTop( );
        List< Questions > questionsListByAddtime = questionsService.questionsListByAddtime( );
        setAttr("questionsListByTop", questionsListByTop);
        setAttr("questionsListByAddtime", questionsListByAddtime);
        List< Doc > docs = usersDocService.docListByLooking( );
        setAttr("docs", docs);
        Page< Userct > userCtByHotOrNews = userCtService.getUserCtByHotOrNews("", "1", 1, 6, 0);
        setAttr("userCtByHotOrNews", userCtByHotOrNews);
        if (isLogin( )) {
            Long countQues = questionsService.countQuesById(getloginUsersId( ));
            setAttr("countQues", countQues);
            Long countQuesanswer = questionsService.countQuesanswerByQuesId(getloginUsersId( ));
            setAttr("countQuesanswer", countQuesanswer);
            Long helpQues = questionsService.countQuesanswerByQuesIdAndStatus(getloginUsersId( ));
            setAttr("helpQues", helpQues);
        }
        render("ques_index.html");
    }


    /**
     * @return void
     * @Author Cc
     * @Description //TODO type = 1 热门 type = 2 最新
     * @Date 2020/3/20
     * @Param []
     **/
    public void quesList( ) {
        String type = getPara("type", "1");
        String text = getPara("text");
        int doctype = getParaToInt("doctype", 0);
        Page< Questions > questionsByHotOrNews = questionsService.getQuestionsByHotOrNews(text, type, getParaToInt("p", 1), Cons.FRONT_PAGE_SIZE, doctype);
        setAttr("questionsByHotOrNews", questionsByHotOrNews);
        List< Doc > docs = usersDocService.docListByLooking( );
        setAttr("docs", docs);
        Page< Userct > userCtByHotOrNews = userCtService.getUserCtByHotOrNews("", "1", 1, 6, 0);
        setAttr("userCtByHotOrNews", userCtByHotOrNews);
        setAttr("type", type);
        setAttr("doctype", doctype);
        setAttr("text", text);
        render("ques_list.html");
    }


    /**
     * @return
     * @Author Cc
     * @Description //TODO 问答发布页
     * @Date 2020/3/21
     * @Param
     **/
    @Before({FrontAuthInterceptor.class})
    public void ques( ) {
        render("ques.html");
    }

    @Before({FrontAuthInterceptor.class, UsersInfoIntreceptor.class})
    public void quesSet( ) {
        Integer id = getParaToInt("id");
        Questions questionsById = questionsService.getQuestionsById(id);
        setAttr("questionsById", questionsById);
        render("ques_set.html");
    }

    /**
     * @return
     * @Author Cc
     * @Description //TODO 问答提交
     * @Date 2020/3/21
     * @Param
     **/
    @Before(QuestionsValidator.class)
    public void saveQues( ) {
        String types = get("types");
        boolean save = types.equals("1") ? false : true;
        UploadFile file = getFile("filepath", "temp");
/*        if (save && file == null) {
            renderJson(Ret.fail("msg", "请上传图片!"));
            return;
        }*/
        Ret ret2 = null;
        if (file != null) {
            ret2 = UploadService.uploadAvatar(getloginUsersId( ), file, "img/question");
            if (ret2.isFail( )) {
                renderJson(ret2);
                return;
            }
        }
        Questions questions = getModel(Questions.class);
        if ("1".equals(types)) {
            if (file != null && ret2.isOk( )) {
                questions.setQuesimg(ret2.getStr("img"));
            }
            if (get("questions.anonymous") == null) {
                //匿名是整型的0变null,所以手动设置
                questions.setAnonymous(0);
            }
            boolean update = questions.removeNullValueAttrs( ).update( );
            if (update) {
                renderJson(Ret.ok("msg", "修改成功"));
            } else {
                renderJson(Ret.ok("msg", "修改成功"));
            }
        } else {
            if (file != null && ret2.isOk( )) {
                questions.setQuesimg(ret2.getStr("img"));
            }
            questions.setAddtime(new Date( ));
            questions.setUsersid(getloginUsersId( ));
            questions.setStatus(0);
            questions.setTops(0);
            questions.setNews(0);
            questions.setSort(0);
            if (get("questions.anonymous") == null) {
                questions.setAnonymous(0);
            }
            if (questions.save( )) {
                renderJson(Ret.ok("msg", "保存成功"));
            } else {
                renderJson(Ret.ok("msg", "保存成功"));
            }
        }
    }


    /**
     * @return void
     * @Author Cc
     * @Description //TODO  问答审核成功
     * @Date 2020/3/18
     * @Param []
     **/
    @Before({FrontAuthInterceptor.class, UsersInfoIntreceptor.class})
    public void quesSuccess( ) {
        Page< Questions > quesList = questionsService.getQuesList(getloginUsersId( ), 1, getPara("text"), getParaToInt("p", 1));
        setAttr("quesList", quesList);
        setAttr("text", getPara("text"));
        setAttr("my_ques", true);
        render("ques_success.html");
    }

    /**
     * @return void
     * @Author Cc
     * @Description //TODO  问答审核失败
     * @Date 2020/3/18
     * @Param []
     **/
    @Before({FrontAuthInterceptor.class, UsersInfoIntreceptor.class})
    public void quesFail( ) {
        Page< Questions > quesList = questionsService.getQuesList(getloginUsersId( ), 2, getPara("text"), getParaToInt("p", 1));
        setAttr("quesList", quesList);
        setAttr("text", getPara("text"));
        setAttr("my_ques", true);
        render("ques_fail.html");
    }

    /**
     * @return void
     * @Author Cc
     * @Description //TODO  问答待审核
     * @Date 2020/3/18
     * @Param []
     **/
    @Before({FrontAuthInterceptor.class, UsersInfoIntreceptor.class})
    public void myQues( ) {
        Page< Questions > quesList = questionsService.getQuesList(getloginUsersId( ), 0, getPara("text"), getParaToInt("p", 1));
        setAttr("quesList", quesList);
        setAttr("text", getPara("text"));
        setAttr("my_ques", true);
        render("my_ques.html");
    }


    public void delQues( ) {
        if (!StrKit.isBlank(getPara("id"))) {
            int id = Integer.parseInt(getPara("id"));
            Ret ret = questionsService.deleteById(id);
            renderJson(ret);
        }
    }

    /**
     * @return void
     * @Author Cc
     * @Description //TODO ajax提交答案
     * @Date 2020/3/23
     * @Param []
     **/
    public void saveQuesanswer( ) {
        String text = getPara("text");
        Integer id = getParaToInt("id");
        if (isLogin( )) {
            Ret ret = questionsService.saveQuesanswer(getloginUsersId( ), text, id);
            renderJson(ret);
        } else {
            Ret fail = Ret.fail("msg", "请先登录");
            renderJson(fail);
        }

    }

    /**
     * @return void
     * @Author Cc
     * @Description //TODO ajax提交答案
     * @Date 2020/3/23
     * @Param []
     **/
    public void huifQuesanswer( ) {
        String text = getPara("text");
        Integer id = getParaToInt("id");
        Integer quesId = getParaToInt("quesId");
        if (isLogin( )) {
            Ret ret = questionsService.huifQuesanswer(getloginUsersId( ), text, id, quesId);
            renderJson(ret);
        } else {
            Ret fail = Ret.fail("msg", "请先登录");
            renderJson(fail);
        }

    }


    /**
     * @return void
     * @Author Cc
     * @Description //TODO 作者查看问题
     * @Date 2020/3/23
     * @Param []
     **/
    @Before({FrontAuthInterceptor.class, UsersInfoIntreceptor.class})
    public void centerQuesDetail( ) {
        Integer id = getParaToInt("id");
        Questions questionsById = questionsService.getQuestionsById(id);
        Page< Quesanswer > quesanswerByQuesId = questionsService.getQuesanswerByQuesId(id, getParaToInt("p", 1));
        setAttr("quesanswerByQuesId", quesanswerByQuesId);
        setAttr("questionsById", questionsById);
        render("center_ques_detail.html");
    }

    /**
     * @return void
     * @Author Cc
     * @Description //TODO 问题详情
     * @Date 2020/3/23
     * @Param []
     **/
    public void quesDetail( ) {
        Integer id = getParaToInt("id");
        Questions questionsById = questionsService.getQuestionsById(id);
        Page< Quesanswer > quesanswerByQuesId = questionsService.getQuesanswerByQuesId(id, getParaToInt("p", 1));
        setAttr("quesanswerByQuesId", quesanswerByQuesId);
        setAttr("questionsById", questionsById);
        Page< Questions > questionsByHotOrNews = questionsService.getQuestionsByHotOrNews("", "1", 1, 8, 0);
        setAttr("questionsByHotOrNews", questionsByHotOrNews);
        render("ques_detail.html");
    }


    /**
     * @return void
     * @Author Cc
     * @Description //TODO 修改评论采用状态
     * @Date 2020/3/24
     * @Param []
     **/
    public void upStatus( ) {
        Integer quesanswerId = getParaToInt("quesanswerId");
        Integer status = getParaToInt("status");
        Ret ret = questionsService.upStatusByQuesanswerId(quesanswerId, status);
        renderJson(ret);
    }

    /**
     * @return
     * @Author Cc
     * @Description //TODO 查看回答的问题
     * @Date 2020/3/24
     * @Param
     **/
    @Before({FrontAuthInterceptor.class, UsersInfoIntreceptor.class})
    public void myAnswer( ) {
        Page< Questions > questionsPage = questionsService.getQuesanswerByUsersId(getloginUsersId( ), getParaToInt("p", 1));
        setAttr("questionsPage", questionsPage);
        setAttr("my_answer",true);
        render("my_answer.html");
    }

}
