审查视图

models/department.go 5.6 KB
唐旭辉 authored
1 2 3
package models

import (
唐旭辉 authored
4
	"encoding/json"
唐旭辉 authored
5
	"fmt"
唐旭辉 authored
6
	"oppmg/common/log"
唐旭辉 authored
7
	"oppmg/protocol"
唐旭辉 authored
8
	"time"
唐旭辉 authored
9 10 11 12

	"github.com/astaxie/beego/orm"
)
唐旭辉 authored
13
type Department struct {
tangxvhui authored
14
	Id                   int64     `orm:"column(id);pk"`
tangxvhui authored
15 16 17 18 19 20 21 22 23 24 25
	CompanyId            int64     `orm:"column(company_id)" description:"公司id"`
	Name                 string    `orm:"column(name);size(30)" description:"部门名称"`
	CreateAt             time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
	ParentId             int64     `orm:"column(parent_id)" description:"父级id"`
	Relation             string    `orm:"column(relation);size(1024)" description:"父子级关系树"`
	DeleteAt             time.Time `orm:"column(delete_at);type(timestamp)" description:"删除时间"`
	UpdateAt             time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
	Manages              string    `orm:"column(managers)" description:"部门负责人id列表 json 数组 []"` //存user_company_id
	IsTop                int8      `orm:"column(is_top)" `
	Level                int       `orm:"column(level)" `
	BusinessDepartmentId int64     `orm:"column(business_department_id)"`
唐旭辉 authored
26 27
}
唐旭辉 authored
28 29
func (t *Department) TableName() string {
	return "department"
唐旭辉 authored
30 31 32
}

func init() {
唐旭辉 authored
33
	orm.RegisterModel(new(Department))
唐旭辉 authored
34 35
}
唐旭辉 authored
36 37 38 39
func (t *Department) GetManagesIds() []int64 {
	var r []int64
	err := json.Unmarshal([]byte(t.Manages), &r)
	if err != nil {
唐旭辉 authored
40
		log.Warn(err.Error())
唐旭辉 authored
41 42 43 44
	}
	return r
}
唐旭辉 authored
45
func (t *Department) SetManages(v []int64) {
46 47 48
	if len(v) == 0 {
		v = []int64{}
	}
唐旭辉 authored
49
	bt, _ := json.Marshal(v)
唐旭辉 authored
50 51
	t.Manages = string(bt)
	return
唐旭辉 authored
52 53
}
54 55
func (t *Department) SetRelation(parent *Department) error {
	if parent == nil {
56
		t.Relation = fmt.Sprintf(",%d,", t.Id)
tangxvhui authored
57
	} else if parent.Id == 0 {
58
		t.Relation = fmt.Sprintf(",%d,", t.Id)
59
	} else {
60
		t.Relation = fmt.Sprintf("%s%d,", parent.Relation, t.Id)
61 62 63 64
	}
	return nil
}
65
//fix:变更主管获取方式
唐旭辉 authored
66 67
func (t *Department) GetManages() []protocol.DepartmentManager {
	ids := t.GetManagesIds()
68
	usercompany, err := GetUserCompanyReal(ids)
唐旭辉 authored
69 70 71 72
	if err != nil {
		log.Error("GetUserNameByIds err :%s", err)
		return nil
	}
唐旭辉 authored
73
	managesdata := []protocol.DepartmentManager{}
74 75 76 77 78 79
	for _, v := range usercompany {
		u, err := GetUserById(v.UserId)
		if err != nil {
			log.Error("GetUserById(%d) err:%s", v.UserId, err)
			continue
		}
唐旭辉 authored
80
		m := protocol.DepartmentManager{
81
			Id: v.Id, Name: u.NickName,
唐旭辉 authored
82 83 84 85 86 87
		}
		managesdata = append(managesdata, m)
	}
	return managesdata
}
tangxvhui authored
88
func (t *Department) GetMembers() []User {
yangfu authored
89 90 91 92 93 94 95 96 97 98
	ids, err := GetUserDepartmentIds(int(t.CompanyId), int(t.Id))
	if err != nil {
		log.Error(err.Error())
		return nil
	}
	users, err := getUserNameByIds(ids)
	if err != nil {
		log.Error("GetUserNameByIds err :%s", err)
		return nil
	}
tangxvhui authored
99
	return users
yangfu authored
100 101
}
102 103 104 105 106 107 108
func (t *Department) IsTopDepartment() bool {
	if t.IsTop == 1 {
		return true
	}
	return false
}
唐旭辉 authored
109
// AddDepartment insert a new Department into database and returns
唐旭辉 authored
110
// last inserted Id on success.
唐旭辉 authored
111 112 113 114 115 116 117
func AddDepartment(m *Department, om ...orm.Ormer) (id int64, err error) {
	var o orm.Ormer
	if len(om) > 0 {
		o = om[0]
	} else {
		o = orm.NewOrm()
	}
唐旭辉 authored
118 119
	m.CreateAt = time.Now()
	m.UpdateAt = time.Now()
唐旭辉 authored
120
	m.DeleteAt = time.Unix(0, 0)
唐旭辉 authored
121 122 123 124
	id, err = o.Insert(m)
	return
}
唐旭辉 authored
125
// GetDepartmentById retrieves Department by Id. Returns error if
唐旭辉 authored
126
// Id doesn't exist
唐旭辉 authored
127
func GetDepartmentById(id int64) (v *Department, err error) {
唐旭辉 authored
128
	o := orm.NewOrm()
唐旭辉 authored
129
	v = &Department{Id: id}
唐旭辉 authored
130 131 132 133 134 135
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}
唐旭辉 authored
136
// UpdateDepartment updates Department by Id and returns error if
唐旭辉 authored
137
// the record to be updated doesn't exist
唐旭辉 authored
138 139 140 141 142 143 144
func UpdateDepartmentById(m *Department, col []string, om ...orm.Ormer) (err error) {
	var o orm.Ormer
	if len(om) > 0 {
		o = om[0]
	} else {
		o = orm.NewOrm()
	}
唐旭辉 authored
145 146 147 148
	var num int64
	m.UpdateAt = time.Now()
	if num, err = o.Update(m, col...); err == nil {
		_ = num
唐旭辉 authored
149 150 151 152
	}
	return
}
唐旭辉 authored
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
func GetDepartmentSubsetByRelation(relation string, om ...orm.Ormer) ([]Department, error) {
	dataSql := `SELECT company_id,parent_id,name,create_at,relation,member,managers 
				FROM department 
				WHERE  relation LIKE ? AND delete_at = 0`
	var (
		o      orm.Ormer
		result []Department
		err    error
	)
	like := relation + "%"
	if len(om) == 0 {
		o = orm.NewOrm()
	} else {
		o = om[0]
	}
	_, err = o.Raw(dataSql, like).QueryRows(&result)
	if err != nil {
		return nil, err
	}
	return result, nil
}
唐旭辉 authored
174 175 176 177 178 179 180 181 182

func GetDepartmentByCompanyId(companyId int64) ([]Department, error) {
	var (
		result []Department
		err    error
	)
	o := orm.NewOrm()
	_, err = o.QueryTable(&Department{}).
		Filter("company_id", companyId).
tangxvhui authored
183
		Filter("delete_at", 0).
唐旭辉 authored
184 185 186
		All(&result)
	return result, err
}
yangfu authored
187 188

func GetDepartmentByIds(departmentIds []int64) ([]Department, error) {
tangxvhui authored
189 190 191
	if len(departmentIds) == 0 {
		return nil, nil
	}
yangfu authored
192 193 194 195 196 197 198 199 200 201
	var (
		result []Department
		err    error
	)
	o := orm.NewOrm()
	_, err = o.QueryTable(&Department{}).
		Filter("id__in", departmentIds).
		All(&result)
	return result, err
}
唐旭辉 authored
202 203 204 205 206 207 208 209 210 211 212 213 214

func ExistDepartmentName(parentId int64, dname string) bool {
	var (
		ok bool
	)
	o := orm.NewOrm()
	ok = o.QueryTable(&Department{}).
		Filter("name", dname).
		Filter("parent_id", parentId).
		Filter("delete_at", 0).
		Exist()
	return ok
}
tangxvhui authored
215 216 217 218 219

func GetDepartmentByBusinessId(businessId int64) (v *Department, err error) {
	v = &Department{}
	o := orm.NewOrm()
	err = o.QueryTable(&Department{}).
tangxvhui authored
220
		Filter("business_department_id", businessId).
tangxvhui authored
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
		Filter("delete_at", 0).
		One(v)
	return v, err

}

func GetTopDepartmentByCompany(companyid int64) (v *Department, err error) {
	v = &Department{}
	o := orm.NewOrm()
	err = o.QueryTable(&Department{}).
		Filter("company_id", companyid).
		Filter("is_top", 1).
		Filter("delete_at", 0).
		One(v)
	return v, err
}