package cygx

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

type CygxUserRemind struct {
	Id          int `orm:"column(id);pk"`
	AdminId     int
	UserId      int
	CreateTime  time.Time
	ModifyTime  time.Time
	Mobile      string `description:"手机号"`
	Email       string `description:"邮箱"`
	CompanyId   int    `description:"公司id"`
	CompanyName string `description:"公司名称"`
	RealName    string `description:"用户实际名称"`
	AdminName   string `description:"管理员销售姓名"`
}

// 添加
func AddCygxUserRemind(item *CygxUserRemind) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	lastId, err = o.Insert(item)
	return
}

// AddCygxUserRemindMulti 批量添加
func AddCygxUserRemindMulti(items []*CygxUserRemind) (err error) {
	o, err := orm.NewOrmUsingDB("hz_cygx").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
}

type CygxUserRemindResp struct {
	Status int `description:"1:添加,2:取消"`
}

func RemoveCygxUserRemind(userId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `DELETE FROM cygx_user_remind WHERE user_id=? `
	_, err = o.Raw(sql, userId).Exec()
	return
}

func RemoveCygxUserRemindByCompanyId(companyId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `DELETE FROM cygx_user_remind WHERE company_id = ? `
	_, err = o.Raw(sql, companyId).Exec()
	return
}

func GetCygxUserRemindCount(userId int) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT COUNT(1) AS count FROM cygx_user_remind WHERE user_id=? `
	err = o.Raw(sql, userId).QueryRow(&count)
	return
}

// 列表
func GetCygxUserRemindList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxUserRemind, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_user_remind as art WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	if startSize+startSize > 0 {
		sql += ` LIMIT ?,?  `
		_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	} else {
		_, err = o.Raw(sql, pars).QueryRows(&items)
	}
	return
}

// 根据公司ID获取列表
func GetCygxUserRemindListByCompanyId(companyId int) (items []*CygxUserRemind, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_user_remind  WHERE 1= 1  AND company_id = ? `
	_, err = o.Raw(sql, companyId).QueryRows(&items)
	return
}