package controllers

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

type ArticleController struct {
	BaseAuthController
}

type ArticleCommonController struct {
	BaseCommonController
}

// @Title 获取报告详情
// @Description 获取报告详情接口
// @Param   ArticleId   query   int  true       "报告ID"
// @Success 200 {object} models.ArticleDetailResp
// @router /detail [get]
func (this *ArticleController) 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
	}
	uid := user.UserId
	articleId, _ := this.GetInt("ArticleId")
	if articleId <= 0 {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误"
		return
	}
	detail, err := models.GetArticleDetailById(articleId)
	if err != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取信息失败"
		return
	}

	collectCount, err := models.GetArticleCollectCount(uid, articleId)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取信息失败"
		br.ErrMsg = "判断是否已收藏失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
		return
	}

	if collectCount > 0 {
		detail.IsCollect = true
	}

	interviewApplyItem, err := models.GetArticleInterviewApply(uid, articleId)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取信息失败"
		br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
		return
	}

	if interviewApplyItem != nil && interviewApplyItem.InterviewApplyId > 0 {
		detail.IsInterviewApply = true
		detail.InterviewApplyStatus = interviewApplyItem.Status
	}

	hasPermission := 2
	articlePermission, err := models.GetArticlePermission(detail.SubCategoryName)
	if err != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
		return
	}
	if articlePermission == nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "报告权限不存在,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
		return
	}
	//GetCompanyPermission
	companyPermission, err := models.GetCompanyPermission(user.CompanyId)
	if err != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
		return
	}
	if strings.Contains(companyPermission, articlePermission.PermissionName) {
		hasPermission = 1
	}
	if hasPermission == 1 {
		//新增浏览记录
		record := new(models.CygxArticleViewRecord)
		record.UserId = uid
		record.ArticleId = articleId
		record.CreateTime = time.Now()
		record.Mobile = user.Mobile
		record.Email = user.Email
		record.CompanyId = user.CompanyId
		record.CompanyName = user.CompanyName
		go models.AddCygxArticleViewRecord(record)
	} else {
		detail.Body = ""
	}
	//获取销售手机号
	sellerItem, err := models.GetSellerByCompanyId(user.CompanyId)
	if err != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取销售数据失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
		return
	}
	if sellerItem != nil {
		detail.SellerMobile = sellerItem.Mobile
	}
	resp := new(models.ArticleDetailResp)
	resp.HasPermission = hasPermission
	resp.Detail = detail
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 收藏
// @Description 收藏
// @Param	request	body models.ArticleCollectReq true "type json string"
// @Success 200 {object} models.FontsCollectResp
// @router /collect [post]
func (this *ArticleController) ArticleCollect() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请重新登录"
		br.Ret = 408
		return
	}
	uid := user.UserId
	var req models.ArticleCollectReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}
	count, err := models.GetArticleCollectCount(uid, req.ArticleId)
	if err != nil {
		br.Msg = "获取数据失败!"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	resp := new(models.ArticleCollectResp)
	if count <= 0 {
		item := new(models.CygxArticleCollect)
		item.ArticleId = req.ArticleId
		item.UserId = uid
		item.CreateTime = time.Now()
		_, err = models.AddCygxArticleCollect(item)
		if err != nil {
			br.Msg = "收藏失败"
			br.ErrMsg = "收藏失败,Err:" + err.Error()
			return
		}
		br.Msg = "收藏成功"
		resp.Status = 1
	} else {
		err = models.RemoveArticleCollect(uid, req.ArticleId)
		if err != nil {
			br.Msg = "取消收藏失败"
			br.ErrMsg = "取消收藏失败,Err:" + err.Error()
			return
		}
		br.Msg = "已取消收藏"
		resp.Status = 2
	}
	collectTotal, err := models.GetArticleCollectUsersCount(req.ArticleId)
	if err != nil {
		br.Msg = "获取数据失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	resp.CollectCount = collectTotal
	br.Ret = 200
	br.Success = true
	br.Data = resp
}

// @Title 访谈申请
// @Description 访谈申请
// @Param	request	body models.ArticleInterviewApplyReq true "type json string"
// @Success 200 {object} models.FontsCollectResp
// @router /interview/apply [post]
func (this *ArticleController) InterviewApply() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请重新登录"
		br.Ret = 408
		return
	}
	uid := user.UserId
	var req models.ArticleInterviewApplyReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}

	article, err := models.GetArticleDetailById(req.ArticleId)
	if err != nil {
		br.Msg = "获取纪要失败!"
		br.ErrMsg = "获取纪要失败,Err:" + err.Error()
		return
	}

	count, err := models.GetArticleInterviewApplyCount(uid, req.ArticleId)
	if err != nil {
		br.Msg = "获取数据失败!"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	resp := new(models.ArticleInterviewApplyResp)
	if count <= 0 {
		item := new(models.CygxInterviewApply)
		item.ArticleId = req.ArticleId
		item.UserId = uid
		item.CompanyId = this.User.CompanyId
		item.Status = "待邀请"
		item.Sort = 1
		item.ArticleTitle = article.Title
		item.InterviewTime = time.Now()
		item.CreateTime = time.Now()
		item.ModifyTime = time.Now()
		item.ArticleIdMd5 = article.ArticleIdMd5
		_, err = models.AddCygxInterviewApply(item)
		if err != nil {
			br.Msg = "申请失败"
			br.ErrMsg = "申请失败,Err:" + err.Error()
			return
		}
		br.Msg = "申请成功"
		resp.Status = 1
	} else {
		err = models.RemoveArticleInterviewApply(uid, req.ArticleId)
		if err != nil {
			br.Msg = "取消申请失败"
			br.ErrMsg = "取消申请失败,Err:" + err.Error()
			return
		}
		br.Msg = "已取消申请"
		resp.Status = 2
	}
	br.Ret = 200
	br.Success = true
	br.Data = resp
}

// @Title 获取报告详情
// @Description 获取报告详情接口
// @Param   ArticleIdMd5   query   int  true       "报告ID"
// @Success 200 {object} models.ArticleDetailResp
// @router /look/detail [get]
func (this *ArticleCommonController) Detail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	articleIdMd5:= this.GetString("ArticleIdMd5")
	if articleIdMd5 =="" {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误"
		return
	}
	resp := new(models.ArticleDetailResp)
	detail, err := models.GetArticleDetailByIdMd5(articleIdMd5)
	if err != nil && err.Error()!=utils.ErrNoRow() {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取信息失败,Err:"+err.Error()
		return
	}
	if detail==nil {
		resp.HasPermission = 2
	}else{
		resp.HasPermission = 1
	}
	resp.Detail = detail
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}