|
|
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) (id int64, err error) {
|
|
|
o := orm.NewOrm()
|
|
|
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
|
|
|
} |
...
|
...
|
|