// Package document_manage
// @Author gmy 2024/9/19 14:09:00
package document_manage

import (
	"encoding/json"
	"eta/eta_mobile/controllers"
	"eta/eta_mobile/models"
	"eta/eta_mobile/models/document_manage_model"
	"eta/eta_mobile/services/document_manage_service"
	"eta/eta_mobile/utils"
	"strings"
)

// DocumentManageController 文档管理库
type DocumentManageController struct {
	controllers.BaseAuthController
}

// ValidateUser
// 处理响应和校验
func ValidateUser(this *DocumentManageController, br *models.BaseResponse) bool {
	// 验证用户是否已登录
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return false
	}

	return true
}

// DocumentClassifyList
// @Title 文档分类列表
// @Description 文档分类列表
// @Success 200 {object} []models.ClassifyVO
// @router /document/classify/list [get]
func (this *DocumentManageController) DocumentClassifyList() {
	br := new(models.BaseResponse).Init()

	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()

	if !ValidateUser(this, br) {
		return
	}

	sysUser := this.SysUser
	classifyList, err := document_manage_service.DocumentClassifyList(sysUser.AdminId)
	if err != nil {
		br.Msg = "获取分类列表失败"
		br.ErrMsg = "获取分类列表失败，Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = classifyList
	return
}

// DocumentCollectClassify
// @Title 收藏/取消分类
// @Description 收藏/取消分类
// @Success 200 “操作成功”
// @router /document/collect/classify [post]
func (this *DocumentManageController) DocumentCollectClassify() {
	br := new(models.BaseResponse).Init()

	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()

	if !ValidateUser(this, br) {
		return
	}

	sysUser := this.SysUser
	type collectClassify struct {
		ClassifyId int `json:"ClassifyId"`
	}

	var req *collectClassify
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + e.Error()
		return
	}

	err := document_manage_service.DocumentCollectClassify(req.ClassifyId, sysUser.AdminId)
	if err != nil {
		br.Msg = "收藏分类失败"
		br.ErrMsg = "收藏分类失败，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	return
}

// DocumentVarietyList
// @Title 文档品种列表
// @Description 文档品种列表
// @Success 200 {object} []models.ChartPermission
// @router /document/variety/list [get]
func (this *DocumentManageController) DocumentVarietyList() {
	br := new(models.BaseResponse).Init()

	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()

	if !ValidateUser(this, br) {
		return
	}

	chartPermissionList, err := document_manage_service.DocumentVarietyList()
	if err != nil {
		br.Msg = "获取品种列表失败"
		br.ErrMsg = "获取品种列表失败，Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = chartPermissionList
	return
}

// DocumentReportList
// @Title 文档管理库报告列表
// @Description 文档管理库报告列表
// @Success 200 {object} document_manage_model.OutsideReportPage
// @router /document/report/list [get]
func (this *DocumentManageController) DocumentReportList() {
	br := new(models.BaseResponse).Init()

	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()

	if !ValidateUser(this, br) {
		return
	}

	// 文档类型 1-文档管理库 2-战研中心-pci
	documentType, err := this.GetInt("DocumentType")
	if err != nil {
		br.Msg = "获取文档类型失败"
		br.ErrMsg = "获取文档类型失败，Err:" + err.Error()
		return
	}
	if documentType == 0 {
		br.Msg = "文档类型不能为空"
		br.ErrMsg = "文档类型不能为空"
		return
	}

	chartPermissionIdString := this.GetString("ChartPermissionIdList")
	var chartPermissionIdList []string
	if strings.TrimSpace(chartPermissionIdString) != "" {
		chartPermissionIdList = strings.Split(chartPermissionIdString, ",")
	}

	classifyIdString := this.GetString("ClassifyIdList")
	var classifyIdList []string
	if strings.TrimSpace(classifyIdString) != "" {
		classifyIdList = strings.Split(classifyIdString, ",")
	}

	keyword := this.GetString("Keyword")
	orderField := this.GetString("OrderField")
	orderType := this.GetString("OrderType")

	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	documentReportPage, err := document_manage_service.DocumentReportList(this.SysUser.AdminId, documentType, chartPermissionIdList, classifyIdList, keyword, orderField, orderType, currentIndex, pageSize)
	if err != nil {
		br.Msg = "获取报告列表失败"
		br.ErrMsg = "获取报告列表失败，Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = documentReportPage
	return
}

// RuiSiReportList
// @Title 睿思报告列表
// @Description 睿思报告列表
// @Success 200 {object} models.ReportListResp
// @router /document/rui/si/report/list [get]
func (this *DocumentManageController) RuiSiReportList() {
	br := new(models.BaseResponse).Init()

	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()

	if !ValidateUser(this, br) {
		return
	}

	classifyIdListStr := this.GetString("ClassifyIdList")
	var classifyIdList []string
	if strings.TrimSpace(classifyIdListStr) != "" {
		classifyIdList = strings.Split(classifyIdListStr, ",")
	}
	chartPermissionIdString := this.GetString("ChartPermissionIdList")
	var chartPermissionIdList []string
	if strings.TrimSpace(chartPermissionIdString) != "" {
		chartPermissionIdList = strings.Split(chartPermissionIdString, ",")
	}

	keyword := this.GetString("Keyword")
	orderField := this.GetString("OrderField")
	orderType := this.GetString("OrderType")

	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	RuiSiReportPage, err := document_manage_service.RuiSiReportListV2(classifyIdList, chartPermissionIdList, keyword, orderField, orderType, this.SysUser.AdminId, currentIndex, pageSize)
	if err != nil {
		br.Msg = "获取报告列表失败"
		br.ErrMsg = "获取报告列表失败，Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = RuiSiReportPage
	return
}

// DocumentRuiSiDetail
// @Title 睿思报告详情
// @Description 睿思报告详情
// @Success 200 “获取成功”
// @router /document/rui/si/detail [get]
func (this *DocumentManageController) DocumentRuiSiDetail() {
	br := new(models.BaseResponse).Init()

	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()

	if !ValidateUser(this, br) {
		return
	}

	// 获取指标数据列表

	reportId, err := this.GetInt("ReportId")
	if err != nil {
		br.Msg = "获取报告ID失败"
		br.ErrMsg = "获取报告ID失败，Err:" + err.Error()
		return
	}

	reportDetail, err := document_manage_service.DocumentRuiSiDetail(reportId)
	if err != nil {
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = &reportDetail
	return
}

// DocumentSave
// @Title 新建文档
// @Description 新建文档
// @Success 200 “操作成功”
// @router /document/save [post]
func (this *DocumentManageController) DocumentSave() {
	br := new(models.BaseResponse).Init()

	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()

	if !ValidateUser(this, br) {
		return
	}

	sysUser := this.SysUser
	var req *document_manage_model.OutsideReportBO
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + e.Error()
		return
	}

	req.SysUserId = sysUser.AdminId
	req.SysUserName = sysUser.AdminName
	err := document_manage_service.DocumentSave(req)
	if err != nil {
		br.Msg = "保存文档失败"
		br.ErrMsg = "保存文档失败，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	return
}

// DocumentDetail
// @Title 文档详情
// @Description 文档详情
// @Success 200 “操作成功”
// @router /document/detail [get]
func (this *DocumentManageController) DocumentDetail() {
	br := new(models.BaseResponse).Init()

	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()

	if !ValidateUser(this, br) {
		return
	}

	// 获取指标数据列表

	outsideReportId, err := this.GetInt("OutsideReportId")
	if err != nil {
		br.Msg = "获取报告ID失败"
		br.ErrMsg = "获取报告ID失败，Err:" + err.Error()
		return
	}

	reportDetail, err := document_manage_service.DocumentReportDetail(outsideReportId)
	if err != nil {
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = &reportDetail
	return
}

// DocumentUpdate
// @Title 编辑文档
// @Description 编辑文档
// @Success 200 “操作成功”
// @router /document/update [post]
func (this *DocumentManageController) DocumentUpdate() {
	br := new(models.BaseResponse).Init()

	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()

	if !ValidateUser(this, br) {
		return
	}

	var req *document_manage_model.OutsideReportBO
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + e.Error()
		return
	}
	err := document_manage_service.DocumentUpdate(req)
	if err != nil {
		br.Msg = "修改文档失败"
		br.ErrMsg = "修改文档失败，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	return
}

// DocumentDelete
// @Title 删除文档
// @Description 删除文档
// @Success 200 “操作成功”
// @router /document/delete [post]
func (this *DocumentManageController) DocumentDelete() {
	br := new(models.BaseResponse).Init()

	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()

	if !ValidateUser(this, br) {
		return
	}

	type DocumentDelete struct {
		OutsideReportId int `json:"OutsideReportId"`
	}

	var req *DocumentDelete
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + e.Error()
		return
	}
	err := document_manage_service.DocumentDelete(req.OutsideReportId)
	if err != nil {
		br.Msg = "删除文档失败"
		br.ErrMsg = "删除文档失败，Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	return
}
