package service

import (
	"errors"

	"github.com/go-admin-team/go-admin-core/sdk/service"
	"gorm.io/gorm"

	"go-admin/app/operate/models"
	"go-admin/app/operate/service/dto"
	"go-admin/common/actions"
	cDto "go-admin/common/dto"
)

type OrgTeam struct {
	service.Service
}

// GetPage 获取OrgTeam列表
func (e *OrgTeam) GetPage(c *dto.OrgTeamGetPageReq, p *actions.DataPermission, list *[]dto.OrgTeamGetPageReply, count *int64) error {
	var err error
	var data models.OrgTeam

	err = e.Orm.Table("org_team as ot").
		Select("ot.id,oc.id as club_id,oc.club_name,ot.team_name,ot.created_at").
		Joins("left join org_club as oc on ot.club_id=oc.id").
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
			cDto.PassDel("ot"),
		).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("OrgTeamService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

// Get 获取OrgTeam对象
func (e *OrgTeam) Get(d *dto.OrgTeamGetReq, p *actions.DataPermission, model *dto.OrgTeamGetPageReply) error {
	var data models.OrgTeam

	err := e.Orm.Table("org_team as ot").
		Select("ot.id,oc.id as club_id,oc.club_name,ot.team_name,ot.created_at").
		Joins("left join org_club as oc on ot.club_id=oc.id").
		Scopes(
			actions.Permission(data.TableName(), p),
		).
		First(model, d.GetId()).Error
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		e.Log.Errorf("Service GetOrgTeam error:%s \r\n", err)
		return err
	}
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err
	}
	return nil
}

// Insert 创建OrgTeam对象
func (e *OrgTeam) Insert(c *dto.OrgTeamInsertReq) error {
	var err error
	var data models.OrgTeam
	c.Generate(&data)
	err = e.Orm.Create(&data).Error
	if err != nil {
		e.Log.Errorf("OrgTeamService Insert error:%s \r\n", err)
		return err
	}
	c.Id = data.Id
	return nil
}

// Update 修改OrgTeam对象
func (e *OrgTeam) Update(c *dto.OrgTeamUpdateReq, p *actions.DataPermission) error {
	var err error
	var data = models.OrgTeam{}
	e.Orm.Scopes(
		actions.Permission(data.TableName(), p),
	).First(&data, c.GetId())
	c.Generate(&data)

	db := e.Orm.Updates(&data)
	if db.Error != nil {
		e.Log.Errorf("OrgTeamService Save error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")
	}
	c.Id = data.Id
	return nil
}

// Remove 删除OrgTeam
func (e *OrgTeam) Remove(d *dto.OrgTeamDeleteReq, p *actions.DataPermission) error {
	var data models.OrgTeam

	db := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).Delete(&data, d.GetId())
	if err := db.Error; err != nil {
		e.Log.Errorf("Service RemoveOrgTeam error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权删除该数据")
	}
	return nil
}

// 根据工作人员id获取userId
func (e *OrgTeam) GetTeamName(teamId int) (error, string) {
	data := new(models.OrgTeam)

	err := e.Orm.Table("org_team").
		Select("team_name").
		Where("id=?", teamId).
		First(&data).Error
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err, ""
	}
	return nil, data.TeamName
}

func (e *OrgTeam) GetParentDeptId(clubId string) (error, int) {
	data := dto.Dept{}

	err := e.Orm.Table("org_club_user as ocu").
		Select("ocu.id, su.dept_id").
		Joins("left join sys_user as su on su.user_id=ocu.user_id").
		Where("id=?", clubId).
		First(&data).Error
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err, 0
	}
	return nil, data.DeptId
}

// Insert 创建OrgTeam对象
func (e *OrgTeam) InsertTeamDept(teamId int, deptId int) error {
	var err error
	var data models.OrgTeamDept
	data.TeamId = teamId
	data.DeptId = deptId
	err = e.Orm.Table("org_team_dept").Create(&data).Error
	if err != nil {
		e.Log.Errorf("OrgTeamService Insert error:%s \r\n", err)
		return err
	}
	return nil
}
