package service

import (
	"strconv"

	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"

	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/orgs/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/orgs/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/orgs/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
)

// 组织管理
type OrgsService struct {
}

// 创建组织管理
func (orgsService *OrgsService) OrgAdd(orgAddCommand *command.OrgAddCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		orgAddCommand.Operator)
	parentId, _ := strconv.Atoi(orgAddCommand.ParentId)
	result, err := creationUserGateway.OrgCreate(allied_creation_user.ReqOrgCreate{
		CompanyId: int(orgAddCommand.Operator.CompanyId),
		IsOrg:     orgAddCommand.IsOrg,
		OrgCode:   orgAddCommand.OrgCode,
		OrgName:   orgAddCommand.OrgName,
		ParentId:  parentId,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	data := struct {
		OrgId int `json:"orgId"`
		command.OrgAddCommand
	}{
		OrgId:         result.OrgId,
		OrgAddCommand: *orgAddCommand,
	}
	return data, nil
}

// 禁用、启用组织管理
func (orgsService *OrgsService) OrgEnable(orgEnableCommand *command.OrgEnableCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		orgEnableCommand.Operator)
	orgId, _ := strconv.Atoi(orgEnableCommand.OrgId)
	_, err := creationUserGateway.OrgEnable(allied_creation_user.ReqOrgEnable{
		OrgId:     orgId,
		OrgStatus: orgEnableCommand.Status,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return orgEnableCommand, nil
}

// 返回组织管理
func (orgsService *OrgsService) OrgGet(orgGetQuery *query.OrgGetQuery) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		orgGetQuery.Operator)
	orgId, _ := strconv.Atoi(orgGetQuery.OrgId)
	result, err := creationUserGateway.OrgGet(allied_creation_user.ReqOrgGet{
		OrgId: orgId,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	data := dto.OrgItem{
		OrgId:         strconv.Itoa(result.OrgID),
		OrgName:       result.OrgName,
		ParentId:      strconv.Itoa(result.ParentID),
		IsOrg:         result.IsOrg,
		OrgCode:       result.OrgCode,
		ParentDepName: result.Ext.ParentDepName,
		OrgStatus:     result.OrgStatus,
	}
	return data, nil
}

// 返回组织管理列表
func (orgsService *OrgsService) OrgList(orgListQuery *query.OrgListQuery) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		orgListQuery.Operator)
	parentOrgId, _ := strconv.Atoi(orgListQuery.ParentId)
	result, err := creationUserGateway.OrgSearch(allied_creation_user.ReqOrgSearch{
		CompanyId: int(orgListQuery.Operator.CompanyId),
		DepName:   orgListQuery.DepName,
		IsOrg:     0,
		Limit:     1000,
		Offset:    0,
		OrgCode:   orgListQuery.OrgCode,
		ParentId:  parentOrgId,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	var (
		dataList = []dto.OrgItem{}
		item     dto.OrgItem
	)
	for _, v := range result.Orgs {
		item = dto.OrgItem{
			OrgId:         strconv.Itoa(v.OrgID),
			OrgName:       v.OrgName,
			ParentId:      strconv.Itoa(v.ParentID),
			IsOrg:         v.IsOrg,
			OrgCode:       v.OrgCode,
			ParentDepName: v.Ext.ParentDepName,
			OrgStatus:     v.OrgStatus,
		}
		dataList = append(dataList, item)
	}
	return dataList, nil
}

// 更新组织管理
func (orgsService *OrgsService) OrgUpdate(orgUpdateCommand *command.OrgUpdateCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		orgUpdateCommand.Operator)
	parentId, _ := strconv.Atoi(orgUpdateCommand.ParentId)
	orgId, _ := strconv.Atoi(orgUpdateCommand.OrgId)
	_, err := creationUserGateway.OrgUpdate(allied_creation_user.ReqOrgUpdate{
		OrgId:    int64(orgId),
		IsOrg:    orgUpdateCommand.IsOrg,
		OrgCode:  orgUpdateCommand.OrgCode,
		OrgName:  orgUpdateCommand.OrgName,
		ParentId: int64(parentId),
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return orgUpdateCommand, nil
}

// 返回组织管理
func (orgsService *OrgsService) OrgGetSubDepartment(orgGetQuery *query.OrgGetSubDepartmentQuery) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		orgGetQuery.Operator)
	orgId, _ := strconv.Atoi(orgGetQuery.OrgId)
	result, err := creationUserGateway.OrgGetSubDepartment(allied_creation_user.ReqOrgGetSubDepartment{
		OrgId: int64(orgId),
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return map[string]interface{}{
		"list": result.Orgs,
	}, nil
}

//DepartmentsUsers 部门用户列表
func (orgsService OrgsService) DepartmentsUsers(departmentsUsersQuery *query.DepartmentsUsersQuery) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		departmentsUsersQuery.Operator)
	orgs, err := creationUserGateway.OrgGetSubDepartment(allied_creation_user.ReqOrgGetSubDepartment{
		OrgId: departmentsUsersQuery.Operator.OrgId,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}

	users, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
		CompanyId:      departmentsUsersQuery.Operator.CompanyId,
		OrganizationId: departmentsUsersQuery.Operator.OrgId,
		Limit:          1000,
		InEnableStatus: []int{domain.UserStatusEnable},
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	departmentUsersDto := &dto.DepartmentUsersDto{}
	if err := departmentUsersDto.LoadDto(departmentsUsersQuery.Type, orgs, users); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return departmentUsersDto, nil
}

func NewOrgsService(options map[string]interface{}) *OrgsService {
	newOrgsService := &OrgsService{}
	return newOrgsService
}