作者 yangfu

用户部门 岗位

  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/astaxie/beego/orm"
  8 +)
  9 +
  10 +type UserDepartment struct {
  11 + Id int64 `orm:"column(id);auto" description:"主键"`
  12 + UserId int64 `orm:"column(user_id)" description:"用户id"`
  13 + CompanyId int `orm:"column(company_id)" description:"公司id"`
  14 + DepartmentId int `orm:"column(department_id)" description:"部门id"`
  15 + CreateTime time.Time `orm:"column(create_time);type(timestamp);null" description:"创建时间"`
  16 + EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"`
  17 +}
  18 +
  19 +func (t *UserDepartment) TableName() string {
  20 + return "user_department"
  21 +}
  22 +
  23 +func init() {
  24 + orm.RegisterModel(new(UserDepartment))
  25 +}
  26 +
  27 +// AddUserDepartment insert a new UserDepartment into database and returns
  28 +// last inserted Id on success.
  29 +func AddUserDepartment(m *UserDepartment) (id int64, err error) {
  30 + o := orm.NewOrm()
  31 + id, err = o.Insert(m)
  32 + return
  33 +}
  34 +
  35 +// GetUserDepartmentById retrieves UserDepartment by Id. Returns error if
  36 +// Id doesn't exist
  37 +func GetUserDepartmentById(id int64) (v *UserDepartment, err error) {
  38 + o := orm.NewOrm()
  39 + v = &UserDepartment{Id: id}
  40 + if err = o.Read(v); err == nil {
  41 + return v, nil
  42 + }
  43 + return nil, err
  44 +}
  45 +
  46 +// UpdateUserDepartment updates UserDepartment by Id and returns error if
  47 +// the record to be updated doesn't exist
  48 +func UpdateUserDepartmentById(m *UserDepartment) (err error) {
  49 + o := orm.NewOrm()
  50 + v := UserDepartment{Id: m.Id}
  51 + // ascertain id exists in the database
  52 + if err = o.Read(&v); err == nil {
  53 + var num int64
  54 + if num, err = o.Update(m); err == nil {
  55 + fmt.Println("Number of records updated in database:", num)
  56 + }
  57 + }
  58 + return
  59 +}
  60 +
  61 +// DeleteUserDepartment deletes UserDepartment by Id and returns error if
  62 +// the record to be deleted doesn't exist
  63 +func DeleteUserDepartment(id int64) (err error) {
  64 + o := orm.NewOrm()
  65 + v := UserDepartment{Id: id}
  66 + // ascertain id exists in the database
  67 + if err = o.Read(&v); err == nil {
  68 + var num int64
  69 + if num, err = o.Delete(&UserDepartment{Id: id}); err == nil {
  70 + fmt.Println("Number of records deleted in database:", num)
  71 + }
  72 + }
  73 + return
  74 +}
  75 +
  76 +
  77 +func GetUserDepartments(userId int64, companyId int64, v interface{}) (err error) {
  78 + o := orm.NewOrm()
  79 + sql := `
  80 +select a.department_id,b.name
  81 +from user_department a INNER JOIN department b on a.department_id = b.id
  82 +where a.user_id =? and a.company_id =? and enable_status =1 and b.delete_at =0`
  83 + if _, err = o.Raw(sql,userId, companyId).QueryRows(v); err == nil {
  84 + return
  85 + }
  86 + return
  87 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/astaxie/beego/orm"
  8 +)
  9 +
  10 +type UserPosition struct {
  11 + Id int `orm:"column(id);pk" description:"唯一键值"`
  12 + UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
  13 + PositionId int `orm:"column(position_id)" description:"表position.id 职位编号"`
  14 + CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
  15 + CompanyId int `orm:"column(company_id)" description:"表company.id 公司编号"`
  16 + EnableStatus int8 `orm:"column(enable_status);null" description:"是否有效 1:有效 0:无效"`
  17 +}
  18 +
  19 +func (t *UserPosition) TableName() string {
  20 + return "user_position"
  21 +}
  22 +
  23 +func init() {
  24 + orm.RegisterModel(new(UserPosition))
  25 +}
  26 +
  27 +// AddUserPosition insert a new UserPosition into database and returns
  28 +// last inserted Id on success.
  29 +func AddUserPosition(m *UserPosition) (id int64, err error) {
  30 + o := orm.NewOrm()
  31 + id, err = o.Insert(m)
  32 + return
  33 +}
  34 +
  35 +// GetUserPositionById retrieves UserPosition by Id. Returns error if
  36 +// Id doesn't exist
  37 +func GetUserPositionById(id int) (v *UserPosition, err error) {
  38 + o := orm.NewOrm()
  39 + v = &UserPosition{Id: id}
  40 + if err = o.Read(v); err == nil {
  41 + return v, nil
  42 + }
  43 + return nil, err
  44 +}
  45 +
  46 +// UpdateUserPosition updates UserPosition by Id and returns error if
  47 +// the record to be updated doesn't exist
  48 +func UpdateUserPositionById(m *UserPosition) (err error) {
  49 + o := orm.NewOrm()
  50 + v := UserPosition{Id: m.Id}
  51 + // ascertain id exists in the database
  52 + if err = o.Read(&v); err == nil {
  53 + var num int64
  54 + if num, err = o.Update(m); err == nil {
  55 + fmt.Println("Number of records updated in database:", num)
  56 + }
  57 + }
  58 + return
  59 +}
  60 +
  61 +// DeleteUserPosition deletes UserPosition by Id and returns error if
  62 +// the record to be deleted doesn't exist
  63 +func DeleteUserPosition(id int) (err error) {
  64 + o := orm.NewOrm()
  65 + v := UserPosition{Id: id}
  66 + // ascertain id exists in the database
  67 + if err = o.Read(&v); err == nil {
  68 + var num int64
  69 + if num, err = o.Delete(&UserPosition{Id: id}); err == nil {
  70 + fmt.Println("Number of records deleted in database:", num)
  71 + }
  72 + }
  73 + return
  74 +}
  75 +
  76 +
  77 +func GetUserPositions(userId int64, companyId int64, v interface{}) (err error) {
  78 + o := orm.NewOrm()
  79 + sql := `
  80 +select a.position_id,b.name
  81 +from user_position a INNER JOIN position b on a.position_id = b.id
  82 +where a.user_id =? and a.company_id =? and a.enable_status =1 and b.enable_status =1`
  83 + if _, err = o.Raw(sql,userId, companyId).QueryRows(v); err == nil {
  84 + return
  85 + }
  86 + return
  87 +}