department.go 4.3 KB
package protocol

import (
	"time"
)

const (
	DepartmentUser = iota //用户所有部门
	DepartmentAll         //公司所有部门
	DepartmentRoot        //公司一级部门
)

const (
	StatisticApproved  = 0 //统计已审核
	StatisticApproving = 0 //统计审核中
)

/*Departments */
type DepartmentsRequest struct {
	//Type int `json:"type" valid:"Required"` //1:公司所有部门  2:用户所在部门
	Type int `json:"type"` //0:用户所在部门 1:公司所有部门
}
type DepartmentsResponse struct {
	Departments []*Department `json:"departments,omitempty"`
}

//获取一级部门列表
func (this DepartmentsResponse) GetCompanyDepartment() *Department {
	if len(this.Departments) == 0 {
		return &Department{}
	}
	if this.Departments[0].PId != 0 {
		return &Department{}
	}
	return this.Departments[0]
}

//获取一级部门列表
func (this DepartmentsResponse) GetRootDepartments() []*Department {
	if len(this.Departments) == 0 {
		return []*Department{}
	}
	if this.Departments[0].PId != 0 {
		return []*Department{}
	}
	return this.Departments[0].Departments
}

//获得部门底下子部门的编号列表(包含本身)
func (this DepartmentsResponse) GetChildDepartmentIds(d *Department, isContainSelf bool) []int {
	idList := make([]int, 0)
	if isContainSelf {
		idList = append(idList, d.DepartmentId)
	}
	idList = append(idList, walkDeparment(d)...)
	return idList
}

//遍历部门
func walkDeparment(d *Department) []int {
	idList := make([]int, 0)
	for i := range d.Departments {
		d := d.Departments[i]
		idList = append(idList, d.DepartmentId)
		if len(d.Departments) > 0 {
			idList = append(idList, walkDeparment(d)...)
		}
	}
	return idList
}

type Department struct {
	DepartmentId  int           `orm:"column(department_id)" json:"id"`
	Name          string        `orm:"column(name)" json:"name"`
	PId           int           `orm:"column(parent_id)" json:"-"`
	ManagerString string        `orm:"column(managers)" json:"-"`
	Relation      string        `orm:"column(relation)" json:"-"`
	CreateTime    time.Time     `orm:"column(create_time)" json:"-"`
	Managers      []int         `json:"-"`
	Departments   []*Department `json:"departments,omitempty"`
}

/*DepartmentStatistics 部门统计*/
type DepartmentStatisticsRequest struct {
	//DId int `json:"did"`//部门编号 //查询所有部门 查询特定部门
	Type int `json:"type"` //0:已审核 0:待审核
}
type DepartmentStatisticsResponse struct {
	Total int                     `json:"approvedTotal"`
	List  []*DepartmentStatistics `json:"departmentStatistics"`
}

func (this *DepartmentStatisticsResponse) Len() int { return len(this.List) }
func (this *DepartmentStatisticsResponse) Less(i, j int) bool {
	//已审核  按照总数从大到小排序;若相同,按照已通过数量从大到小排序;若还相同,按照成果数量从大到小排序;*/
	if this.List[i].ACTotal < this.List[j].ACTotal {
		return true
	}
	if this.List[i].ACTotal == this.List[j].ACTotal && this.List[i].ChanceApprovedTotal < this.List[j].ChanceApprovedTotal {
		return true
	}
	if this.List[i].ACTotal == this.List[j].ACTotal && this.List[i].ChanceApprovedTotal == this.List[j].ChanceApprovedTotal && this.List[i].AchievementTotal < this.List[j].AchievementTotal {
		return true
	}
	if this.List[i].ACTotal == this.List[j].ACTotal && this.List[i].ChanceApprovedTotal == this.List[j].ChanceApprovedTotal && this.List[i].AchievementTotal == this.List[j].AchievementTotal {
		if this.List[i].Dep.Time > this.List[j].Dep.Time {
			return true
		}
	}
	return false
}
func (this *DepartmentStatisticsResponse) Swap(i, j int) {
	this.List[i], this.List[j] = this.List[j], this.List[i]
}

//部门统计项
type DepartmentStatistics struct {
	Dep                  Dep `json:"dep"`                  //部门
	ChanceApprovedTotal  int `json:"chanceApprovedTotal"`  //已审核的机会
	ChanceApprovingTotal int `json:"chanceApprovingTotal"` //待审核的机会
	AchievementTotal     int `json:"achievementTotal"`     //已创建的成果 (显示)
	ChanceReservedTotal  int `json:"chanceReservedTotal"`  //储备的机会

	ACTotal int `json:"-"` //机会成果总数 (显示)
}

/*DepartmentStatistic 单部门统计*/
type DepartmentStatisticRequest struct {
	DepartmentId int64 `json:"departmentId"`
}
type DepartmentStatisticResponse struct {
	DepartmentStatistic DepartmentStatistics `json:"departmentStatistic"`
}