package data_approve

import (
	"encoding/json"
	"eta_gn/eta_api/controllers"
	"eta_gn/eta_api/models"
	"eta_gn/eta_api/models/data_manage/data_approve/request"
	"eta_gn/eta_api/models/data_manage/data_approve/response"
	dataApproveServ "eta_gn/eta_api/services/data/data_approve"
	"eta_gn/eta_api/utils"
	"fmt"

	"github.com/rdlucklib/rdluck_tools/paging"
)

// DataApproveController
// @Description: 数据资产审批
type DataApproveController struct {
	controllers.BaseAuthController
}

// List
// @Title 审批列表
// @Description 审批列表
// @Param   PageSize			query	int		true	"每页数据条数"
// @Param   CurrentIndex		query	int		true	"当前页页码"
// @Param   DataType			query   int     true	"审批类型;1:指标审批;2:图表审批"
// @Param   ListType			query   int     true	"列表类型:1-待处理;2-已处理;3-我发起的"
// @Param   ClassifyId			query	int		false	"分类ID"
// @Param   Keyword				query	string	false	"搜索关键词"
// @Param   ApproveState		query	int		false	"审批状态:1-待审批;2-已审批;3-已驳回;4-已撤回"
// @Param   TimeType			query	int		false	"时间类型:1-提交时间;2-处理时间;3-审批时间"
// @Param   StartTime			query	string	false	"开始时间"
// @Param   EndTime				query	string	false	"结束时间"
// @Param   SortField			query	int		false	"排序字段:1-提交时间;2-处理时间;3-审批时间"
// @Param   SortRule			query	int		false	"排序方式: 1-正序; 2-倒序(默认)"
// @Success 200 {object} report_approve.DataApproveListResp
// @router /list [get]
func (this *DataApproveController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()

	sysUser := this.SysUser
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	dataType, _ := this.GetInt("DataType", utils.DataApproveTypeEdb)
	listType, _ := this.GetInt("ListType")
	approveState, _ := this.GetInt("ApproveState")
	timeType, _ := this.GetInt("TimeType")
	startTime := this.GetString("StartTime")
	endTime := this.GetString("EndTime")
	sortField, _ := this.GetInt("SortField")
	sortRule, _ := this.GetInt("SortRule")
	keyword := this.GetString("Keyword")

	if pageSize <= 0 {
		pageSize = utils.PageSize10
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize := paging.StartIndex(currentIndex, pageSize)

	var list []*response.DataApproveItemOrmResp
	var total int
	var msg string
	var err error

	switch listType {
	case 1:
		list, total, msg, err = dataApproveServ.ProcessingDataApprove(sysUser.AdminId, dataType, timeType, sortField, sortRule, startSize, pageSize, sysUser.RealName, startTime, endTime, keyword)
	case 2:
		list, total, msg, err = dataApproveServ.SolvedDataApprove(sysUser.AdminId, dataType, timeType, sortField, sortRule, approveState, startSize, pageSize, sysUser.RealName, startTime, endTime, keyword)
	case 3:
		list, total, msg, err = dataApproveServ.MyApplyDataApproves(sysUser.AdminId, dataType, timeType, sortField, sortRule, approveState, startSize, pageSize, sysUser.RealName, startTime, endTime, keyword)
	default:
		br.Msg = "列表类型错误"
		return
	}
	if err != nil {
		if msg != "" {
			br.Msg = msg
		} else {
			br.Msg = "获取审批列表失败"
		}
		br.ErrMsg = "获取审批列表失败, Err: " + err.Error()
		return
	}

	resp := new(response.DataApproveListResp)
	page := paging.GetPaging(currentIndex, pageSize, total)
	resp.List = list
	resp.Paging = page

	br.Msg = "获取审批列表成功"
	br.Data = resp
	br.Ret = 200
	br.Success = true
}

// Approve
// @Title 通过审批
// @Description 通过审批
// @Param	request	body report_approve.DataApprovePassReq true "type json string"
// @Success 200 string "操作成功"
// @router /approve [post]
func (this *DataApproveController) Approve() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		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 request.DataApprovePassReq
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数有误"
		br.ErrMsg = "参数解析失败, Err: " + e.Error()
		return
	}
	if req.DataApproveId <= 0 {
		br.Msg = "参数有误"
		br.ErrMsg = fmt.Sprintf("参数有误, DataApproveId: %d", req.DataApproveId)
		return
	}

	// 通过审批
	msg, err := dataApproveServ.PassDataApprove(req.DataApproveId, sysUser.AdminId)
	if err != nil {
		if msg != "" {
			br.Msg = msg
		} else {
			br.Msg = "操作失败"
		}
		br.ErrMsg = "通过审批失败, Err: " + err.Error()
		return
	}

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

// Detail
// @Title 审批详情
// @Description 审批详情
// @Param   DataApproveId  query  int  true  "审批ID"
// @Success 200 {object} report_approve.DataApproveDetail
// @router /detail [get]
func (this *DataApproveController) Detail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录,SysUser Is Empty"
		br.Ret = 408
		return
	}
	approveId, _ := this.GetInt("DataApproveId")
	if approveId <= 0 {
		br.Msg = "参数有误"
		br.ErrMsg = fmt.Sprintf("参数有误, DataApproveId: %d", approveId)
		return
	}

	resp, msg, err := dataApproveServ.GetApproveDetail(approveId)
	if err != nil {
		if msg != "" {
			br.Msg = msg
		} else {
			br.Msg = "获取审批详情失败"
		}
		br.ErrMsg = "获取审批详情失败, Err: " + err.Error()
		return
	}

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

// Refuse
// @Title 驳回审批
// @Description 驳回审批
// @Param	request	body request.DataApproveRefuseReq true "type json string"
// @Success 200 string "操作成功"
// @router /refuse [post]
func (this *DataApproveController) Refuse() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		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 request.DataApproveRefuseReq
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数有误"
		br.ErrMsg = "参数解析失败, Err: " + e.Error()
		return
	}
	if req.DataApproveId <= 0 {
		br.Msg = "参数有误"
		br.ErrMsg = fmt.Sprintf("参数有误, DataApproveId: %d", req.DataApproveId)
		return
	}
	maxStrLen := 500
	approveLen := len([]rune(req.ApproveRemark))
	if approveLen > maxStrLen {
		br.Msg = fmt.Sprintf("审批驳回原因不能超过%d字", maxStrLen)
		return
	}
	msg, err := dataApproveServ.DataApproveRefuse(req.DataApproveId, sysUser.AdminId, req.ApproveRemark)
	if err != nil {
		if msg != "" {
			br.Msg = msg
		} else {
			br.Msg = "操作失败"
		}
		br.ErrMsg = "驳回审批失败, Err: " + err.Error()
		return
	}

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

// Cancel
// @Title 撤销审批
// @Description 撤销审批
// @Param	request	body report_approve.DataApproveCancelReq true "type json string"
// @Success 200 string "操作成功"
// @router /cancel [post]
func (this *DataApproveController) Cancel() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		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 request.DataApproveCancelReq
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数有误"
		br.ErrMsg = "参数解析失败, Err: " + e.Error()
		return
	}
	if req.DataApproveId <= 0 {
		br.Msg = "参数有误"
		br.ErrMsg = fmt.Sprintf("参数有误, DataApproveId: %d", req.DataApproveId)
		return
	}

	// 撤销审批
	msg, e := dataApproveServ.DataApproveCancel(req.DataApproveId, sysUser.AdminId, sysUser.RealName)
	if e != nil {
		if msg != "" {
			br.Msg = msg
		} else {
			br.Msg = "操作失败"
		}
		br.ErrMsg = "撤销审批失败, Err: " + e.Error()
		return
	}

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

// MessageList
// @Title 审批消息列表
// @Description 审批消息列表
// @Param   PageSize			query	int		true	"每页数据条数"
// @Param   CurrentIndex		query	int		true	"当前页页码"
// @Param   DataType			query   int     true	"审批类型;1:指标审批;2:图表审批"
// @Success 200 {object} report_approve.DataApproveMessageListResp
// @router /message/list [get]
func (this *DataApproveController) MessageList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录,SysUser Is Empty"
		br.Ret = 408
		return
	}
	currentIndex, _ := this.GetInt("currentIndex")
	pageSize, _ := this.GetInt("pageSize")
	dataType, _ := this.GetInt("DataType", utils.DataApproveTypeEdb)

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

	resp := new(response.DataApproveMessageListResp)
	resp.List = make([]*response.DataApproveMessageItem, 0)
	list, total, unRead, msg, err := dataApproveServ.GetDataApproveMessage(sysUser.AdminId, dataType, startSize, pageSize)
	if err != nil {
		if msg != "" {
			br.Msg = msg
		} else {
			br.Msg = "获取审批消息失败"
		}
		br.ErrMsg = "获取审批消息失败, Err: " + err.Error()
		return
	}
	resp.List = list
	resp.UnreadTotal = unRead

	page := paging.GetPaging(currentIndex, pageSize, total)
	resp.Paging = page
	br.Data = resp
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}

// MessageRead
// @Title 消息已读
// @Description 消息已读
// @Param	request	body report_approve.DataApproveMessageReadReq true "type json string"
// @Success 200 string "操作成功"
// @router /message/read [post]
func (this *DataApproveController) MessageRead() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		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 request.DataApproveMessageReadReq
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数有误"
		br.ErrMsg = "参数解析失败, Err: " + e.Error()
		return
	}
	if req.MessageId <= 0 {
		br.Msg = "参数有误"
		br.ErrMsg = fmt.Sprintf("参数有误, MessageId: %d", req.MessageId)
		return
	}

	msg, err := dataApproveServ.ReadBiMessage(req.MessageId, sysUser.AdminId)
	if err != nil {
		if msg != "" {
			br.Msg = msg
		} else {
			br.Msg = "操作失败"
		}
		br.ErrMsg = "消息已读失败, Err: " + err.Error()
		return
	}

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

// CheckApproveOpen
// @Title 校验是否开启审批
// @Description 校验是否开启审批
// @Param	request	body report_approve.DataApproveCheckApproveOpenReq true "type json string"
// @Success 200 string "操作成功"
// @router /check_open [post]
func (this *DataApproveController) CheckApproveOpen() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		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 request.DataApproveCheckApproveOpenReq
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数有误"
		br.ErrMsg = "参数解析失败, Err: " + e.Error()
		return
	}

	// 校验是否开启了审批流
	opening, e := dataApproveServ.CheckOpenApprove(req.DataType)
	if e != nil {
		br.Msg = "操作失败"
		br.ErrMsg = "校验数据资产是否开启审批流失败, Err: " + e.Error()
		return
	}

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