package com.jfinal.club.my.ques;

import com.jfinal.aop.Inject;
import com.jfinal.club.common.model.Quesanswer;
import com.jfinal.club.common.model.Questions;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName QuestionsService
 * @Description TODO
 * @Author caolong
 * @Date 2020/3/21 18:29
 */
public class QuestionsService {


    @Inject
    Questions questionsdao = new Questions( ).dao( );


    @Inject
    Quesanswer quesanswer = new Quesanswer( ).dao( );

    public Ret saveQues(Questions questions, String avatarUrl, int userId) {
        questions.setQuesimg(avatarUrl);
        questions.setAddtime(new Date( ));
        questions.setUsersid(userId);
        questions.setStatus(0);
        questions.setTops(0);
        questions.setNews(0);
        questions.setSort(0);
        if (questions.save( )) {
            return Ret.ok("msg", "添加成功");
        } else {
            return Ret.fail("msg", "添加失败");
        }
    }

    public Ret upQues(Questions questions, String avatarUrl, int usersid, Boolean keepLogin) {
        if (!StrKit.isBlank(avatarUrl)) {
            questions.setQuesimg(avatarUrl);
        }
        questions.setAddtime(new Date( ));
        if (keepLogin) {
            questions.setAnonymous(1);
        } else {
            questions.setAnonymous(0);
        }
        questions.setUsersid(usersid);
        questions.setStatus(0);
        questions.setTops(0);
        questions.setSort(0);
        questions.setNews(0);
        if (questions.update( )) {
            return Ret.ok("msg", "添加成功");
        } else {
            return Ret.fail("msg", "添加失败");
        }
    }

    public Page< Questions > getQuesList(int usersId, int status, String text, int pageNum) {
        String parm = "";
        if (!StrKit.isBlank(text)) {
            parm = "%" + text + "%";
        } else {
            parm = "%%";
        }
        Page< Questions > questionsPage = null;
        if (status == 1) {
//            Long aLong = Db.queryLong("select count(*) from quesanswer where level = 0");
            questionsPage = questionsdao.paginate(pageNum, 10, "select * ", "from ( select q.* ,count(w.id) as num from questions as q left JOIN quesanswer as w ON q.id = w.quesid and level = 0 GROUP BY q.id order by q.tops desc, q.addtime desc) a where  a.usersid = ? and a.status = ? and a.quesname like ? ", usersId, status, parm);
        } else {
            questionsPage = questionsdao.paginate(pageNum, 10, "select * ", "from questions where usersid = ? and status = ? and quesname like ? order by tops desc, addtime desc", usersId, status, parm);
        }
        return questionsPage;
    }


    public Ret deleteById(int id) {
        boolean b = questionsdao.deleteById(id);
        if (b) {
            return Ret.ok("msg", "删除成功");
        } else {
            return Ret.fail("msg", "删除失败");
        }
    }

    public Questions getQuestionsById(int id) {
        return questionsdao.findById(id);
    }


    public Page< Questions > getUserCtListAll(String text, int size, int pageNum) {
        String parm = "";
        if (!StrKit.isBlank(text)) {
            parm = "%" + text + "%";
        } else {
            parm = "%%";
        }
        return questionsdao.paginate(pageNum, size, "select * ", "from questions where status = 1 and quesname like ? order by tops desc,addtime desc", parm);
    }

    /**
     * @return java.lang.Long
     * @Author Cc
     * @Description //TODO 查看提问帖子数量
     * @Date 2020/3/23
     * @Param [usersId]
     **/
    public Long countQuesById(int usersId) {
        return Db.queryLong("select count(*) from questions where usersid = ? and status = 1", usersId);
    }

    /**
     * @return java.lang.Long
     * @Author Cc
     * @Description //TODO 查看回答帖子数量
     * @Date 2020/3/23
     * @Param [usersId]
     **/
    public Long countQuesanswerByQuesId(int usersId) {
        return Db.queryLong("select count(*) from quesanswer where usersid = ? and level = 0", usersId);
    }

    public Long countQuesanswerByQuesIdAndStatus(int usersId) {
        return Db.queryLong("select count(*) from quesanswer where usersid = ? and level = 1 and status = 1", usersId);
    }


    public List< Questions > questionsListByTop( ) {
        return questionsdao.find("select * from questions where status = 1 and tops = 1 order by addtime desc");
    }

    public List< Questions > questionsListByAddtime( ) {
        return questionsdao.find("select q.* ,count(w.id) as num from questions  q LEFT JOIN quesanswer w ON q.id = w.quesid  where  q.status = 1 GROUP BY q.id order by q.news desc, q.addtime desc limit 6");
    }

    public Page< Questions > getQuestionsByHotOrNews(String text, String type, int pageNum, int size, int doctype) {
        String parm = "";
        if (!StrKit.isBlank(text)) {
            parm = "%" + text + "%";
        } else {
            parm = "%%";
        }
        if (!StrKit.isBlank(type)) {
            if ("1".equals(type)) {
                return questionsdao.paginate(pageNum, size, "select q.* ,count(w.id) as num", "from questions  q LEFT JOIN quesanswer w ON q.id = w.quesid where q.quesname like ? and q.status = 1 GROUP BY q.id order by q.tops desc, q.addtime desc", parm);
            } else if ("2".equals(type)) {
                return questionsdao.paginate(pageNum, size, "select q.* ,count(w.id) as num", "from questions  q LEFT JOIN quesanswer w ON q.id = w.quesid where q.quesname like ? and q.status = 1 GROUP BY q.id order by q.news desc, q.addtime desc", parm);
            } else {
                return questionsdao.paginate(pageNum, size, "select q.* ,count(w.id) as num", "from questions  q LEFT JOIN quesanswer w ON q.id = w.quesid where q.quesname like ? and q.status = 1 and q.doctype = ? GROUP BY q.id order by q.news desc, q.addtime desc", parm, doctype);
            }
        }
        return null;
    }

    /**
     * @return com.jfinal.plugin.activerecord.Page<com.jfinal.club.common.model.Quesanswer>
     * @Author Cc
     * @Description //TODO  通过问答id查看评论
     * @Date 2020/3/24
     * @Param [quesId, pageNum]
     **/
    public Page< Quesanswer > getQuesanswerByQuesId(int quesId, int pageNum) {
        List< Quesanswer > getQuesanswerByLevel = null;
        Page< Quesanswer > quesanswerPage = quesanswer.paginate(pageNum, 10, "select u.*,q.*", "from quesanswer q , users u where q.usersid = u.id and q.quesid = ? and level = 0 order by q.status desc ,q.addtime desc", quesId);
        for (Quesanswer quesanswer : quesanswerPage.getList( )) {
            if (getQuesanswerByLevel(quesanswer.getId( )) != null) {
                getQuesanswerByLevel = new ArrayList<>( );
                getQuesanswerByLevel.addAll(getQuesanswerByLevel(quesanswer.getId( )));
            }
            quesanswer.setGetQuesanswerByLevel(getQuesanswerByLevel);
        }
        return quesanswerPage;
    }


    public Ret saveQuesanswer(int usersid, String text, int quesId) {
        Quesanswer quesanswer = new Quesanswer( );
        quesanswer.setText(text);
        quesanswer.setLevel(0);
        quesanswer.setStatus(0);
        quesanswer.setAddtime(new Date( ));
        quesanswer.setUsersid(usersid);
        quesanswer.setQuesid(quesId);
        boolean save = quesanswer.save( );
        if (save) {
            return Ret.ok("msg", "评论成功");
        } else {
            return Ret.fail("msg", "评论失败");
        }
    }


    public Ret huifQuesanswer(int usersid, String text, int quesanswerId, int quesId) {
        Quesanswer quesanswer = new Quesanswer( );
        quesanswer.setText(text);
        quesanswer.setLevel(quesanswerId);
        quesanswer.setStatus(0);
        quesanswer.setAddtime(new Date( ));
        quesanswer.setUsersid(usersid);
        quesanswer.setQuesid(quesId);
        boolean save = quesanswer.save( );
        if (save) {
            return Ret.ok("msg", "评论成功");
        } else {
            return Ret.fail("msg", "评论失败");
        }
    }

    /**
     * @return java.util.List<com.jfinal.club.common.model.Quesanswer>
     * @Author Cc
     * @Description //TODO 通过评论id  查看回复评论
     * @Date 2020/3/24
     * @Param [level]
     **/
    public List< Quesanswer > getQuesanswerByLevel(int level) {
        return quesanswer.find("select u.* ,q.* from quesanswer q , users u where u.id = q.usersid and  level = ?", level);
    }

    public Ret upStatusByQuesanswerId(int quesanswerId, int status) {
        quesanswer.setId(quesanswerId);
        quesanswer.setStatus(status);
        boolean update = quesanswer.update( );
        if (update) {
            return Ret.ok("msg", "修改成功");
        } else {
            return Ret.fail("msg", "修改失败");
        }
    }

    /**
     * @return
     * @Author Cc
     * @Description //TODO  根据用户查询回复的问题
     * @Date 2020/3/24
     * @Param
     **/
    public Page< Questions > getQuesanswerByUsersId(int usersId, int pageNum) {
        return questionsdao.paginate(pageNum, 10, "select q.*", "from quesanswer as w , questions as q  where w.quesid = q.id and w.usersid = ? and w.level = 0 GROUP BY q.id order by w.addtime", usersId);
    }


    /**
     * @return
     * @Author Cc
     * @Description //TODO 首页推荐
     * @Date 2020/3/24
     * @Param
     **/
    public Page< Questions > getQuesBySort(int pageNum, int size) {
        return questionsdao.paginate(pageNum, size, "select q.*,u.tximg,u.nickname", "from questions q ,users u where q.usersid = u.id and status = 1 order by sort desc,addtime desc");
    }

}
