package models

import (
	"fmt"
	"time"

	"github.com/astaxie/beego/orm"
)

type User struct {
	Id       int64  `orm:"column(id);pk" description:"用户id"`
	NickName string `orm:"column(nick_name);size(100)" description:"昵称"`
	Phone    string `orm:"column(phone);size(40)" description:"手机号码"`
	Passwd   string `orm:"column(passwd);size(128)" description:"密码"`
	Icon     string `orm:"column(icon);size(255)" description:"头像"`
	//CompanyId     int       `orm:"column(company_id)" description:"公司Id"`
	//DepartmentId  int       `orm:"column(department_id)" description:"部门id"`
	//PositionId    int       `orm:"column(position_id)" description:"职位id"`
	CsAccount     int64     `orm:"column(cs_account)" description:"客服有话说ID"`
	IsKefu        int8      `orm:"column(is_kefu)" description:"是否是客服 0:否 1:是"`
	ImToken       string    `orm:"column(im_token);size(128)" description:"网易云token"`
	LastLoginTime time.Time `orm:"column(last_login_time);type(timestamp);auto_now_add" description:"最后一次登录时间"`
	CreateAt      time.Time `orm:"column(create_at);type(timestamp);auto_now_add" description:"创建时间"`
	EnableStatus  int8      `orm:"column(enable_status)" description:"是否有效"`
	Accid         int64     `orm:"column(accid)" description:"网易云id"`
	UserCenterId  int64     `orm:"column(user_center_id)" description:"统一用户中心用户id"`
}

func (t *User) TableName() string {
	return "user"
}

func init() {
	orm.RegisterModel(new(User))
}

// AddUsers insert a new Users into database and returns
// last inserted Id on success.
func AddUsers(m *User) (id int64, err error) {
	o := orm.NewOrm()
	id, err = o.Insert(m)
	return
}

// GetUsersById retrieves Users by Id. Returns error if
// Id doesn't exist
func GetUsersById(id int64) (v *User, err error) {
	o := orm.NewOrm()
	v = &User{Id: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

// UpdateUsers updates Users by Id and returns error if
// the record to be updated doesn't exist
func UpdateUsersById(m *User) (err error) {
	o := orm.NewOrm()
	v := User{Id: m.Id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteUsers deletes Users by Id and returns error if
// the record to be deleted doesn't exist
func DeleteUsers(id int64) (err error) {
	o := orm.NewOrm()
	v := User{Id: id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&User{Id: id}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

func GetUserByMobile(mobile string) (v *User, err error) {
	o := orm.NewOrm()
	sql := "select * from user where phone=? and enable_status=1"
	if err = o.Raw(sql, mobile).QueryRow(&v); err == nil {
		return v, nil
	}
	return nil, err
}

func GetUserByUcenterId(uid int64) (v *User, err error) {
	o := orm.NewOrm()
	sql := "select * from user where user_center_id=? and enable_status=1"
	if err = o.Raw(sql, uid).QueryRow(&v); err == nil {
		return v, nil
	}
	return nil, err
}

func GetUserByRole(roleId int) (v []*User, err error) {
	o := orm.NewOrm()
	sql := `select * from user where is_kefu = 1 and enable_status=1`
	if _, err = o.Raw(sql).QueryRows(&v); err == nil {
		return v, nil
	}
	return nil, err
}