package controllers

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

// ReportAuthorController 报告作者
type ReportAuthorController struct {
	BaseAuthController
}

// ReportAuthorCommonController 报告作者
type ReportAuthorCommonController struct {
	BaseCommonController
}

// Author
// @Title 获取报告作者接口
// @Description 获取报告作者
// @Param   AuthorType   query   int  true       "来源类型,1:中文,2:英文"
// @Param   Keyword   query   string  true       "搜索关键词"
// @Param   CurrentIndex   query   int  true       "当前页页码,从1开始"
// @Param   StartDate   query   string  true       "开始时间"
// @Success 200 {object} models.ReportAuthorResp
// @router /author [get]
func (this *ReportAuthorController) Author() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	//来源类型,1:中文,2:英文
	authorType := this.GetString("AuthorType", "1")
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")

	keyword := this.GetString("Keyword")

	var startSize int
	if pageSize <= 0 {
		pageSize = utils.PageSize50
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = utils.StartIndex(currentIndex, pageSize)
	var condition string
	var pars []interface{}
	condition += ` AND author_type = ? `
	pars = append(pars, authorType)

	if keyword != `` {
		condition += ` AND report_author like ? `
		pars = append(pars, utils.GetLikeKeyword(keyword))
	}

	total, items, err := models.GetReportAuthorList(condition, pars, startSize, pageSize)
	if err != nil {
		br.Msg = "获取失败!"
		br.ErrMsg = "获取失败,Err:" + err.Error()
		return
	}
	page := paging.GetPaging(currentIndex, pageSize, total)
	resp := models.ReportAuthorResp{
		List:   items,
		Paging: page,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// AddAuthor
// @Title 新增报告作者接口
// @Description 新增报告作者接口
// @Param	request	body models.AddReportAuthorReq true "type json string"
// @Success 200 Ret=200 添加成功
// @router /author/add [post]
func (this *ReportAuthorController) AddAuthor() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.AddReportAuthorReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}

	authorName := strings.TrimSuffix(req.Author, " ")
	authorName = strings.TrimPrefix(authorName, " ")
	if authorName == `` {
		br.Msg = "请输入名称"
		br.ErrMsg = "请输入名称"
		br.IsSendEmail = false
		return
	}
	if req.AuthorType != 1 && req.AuthorType != 2 {
		br.Msg = "请选择类型"
		br.ErrMsg = "请选择类型"
		br.IsSendEmail = false
		return
	}
	item, err := models.GetReportAuthorByAuthor(authorName, req.AuthorType)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取数据异常!"
		br.ErrMsg = "获取数据异常,Err:" + err.Error()
		return
	}
	if item != nil {
		br.Msg = "已存在该作者"
		br.ErrMsg = "已存在该作者"
		br.IsSendEmail = false
		return
	}
	item = &models.ReportAuthor{
		Id:           0,
		ReportAuthor: req.Author,
		AuthorType:   req.AuthorType,
		Enable:       1,
		IsDelete:     0,
		CreateTime:   time.Now(),
		ModifyTime:   time.Now(),
	}
	authorId, err := models.AddReportAuthor(item)
	if err != nil {
		br.Msg = "添加作者失败!"
		br.ErrMsg = "添加作者失败,Err:" + err.Error()
		return
	}
	item.Id = int(authorId)

	br.Ret = 200
	br.Success = true
	br.IsAddLog = true
	br.Msg = "添加成功"
	//br.Data = resp
}

// EditAuthor
// @Title 编辑报告作者接口
// @Description 编辑报告作者接口
// @Param	request	body models.AddReportAuthorReq true "type json string"
// @Success 200 Ret=200 编辑成功
// @router /author/edit [post]
func (this *ReportAuthorController) EditAuthor() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.AddReportAuthorReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}

	authorName := strings.TrimSuffix(req.Author, " ")
	authorName = strings.TrimPrefix(authorName, " ")
	otherItem, err := models.GetReportAuthorByAuthorAndId(authorName, req.AuthorType, req.Id)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取数据异常!"
		br.ErrMsg = "获取数据异常,Err:" + err.Error()
		return
	}
	if otherItem != nil {
		br.Msg = "已存在该作者名称,请重新输入"
		br.ErrMsg = "已存在该作者名称,请重新输入"
		br.IsSendEmail = false
		return
	}

	item, err := models.GetReportAuthorById(req.Id)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取数据异常!"
		br.ErrMsg = "获取数据异常,Err:" + err.Error()
		return
	}
	if item == nil {
		br.Msg = "不存在该作者"
		br.ErrMsg = "不存在该作者"
		br.IsSendEmail = false
		return
	}
	//原名称
	oldAuthorName := item.ReportAuthor

	item.ReportAuthor = authorName
	item.ModifyTime = time.Now()
	err = item.Update([]string{"ReportAuthor", "ModifyTime"})
	if err != nil {
		br.Msg = "编辑作者失败!"
		br.ErrMsg = "编辑作者失败,Err:" + err.Error()
		return
	}

	// 更改原有报告中的名称
	{
		var condition string
		var pars []interface{}
		var count int
		condition = " AND author = ? "
		pars = append(pars, oldAuthorName)
		if item.AuthorType == 1 {
			count, err = models.ModifyReportAuthor(condition, pars, authorName)
		} else {
			count, err = models.ModifyEnglishReportAuthor(condition, pars, authorName)
		}
		if err != nil && err.Error() != utils.ErrNoRow() {
			br.Msg = "获取数据异常!"
			br.ErrMsg = "获取是否存在该作者的报告数据异常,Err:" + err.Error()
			return
		}
		if count > 0 {
			br.Msg = "该作者名称有关联报告,不可删除"
			br.ErrMsg = "该作者名称有关联报告,不可删除"
			br.IsSendEmail = false
			return
		}
	}

	br.Ret = 200
	br.Success = true
	br.IsAddLog = true
	br.Msg = "编辑成功"
}

// EnableAuthor
// @Title 禁用/启用报告作者接口
// @Description 禁用/启用报告作者接口
// @Param	request	body models.EnableReportAuthorReq true "type json string"
// @Success 200 Ret=200 启用成功
// @router /author/enable [post]
func (this *ReportAuthorController) EnableAuthor() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.EnableReportAuthorReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}

	item, err := models.GetReportAuthorById(req.Id)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取数据异常!"
		br.ErrMsg = "获取数据异常,Err:" + err.Error()
		return
	}
	if item == nil {
		br.Msg = "不存在该作者"
		br.ErrMsg = "不存在该作者"
		br.IsSendEmail = false
		return
	}

	// 剩余作者数
	{
		var condition string
		var pars []interface{}
		condition = " AND author_type = ?"
		pars = append(pars, item.AuthorType)
		total, err := models.GetReportAuthorCount(condition, pars)
		if err != nil && err.Error() != utils.ErrNoRow() {
			br.Msg = "获取数据异常!"
			br.ErrMsg = "获取剩余作者数据异常,Err:" + err.Error()
			return
		}
		if total <= 1 {
			br.Msg = "该作者名称为最后一个,不可禁用"
			br.ErrMsg = "该作者名称为最后一个,不可禁用"
			br.IsSendEmail = false
			return
		}
	}

	item.Enable = req.EnableType
	item.ModifyTime = time.Now()
	err = item.Update([]string{"Enable", "ModifyTime"})
	if err != nil {
		br.Msg = "操作失败!"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.IsAddLog = true
	br.Msg = "编辑成功"
}

// DeleteAuthor
// @Title 删除报告作者接口
// @Description 删除报告作者接口
// @Param	request	body models.AddReportAuthorReq true "type json string"
// @Success 200 Ret=200 启用成功
// @router /author/delete [post]
func (this *ReportAuthorController) DeleteAuthor() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.DeleteReportAuthorReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}

	item, err := models.GetReportAuthorById(req.Id)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取数据异常!"
		br.ErrMsg = "获取数据异常,Err:" + err.Error()
		return
	}
	if item == nil {
		br.Msg = "不存在该作者"
		br.ErrMsg = "不存在该作者"
		br.IsSendEmail = false
		return
	}

	// 剩余作者数
	{
		var condition string
		var pars []interface{}
		condition = " AND author_type = ?"
		pars = append(pars, item.AuthorType)
		total, err := models.GetReportAuthorCount(condition, pars)
		if err != nil && err.Error() != utils.ErrNoRow() {
			br.Msg = "获取数据异常!"
			br.ErrMsg = "获取剩余作者数据异常,Err:" + err.Error()
			return
		}
		if total <= 1 {
			br.Msg = "该作者名称为最后一个,不可删除"
			br.ErrMsg = "该作者名称为最后一个,不可删除"
			br.IsSendEmail = false
			return
		}
	}

	// 获取是否存在该作者的报告
	{
		var condition string
		var pars []interface{}
		var count int
		condition = " AND author = ? "
		pars = append(pars, item.ReportAuthor)
		if item.AuthorType == 1 {
			count, err = models.GetReportListCount(condition, pars)
		} else {
			count, err = models.GetEnglishReportListCount(condition, pars, "")
		}
		if err != nil && err.Error() != utils.ErrNoRow() {
			br.Msg = "获取数据异常!"
			br.ErrMsg = "获取是否存在该作者的报告数据异常,Err:" + err.Error()
			return
		}
		if count > 0 {
			br.Msg = "该作者名称有关联报告,不可删除"
			br.ErrMsg = "该作者名称有关联报告,不可删除"
			br.IsSendEmail = false
			return
		}
	}

	item.IsDelete = 1
	item.ModifyTime = time.Now()
	err = item.Update([]string{"IsDelete", "ModifyTime"})
	if err != nil {
		br.Msg = "操作失败!"
		br.ErrMsg = "操作失败,Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.IsAddLog = true
	br.Msg = "删除成功"
}