package report_approve

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

// ReportApproveRecord 报告审批记录表
type ReportApproveRecord struct {
	ReportApproveRecordId int       `orm:"column(report_approve_record_id);pk" description:"审批记录ID"`
	ReportApproveId       int       `description:"审批ID"`
	State                 int       `description:"审批状态：1-待审批；2-已审批；3-已驳回；4-已撤回"`
	NodeId                int       `description:"节点ID"`
	NodeType              int       `description:"节点类型：0-审批；1-抄送"`
	PrevNodeId            int       `description:"上级节点ID"`
	NextNodeId            int       `description:"下级节点ID"`
	ApproveType           int       `description:"审批类型：1-依次审批；2-会签；3-或签"`
	ApproveUserId         int       `description:"审批人ID"`
	ApproveUserName       string    `description:"审批人姓名"`
	ApproveUserSort       int       `description:"依次审批：用户的审批顺序"`
	ApproveRemark         string    `description:"审批备注（驳回备注等）"`
	ApproveTime           time.Time `description:"审批时间"`
	CreateTime            time.Time `description:"创建时间"`
	ModifyTime            time.Time `description:"修改时间"`
}

var ReportApproveRecordCols = struct {
	ReportApproveRecordId string
	ReportApproveId       string
	State                 string
	NodeId                string
	NodeType              string
	PrevNodeId            string
	NextNodeId            string
	ApproveType           string
	ApproveRemark         string
	ApproveUserId         string
	ApproveUserName       string
	ApproveUserSort       string
	ApproveTime           string
	CreateTime            string
	ModifyTime            string
}{
	ReportApproveRecordId: "report_approve_record_id",
	ReportApproveId:       "report_approve_id",
	State:                 "state",
	NodeId:                "node_id",
	NodeType:              "node_type",
	PrevNodeId:            "prev_node_id",
	NextNodeId:            "next_node_id",
	ApproveType:           "approve_type",
	ApproveUserId:         "approve_user_id",
	ApproveUserName:       "approve_user_name",
	ApproveUserSort:       "approve_user_sort",
	ApproveRemark:         "approve_remark",
	ApproveTime:           "approve_time",
	CreateTime:            "create_time",
	ModifyTime:            "modify_time",
}

func (m *ReportApproveRecord) TableName() string {
	return "report_approve_record"
}

func (m *ReportApproveRecord) PrimaryId() string {
	return ReportApproveRecordCols.ReportApproveRecordId
}

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

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

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

func (m *ReportApproveRecord) 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.ReportApproveRecordId).Exec()
	return
}

func (m *ReportApproveRecord) 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 *ReportApproveRecord) GetItemById(id int) (item *ReportApproveRecord, 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 *ReportApproveRecord) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *ReportApproveRecord, 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 *ReportApproveRecord) 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 *ReportApproveRecord) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ReportApproveRecord, 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 *ReportApproveRecord) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*ReportApproveRecord, 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
}

// ReportApproveRecordItem 报告审批记录信息
type ReportApproveRecordItem struct {
	ReportApproveRecordId int    `description:"审批记录ID"`
	ReportApproveId       int    `description:"审批ID"`
	State                 int    `description:"审批状态：1-待审批；2-已审批；3-已驳回；4-已撤回"`
	NodeId                int    `description:"节点ID"`
	NodeType              int    `description:"节点类型：0-审批；1-抄送"`
	PrevNodeId            int    `description:"上级节点ID"`
	NextNodeId            int    `description:"下级节点ID"`
	ApproveType           int    `description:"审批类型：1-依次审批；2-会签；3-或签"`
	ApproveUserId         int    `description:"审批人ID"`
	ApproveUserName       string `description:"审批人姓名"`
	ApproveUserSort       int    `description:"依次审批：用户的审批顺序"`
	ApproveRemark         string `description:"审批备注（驳回备注等）"`
	ApproveTime           string `description:"审批时间"`
	CreateTime            string `description:"创建时间"`
	ModifyTime            string `description:"修改时间"`
}

// FormatReportApproveRecord2Item 格式化报告审批记录
func FormatReportApproveRecord2Item(origin *ReportApproveRecord) (item *ReportApproveRecordItem) {
	item = new(ReportApproveRecordItem)
	if origin == nil {
		return
	}
	item.ReportApproveRecordId = origin.ReportApproveRecordId
	item.ReportApproveId = origin.ReportApproveId
	item.State = origin.State
	item.NodeId = origin.NodeId
	item.NodeType = origin.NodeType
	item.PrevNodeId = origin.PrevNodeId
	item.NextNodeId = origin.NextNodeId
	item.ApproveType = origin.ApproveType
	item.ApproveUserId = origin.ApproveUserId
	item.ApproveUserName = origin.ApproveUserName
	item.ApproveUserSort = origin.ApproveUserSort
	item.ApproveRemark = origin.ApproveRemark
	item.ApproveTime = utils.TimeTransferString(utils.FormatDateTime, origin.ApproveTime)
	item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
	item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
	return
}

// ReportApproveDetailNodeUserRecord 审批详情-节点用户审批记录
type ReportApproveDetailNodeUserRecord struct {
	ReportApproveRecordId int    `description:"审批记录ID"`
	State                 int    `description:"审批状态：1-待审批；2-已审批；3-已驳回；4-已撤回"`
	ApproveUserId         int    `description:"审批人ID"`
	ApproveUserName       string `description:"审批人姓名"`
	ApproveRemark         string `description:"审批备注"`
	ApproveTime           string `description:"审批时间"`
}
