package company

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

type CompanyHistoryRemark struct {
	HistoryId    int       `orm:"column(history_id);pk" comment:"备注id"`
	CompanyId    int       `comment:"客户ID"`
	ProductId    int       `comment:"产品id"`
	Content      string    `comment:"备注内容"`
	SysAdminId   int       `comment:"创建人ID"`
	SysAdminName string    `comment:"创建人姓名"`
	CreateTime   time.Time `comment:"创建时间"`
	ModifyTime   time.Time `comment:"更新时间"`
	TableName    string    `comment:"表名"`
	TableId      int       `comment:"表ID"`
	UserId       int       `comment:"用户ID"`
	Mobile       string    `comment:"手机号"`
	Email        string    `comment:"邮箱"`
	RealName     string    `comment:"用户实际名称"`
	ShowTime     time.Time `comment:"对外展示的创建时间"`
}

// CompanyHistoryRemarkReq 新增历史备注请求
type CompanyHistoryRemarkReq struct {
	CompanyId int    `description:"客户id"`
	Content   string `description:"备注内容"`
}

// 新增
func AddCompanyHistoryRemark(remark *CompanyHistoryRemark) (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(remark)
	return
}

type CompanyHistoryRemarkResp struct {
	CompanyId    int    `comment:"客户ID"`
	Content      string `comment:"备注内容"`
	SysAdminName string `comment:"创建人姓名"`
	RemarkType   string `comment:"备注类型"`
	CreateTime   string `comment:"对外展示的创建时间"`
}

type CompanyHistoryRemarkListResp struct {
	List []*CompanyHistoryRemarkResp
}

// GetCompanyHistoryRemarkList 获取备注列表
func GetCompanyHistoryRemarkList(CompanyId, ProductId string) (items []*CompanyHistoryRemark, err error) {
	o := orm.NewOrm()
	sql := "SELECT * FROM company_history_remark WHERE company_id=? AND product_id=? ORDER BY show_time DESC "
	_, err = o.Raw(sql, CompanyId, ProductId).QueryRows(&items)
	return
}

// DelCompanyHistoryRemark 删除客户历史备注
func DelCompanyHistoryRemark(tableName string, tableId int) (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM company_history_remark WHERE table_name = ? AND  table_id = ?   `
	_, err = o.Raw(sql, tableName, tableId).Exec()
	return
}

// GetCompanyHistoryRemarkList 获取备注列表
func GetCompanyHistoryRemarkListinit() (items []*CompanyHistoryRemark, err error) {
	o := orm.NewOrm()
	sql := "SELECT * FROM company_history_remark  WHERE	 table_id > 0   "
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// AddCompanyHistoryRemarkMultiinit 批量添加
func AddCompanyHistoryRemarkMultiinit(items []*CompanyHistoryRemark) (err error) {
	if len(items) == 0 {
		return
	}
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	if len(items) > 0 {
		//批量添加流水信息
		_, err = o.InsertMulti(len(items), items)
	}
	return
}
