package company

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type CompanyApprovalMessage struct {
	Id                int       `orm:"column(id);pk"`
	CreateUserId      int       `description:"申请者id"`
	ReceiveUserId     int       `description:"审批者id"`
	MessageStatus     int       `description:"消息状态:0未读，1:已读"`
	SourceType        int       `description:"消息来源类型，1:客户，2：合同，3：用印"`
	Remark            string    `description:"备注信息"`
	Content           string    `description:"消息内容"`
	CompanyId         int       `description:"客户id"`
	CompanyName       string    `description:"客户名称"`
	CreateTime        time.Time `description:"创建时间"`
	ModifyTime        time.Time `description:"修改时间"`
	CompanyApprovalId int       `description:"审批单id"`
	ApprovalStatus    int       `description:"审批状态：1:待审批，2:已审批，3:已驳回"`
	OperationStatus   int       `description:"消息状态:1:待审批，2:已审批"`
	MessageType       int       `description:"1:申请消息，2:审批结果，3：文字消息"`
	MessageInfo       string    `description:"消息主要内容，json数据"`
}

// 添加审批消息
func AddCompanyApprovalMessage(item *CompanyApprovalMessage) (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(item)
	return
}

// GetCompanyApprovalMessageById 主键获取消息
func GetCompanyApprovalMessageById(id int) (item *CompanyApprovalMessage, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM company_approval_message WHERE id = ? LIMIT 1`
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

type CompanyApprovalMessageList struct {
	Id                int    `orm:"column(id);pk"`
	MessageStatus     int    `description:"消息状态:0未读，1:已读，2：作废"`
	SourceType        int    `description:"消息来源类型，1:客户，2：合同 3：用印"`
	Remark            string `description:"备注信息"`
	Content           string `description:"消息内容"`
	CompanyName       string `description:"客户名称"`
	CreateTime        string `description:"创建时间"`
	RealName          string `description:"销售名称"`
	CompanyApprovalId int    `description:"审批单id"`
	ApprovalStatus    int    `description:"审批状态：1:待审批，2:已审批，3:已驳回"`
}

type CompanyApprovalMessageListResp struct {
	List  []*CompanyApprovalMessageList
	Total int `description:"总数据条数"`
}

// 待办消息列表
type CompanyApprovalMessageListV2Resp struct {
	Company      CompanyApprovalMessageListResp  `description:"客户"`
	Contract     CompanyApprovalMessageListResp  `description:"合同"`
	Seal         CompanyApprovalMessageListResp  `description:"用印"`
	EdbReplace   CompanyApprovalMessageListResp  `description:"指标替换"`
	ETATrial     ETATrialApprovalMessageListResp `description:"ETA试用"`
	BusinessTrip CompanyApprovalMessageListResp  `description:"出差"`
}

type ETATrialApprovalMessageListResp struct {
	List  []*ETATrialApprovalMessageList
	Total int `description:"总数据条数"`
}

type ETATrialApprovalMessageList struct {
	Id            int    `orm:"column(id);pk"`
	MessageStatus int    `description:"消息状态:0未读，1:已读，2：作废"`
	SourceType    int    `description:"消息来源类型，1:客户，2：合同 3：用印 4：指标替换 5：ETA试用"`
	Remark        string `description:"备注信息"`
	Content       string `description:"消息内容"`
	CompanyName   string `description:"客户名称"`
	CreateTime    string `description:"创建时间"`
	RealName      string `description:"销售名称"`
	Redirect      int    `description:"跳转路径 1用户列表 2审批列表"`
}

func GetCompanyApprovalMessageList(sysUserId int, startTime string) (items []*CompanyApprovalMessageList, err error) {
	sql := `SELECT a.*,b.real_name FROM company_approval_message AS a
			INNER JOIN admin AS b ON a.create_user_id=b.admin_id
			WHERE receive_user_id=? AND a.operation_status=1 AND message_status !=2 `
	if startTime != "" {
		sql += ` AND create_time>='` + startTime + `'`
	}
	sql += ` ORDER BY create_time DESC `
	o := orm.NewOrm()
	_, err = o.Raw(sql, sysUserId).QueryRows(&items)
	return
}

func GetCompanyApprovalMessageCount(sysUserId int, startTime string) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM company_approval_message AS a
			INNER JOIN admin AS b ON a.create_user_id=b.admin_id
			WHERE receive_user_id=? AND a.operation_status=1 AND message_status !=2 `
	if startTime != "" {
		sql += ` AND create_time>='` + startTime + `'`
	}
	o := orm.NewOrm()
	err = o.Raw(sql, sysUserId).QueryRow(&count)
	return
}

// 修改消息信息
func ModifyCompanyApprovalMessage(companyApprovalId, sourceType int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE company_approval_message SET operation_status=2,modify_time=NOW() 
WHERE company_approval_id=? AND source_type=?  AND message_type=1 AND operation_status=1 `
	_, err = o.Raw(sql, companyApprovalId, sourceType).Exec()
	return
}

// 申请服务更新请求参数
type CompanyApprovalMessageReadReq struct {
	Id int `description:"消息id"`
}

func ModifyCompanyApprovalMessageStatus(companyApprovalMessageId int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE company_approval_message SET message_status=1,modify_time=NOW() WHERE id=?`
	_, err = o.Raw(sql, companyApprovalMessageId).Exec()
	return
}

// 消息作废
func CancelCompanyApprovalMessage(companyApprovalId, sourceType int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE company_approval_message SET message_status=2,modify_time=NOW() 
WHERE company_approval_id=? AND source_type=?  AND message_type=1 AND operation_status=1 `
	//sql := `UPDATE company_approval_message SET message_status=1,modify_time=NOW() WHERE id=?`
	_, err = o.Raw(sql, companyApprovalId, sourceType).Exec()
	return
}
func GetAdminCompanyApprovalMessageList(startTime string) (items []*CompanyApprovalMessageList, err error) {
	sql := `SELECT a.*,b.real_name FROM company_approval_message AS a
			INNER JOIN admin AS b ON a.create_user_id=b.admin_id
			WHERE receive_user_id = -1 AND a.operation_status=1 AND message_status !=2 `
	if startTime != "" {
		sql += ` AND create_time>='` + startTime + `'`
	}
	sql += ` ORDER BY create_time DESC `
	o := orm.NewOrm()
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 消息删除
func DeleteCompanyApprovalMessage(companyApprovalId, sourceType int) (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM company_approval_message WHERE company_approval_id=? AND source_type=? `
	_, err = o.Raw(sql, companyApprovalId, sourceType).Exec()
	return
}

// CancelCompanyApprovalMessage 消息作废
func IsReadCompanyApprovalMessage(companyApprovalId, sourceType int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE company_approval_message SET message_status=1,modify_time=NOW() 
WHERE company_approval_id=? AND source_type=?  AND message_type=1 AND operation_status=1 `
	_, err = o.Raw(sql, companyApprovalId, sourceType).Exec()
	return
}

// ModifyCompanyApprovalMessageStatusByCond 条件修改已读状态
func ModifyCompanyApprovalMessageStatusByCond(cond string, pars []interface{}) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE company_approval_message SET message_status = 1, modify_time = NOW() WHERE 1 = 1 %s`
	sql = fmt.Sprintf(sql, cond)
	_, err = o.Raw(sql, pars).Exec()
	return
}
