package data_manage_permission

import (
	"eta/eta_api/utils"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"strings"
	"time"
)

// DataPermissionMessage
// @Description: 数据权限变更消息表
type DataPermissionMessage struct {
	DataPermissionMessageId int64     `json:"data_permission_message_id" orm:"column(data_permission_message_id);pk"`
	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) {
	o := orm.NewOrmUsingDB("data")
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.DataPermissionMessageId = id
	return
}

func (m *DataPermissionMessage) CreateMulti(items []*DataPermissionMessage) (err error) {
	if len(items) == 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	_, err = o.InsertMulti(500, items)
	return
}

func (m *DataPermissionMessage) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Update(m, cols...)
	return
}

func (m *DataPermissionMessage) Del() (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	_, err = o.Raw(sql, m.DataPermissionMessageId).Exec()
	return
}

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

func (m *DataPermissionMessage) GetItemById(id int) (item *DataPermissionMessage, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

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

func (m *DataPermissionMessage) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*DataPermissionMessage, err error) {
	o := orm.NewOrmUsingDB("data")
	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)
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	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
}
