package service

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

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

type OrgUser struct {
	service.Service
}

// GetPage 获取OrgUser列表
func (e *OrgUser) GetPage(c *dto.OrgUserGetPageReq, p *actions.DataPermission, list *[]dto.OrgUserGetPageReply, count *int64) error {
	var err error
	var data models.SysUser
	field := "su.user_id, su.username, su.nick_name, su.nick_name_en, su.role_id, sr.role_name, su.status"

	err = e.Orm.Table("sys_user as su").
		Select(field).
		Joins("left join sys_role as sr on su.role_id = sr.role_id").
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
			cDto.PassDel("su"),
		).
		Where("su.user_type=?", 2).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("OrgTeamUserService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

// Get 获取OrgTeamUser对象
func (e *OrgUser) Get(d *dto.OrgUserGetReq, p *actions.DataPermission, model *dto.OrgUserGetReply) error {
	field := "su.user_id, su.username, su.nick_name, su.nick_name_en, su.role_id, su.status"

	err := e.Orm.Table("sys_user as su").
		Select(field).
		First(model, d.GetId()).Error
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		e.Log.Errorf("Service GetOrgTeamUser error:%s \r\n", err)
		return err
	}
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err
	}
	return nil
}

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

	c.UserId = data.UserId
	return nil
}

// Update 修改OrgTeamUser对象
func (e *OrgUser) Update(c *dto.OrgUserUpdateReq, p *actions.DataPermission) error {
	var err error
	var data = models.SysUser{}
	e.Orm.Scopes(
		actions.Permission(data.TableName(), p),
	).First(&data, c.GetId())
	userId := data.UserId
	c.Generate(&data)
	db := e.Orm.Updates(&data)
	if db.Error != nil {
		e.Log.Errorf("OrgTeamUserService Save error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")
	}

	c.UserId = userId
	return nil
}

// Remove 删除OrgTeamUser
func (e *OrgUser) Remove(d *dto.OrgUserDeleteReq, p *actions.DataPermission) error {
	var data models.SysUser

	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 RemoveOrgTeamUser error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权删除该数据")
	}
	return nil
}

func (e *OrgUser) IsBing(userId string, username string) (error, int64) {
	var err error
	var data models.SysUser
	var i int64
	err = e.Orm.Model(&data).Where("username = ?", username).
		Scopes(cDto.SetWhereNE("", "user_id", userId)).
		Count(&i).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return err, 1
	}
	return nil, i
}
