/**
 * 请勿将俱乐部专享资源复制给其他人，保护知识产权即是保护我们所在的行业，进而保护我们自己的利益
 * 即便是公司的同事，也请尊重 JFinal 作者的努力与付出，不要复制给同事
 * <p>
 * 如果你尚未加入俱乐部，请立即删除该项目，或者现在加入俱乐部：http://jfinal.com/club
 * <p>
 * 俱乐部将提供 jfinal-club 项目文档与设计资源、专用 QQ 群，以及作者在俱乐部定期的分享与答疑，
 * 价值远比仅仅拥有 jfinal club 项目源代码要大得多
 * <p>
 * JFinal 俱乐部是五年以来首次寻求外部资源的尝试，以便于有资源创建更加
 * 高品质的产品与服务，为大家带来更大的价值，所以请大家多多支持，不要将
 * 首次的尝试扼杀在了摇篮之中
 */

package com.jfinal.club.my.newsfeed;

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.*;
import com.jfinal.club.download.DownloadService;
import com.jfinal.club.login.LoginService;
import com.jfinal.club.ws.DocWsService;
import com.jfinal.club.ws.DocWsValidator;
import com.jfinal.core.ActionKey;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import lombok.extern.log4j.Log4j;

import java.util.List;

/**
 * 个人信息
 */
@Log4j
@Before({FrontAuthInterceptor.class, UsersInfoIntreceptor.class})
public class UsersController extends BaseController {

    @Inject
    UsersService usersService;

    @Inject
    DownloadService downloadService;

    @Inject
    DocWsService docWsService;

    @ActionKey("/my")
    public void mewsFeed() {
        render("my_infor.html");
    }


    public void download() {
        Page<Download> downloadList = downloadService.getDownloadList(getloginUsersId(), getPara("text"), getParaToInt("p", 1));
        setAttr("downloadList", downloadList);
        render("download.html");
    }


    public void doUsers() {
        Users users = getModel(Users.class);
        Users loginUsers = getAttr(LoginService.loginUsersCacheName);
        Ret ret = usersService.updateUsersInfo(users, loginUsers.getId());
        if (ret.isOk()) {
            String sessionId = ret.getStr(LoginService.sessionIdName);
            int maxAgeInSeconds = ret.getInt("maxAgeInSeconds");
            setCookie(LoginService.sessionIdName, sessionId, maxAgeInSeconds, true);
            setAttr(LoginService.loginUsersCacheName, ret.get(LoginService.loginUsersCacheName));
        }
        renderJson(ret);
    }


    /**
     * @return
     * @Author Cc
     * @Description //TODO type  = 1 法律文档 2 法律百科 3 法律问答
     * @Date 2020/3/24
     * @Param
     **/
    public void myCollect() {
        Integer type = getParaToInt("type", 1);
        Object p = usersService.myCollectByType(type, getloginUsersId(), getParaToInt("p", 1));
        try {
            if (type == 1) {
                Page<Doc> doc = (Page<Doc>) p;
                setAttr("listPage", doc);
            } else if (type == 2) {
                Page<Userct> userct = (Page<Userct>) p;
                setAttr("listPage", userct);
            } else {
                Page<Questions> questionsPage = (Page<Questions>) p;
                setAttr("listPage", questionsPage);
            }
        } catch (ClassCastException c) {
            log.debug("强转类型异常");
        }
        setAttr("type", type);
        render("my_collect.html");
    }

    /**
     * @return void
     * @Author Cc
     * @Description //TODO 我的文书
     * @Date 2020/3/24
     * @Param []
     **/
    public void myWs() {
        Page<Docws> p = docWsService.getDocwsByUsersId(getloginUsersId(), getParaToInt("p", 1), 1);
        setAttr("docwsPage", p);
        render("my_ws.html");
    }

    /**
     * @return void
     * @Author Cc
     * @Description //TODO 删除文书
     * @Date 2020/3/24
     * @Param []
     **/
    public void delWs() {
        if (!StrKit.isBlank(getPara("id"))) {
            int id = Integer.parseInt(getPara("id"));
            Ret ret = docWsService.delDocwsById(id);
            renderJson(ret);
        }
    }

    /**
     * @return void
     * @Author Cc
     * @Description //TODO 文书编辑
     * @Date 2020/3/24
     * @Param []
     **/
    public void wsEdit() {
        Integer id = getParaToInt("id");
        Docws docwsById = docWsService.getDocwsById(id);
        List<Nation> nations = docWsService.saveNation();
        setAttr("nations", nations);
        setAttr("docwsById", docwsById);
        render("ws_edit.html");
    }

    /**
     * @return void
     * @Author Cc
     * @Description //TODO 文书修改
     * @Date 2020/3/24
     * @Param []
     **/
    @Before(DocWsValidator.class)
    public void wsUpdat() {
        Docws model = getModel(Docws.class);
        Ret ret = docWsService.saveDocws(model);
        renderJson(ret);
    }

}
