package com.jfinal.club.my.newsfeed;

import com.jfinal.club.common.model.*;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;

import static com.jfinal.club.login.LoginService.loginUsersCacheName;
import static com.jfinal.club.login.LoginService.sessionIdName;

/**
 * @ClassName UsersService
 * @Description TODO
 * @Author caolong
 * @Date 2020/3/17 16:31
 */
public class UsersService {


    private Users usersDao = new Users().dao();

    private Doc doc = new Doc().dao();

    private Loveings loveings = new Loveings().dao();

    private Userct userct = new Userct().dao();

    private Questions questions = new Questions().dao();

    public Users findUsersById(int usersid) {
        return usersDao.findById(usersid);
    }

    public Ret updateUsersInfo(Users users, Integer userId) {
        users.setId(userId);
        boolean update = users.removeNullValueAttrs().update();
        if (update) {
            return Ret.ok("msg","更新成功");
        } else {
            return Ret.fail("msg", "修改失败");
        }
    }

    /**
     * 修改密码 手机号码
     */
    public Ret updatePassword(int usersId, String pwd, String rpwd) {
        if (StrKit.isBlank(pwd)) {
            return Ret.fail("msg", "新密码不能为空");
        }
        if (StrKit.isBlank(rpwd)) {
            return Ret.fail("msg", "确认密码不能为空");
        }
        if (pwd.length() < 6) {
            return Ret.fail("msg", "新密码长度不能小于 6");
        }
        Users users = usersDao.findById(usersId);
        int result = Db.update("update users set pwds=? where id=? limit 1", pwd, usersId);
        if (result > 0) {
            return Ret.ok("msg", "密码更新成功");
        } else {
            return Ret.fail("msg", "未找到账号，请联系管理员");
        }
    }

    /**
     * 修改密码  旧密码
     */
    public Ret updatePassword(int usersId, String oldpwds, String newpwds, String rnewpwds) {
        if (newpwds.length() < 6) {
            return Ret.fail("msg", "新密码长度不能小于 6");
        }
        if (!newpwds.equals(rnewpwds)) {
            return Ret.fail("msg", "俩次密码输入不一致");
        }
        Users users = usersDao.findById(usersId);
        if (!users.getPwds().equals(oldpwds)) {
            return Ret.fail("msg", "旧密码错误,请重新输入");
        }
        int result = Db.update("update users set pwds=? where id=? limit 1", newpwds, usersId);
        if (result > 0) {
            return Ret.ok("msg", "密码更新成功");
        } else {
            return Ret.fail("msg", "未找到账号，请联系管理员");
        }
    }


    public Object myCollectByType(int type, int usersId, int pageNum) {
        String loveType = "";
        if (type == 1) {
            return doc.paginate(pageNum, 10, "select d.* ", "from loveings l ,doc d where l.type = 'doc' and l.objid = d.id and l.usersid = ? order by l.addtime desc", usersId);
        } else if (type == 2) {
            return userct.paginate(pageNum, 10, "select c.* ", "from loveings l ,userct c where l.type = 'userct' and l.objid = c.id and l.usersid = ? order by l.addtime desc", usersId);
        } else {
            return questions.paginate(pageNum, 10, "select q.* ", "from loveings l ,questions q where l.type = 'ques' and l.objid = q.id and l.usersid = ? order by l.addtime desc", usersId);
        }
    }

}
