package com.huiju.foyo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import com.huiju.foyo.common.Md5Utils;
import com.huiju.foyo.common.ResponseBean;
import com.huiju.foyo.session.SessionLoad;
import com.huiju.foyo.dao.SysUserMapper;
import com.huiju.foyo.model.SysUser;
import com.huiju.foyo.service.ISysUserService;
import com.huiju.foyo.session.UserControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 案例表  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-08-21
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public SysUser getUserByLoginName(String name) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name", name);
        wrapper.last("limit 1");

        return sysUserMapper.selectOne(wrapper);
    }


    @Override
    public ResponseBean login(String jsonString) {
        ResponseBean responseBean = new ResponseBean();

        JSONObject params = JSONObject.parseObject(jsonString);
        if (null == params) {
            responseBean.addError("请填写用户名及密码");
            return responseBean;
        }

        String name = params.getString("username");
        String passed = params.getString("password");

        if (null == name || "".equals(name.trim()) || null == passed || "".equals(passed.trim())) {
            responseBean.addError("用户名或者密码不正确");
            return responseBean;
        }

        SysUser sysUser = getUserByLoginName(name);

        // 比较密码是否一致
        if (!Md5Utils.isMd5ThanPass(sysUser.getPasswd(), passed)){
            responseBean.addError("用户名或者密码不正确");
            return responseBean;
        }

        // 生成 session 或者 token
        UserControl.setUser(sysUser);

        // 返回内容密码为空
        sysUser.setPasswd(null);

        responseBean.addSuccess(sysUser);
        return responseBean;
    }

    @Override
    public ResponseBean upPass(String oldPass, String newPass) {
        ResponseBean responseBean = new ResponseBean();

        SysUser sessionUser = UserControl.getUser();

        // 根据用户名查询最新信息
        SysUser userByLoginName = getUserByLoginName(sessionUser.getName());
        if (!Md5Utils.isMd5ThanPass(userByLoginName.getPasswd(), oldPass)) {
            responseBean.addError("请输入正确的旧密码");
            return responseBean;
        }

        if (newPass.length() < 6) {
            responseBean.addError("新密码不能小于6位");
            return responseBean;
        }

        String encryptionMd5 = Md5Utils.encryptionMd5(newPass);
        userByLoginName.setPasswd(encryptionMd5);

        this.updateById(userByLoginName);

        responseBean.addSuccess("操作成功");
        return responseBean;
    }
}
