package company

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

type CompanyOperationRecord struct {
	Id                  int       `orm:"column(id);pk"`
	CompanyId           int       `description:"客户id"`
	CompanyName         string    `description:"客户名称"`
	SellerId            int       `description:"销售员id"`
	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:"审批人内容"`
	ApproveContentExtra string    `description:"审批人内容补充"`
	ApproveRemark       string    `description:"审批人内容"`
	Status              string    `description:"状态"`
}

type CompanyOperationRecordList struct {
	Id                  int    `orm:"column(id);pk"`
	CompanyId           int64  `description:"客户id"`
	CompanyName         string `description:"客户名称"`
	SysUserId           int    `description:"操作者id"`
	SysRealName         string `description:"操作者名称"`
	Remark              string `description:"备注"`
	Operation           string `description:"操作"`
	ApproveContent      string `description:"详情"`
	ApproveContentExtra string `description:"详情补充"`
	CreateTime          string `description:"操作时间"`
	ProductId           int
	ProductName         string
}

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

func GetCompanyOperationRecordList(companyId int) (items []*CompanyOperationRecordList, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM company_operation_record WHERE company_id=? ORDER BY create_time DESC `
	_, err = o.Raw(sql, companyId).QueryRows(&items)
	return
}

type CompanyOperationRecordListResp struct {
	List       []*CompanyOperationRecordList
	ShowButton bool
}

//获取客户正式转试用的次数
func GetCompanyProductOperationRecordCount(companyId, productId int) (total int64, err error) {
	o := orm.NewOrm()
	sql := `SELECT count(1) ct FROM company_operation_record 
WHERE company_id=? AND product_id=? and operation="try_out" `
	err = o.Raw(sql, companyId, productId).QueryRow(&total)
	return
}

func UpdateCompanyOperationRecord(item *CompanyOperationRecord) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Update(item, "approve_content", "approve_content_extra")
	return
}

func ReadCompanyOperationRecord(companyId int) (items []*CompanyOperationRecord, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM company_operation_record WHERE company_id=? AND operation='try_out' ORDER BY create_time DESC `
	_, err = o.Raw(sql, companyId).QueryRows(&items)
	return
}

// GetTryOutCompanyOperationRecordList 获取客户正式转试用变更的操作记录
func GetTryOutCompanyOperationRecordList() (list []*CompanyOperationRecord, err error) {
	o := orm.NewOrm()
	sql := ` select * from company_operation_record where operation="try_out";`
	_, err = o.Raw(sql).QueryRows(&list)
	return
}
