package cygx

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

type CygxApplyRecord struct {
	ApplyRecordId            int    `orm:"column(apply_record_id);pk" description:"申请试用id"`
	BusinessCardUrl          string `description:"名片地址"`
	RealName                 string `description:"姓名"`
	CompanyName              string `description:"公司名称"`
	Mobile                   string `description:"手机号"`
	CreateTime               string `description:"创建时间"`
	ApplyMethod              int    `description:"1:已付费客户申请试用，2:非客户申请试用"`
	Status                   int    `description:"0:未处理,1:已处理"`
	DealTime                 string `description:"处理时间"`
	Email                    string `description:"邮箱"`
	SellerName               string `description:"销售"`
	CompanyIdPay             int    `description:"1：为潜在客户，大于1现有客户"`
	InviteeMobile            string `description:"邀请人手机号"`
	InviteeCompany           string `description:"邀请人公司名称"`
	InviteeCompanyId         string `description:"邀请人公司ID"`
	ApplySource              string `description:"申请来源"`
	InviteeEmail             string `description:"邀请人邮箱"`
	IsInviteeComanyExistence bool   `description:"邀请公司是否还存在"`
	InviteeCompanyNum        int    `description:"邀请公司是否还存在（辅助字段）"`
	CompanyIdType            int    `description:"用户状态，1：潜在客户 、2：现有客户 、3：FICC客户 、4：现有客户（正式，无对应权限） 、5：现有客户（试用，无对应权限）  、6：现有客户（试用暂停） 、7：现有客户（冻结） 、8：现有客户（流失） "`
	CompanyIdTypeName        string `description:"用户状态，1：潜在客户 、2：现有客户 、3：FICC客户 、4：现有客户（正式，无对应权限） 、5：现有客户（试用，无对应权限）  、6：现有客户（试用暂停） 、7：现有客户（冻结） 、8：现有客户（流失） "`
	RegisterPlatform         int    `description:"来源 1小程序，2：网页"`
	InviteCompanySource      int    `description:"三方来源 ，1：弘则本身，2:络町"`
	ApplicationSource        string `description:"申请来源"`
}

func GetCygxApplyRecord(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxApplyRecord, err error) {
	o := orm.NewOrm()

	sql := ` SELECT DISTINCT a.*,i.invitee_mobile,i.invitee_company,i.invitee_email,invitee_company_id, GROUP_CONCAT(b.seller_name ORDER BY b.product_id ASC SEPARATOR '/') AS seller_name,c.email,(SELECT COUNT(1) FROM company WHERE company_id =invitee_company_id ) as invitee_company_num
        FROM cygx_apply_record AS a
		LEFT JOIN company_product AS b ON a.company_id_pay=b.company_id
		LEFT JOIN wx_user AS c ON a.user_id=c.user_id 
		LEFT JOIN user_invitee AS i ON a.user_id = i.inviteed_user_id WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` GROUP BY a.apply_record_id  ORDER BY a.create_time DESC  LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func GetCygxApplyRecordCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	//sql := `SELECT COUNT(1) AS count FROM cygx_apply_record AS a
	//	LEFT JOIN company_product AS b ON a.company_id_pay=b.company_id
	//	LEFT JOIN wx_user AS c ON a.user_id=c.user_id WHERE 1=1 `
	//
	//if condition != "" {
	//	sql += condition
	//}
	sql := `SELECT COUNT(1) AS count FROM ( SELECT DISTINCT a.*,
GROUP_CONCAT(b.seller_name ORDER BY b.product_id ASC SEPARATOR '/') AS seller_name,c.email 
        FROM cygx_apply_record AS a
		LEFT JOIN company_product AS b ON a.company_id_pay=b.company_id
		LEFT JOIN wx_user AS c ON a.user_id=c.user_id WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` GROUP BY a.apply_record_id ) b `
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

type CygxApplyRecordListResp struct {
	List   []*CygxApplyRecord
	Paging *paging.PagingItem `description:"分页数据"`
}

type CygxDealReq struct {
	ApplyRecordId int `description:"申请试用id"`
}

func DealCygxApplyRecord(applyRecordId, sysUserId int) (err error) {
	sql := `UPDATE cygx_apply_record SET status=1,deal_time=NOW(),sys_user_id=? WHERE apply_record_id=? `
	o := orm.NewOrm()
	_, err = o.Raw(sql, sysUserId, applyRecordId).Exec()
	return
}
