package controllers

import (
	"encoding/json"
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/utils"
	"strconv"
	"time"
)

// 问卷调查
type QuestionnaireController struct {
	BaseAuthController
}

// @Title 详情
// @Description 详情接口
// @Param   IsBestNew   query   bool  false       "是否获取最新的一条问卷消息"
// @Param   QuestionnaireId   query   int  true       "问卷ID"
// @Success Ret=200 {object} cygx.GetCygxTacticsTimeLineResp
// @router /detail [get]
func (this *QuestionnaireController) Detail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
		return
	}
	resp := new(models.GetCygxQuestionnaireDetailResp)
	questionnaireId, _ := this.GetInt("QuestionnaireId")
	isBestNew, _ := this.GetBool("IsBestNew", false)
	var err error
	detail := new(models.CygxQuestionnaireResp)
	//如果是最新的就获取最新的一套问卷信息
	if isBestNew {
		detail, err = models.GetCygxQuestionnaireDetailBestNew()
		if err != nil {
			br.Msg = "详情不存在"
			br.ErrMsg = "获取失败，GetCygxQuestionnaireDetailBestNew Err:" + err.Error()
			return
		}
		questionnaireId = detail.QuestionnaireId
	} else {
		detail, err = models.GetCygxQuestionnaireDetail(questionnaireId)
		if err != nil {
			br.Msg = "详情不存在"
			br.ErrMsg = "获取失败，GetCygxQuestionnaireDetail Err:" + err.Error()
			return
		}
	}

	ListTheme, err := models.GetCygxQuestionnaireThemeListByQuestionnaireId(questionnaireId)
	if err != nil {
		br.Msg = "详情不存在"
		br.ErrMsg = "获取失败，GetCygxQuestionnaireThemeListByQuestionnaireId Err:" + err.Error()
		return
	}
	detail.ListTheme = ListTheme
	resp.Detail = detail
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 问卷投票
// @Description 问卷投票接口
// @Param   QuestionnaireId   query   int  true       "问卷ID"
// @Success Ret=200 {object} cygx.GetCygxTacticsTimeLineResp
// @router /vote_add [post]
func (this *QuestionnaireController) VoteAdd() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
		return
	}
	uid := user.UserId
	var req models.CygxQuestionnaireVoteReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if uid == 0 {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
	}

	questionnaireId := req.QuestionnaireId
	questionnaireThemeIds := req.QuestionnaireThemeIds
	content := req.Content
	if len(questionnaireThemeIds) == 0 && content == "" {
		br.Msg = "提交信息不能为空"
		return
	}

	detail, err := models.GetCygxQuestionnaireDetail(questionnaireId)
	if err != nil {
		br.Msg = "提交失败"
		br.ErrMsg = "获取失败，GetCygxQuestionnaireDetail Err:" + err.Error()
		return
	}

	endTime := utils.StrTimeToTime(detail.EndTime + " 23:59:59") //时间字符串格式转时间格式
	if endTime.Before(time.Now()) {
		br.Msg = "此次投票已截止"
		return
	}

	lenCheck := len(questionnaireThemeIds)
	if lenCheck > detail.MaxChooseTotal {
		br.Msg = "最多可选择" + strconv.Itoa(detail.MaxChooseTotal) + "项"
		return
	}

	sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}

	var voteItems []*models.CygxQuestionnaireVote
	//主题ID提交问卷需要写入的信息
	if lenCheck > 0 {
		var condition string
		var pars []interface{}
		condition += " AND  user_id = ?  AND questionnaire_id = ? "
		pars = append(pars, user.UserId, questionnaireId)

		totalhistory, err := models.GetCygxQuestionnaireVoteCount(condition, pars)
		if err != nil {
			br.Msg = "提交失败"
			br.ErrMsg = "获取失败，GetCygxQuestionnaireDetail Err:" + err.Error()
			return
		}
		if totalhistory > 0 {
			br.Msg = "您已投过票"
			return
		}
		for _, v := range questionnaireThemeIds {
			item := new(models.CygxQuestionnaireVote)
			item.QuestionnaireId = questionnaireId
			item.QuestionnaireThemeId = v
			item.UserId = user.UserId
			item.CreateTime = time.Now()
			item.ModifyTime = time.Now()
			item.Mobile = user.Mobile
			item.Email = user.Email
			item.CompanyId = user.CompanyId
			item.CompanyName = user.CompanyName
			item.RealName = user.RealName
			if sellerItem != nil {
				item.SellerName = sellerItem.RealName
			}
			item.RegisterPlatform = utils.REGISTER_PLATFORM
			voteItems = append(voteItems, item)
		}
	}

	//其它主题类型提交需要写入的信息
	itemOther := new(models.CygxQuestionnaireVoteOtherTheme)
	if content != "" {
		itemOther.QuestionnaireId = questionnaireId
		itemOther.UserId = user.UserId
		itemOther.CreateTime = time.Now()
		itemOther.ModifyTime = time.Now()
		itemOther.Mobile = user.Mobile
		itemOther.Email = user.Email
		itemOther.CompanyId = user.CompanyId
		itemOther.CompanyName = user.CompanyName
		itemOther.RealName = user.RealName
		itemOther.Content = content
		if sellerItem != nil {
			itemOther.SellerName = sellerItem.RealName
		}
		itemOther.RegisterPlatform = utils.REGISTER_PLATFORM
	}
	err = models.AddCygxQuestionnaireVote(voteItems, itemOther)
	if err != nil {
		br.Msg = "提交失败"
		br.ErrMsg = "获取失败，AddCygxQuestionnaireVote Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "提交成功"
}
