package models

import (
	"hongze/hongze_task/utils"
	"rdluck_tools/orm"
	"time"
)

type CompanyOperationRecord struct {
	Id              int       `orm:"column(id);pk"`
	CompanyId       int       `description:"客户id"`
	CompanyName     string    `description:"客户名称"`
	SysUserId       int       `description:"操作者id"`
	SysRealName     string    `description:"操作者名称"`
	Remark          string    `description:"备注"`
	Operation       string    `description:"操作"`
	CreateTime      time.Time `description:"操作时间"`
	ProductId       int       `description:"产品id"`
	ProductName     string    `description:"产品名称"`
	ApproveUserId   int       `description:"审批人id"`
	ApproveRealName string    `description:"审批人姓名"`
	ApproveContent  string    `description:"审批人内容"`
	ApproveRemark   string    `description:"审批人内容"`
	Status          string    `description:"状态"`
}

//新增操作记录
func AddCompanyOperationRecord(item *CompanyOperationRecord) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

func CompanyFreeze(companyId, productId int) (err error) {
	o := orm.NewOrm()
	freezeStartDate := time.Now().Format(utils.FormatDate)
	freezeEndDate := time.Now().AddDate(0, 3, 0).Format(utils.FormatDate)

	sql := `UPDATE company_product SET status='冻结',freeze_time=NOW(),modify_time=NOW(),start_date=?,end_date=?,freeze_start_date=?,freeze_end_date=? WHERE company_id=? AND product_id=? `
	_, err = o.Raw(sql, freezeStartDate, freezeEndDate, freezeStartDate, freezeEndDate, companyId, productId).Exec()
	if err != nil {
		return err
	}
	sql = `UPDATE company SET type=3,last_updated_time=NOW(),start_date=?,end_date=? WHERE company_id=? `
	_, err = o.Raw(sql, freezeStartDate, freezeEndDate, companyId).Exec()
	if err != nil {
		return err
	}
	return
}

func CompanyLoss(companyId, productId int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE company_product SET status='流失',loss_time=NOW(),modify_time=NOW(),lose_reason=‘冻结到期系统自动流失’ WHERE company_id=? AND product_id=? `
	_, err = o.Raw(sql, companyId, productId).Exec()
	if err != nil {
		return err
	}
	sql = `UPDATE company SET type=3,last_updated_time=NOW() WHERE company_id=? `
	_, err = o.Raw(sql, companyId).Exec()
	if err != nil {
		return err
	}
	return
}

func CompanyTryOut(companyId, productId int) (err error) {
	o := orm.NewOrm()
	startDate := time.Now().Format(utils.FormatDate)
	endDate := time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)

	sql := `UPDATE company_product SET status='试用',start_date=?,end_date=?,modify_time=NOW() WHERE company_id=? AND product_id=? `
	_, err = o.Raw(sql, startDate, endDate, companyId, productId).Exec()
	if err != nil {
		return err
	}
	sql = `UPDATE company SET type=2,last_updated_time=NOW(),start_date=?,end_date=? WHERE company_id=? `
	_, err = o.Raw(sql, startDate, endDate, companyId).Exec()
	if err != nil {
		return err
	}
	return
}

func GetCompanyOldDataSync() (items []*Company, err error) {
	sql := `SELECT * FROM company WHERE company_id NOT IN(
			SELECT company_id FROM company_product
			)
			AND company_id<>1 `
	o := orm.NewOrm()
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type CompanyProduct struct {
	CompanyProductId int       `orm:"column(company_product_id);pk" description:"客户产品id"`
	CompanyId        int       `description:"客户id"`
	ProductId        int       `description:"产品id"`
	ProductName      string    `description:"产品名称"`
	CompanyName      string    `description:"客户名称"`
	Source           string    `description:"来源"`
	Reasons          string    `description:"新增理由"`
	Status           string    `description:"客户状态"`
	IndustryId       int       `description:"行业id"`
	IndustryName     string    `description:"行业名称"`
	SellerId         int       `description:"销售id"`
	SellerName       string    `description:"销售名称"`
	GroupId          int       `description:"销售分组id"`
	DepartmentId     int       `description:"销售部门id"`
	IsSuspend        int       `description:"1:暂停,0:启用"`
	SuspendTime      time.Time `description:"暂停启用时间"`
	ApproveStatus    string    `description:"审批状态:'审批中','通过','驳回'"`
	FreezeTime       time.Time `description:"冻结时间"`
	Remark           string    `description:"备注信息"`
	CreateTime       time.Time `description:"创建时间"`
	ModifyTime       time.Time `description:"修改时间"`
	StartDate        string    `description:"开始日期"`
	EndDate          string    `description:"结束日期"`
	LoseReason       string    `description:"流失原因"`
	LossTime         time.Time `description:"流失时间"`
	CompanyType      string    `description:"客户类型"`
}

//获取产品详情
func GetCompanyProduct(companyId int,productId int)(companyProduct *CompanyProduct,err error){
	o := orm.NewOrm()
	sql := `SELECT *
				FROM company_product WHERE company_id= ? AND product_id = ? `
	err = o.Raw(sql,companyId,productId).QueryRow(&companyProduct)
	return
}

//新增客户产品
func AddCompanyProduct(item *CompanyProduct) (newId int64, err error) {
	o := orm.NewOrm()
	newId, err = o.Insert(item)
	return
}

type Sellers struct {
	AdminId      int    `description:"销售id"`
	RealName     string `description:"销售姓名"`
	Email        string `description:"销售邮箱"`
	OpenId       string `description:"销售openid"`
	Mobile       string `description:"销售电话"`
	RoleTypeCode string `description:"角色编码"`
}

func GetSellers() (items []*Sellers, err error) {
	o := orm.NewOrm()
	sql := `SELECT a.real_name,a.email,b.open_id,a.mobile,b.mobile,a.role_type_code,a.admin_id
           FROM admin AS a
			LEFT JOIN wx_user AS b ON a.mobile=b.mobile
			WHERE role_type_code IN('ficc_seller','ficc_admin','rai_seller','rai_admin')
			AND b.open_id<>'' `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func GetRemindCompany(sellerId int, endDate string) (items []*CompanyProduct, err error) {
	o := orm.NewOrm()
	sql := ` SELECT b.end_date,a.company_name,b.status,b.seller_id,b.seller_name
             FROM company a
			 INNER JOIN company_product AS b ON a.company_id=b.company_id
			 WHERE  b.seller_id=? AND  b.end_date = ?
			AND b.status IN('正式','试用') ORDER BY b.status DESC `
	_,err=o.Raw(sql, sellerId, endDate).QueryRows(&items)
	return
}
