package eta_trial

import (
	"github.com/beego/beego/v2/client/orm"
	"github.com/rdlucklib/rdluck_tools/paging"
	"time"
)

type EtaTrialApproval struct {
	ApprovalId      int    `orm:"column(approval_id);pk" description:"eta审批id"`
	UserName        string `description:"客户名称"`
	CompanyName     string `description:"客户公司姓名"`
	Position        string `description:"职位"`
	Mobile          string `description:"手机号"`
	SellerId        int    `description:"销售id"`
	Seller          string `description:"销售员名称"`
	CreateTime      time.Time
	ModifyTime      time.Time
	ApprovalContent string `description:"待审内容"`
	ApplyMethod     int    `description:"申请类型：1:申请账号 2:申请启用"`
	ApplyReasons    string `description:"申请理由"`
	ApprovalRemark  string `description:"审批描述"`
	ApprovalStatus  string `description:"审批状态 '待审批','已审批','驳回','已撤回'"`
}

func GetETATrialApprovalByMobile(mobile string) (item *EtaTrialApproval, err error) {
	sql := `SELECT * FROM eta_trial_approval WHERE mobile = ? ORDER BY create_time DESC LIMIT 1`
	o := orm.NewOrm()

	err = o.Raw(sql, mobile).QueryRow(&item)
	return
}

// AddETATrialApproval 新增审批
func AddETATrialApproval(item *EtaTrialApproval) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

type ETATrialListResp struct {
	BannedList   []*ETATrialAddRespItem
	RepeatList   []*ETATrialAddRespItem
	SuccessList  []*ETATrialAddRespItem
	InternalList []*ETATrialAddRespItem
}

type ETATrialAddRespItem struct {
	UserName    string `description:"客户名称"`
	CompanyName string `description:"客户公司姓名"`
	Position    string `description:"职位"`
	Mobile      string `description:"手机号"`
	Seller      string `description:"销售"`
}

type ETAAddEnableReq struct {
	UserName     string `description:"客户名称"`
	CompanyName  string `description:"客户公司姓名"`
	Position     string `description:"职位"`
	Mobile       string `description:"手机号"`
	ApplyReasons string `description:"申请理由"`
}

type ETATrialApplyRevokeReq struct {
	Mobile string `description:"客户手机号"`
}

// 消息撤回
func RevokeApproval(mobile string) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE eta_trial_approval SET approval_status=4,modify_time=NOW() 
WHERE mobile=? AND approval_status=1  `
	_, err = o.Raw(sql, mobile).Exec()
	return
}

func DelApproval(approvalId int) (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM eta_trial_approval WHERE approval_id = ?  `
	_, err = o.Raw(sql, approvalId).Exec()
	return
}

func RejectApproval(approvalId int, reason string) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE eta_trial_approval SET approval_status=3,modify_time=NOW(), approval_remark = ? 
WHERE approval_id=? `
	_, err = o.Raw(sql, reason, approvalId).Exec()
	return
}

func ApprovalTrial(approvalId int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE eta_trial_approval SET approval_status=2,modify_time=NOW()  
WHERE approval_id=?  `
	_, err = o.Raw(sql, approvalId).Exec()
	return
}

func GetETATrialApprovalById(approvalId int) (item *EtaTrialApproval, err error) {
	sql := `SELECT * FROM eta_trial_approval WHERE approval_id = ? `
	o := orm.NewOrm()

	err = o.Raw(sql, approvalId).QueryRow(&item)
	return
}

func GetETATrialApprovalBySellerId(condition, sortStr string, pars []interface{}, startSize, pageSize int) (items []*EtaTrialApprovalResp, err error) {
	sql := `SELECT
				a.*, b.account, b.password
			FROM
				eta_trial_approval AS a
			LEFT JOIN eta_trial AS b ON a.mobile = b.mobile AND a.approval_status = '已审批'
			WHERE
				1 = 1 AND a.seller_id = ? `
	o := orm.NewOrm()
	if condition != "" {
		sql += condition
	}
	sql += sortStr + `  LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func GetETATrialApprovalCountBySellerId(condition, sortStr string, pars []interface{}) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM eta_trial_approval AS a WHERE 1=1 AND a.seller_id = ? `
	o := orm.NewOrm()
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

type ETATrialApprovalListRespList struct {
	List   []*EtaTrialApprovalResp
	Paging *paging.PagingItem
}

type EtaTrialApprovalResp struct {
	ApprovalId      int    `orm:"column(approval_id);pk" description:"eta审批id"`
	UserName        string `description:"客户名称"`
	CompanyName     string `description:"客户公司姓名"`
	Position        string `description:"职位"`
	Mobile          string `description:"手机号"`
	SellerId        int    `description:"销售id"`
	Seller          string `description:"销售员名称"`
	CreateTime      string
	ModifyTime      string
	ApprovalContent string `description:"待审内容"`
	ApplyMethod     int    `description:"申请类型：1:申请账号 2:申请启用"`
	ApplyReasons    string `description:"申请理由"`
	ApprovalRemark  string `description:"审批描述"`
	ApprovalStatus  string `description:"审批状态 '待审批','已审批','驳回','已撤回'"`
	Account         string `description:"账号"`
	Password        string `description:"密码-明文"`
}

func GetETATrialApprovalList(condition, sortStr string, pars []interface{}, startSize, pageSize int) (items []*EtaTrialApprovalResp, err error) {
	sql := `SELECT * FROM eta_trial_approval WHERE 1=1  `
	o := orm.NewOrm()
	if condition != "" {
		sql += condition
	}
	sql += sortStr + `  LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func GetETATrialApprovalListCount(condition string, pars []interface{}) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM eta_trial_approval WHERE 1=1 `
	o := orm.NewOrm()
	if condition != "" {
		sql += condition
	}

	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

type RevokeReq struct {
	Mobile string
}

type DelReq struct {
	ApprovalId int
}

type RejectReq struct {
	ApprovalId   int
	RejectReason string
}

func ResubmitTrialNoReasons(userName, companyName, position string, approvalId int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE eta_trial_approval SET approval_status=1,modify_time=NOW(), user_name = ?,company_name=?, position= ?  
WHERE approval_id=? `
	_, err = o.Raw(sql, userName, companyName, position, approvalId).Exec()
	return
}

func ResubmitTrial(approvalId int, applyReasons string) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE eta_trial_approval SET approval_status=1,modify_time=NOW(), apply_reasons = ?   
WHERE approval_id=? `
	_, err = o.Raw(sql, applyReasons, approvalId).Exec()
	return
}
