package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
)

type OrganizationService struct {
}

// CreateOrganization 创建Organization记录
// Author [piexlmax](https://github.com/piexlmax)
func (organizationService *OrganizationService) CreateOrganization(organization autocode.Organization) (err error) {
	err = global.GVA_DB.Create(&organization).Error
	return err
}

// DeleteOrganization 删除Organization记录
// Author [piexlmax](https://github.com/piexlmax)
func (organizationService *OrganizationService) DeleteOrganization(organization autocode.Organization) (err error) {
	err = global.GVA_DB.Model(organization).Delete("code = ?", organization.CODE).Error
	return err
}

// DeleteOrganizationByIds 批量删除Organization记录
// Author [piexlmax](https://github.com/piexlmax)
func (organizationService *OrganizationService) DeleteOrganizationByIds(codes []string) (err error) {
	err = global.GVA_DB.Delete(&[]autocode.Organization{}, "code in ?", codes).Error
	return err
}

// UpdateOrganization 更新Organization记录
// Author [piexlmax](https://github.com/piexlmax)
func (organizationService *OrganizationService) UpdateOrganization(organization autocode.Organization) (err error) {
	err = global.GVA_DB.Model(organization).Where("code = ?", organization.CODE).Updates(&organization).Error
	return err
}

// GetOrganization 根据id获取Organization记录
// Author [piexlmax](https://github.com/piexlmax)
func (organizationService *OrganizationService) GetOrganization(code string) (err error, organization autocode.Organization) {
	err = global.GVA_DB.Where("code = ?", code).First(&organization).Error
	return
}

// GetOrganizationInfoList 分页获取Organization记录
// Author [piexlmax](https://github.com/piexlmax)
func (organizationService *OrganizationService) GetOrganizationInfoList(info autoCodeReq.OrganizationSearch) (err error, list []autocode.Organization, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&autocode.Organization{})
	if info.PARENTCODE != "" {
		db.Where("PARENT_CODE=?", info.PARENTCODE)
	}
	var organizations []autocode.Organization
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&organizations).Error
	return err, organizations, total
}

func (organizationService *OrganizationService) GetOrganizationInfoLists() (err error, list []autocode.Organization) {
	// 创建db
	db := global.GVA_DB.Model(&autocode.Organization{})
	var organizations []autocode.Organization
	err = db.Find(&organizations).Error
	return err, organizations
}

func (organizationService *OrganizationService) GetBaseChildrenList(org *autocode.Organization, treeMap map[string][]autocode.Organization) (err error) {
	org.Children = treeMap[org.CODE]
	for i := 0; i < len(org.Children); i++ {
		err = organizationService.GetBaseChildrenList(&org.Children[i], treeMap)
	}
	return err
}
