package day_new

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

// 企业微信用户信息表
type WeworkUser struct {
	Id             uint64    `orm:"column(id);pk;" description:"自增ID"`
	WwUserId       string    `orm:"column(ww_user_id)" description:"企业微信成员userid"`
	WwExtendUserId string    `orm:"column(ww_extend_user_id)" description:"非企业成员id（external_userid）"`
	WwNickName     string    `orm:"column(ww_nick_name)" description:"企业微信昵称/微信昵称"`
	WwDeptId       int       `orm:"column(ww_dept_id)" description:"企业微信内部门ID"`
	CreateTime     time.Time `orm:"column(create_time)" description:"创建时间"`
	ModifyTime     time.Time `orm:"column(modify_time)" description:"修改时间"`
}

// AddWeworkUserMulti 批量新增用户
func AddWeworkUserMulti(list []*WeworkUser) (err error) {
	o := orm.NewOrm()
	_, err = o.InsertMulti(len(list), list)
	return
}

// GetAllWeworkUser 获取所有企业微信用户
func GetAllWeworkUser() (list []*WeworkUser, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM wework_user`
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// GetEmptyNickNameWeworkUser 获取所有企业微信昵称为空的用户
func GetEmptyNickNameWeworkUser() (list []*WeworkUser, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM wework_user where (ww_nick_name = "" or ww_nick_name is null)`
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// GetWeworkUserByUserIds 根据员工ID查询员工姓名
func GetWeworkUserByUserIds(userIds string) (list []*WeworkUser, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM wework_user where ww_user_id in (`+userIds+`)`
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// GetWeworkUserByExtendUserIds 根据客户联系人ID查询昵称
func GetWeworkUserByExtendUserIds(extendUserIds string) (list []*WeworkUser, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM wework_user where ww_extend_user_id in (`+extendUserIds+`)`
	_, err = o.Raw(sql).QueryRows(&list)
	return
}
// MultiUpdateWeworkUserName 批量更新用户姓名
func MultiUpdateWeworkUserName(multiSql string, updateSendIds string) (err error)  {
	o := orm.NewOrm()
	sql := `UPDATE wework_user 
SET modify_time=NOW(), ww_nick_name =
CASE id `+multiSql+` END 
WHERE 
	id IN (`+updateSendIds+` ) `
	_, err = o.Raw(sql).Exec()
	return
}