package data_manage_permission

import (
	"eta/eta_api/models/data_manage/data_manage_permission"
	"eta/eta_api/utils"
)

type MessageDetailItem struct {
	DataPermissionMoveRecordId int64  ` orm:"column(data_permission_move_record_id);pk"` // 数据操作记录id
	Source                     int32  // 数据来源，1：手工数据指标 2：钢联化工数据库 3：ETA指标库 4：ETA预测指标 5：图库 6：ETA表格
	SubSource                  int32  // 子来源 ：ETA表格中的各种表格类型，以及图表的来源（这个是后续的扩展方向）
	OpUniqueCode               string // 操作的唯一编码，主要是记录统一操作的日志
	DataId                     string // 资产id（指标、图表、表格）
	DataCode                   string // 资产code（指标、图表、表格）
	DataName                   string // 资产名称（指标、图表、表格）
	OriginalUserId             int32  // 原创建人id
	OriginalUserName           string // 原创建人名称
	NewUserId                  int32  // 新创建人id
	NewUserName                string // 新创建人名称
	CreateTime                 string
}

// GetMessageDetail
// @Description: 获取消息详情
// @author: Roc
// @datetime 2024-04-07 10:07:06
// @param messageInfo *data_manage_permission.DataPermissionMessage
// @param source int
// @param subSource int
// @param startSize int
// @param pageSize int
// @return resp interface{}
// @return err error
func GetMessageDetail(messageInfo *data_manage_permission.DataPermissionMessage, source, subSource, startSize, pageSize int) (total int, resp interface{}, err error) {
	switch messageInfo.OpType {
	// 操作类型，1：单个资产转移，2：资产模块转移，3：资产授权，4：资产分类授权
	case 1, 2:
		tmpTotal, list, tmpErr := data_manage_permission.GetDataPermissionMoveRecordListByOpUniqueCode(messageInfo.OpUniqueCode, source, subSource, startSize, pageSize)
		if tmpErr != nil {
			err = tmpErr
			return
		}
		total = tmpTotal
		resp, err = formatMessageDetailItemByDataPermissionMoveRecord(list)
	case 3:
		tmpTotal, list, tmpErr := data_manage_permission.GetDataPermissionNoAuthRecordListByUserId(messageInfo.ReceiveUserId, messageInfo.Source, messageInfo.SubSource, startSize, pageSize)
		if tmpErr != nil {
			err = tmpErr
			return
		}
		total = tmpTotal
		resp, err = formatMessageDetailItemByDataPermissionNoAuthRecord(list)
	case 4:
		tmpTotal, list, tmpErr := data_manage_permission.GetDataPermissionClassifyNoAuthRecordListByUserId(messageInfo.ReceiveUserId, messageInfo.Source, messageInfo.SubSource, startSize, pageSize)
		if tmpErr != nil {
			err = tmpErr
			return
		}
		total = tmpTotal
		resp, err = formatMessageDetailItemByDataPermissionClassifyNoAuthRecord(list)

	}

	return
}

// formatMessageDetailItemByDataPermissionMoveRecord
// @Description: 根据转移指标权限数据格式化列表数据
// @author: Roc
// @datetime 2024-04-07 20:20:09
// @param items []*data_manage_permission.DataPermissionMoveRecord
// @return resp []*MessageDetailItem
// @return err error
func formatMessageDetailItemByDataPermissionMoveRecord(items []*data_manage_permission.DataPermissionMoveRecord) (resp []*MessageDetailItem, err error) {
	resp = make([]*MessageDetailItem, 0)
	for _, v := range items {
		resp = append(resp, &MessageDetailItem{
			DataPermissionMoveRecordId: v.DataPermissionMoveRecordId,
			Source:                     v.Source,
			SubSource:                  v.SubSource,
			OpUniqueCode:               v.OpUniqueCode,
			DataId:                     v.DataId,
			DataCode:                   v.DataCode,
			DataName:                   v.DataName,
			OriginalUserId:             v.OriginalUserId,
			OriginalUserName:           v.OriginalUserName,
			NewUserId:                  v.NewUserId,
			NewUserName:                v.NewUserName,
			CreateTime:                 utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
		})
	}

	return
}

// formatMessageDetailItemByDataPermissionNoAuthRecord
// @Description: 根据资产授权数据权限格式化列表数据
// @author: Roc
// @datetime 2024-04-07 20:20:00
// @param items []*data_manage_permission.DataPermissionNoAuthRecord
// @return resp []*MessageDetailItem
// @return err error
func formatMessageDetailItemByDataPermissionNoAuthRecord(items []*data_manage_permission.DataPermissionNoAuthRecord) (resp []*MessageDetailItem, err error) {
	resp = make([]*MessageDetailItem, 0)
	for _, v := range items {
		resp = append(resp, &MessageDetailItem{
			DataPermissionMoveRecordId: v.DataPermissionNoAuthRecordId,
			Source:                     v.Source,
			SubSource:                  v.SubSource,
			OpUniqueCode:               v.OpUniqueCode,
			DataId:                     v.DataId,
			DataCode:                   v.DataCode,
			DataName:                   v.DataName,
			//OriginalUserId:             v.OriginalUserId,
			//OriginalUserName:           v.OriginalUserName,
			//NewUserId:                  v.NewUserId,
			//NewUserName:                v.NewUserName,
			CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
		})
	}

	return
}

// formatMessageDetailItemByDataPermissionClassifyNoAuthRecord
// @Description: 根据资产分类授权数据权限格式化列表数据
// @author: Roc
// @datetime 2024-04-07 20:19:39
// @param items []*data_manage_permission.DataPermissionClassifyNoAuthRecord
// @return resp []*MessageDetailItem
// @return err error
func formatMessageDetailItemByDataPermissionClassifyNoAuthRecord(items []*data_manage_permission.DataPermissionClassifyNoAuthRecord) (resp []*MessageDetailItem, err error) {
	resp = make([]*MessageDetailItem, 0)
	for _, v := range items {
		resp = append(resp, &MessageDetailItem{
			DataPermissionMoveRecordId: v.DataPermissionClassifyNoAuthRecordId,
			Source:                     v.Source,
			SubSource:                  v.SubSource,
			OpUniqueCode:               v.OpUniqueCode,
			DataId:                     v.ClassifyId,
			DataCode:                   v.ClassifyId,
			DataName:                   v.ClassifyName,
			//OriginalUserId:             v.OriginalUserId,
			//OriginalUserName:           v.OriginalUserName,
			//NewUserId:                  v.NewUserId,
			//NewUserName:                v.NewUserName,
			CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
		})
	}

	return
}
