package controllers

import (
	"encoding/json"
	"hongze/hongze_admin/models"
	"hongze/hongze_admin/utils"
	"time"
)

type SysUserController struct {
	BaseCommonController
}

// @Title 系统用户登录接口
// @Description 系统用户登录
// @Param	request	body models.LoginReq true "type json string"
// @Success 200 {object} models.LoginResp
// @router /login [post]
func (this *SysUserController) Login() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.LoginReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}

	if req.Username == "" {
		br.Msg = "请输入账号"
		return
	}

	if req.Password == "" {
		br.Msg = "请输入密码"
		return
	}
	sysUser, err := models.CheckSysUser(req.Username, req.Password)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "登录失败,账号或密码错误"
			br.ErrMsg = "登录失败,账号或密码错误,Err:" + err.Error()
			return
		}
		br.Msg = "登录失败,账号或密码错误"
		br.ErrMsg = "登录失败,Err:" + err.Error()
		return
	}
	if sysUser == nil {
		br.Msg = "登录失败,账号或密码错误"
		br.ErrMsg = "登录失败,sysUser is nil "
		return
	}
	account := utils.MD5(req.Username)
	token := utils.GenToken(account)
	sysSession := new(models.SysSession)
	sysSession.UserName = req.Username
	sysSession.SysUserId = sysUser.AdminId
	sysSession.ExpiredTime = time.Now().AddDate(0, 0, 90)
	sysSession.CreatedTime = time.Now()
	sysSession.LastUpdatedTime = time.Now()
	sysSession.AccessToken=token
	err = models.AddSysSession(sysSession)
	if err != nil {
		br.ErrMsg = "新增session信息失败,Err:" + err.Error()
		br.Msg = "登录失败"
		return
	}
	resp := new(models.LoginResp)
	resp.Authorization = token
	resp.Authorization = "authorization="+token+";account="+account
	br.Ret = 200
	br.Success = true
	br.Msg = "登录成功"
	br.Data = resp
	//新增登录记录
	{
		record := new(models.SysUserLoginRecord)
		record.Uid = sysUser.AdminId
		record.UserName = req.Username
		record.Ip = this.Ctx.Input.IP()
		record.Stage = "login"
		record.CreateTime = time.Now()
		go models.AddSysUserLoginRecord(record)
	}
}

type SysUserAuthController struct {
	BaseAuthController
}

// @Title 修改密码
// @Description 修改密码
// @Param	request	body models.ModifyPwdReq true "type json string"
// @Success 200 {object} models.LoginResp
// @router /modify/pwd [post]
func (this *SysUserAuthController) ModifyPwd() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.ModifyPwdReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "登录失败,账号或密码错误"
		br.ErrMsg = "登录失败,sysUser is nil "
		return
	}
	if req.OldPwd == "" {
		br.Msg = "原始密码不能为空"
		return
	}

	if req.NewPwd == "" {
		br.Msg = "新密码不能为空"
		return
	}

	if sysUser.Password != req.OldPwd {
		br.Msg = "旧密码错误,请重新输入"
		return
	}
	err = models.ModifyPwd(sysUser.AdminId, req.NewPwd)
	if err != nil {
		br.Msg = "修改失败"
		br.ErrMsg = "修改失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "修改成功"
}