package eta_trial

import (
	"encoding/json"
	"eta/eta_mobile/controllers"
	"eta/eta_mobile/models"
	"eta/eta_mobile/models/eta_trial"
	etaTrialService "eta/eta_mobile/services/eta_trial"
	"eta/eta_mobile/utils"
)

// EtaTrialController ETA试用
type EtaTrialController struct {
	controllers.BaseAuthController
}

// UpdateActiveTime
// @Title 累计活跃时长
// @Description 累计活跃时长
// @Param   ActiveTime	query	int		true	"活跃时长"
// @Param   Part		query	string	true	"活跃板块"
// @Success 200 {object} models.ETATrialAddReq
// @router /user/active [post]
func (this *EtaTrialController) UpdateActiveTime() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	// 仅试用平台用
	if utils.BusinessCode != utils.BusinessCodeSandbox {
		br.Ret = 200
		br.Success = true
		br.Msg = "操作成功"
		return
	}

	var req eta_trial.UpdateUserActiveTimeReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.Part == "" {
		br.Ret = 200
		br.Success = true
		br.Msg = "操作成功"
		return
	}

	params := etaTrialService.UpdateEtaTrialUserActiveTimeReq{
		UserName:   sysUser.RealName,
		Mobile:     sysUser.Mobile,
		ActiveTime: req.ActiveTime,
		Part:       req.Part,
	}
	res, e := etaTrialService.UpdateEtaTrialUserActiveTime(params)
	if e != nil {
		br.Msg = "操作失败"
		br.ErrMsg = "更新ETA试用用户活跃时长失败, Err: " + e.Error()
		return
	}
	if !res {
		br.Msg = "操作失败"
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "新增成功"
}

// UpdateLoginDuration
// @Title 更新用户登录时长
// @Description 更新用户登录时长
// @Param   ActiveTime	query	int		true	"活跃时长"
// @Success 200 {object} models.ETATrialAddReq
// @router /user/login_duration [post]
func (this *EtaTrialController) UpdateLoginDuration() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	// 仅试用平台用
	if utils.BusinessCode != utils.BusinessCodeSandbox {
		br.Ret = 200
		br.Success = true
		br.Msg = "操作成功"
		return
	}
	var req eta_trial.UpdateUserLoginDurationReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	params := etaTrialService.UpdateEtaTrialUserLoginDurationReq{
		UserName:   sysUser.RealName,
		Mobile:     sysUser.Mobile,
		ActiveTime: req.ActiveTime,
	}
	res, e := etaTrialService.UpdateEtaTrialUserLoginDuration(params)
	if e != nil {
		br.Msg = "操作失败"
		br.ErrMsg = "更新ETA试用用户登录时长失败, Err: " + e.Error()
		return
	}
	if !res {
		br.Msg = "操作失败"
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "新增成功"
}
