作者 唐旭辉

更新

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