package service

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

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

type OrgMatchEvaluate struct {
	service.Service
}

func CreateSearch(key string, act string, value string) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if value != "" {
			switch act {
			case "=":
				return db.Where(key+" = ?", value)
			case "like":
				return db.Where(key+" like '%?%'", value)
			case ">":
				return db.Where(key+" > ?", value)
			case "<":
				return db.Where(key+" > ?", value)
			case "<>":
				return db.Where(key+" <> ?", value)
			default:
				return db
			}
		} else {
			return db
		}
	}
}

// GetPage 获取OrgMatchEvaluate列表
func (e *OrgMatchEvaluate) GetPage(c *dto.OrgMatchEvaluateGetPageReq, p *actions.DataPermission, list *[]dto.OrgMatchEvaluateGetPageReply, count *int64) error {
	var err error

	err = e.Orm.Table("org_match_team_player as omtp").
		Select("omtp.id, ol.league_name, od.division_name, os.season_name, om.rounds, omtp.player_name, om.match_start_time, om.match_end_time, ome.mv_status, ome.status, ome.remark").
		Joins("left join org_match_evaluate as ome on omtp.player_id = ome.player_id").
		Joins("left join org_match as om on omtp.match_id = om.id").
		Joins("left join org_league as ol on ol.id = om.league_id").
		Joins("left join org_division as od on od.id = om.division_id").
		Joins("left join org_season as os on os.id = om.season_id").
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission("omtp", p),
		).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("OrgMatchService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

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

	err := e.Orm.Model(&data).
		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 GetOrgMatchEvaluate error:%s \r\n", err)
		return err
	}
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err
	}
	return nil
}

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

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

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

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

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