作者 唐旭辉

更新

1 package models 1 package models
2 2
3 import ( 3 import (
  4 + "encoding/json"
4 "errors" 5 "errors"
5 "fmt" 6 "fmt"
  7 + "oppmg/common/log"
6 "reflect" 8 "reflect"
7 "strings" 9 "strings"
8 "time" 10 "time"
@@ -11,16 +13,16 @@ import ( @@ -11,16 +13,16 @@ import (
11 ) 13 )
12 14
13 type Department struct { 15 type Department struct {
14 - Id int `orm:"column(id);auto"`  
15 - CompanyId int `orm:"column(company_id)" description:"公司id"` 16 + Id int64 `orm:"column(id);auto"`
  17 + CompanyId int64 `orm:"column(company_id)" description:"公司id"`
16 Name string `orm:"column(name);size(30)" description:"部门名称"` 18 Name string `orm:"column(name);size(30)" description:"部门名称"`
17 CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"` 19 CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
18 - ParentId int `orm:"column(parent_id)" description:"父级id"` 20 + ParentId int64 `orm:"column(parent_id)" description:"父级id"`
19 Relation string `orm:"column(relation);size(1024)" description:"父子级关系树"` 21 Relation string `orm:"column(relation);size(1024)" description:"父子级关系树"`
20 DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"` 22 DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"`
21 UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"` 23 UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
22 Member int `orm:"column(member)" description:"成员数量"` 24 Member int `orm:"column(member)" description:"成员数量"`
23 - Admin int `orm:"column(admin);null" description:"部门负责人id"` 25 + Manages string `orm:"column(managers)" description:"部门负责人id列表 json 数组 []"`
24 } 26 }
25 27
26 func (t *Department) TableName() string { 28 func (t *Department) TableName() string {
@@ -31,6 +33,15 @@ func init() { @@ -31,6 +33,15 @@ func init() {
31 orm.RegisterModel(new(Department)) 33 orm.RegisterModel(new(Department))
32 } 34 }
33 35
  36 +func (t *Department) GetManagesIds() []int64 {
  37 + var r []int64
  38 + err := json.Unmarshal([]byte(t.Manages), &r)
  39 + if err != nil {
  40 + log.Error(err.Error())
  41 + }
  42 + return r
  43 +}
  44 +
34 // AddDepartment insert a new Department into database and returns 45 // AddDepartment insert a new Department into database and returns
35 // last inserted Id on success. 46 // last inserted Id on success.
36 func AddDepartment(m *Department) (id int64, err error) { 47 func AddDepartment(m *Department) (id int64, err error) {
@@ -41,7 +52,7 @@ func AddDepartment(m *Department) (id int64, err error) { @@ -41,7 +52,7 @@ func AddDepartment(m *Department) (id int64, err error) {
41 52
42 // GetDepartmentById retrieves Department by Id. Returns error if 53 // GetDepartmentById retrieves Department by Id. Returns error if
43 // Id doesn't exist 54 // Id doesn't exist
44 -func GetDepartmentById(id int) (v *Department, err error) { 55 +func GetDepartmentById(id int64) (v *Department, err error) {
45 o := orm.NewOrm() 56 o := orm.NewOrm()
46 v = &Department{Id: id} 57 v = &Department{Id: id}
47 if err = o.Read(v); err == nil { 58 if err = o.Read(v); err == nil {
@@ -145,7 +156,7 @@ func UpdateDepartmentById(m *Department) (err error) { @@ -145,7 +156,7 @@ func UpdateDepartmentById(m *Department) (err error) {
145 156
146 // DeleteDepartment deletes Department by Id and returns error if 157 // DeleteDepartment deletes Department by Id and returns error if
147 // the record to be deleted doesn't exist 158 // the record to be deleted doesn't exist
148 -func DeleteDepartment(id int) (err error) { 159 +func DeleteDepartment(id int64) (err error) {
149 o := orm.NewOrm() 160 o := orm.NewOrm()
150 v := Department{Id: id} 161 v := Department{Id: id}
151 // ascertain id exists in the database 162 // ascertain id exists in the database
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +
  7 + "github.com/astaxie/beego/orm"
  8 +)
  9 +
  10 +type UserCompany struct {
  11 + Id int64 `orm:"column(id);auto" description:"唯一标识"`
  12 + CompanyId int64 `orm:"column(company_id)" description:"表company.id 公司编号"`
  13 + UserId int64 `orm:"column(user_id)" description:"表user.id 用户编号"`
  14 + DepartmentId int `orm:"column(department_id)" description:"部门id"`
  15 + PositionId int `orm:"column(position_id)" description:"职位id"`
  16 + ChanceTotal int `orm:"column(chance_total)" description:"发表机会数"`
  17 + CommentTotal int `orm:"column(comment_total)" description:"发表评论总数"`
  18 + CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
  19 + UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
  20 +}
  21 +
  22 +func (t *UserCompany) TableName() string {
  23 + return "user_company"
  24 +}
  25 +
  26 +func init() {
  27 + orm.RegisterModel(new(UserCompany))
  28 +}
  29 +
  30 +// AddUserCompany insert a new UserCompany into database and returns
  31 +// last inserted Id on success.
  32 +func AddUserCompany(m *UserCompany) (id int64, err error) {
  33 + o := orm.NewOrm()
  34 + id, err = o.Insert(m)
  35 + return
  36 +}
  37 +
  38 +// UpdateUserCompany updates UserCompany by Id and returns error if
  39 +// the record to be updated doesn't exist
  40 +func UpdateUserCompanyById(m *UserCompany) (err error) {
  41 + o := orm.NewOrm()
  42 + v := UserCompany{Id: m.Id}
  43 + // ascertain id exists in the database
  44 + if err = o.Read(&v); err == nil {
  45 + var num int64
  46 + if num, err = o.Update(m); err == nil {
  47 + fmt.Println("Number of records updated in database:", num)
  48 + }
  49 + }
  50 + return
  51 +}
  52 +
  53 +func GetUserCompanyBy(userid int64, companyId int64) (*UserCompany, error) {
  54 + o := orm.NewOrm()
  55 + v := &UserCompany{
  56 + UserId: userid,
  57 + CompanyId: companyId,
  58 + }
  59 + err := o.Read(v, "UserId", "CompanyId")
  60 + if err != nil {
  61 + return nil, err
  62 + }
  63 + return v, nil
  64 +}
  1 +package protocol
  2 +
  3 +import "sort"
  4 +
  5 +//输入框类型
  6 +const (
  7 + inputTypeCheck string = "check-box" //多选宽
  8 + inputTypeText string = "text" //单行文本宽
  9 + InputTypeRedio string = "redio" //单选框
  10 +)
  11 +
  12 +//InputElement 自定义表单项
  13 +type InputElement struct {
  14 + Sort int `json:"sort"` //排序
  15 + Lable string `json:"lable"` //标题
  16 + InputType string `json:"input_type"` //输入类型
  17 + ValueList string `json:"value_list"` //输入候选值
  18 + Required bool `json:"required"` //是否必填
  19 + Placeholder string `json:"Placeholder"` //帮助用户填写输入字段的提示
  20 + Disable bool `json:"disable ` //"显示隐藏",
  21 + CurrentValue string `json:"current_value"` //"当前填写的值"
  22 +}
  23 +
  24 +//自定义表单
  25 +type CustomForm []InputElement
  26 +
  27 +var (
  28 + _ sort.Interface = new(CustomForm)
  29 +)
  30 +
  31 +//实现排序接口
  32 +func (a CustomForm) Len() int { return len(a) }
  33 +func (a CustomForm) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  34 +func (a CustomForm) Less(i, j int) bool {
  35 + return a[i].Sort < a[j].Sort
  36 +}
  37 +
  38 +//IValidateInput 自定义输入项校验接口
  39 +type IValidateInput interface {
  40 + ValidateInput() error //校验当前输入值
  41 + ValidateConfig() error //校验自定义的输入项设置
  42 +}
  43 +
  44 +type ValidateInputText struct {
  45 + InputElement
  46 +}
  47 +
  48 +var (
  49 + _ IValidateInput = ValidateInputText{}
  50 +)
  51 +
  52 +func (input ValidateInputText) ValidateInput() error {
  53 + return nil
  54 +}
  55 +func (input ValidateInputText) ValidateConfig() error {
  56 + return nil
  57 +}
  58 +
  59 +//ValidateInputRedio 单选项校验
  60 +type ValidateInputRedio struct {
  61 + InputElement
  62 +}
  63 +
  64 +var (
  65 + _ IValidateInput = ValidateInputRedio{}
  66 +)
  67 +
  68 +func (input ValidateInputRedio) ValidateInput() error {
  69 + return nil
  70 +}
  71 +
  72 +func (input ValidateInputRedio) ValidateConfig() error {
  73 + return nil
  74 +}
  1 +package protocol
  2 +
  3 +//RequestDepartmentAdd 部门设置
  4 +type RequestDepartmentAdd struct {
  5 + CompanyID int64 `json:"company_id"` //公司
  6 + Name string `json:"name"` //部门名字
  7 + ParantID int64 `json:"parant_id"` //父级部门Id
  8 + Managers []int64 `json:"admin_id"` //主管userid
  9 +}
  10 +
  11 +//ResponseDepartmentInfo ...
  12 +type ResponseDepartmentInfo struct {
  13 + ID int64 `json:"id`
  14 + CompanyID int64 `json:"company_id"` //公司
  15 + Name string `json:"name"` //部门名字
  16 + ParantID int64 `json:"parant_id"` //父级部门Id
  17 + Manages []DepartmentManager `json:"manages"` //部门管理员
  18 + Member int `json:"member"` //成员数
  19 + ParantName string `json:"parant_name"` //父级部门名字
  20 +}
  21 +
  22 +type DepartmentManager struct {
  23 + UserId int `json:"user_id"`
  24 + Name string `json:"name`
  25 +}
  26 +
  27 +//RequestDepartmentEdit 编辑
  28 +type RequestDepartmentEdit struct {
  29 + ID int64 `json:"id"`
  30 + RequestDepartmentAdd
  31 +}
  32 +
  33 +//RequestDepartmentDelete ...
  34 +type RequestDepartmentDelete struct {
  35 + ID int64 `json:"id"`
  36 + CompanyID int64 `json:"company_id"` //公司
  37 +}
  38 +
  39 +//ResponseDepartmentList ....
  40 +type ResponseDepartmentList struct {
  41 + List []ResponseDepartmentInfo
  42 +}
@@ -2,7 +2,7 @@ package protocol @@ -2,7 +2,7 @@ package protocol
2 2
3 //RequestRoleAdd 添加角色信息操作入参 3 //RequestRoleAdd 添加角色信息操作入参
4 type RequestRoleAdd struct { 4 type RequestRoleAdd struct {
5 - CompanyID int `json:"company,omitempty"` 5 + CompanyID int `json:"company"`
6 Name string `json:"name"` 6 Name string `json:"name"`
7 Descript string `json:"descript"` 7 Descript string `json:"descript"`
8 } 8 }
@@ -15,10 +15,8 @@ type RequestRoleDelete struct { @@ -15,10 +15,8 @@ type RequestRoleDelete struct {
15 15
16 //RequestRoleEdit 编辑角色信息入参 16 //RequestRoleEdit 编辑角色信息入参
17 type RequestRoleEdit struct { 17 type RequestRoleEdit struct {
18 - ID int `json:"id"`  
19 - Name string `json:"name"`  
20 - CompanyID int `json:"company_id"`  
21 - Descript string `json:"descript"` 18 + ID int `json:"id"`
  19 + RequestRoleAdd
22 } 20 }
23 21
24 //RequestRoleOne 获取一个角色数据 22 //RequestRoleOne 获取一个角色数据
  1 +package company
  2 +
  3 +import (
  4 + "fmt"
  5 + "oppmg/common/log"
  6 + "oppmg/models"
  7 + "oppmg/protocol"
  8 + "oppmg/utils"
  9 +
  10 + "github.com/astaxie/beego/orm"
  11 +)
  12 +
  13 +func DepartmentAdd(param protocol.RequestDepartmentAdd) (protocol.ResponseDepartmentInfo, error) {
  14 +
  15 + r := protocol.ResponseDepartmentInfo{}
  16 + return r, nil
  17 +
  18 +}
  19 +
  20 +func DepartmentEdit(param protocol.RequestDepartmentEdit) (protocol.ResponseDepartmentInfo, error) {
  21 + var (
  22 + depart *models.Department
  23 + err error
  24 + result protocol.ResponseDepartmentInfo
  25 + )
  26 + result = protocol.ResponseDepartmentInfo{
  27 + ID: param.ID,
  28 + Name: param.Name,
  29 + ParantID: param.ParantID,
  30 + }
  31 + depart, err = models.GetDepartmentById(param.ID)
  32 + if err != nil {
  33 + e := fmt.Errorf("GetDepartmentById(%d) err:%s", param.ID, err)
  34 + log.Error(e.Error())
  35 + return result, protocol.NewErrWithMessage("1", e)
  36 + }
  37 + if depart.CompanyId != param.CompanyID {
  38 + e := fmt.Errorf("depart.CompanyId(%d) !=param.CompanyID(%d)", depart.CompanyId, param.CompanyID)
  39 + log.Error(e.Error())
  40 + return result, protocol.NewErrWithMessage("1", e)
  41 + }
  42 + //确认部门主管变更情况
  43 + var (
  44 + oldmanage []int64
  45 + )
  46 + oldmanage = depart.GetManagesIds()
  47 + diffmanage := utils.ArrayInt64Diff(param.Managers, oldmanage)
  48 + for i := range diffmanage {
  49 + _, err = models.GetUserCompanyBy(diffmanage[i], param.CompanyID)
  50 + if err != nil {
  51 + e := fmt.Errorf("GetUserCompanyBy(%d,%d) err:%s", diffmanage[i], param.CompanyID, err)
  52 + log.Error(e.Error())
  53 + return result, protocol.NewErrWithMessage("1", e)
  54 + }
  55 + }
  56 + //处理部门上级发生变化的情况
  57 + if depart.ParentId != param.ParantID {
  58 + //oldRelation := strings.Split(depart.Relation, "")
  59 + }
  60 + //更新部门数据
  61 + departmentInfoUpdate(depart, &param)
  62 + return result, nil
  63 +}
  64 +
  65 +//DepartmentParentChange 处理部门上级发生变化的情况
  66 +func departmentInfoUpdate(old *models.Department, new *protocol.RequestDepartmentEdit) error {
  67 +
  68 + o := orm.NewOrm()
  69 + o.Begin()
  70 +
  71 + o.Commit()
  72 + return nil
  73 +}
  74 +
  75 +func DepartmentDelete(parm protocol.RequestDepartmentDelete) error {
  76 + return nil
  77 +}
  78 +
  79 +func DepartmentListAll() (protocol.ResponseDepartmentList, error) {
  80 + r := protocol.ResponseDepartmentList{}
  81 + return r, nil
  82 +}
1 package utils 1 package utils
  2 +
  3 +import (
  4 + "strings"
  5 +)
  6 +
  7 +type ArrayCmpare interface {
  8 +}
  9 +
  10 +//ArrayInt64Diff 返回切片的差集:arr1-arr2
  11 +func ArrayInt64Diff(arr1 []int64, arr2 []int64) []int64 {
  12 + setmap := make(map[int64]bool)
  13 + for i := range arr2 {
  14 + setmap[arr1[i]] = true
  15 + }
  16 + var result []int64
  17 + for i := range arr1 {
  18 + if _, ok := setmap[arr1[i]]; !ok {
  19 + result = append(result, arr1[i])
  20 + }
  21 + }
  22 +
  23 + return result
  24 +}
  25 +
  26 +//ArrayStringIn 检查s字符串是否在切片sl中
  27 +func ArrayStringIn(arr1 []string, s string) bool {
  28 + for _, v := range arr1 {
  29 + if strings.Compare(v, s) == 0 {
  30 + return true
  31 + }
  32 + }
  33 + return false
  34 +}
@@ -14,17 +14,17 @@ func PrintLogSql(sql string, param ...interface{}) { @@ -14,17 +14,17 @@ func PrintLogSql(sql string, param ...interface{}) {
14 log.Debug(format, sql, fmt.Sprint(param...)) 14 log.Debug(format, sql, fmt.Sprint(param...))
15 } 15 }
16 16
17 -//ExcuteSql 执行原生sql语句  
18 -func ExcuteSql(result interface{}, sqlstr string, param ...interface{}) error { 17 +//ExcuteQueryOne 执行原生sql查询单条记录;结果用结构体接收
  18 +func ExcuteQueryOne(result interface{}, sqlstr string, param ...interface{}) error {
19 PrintLogSql(sqlstr, param...) 19 PrintLogSql(sqlstr, param...)
20 var err error 20 var err error
21 o := orm.NewOrm() 21 o := orm.NewOrm()
22 - err = ExcuteSqlWithOrmer(o, result, sqlstr, param) 22 + err = ExcuteQueryOneWithOrmer(o, result, sqlstr, param)
23 return err 23 return err
24 } 24 }
25 25
26 -//ExcuteSqlWithOrmer 执行原生sql语句  
27 -func ExcuteSqlWithOrmer(o orm.Ormer, result interface{}, sqlstr string, param ...interface{}) error { 26 +//ExcuteQueryOneWithOrmer 执行原生sql查询单条
  27 +func ExcuteQueryOneWithOrmer(o orm.Ormer, result interface{}, sqlstr string, param ...interface{}) error {
28 PrintLogSql(sqlstr, param...) 28 PrintLogSql(sqlstr, param...)
29 var err error 29 var err error
30 err = o.Raw(sqlstr, param).QueryRow(result) 30 err = o.Raw(sqlstr, param).QueryRow(result)
@@ -34,6 +34,28 @@ func ExcuteSqlWithOrmer(o orm.Ormer, result interface{}, sqlstr string, param .. @@ -34,6 +34,28 @@ func ExcuteSqlWithOrmer(o orm.Ormer, result interface{}, sqlstr string, param ..
34 return nil 34 return nil
35 } 35 }
36 36
  37 +//ExcuteQuerySql 执行原生sql查询多条记录
  38 +func ExcuteQueryAll(result interface{}, sqlstr string, param ...interface{}) error {
  39 + PrintLogSql(sqlstr, param...)
  40 + var err error
  41 + o := orm.NewOrm()
  42 + err = ExcuteQueryOneWithOrmer(o, result, sqlstr, param)
  43 + return err
  44 +}
  45 +
  46 +//ExcuteQueryOneWithOrmer 执行原生sql查询多条记录
  47 +func ExcuteQueryAllWithOrmer(o orm.Ormer, result interface{}, sqlstr string, param ...interface{}) error {
  48 + PrintLogSql(sqlstr, param...)
  49 + var (
  50 + err error
  51 + )
  52 + _, err = o.Raw(sqlstr, param).QueryRows(result)
  53 + if err != nil {
  54 + return fmt.Errorf("SQL EXCUTE err:%s", err)
  55 + }
  56 + return nil
  57 +}
  58 +
37 type QueryDataByPage struct { 59 type QueryDataByPage struct {
38 CountSql string 60 CountSql string
39 DataSql string 61 DataSql string
@@ -49,6 +71,7 @@ func NewQueryDataByPage(countsql, datasql string) *QueryDataByPage { @@ -49,6 +71,7 @@ func NewQueryDataByPage(countsql, datasql string) *QueryDataByPage {
49 } 71 }
50 } 72 }
51 73
  74 +//AddParam 添加条件参数
52 func (q *QueryDataByPage) AddParam(param ...interface{}) { 75 func (q *QueryDataByPage) AddParam(param ...interface{}) {
53 q.Param = param 76 q.Param = param
54 } 77 }
@@ -58,6 +81,7 @@ func (q *QueryDataByPage) LimitPage(offset, num int) { @@ -58,6 +81,7 @@ func (q *QueryDataByPage) LimitPage(offset, num int) {
58 q.num = num 81 q.num = num
59 } 82 }
60 83
  84 +//Query 执行分页查询
61 func (q *QueryDataByPage) Query(result interface{}) (pageinfo protocol.ResponsePageInfo, err error) { 85 func (q *QueryDataByPage) Query(result interface{}) (pageinfo protocol.ResponsePageInfo, err error) {
62 pagebegin := (q.offset - 1) * q.num 86 pagebegin := (q.offset - 1) * q.num
63 if pagebegin < 0 { 87 if pagebegin < 0 {
@@ -67,7 +91,7 @@ func (q *QueryDataByPage) Query(result interface{}) (pageinfo protocol.ResponseP @@ -67,7 +91,7 @@ func (q *QueryDataByPage) Query(result interface{}) (pageinfo protocol.ResponseP
67 total int 91 total int
68 ) 92 )
69 o := orm.NewOrm() 93 o := orm.NewOrm()
70 - err = ExcuteSqlWithOrmer(o, &total, q.CountSql, q.Param...) 94 + err = ExcuteQueryOneWithOrmer(o, &total, q.CountSql, q.Param...)
71 if err != nil { 95 if err != nil {
72 return 96 return
73 } 97 }
@@ -75,7 +99,7 @@ func (q *QueryDataByPage) Query(result interface{}) (pageinfo protocol.ResponseP @@ -75,7 +99,7 @@ func (q *QueryDataByPage) Query(result interface{}) (pageinfo protocol.ResponseP
75 return protocol.ResponsePageInfo{CurrentPage: q.offset, TotalPage: total}, nil 99 return protocol.ResponsePageInfo{CurrentPage: q.offset, TotalPage: total}, nil
76 } 100 }
77 q.DataSql = fmt.Sprintf("%s limit %d,%d", q.DataSql, pagebegin, q.num) 101 q.DataSql = fmt.Sprintf("%s limit %d,%d", q.DataSql, pagebegin, q.num)
78 - err = ExcuteSqlWithOrmer(o, result, q.DataSql, q.Param...) 102 + err = ExcuteQueryAllWithOrmer(o, result, q.DataSql, q.Param...)
79 if err != nil { 103 if err != nil {
80 return 104 return
81 } 105 }