package domain

import "time"

// 组织 organization
type Org struct {
	// 组织ID
	OrgId int64 `json:"orgId,omitempty"`
	// 企业id
	CompanyId int64 `json:"companyId,omitempty"`
	// 创建时间
	CreatedAt time.Time `json:"createdAt,omitempty"`
	// 更新时间
	UpdatedAt time.Time `json:"updatedAt,omitempty"`
	// 删除时间
	DeletedAt time.Time `json:"deletedAt,omitempty"`
	// 组织编码
	OrgCode string `json:"orgCode,omitempty"`
	// 组织名称
	OrgName string `json:"orgName,omitempty"`
	// 扩展数据
	Ext *Ext `json:"ext,omitempty"`
	// 是否是组织标识 1:是 2:不是
	IsOrg int `json:"isOrg,omitempty"`
	// 组织状态 1:启用 2:禁用 3.删除
	OrgStatus int `json:"orgStatus,omitempty"`
	// 父级ID
	ParentId int64 `json:"parentId,omitempty"`
	// 父级节点路径("0,11,12,")
	ParentPath string `json:"parentPath,omitempty"`
}

type OrgRepository interface {
	Save(org *Org) (*Org, error)
	Remove(org *Org) (*Org, error)
	FindOne(queryOptions map[string]interface{}) (*Org, error)
	Find(queryOptions map[string]interface{}) (int64, []*Org, error)
}

func (org *Org) Identify() interface{} {
	if org.OrgId == 0 {
		return nil
	}
	return org.OrgId
}

func (org *Org) Update(data map[string]interface{}) error {
	if createdAt, ok := data["createdAt"]; ok {
		org.CreatedAt = createdAt.(time.Time)
	}
	if updatedAt, ok := data["updatedAt"]; ok {
		org.UpdatedAt = updatedAt.(time.Time)
	}
	if deletedAt, ok := data["deletedAt"]; ok {
		org.DeletedAt = deletedAt.(time.Time)
	}
	if orgCode, ok := data["orgCode"]; ok {
		org.OrgCode = orgCode.(string)
	}
	if orgName, ok := data["orgName"]; ok {
		org.OrgName = orgName.(string)
	}
	if userName, ok := data["userName"]; ok {
		org.Ext.UserName = userName.(string)
	}
	if orgName, ok := data["orgName"]; ok {
		org.Ext.OrgName = orgName.(string)
	}
	if phone, ok := data["phone"]; ok {
		org.Ext.Phone = phone.(string)
	}
	if depName, ok := data["depName"]; ok {
		org.Ext.DepName = depName.(string)
	}
	if parentDepName, ok := data["parentDepName"]; ok {
		org.Ext.ParentDepName = parentDepName.(string)
	}
	if isOrg, ok := data["isOrg"]; ok {
		org.IsOrg = isOrg.(int)
	}
	if parentId, ok := data["parentId"]; ok {
		org.ParentId = parentId.(int64)
	}
	if parentPath, ok := data["parentPath"]; ok {
		org.ParentPath = parentPath.(string)
	}
	return nil
}

// 部门
type Department struct {
	// 部门ID
	DepartmentId int64 `json:"departmentId"`
	// 部门名称
	DepartmentName string `json:"departmentName"`
	// 部门编号
	DepartmentNumber string `json:"departmentNumber"`
}

// 通过组织获取当前部门信息
func (org *Org) ConvDep() *Department {
	return &Department{
		DepartmentId:     org.OrgId,
		DepartmentName:   org.OrgName,
		DepartmentNumber: org.OrgCode,
	}
}