package report_approve

import (
	"eta/eta_mobile/utils"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"github.com/rdlucklib/rdluck_tools/paging"
	"strings"
	"time"
)

// ReportApproveMessage 报告审批消息表
type ReportApproveMessage struct {
	Id              int       `orm:"column(id);pk"`
	SendUserId      int       `description:"发送人ID"`
	ReceiveUserId   int       `description:"接收者ID"`
	Content         string    `description:"消息内容"`
	Remark          string    `description:"备注信息"`
	ReportApproveId int       `description:"审批ID"`
	ApproveState    int       `description:"审批状态：1-待审批；2-已审批；3-已驳回；4-已撤回"`
	IsRead          int       `description:"是否已读：0-未读；1-已读"`
	CreateTime      time.Time `description:"创建时间"`
	ModifyTime      time.Time `description:"修改时间"`
}

var ReportApproveMessageCols = struct {
	Id              string
	SendUserId      string
	ReceiveUserId   string
	Content         string
	Remark          string
	ReportApproveId string
	ApproveState    string
	IsRead          string
	CreateTime      string
	ModifyTime      string
}{
	Id:              "id",
	SendUserId:      "send_user_id",
	ReceiveUserId:   "receive_user_id",
	Content:         "content",
	Remark:          "remark",
	ReportApproveId: "report_approve_id",
	ApproveState:    "approve_state",
	IsRead:          "is_read",
	CreateTime:      "create_time",
	ModifyTime:      "modify_time",
}

func (m *ReportApproveMessage) TableName() string {
	return "report_approve_message"
}

func (m *ReportApproveMessage) PrimaryId() string {
	return ReportApproveMessageCols.Id
}

func (m *ReportApproveMessage) Create() (err error) {
	o := orm.NewOrmUsingDB("rddp")
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.Id = int(id)
	return
}

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

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

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

func (m *ReportApproveMessage) MultiDel(menuIds []int) (err error) {
	if len(menuIds) == 0 {
		return
	}
	o := orm.NewOrmUsingDB("rddp")
	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 *ReportApproveMessage) GetItemById(id int) (item *ReportApproveMessage, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

func (m *ReportApproveMessage) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *ReportApproveMessage, err error) {
	o := orm.NewOrmUsingDB("rddp")
	order := ``
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
	err = o.Raw(sql, pars).QueryRow(&item)
	return
}

func (m *ReportApproveMessage) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("rddp")
	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 *ReportApproveMessage) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ReportApproveMessage, err error) {
	o := orm.NewOrmUsingDB("rddp")
	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`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

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

// ReportApproveMessageItem 报告审批消息信息
type ReportApproveMessageItem struct {
	Id              int
	SendUserId      int    `description:"发送人ID"`
	ReceiveUserId   int    `description:"接收者ID"`
	Content         string `description:"消息内容"`
	Remark          string `description:"备注信息"`
	ReportApproveId int    `description:"审批ID"`
	ApproveState    int    `description:"审批状态：1-待审批；2-已审批；3-已驳回；4-已撤回"`
	IsRead          int    `description:"是否已读：0-未读；1-已读"`
	CreateTime      string `description:"创建时间"`
	ModifyTime      string `description:"修改时间"`
}

// FormatReportApproveMessage2Item 格式化报告审批消息
func FormatReportApproveMessage2Item(origin *ReportApproveMessage) (item *ReportApproveMessageItem) {
	item = new(ReportApproveMessageItem)
	if origin == nil {
		return
	}
	item.Id = origin.Id
	item.SendUserId = origin.SendUserId
	item.ReceiveUserId = origin.ReceiveUserId
	item.Content = origin.Content
	item.Remark = origin.Remark
	item.ReportApproveId = origin.ReportApproveId
	item.ApproveState = origin.ApproveState
	item.IsRead = origin.IsRead
	item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
	item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
	return
}

// ReportApproveMessageListReq 审批消息列表请求参数
type ReportApproveMessageListReq struct {
	PageSize     int `form:"PageSize"`
	CurrentIndex int `form:"CurrentIndex"`
}

// ReportApproveMessageListResp 审批消息列表响应体
type ReportApproveMessageListResp struct {
	List        []*ReportApproveMessageItem
	Paging      *paging.PagingItem `description:"分页数据"`
	UnreadTotal int                `description:"消息未读数"`
}

// ReportApproveMessageReadReq 审批消息已读请求体
type ReportApproveMessageReadReq struct {
	MessageId int `description:"审批消息ID"`
}
