department_charge.go
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package models
import (
"fmt"
"oppmg/utils"
"time"
"github.com/astaxie/beego/orm"
)
type DepartmentCharge struct {
Id int64 `orm:"column(id);auto"`
CompanyId int64 `orm:"column(company_id)" description:"公司id"`
DepartmentId int64 `orm:"column(department_id)" description:"部门id"`
UserCompanyId int64 `orm:"column(user_company_id)" description:"用户id"`
CreateTime time.Time `orm:"column(create_time);type(timestamp)"`
Enabled int8 `orm:"column(enabled)"`
}
func (t *DepartmentCharge) TableName() string {
return "department_charge"
}
func init() {
orm.RegisterModel(new(DepartmentCharge))
}
const (
DEPARTMENT_CHARGE_ENABLE_YES int8 = 1
DEPARTMENT_CHARGE_ENABLE_NO int8 = 2
)
// AddDepartmentCharge insert a new DepartmentCharge into database and returns
// last inserted Id on success.
func AddDepartmentCharge(m *DepartmentCharge, o orm.Ormer) (id int64, err error) {
id, err = o.Insert(m)
return
}
// UpdateDepartmentCharge updates DepartmentCharge by Id and returns error if
// the record to be updated doesn't exist
func UpdateDepartmentChargeById(m *DepartmentCharge) (err error) {
o := orm.NewOrm()
v := DepartmentCharge{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
}
func GetDepartmentCharge(departmentId int64) ([]DepartmentCharge, error) {
var (
data []DepartmentCharge
err error
)
o := orm.NewOrm()
_, err = o.QueryTable(&DepartmentCharge{}).
Filter("department_id", departmentId).
Filter("enabled", 1).
All(&data)
if err == orm.ErrNoRows {
return data, nil
}
return data, err
}
//ChangeDepartmentCharge 变更部门的主管
func ChangeDepartmentCharge(companyId int64, departmentId int64, userCompanyid []int64, om orm.Ormer) error {
var (
err error
charges []DepartmentCharge
oldChargeIds []int64
)
charges, err = GetDepartmentCharge(departmentId)
if err != nil {
return fmt.Errorf("获取部门主管数据失败:%s", err)
}
for _, v := range charges {
oldChargeIds = append(oldChargeIds, v.Id)
}
var (
delIds []int64
addIds []int64
addCharges []DepartmentCharge
)
delIds = utils.ArrayInt64Diff(oldChargeIds, userCompanyid)
addIds = utils.ArrayInt64Diff(userCompanyid, oldChargeIds)
nowTime := time.Now()
for _, v := range addIds {
m := DepartmentCharge{
CompanyId: companyId,
UserCompanyId: v,
DepartmentId: departmentId,
Enabled: DEPARTMENT_CHARGE_ENABLE_YES,
CreateTime: nowTime,
}
addCharges = append(addCharges, m)
}
if len(delIds) > 0 {
_, err = om.QueryTable(&DepartmentCharge{}).
Filter("department_id", departmentId).
Filter("user_company_id__in", delIds).
Update(orm.Params{
"enable": DEPARTMENT_CHARGE_ENABLE_NO,
})
if err != nil {
return fmt.Errorf("更新department_charge数据失败,err:%s", err)
}
}
if len(addCharges) > 0 {
_, err = om.InsertMulti(10, addCharges)
if err != nil {
return fmt.Errorf("添加department_charge数据失败,err:%s", err)
}
}
return nil
}