package data_manage_permission

import (
	"eta_gn/eta_api/global"
	"eta_gn/eta_api/utils"
	"fmt"
	"strings"
	"time"
)

// DataPermissionMessage
// @Description: 数据权限变更消息表
type DataPermissionMessage struct {
	DataPermissionMessageId int64     `json:"data_permission_message_id" orm:"column(data_permission_message_id);pk" gorm:"primaryKey" `
	SendUserId              int32     `json:"send_user_id"`    // 发送人ID
	ReceiveUserId           int32     `json:"receive_user_id"` // 接收者ID
	Content                 string    `json:"content"`         // 消息内容
	Remark                  string    `json:"remark"`          // 备注信息
	OpType                  int32     `json:"op_type"`         // 操作类型,1:单个资产转移,2:资产模块转移,3:资产授权,4:资产分类授权
	Source                  int32     `description:"来源id"`
	SubSource               int32     `description:"子来源id"`
	OpUniqueCode            string    `json:"op_unique_code"` // 操作编码
	IsRead                  int32     `json:"is_read"`        // 是否已读:0-未读;1-已读
	CreateTime              time.Time `json:"create_time"`    // 创建时间
	ModifyTime              time.Time `json:"modify_time"`    // 修改时间
}

var DataPermissionMessageCols = struct {
	DataPermissionMessageId string
	SendUserId              string
	ReceiveUserId           string
	Content                 string
	Remark                  string
	OpType                  string
	Source                  string
	SubSource               string
	OpUniqueCode            string
	IsRead                  string
	CreateTime              string
	ModifyTime              string
}{
	DataPermissionMessageId: "data_permission_message_id",
	SendUserId:              "send_user_id",
	ReceiveUserId:           "receive_user_id",
	Content:                 "content",
	Remark:                  "remark",
	OpType:                  "op_type",
	Source:                  "source",
	SubSource:               "sub_source",
	OpUniqueCode:            "op_unique_code",
	IsRead:                  "is_read",
	CreateTime:              "create_time",
	ModifyTime:              "modify_time",
}

func (m *DataPermissionMessage) TableName() string {
	return "data_permission_message"
}

func (m *DataPermissionMessage) PrimaryId() string {
	return DataPermissionMessageCols.DataPermissionMessageId
}

func (m *DataPermissionMessage) Create() (err error) {
	err = global.DmSQL["data"].Create(m).Error

	return
}

func (m *DataPermissionMessage) CreateMulti(items []*DataPermissionMessage) (err error) {
	if len(items) == 0 {
		return
	}
	err = global.DmSQL["data"].CreateInBatches(items, utils.MultiAddNum).Error

	return
}

func (m *DataPermissionMessage) Update(cols []string) (err error) {
	err = global.DmSQL["data"].Select(cols).Updates(m).Error

	return
}

func (m *DataPermissionMessage) Del() (err error) {
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	err = global.DmSQL["data"].Exec(sql, m.DataPermissionMessageId).Error

	return
}

func (m *DataPermissionMessage) MultiDel(menuIds []int) (err error) {
	if len(menuIds) == 0 {
		return
	}
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.PrimaryId(), utils.GetOrmInReplace(len(menuIds)))
	err = global.DmSQL["data"].Exec(sql, menuIds).Error

	return
}

func (m *DataPermissionMessage) GetItemById(id int) (item *DataPermissionMessage, err error) {
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	err = global.DmSQL["data"].Raw(sql, id).First(&item).Error

	return
}

func (m *DataPermissionMessage) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
	err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error

	return
}

func (m *DataPermissionMessage) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*DataPermissionMessage, err error) {
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
	pars = append(pars, startSize)
	pars = append(pars, pageSize)
	err = global.DmSQL["data"].Raw(sql, pars...).Scan(&items).Error

	return
}

// FormatDataPermissionMessage2Item 格式化消息
func FormatDataPermissionMessage2Item(origin *DataPermissionMessage) (item *DataPermissionMessageItem) {
	item = &DataPermissionMessageItem{
		DataPermissionMessageId: origin.DataPermissionMessageId,
		SendUserId:              origin.SendUserId,
		ReceiveUserId:           origin.ReceiveUserId,
		Content:                 origin.Content,
		Remark:                  origin.Remark,
		OpType:                  origin.OpType,
		Source:                  origin.Source,
		SubSource:               origin.SubSource,
		OpUniqueCode:            origin.OpUniqueCode,
		IsRead:                  origin.IsRead,
		CreateTime:              utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime),
		ModifyTime:              utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime),
	}
	return
}