package english_report

import (
	"encoding/json"
	"eta/eta_api/controllers"
	"eta/eta_api/models"
	"eta/eta_api/services"
	"eta/eta_api/utils"
	"github.com/rdlucklib/rdluck_tools/paging"
	"html"
	"time"
)

// EnglishPolicyReportController 研报活动模块
type EnglishPolicyReportController struct {
	controllers.BaseAuthController
}

// @Title 获取策略报告详情接口
// @Description 获取报告详情
// @Param   Id   query   int  true       "报告ID"
// @Success 200 {object} models.EnglishPolicyReportDetailView
// @router /policy/detail [get]
func (this *EnglishPolicyReportController) Detail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	reportId, err := this.GetInt("Id")
	if err != nil {
		br.Msg = "获取参数失败!"
		br.ErrMsg = "获取参数失败,Err:" + err.Error()
		return
	}
	if reportId <= 0 {
		br.Msg = "参数错误"
		return
	}
	item, err := models.GetEnglishPolicyReportById(reportId)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "报告不存在!"
			return
		}
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败,Err:" + err.Error()
		return
	}

	item.Content = html.UnescapeString(item.Content)

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

// @Title 获取报告列表接口
// @Description 获取报告列表
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码,从1开始"
// @Param   StartDate   query   string  true       "开始时间"
// @Param   EndDate   query   string  true       "结束时间"
// @Param   State   query   int  true       "状态"
// @Param   KeyWord   query   string  true       "搜索关键词"
// @Success 200 {object} models.ReportListResp
// @router /policy/list [get]
func (this *EnglishPolicyReportController) ListReport() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录,SysUser Is Empty"
		br.Ret = 408
		return
	}

	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")

	startDate := this.GetString("StartDate")
	endDate := this.GetString("EndDate")
	state, _ := this.GetInt("State")
	keyWord := this.GetString("KeyWord")

	var startSize int
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = utils.StartIndex(currentIndex, pageSize)

	var condition string
	var pars []interface{}

	if keyWord != "" {
		condition += ` AND (title LIKE '%` + keyWord + `%' OR author LIKE '%` + keyWord + `%' ) `
	}
	if startDate != "" {
		condition += ` AND publish_time >= ? `
		pars = append(pars, startDate)
	}
	if endDate != "" {
		endTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
		endTime = endTime.AddDate(0, 0, 1)
		condition += ` AND publish_time <= ? `
		pars = append(pars, endTime)
	}

	if state > 0 {
		condition += ` AND state = ? `
		pars = append(pars, state)
	}
	total, err := models.GetEnglishPolicyReportListCount(condition, pars)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败,Err:" + err.Error()
		return
	}
	list, err := models.GetEnglishPolicyReportList(condition, pars, startSize, pageSize)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败,Err:" + err.Error()
		return
	}
	page := paging.GetPaging(currentIndex, pageSize, total)
	resp := new(models.EnglishPolicyReportListResp)
	resp.Paging = page
	resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 同步报告接口
// @Description 同步报告接口
// @Param	request	body models.SyncEnglishPolicyReq true "type json string"
// @Success 200 Ret=200 发布成功
// @router /policy/sync [post]
func (this *EnglishPolicyReportController) SyncReport() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录,SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req models.SyncEnglishPolicyReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}
	reportId := req.Id
	if reportId <= 0 {
		br.Msg = "请选择策略报告"
		return
	}
	err, errMsg := services.EnglishPolicyReportSync(reportId, sysUser)
	if err != nil {
		br.Msg = err.Error()
		br.ErrMsg = errMsg
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "同步成功"
}

// @Title 获取最新的策略报告
// @Description 获取最新的策略报告
// @Param	request	body models.SyncEnglishPolicyReq true "type json string"
// @Success 200 Ret=200 获取成功
// @router /policy/pull [get]
func (this *EnglishPolicyReportController) PullPolicyData() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	data, err, msg := services.PullPolicyReport()
	if err != nil {
		br.Msg = msg
		br.ErrMsg = err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = data
}