package backend

import (
	"context"
	"errors"
	"kpt-pasture/model"

	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
	"gitee.com/xuyiping_admin/pkg/xerr"
	"gorm.io/gorm"
)

// GetCurrentUserName 获取当前用户名
func (s *StoreEntry) GetCurrentUserName(ctx context.Context) (string, error) {
	userNameInter := ctx.Value(CurrentUserName)
	if userNameInter == nil {
		return "", xerr.Customf("cannot userName")
	}

	if userName, ok := userNameInter.(string); ok {
		return userName, nil
	} else {
		return "", xerr.Customf("waring userName")
	}
}

func (s *StoreEntry) GetCurrentSystemUser(ctx context.Context) (*model.SystemUser, error) {
	// 解析token
	userName, err := s.GetCurrentUserName(ctx)
	if err != nil {
		return nil, xerr.WithStack(err)
	}
	// 根据用户token获取用户数据
	systemUser := &model.SystemUser{Name: userName}
	if err = s.DB.Where("name = ?", userName).
		Where("is_show = ? and is_delete = ?", pasturePb.IsShow_Ok, pasturePb.IsShow_Ok).
		First(systemUser).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, xerr.Custom("当前登录用户数据不存在")
		}
		return nil, xerr.WithStack(err)
	}
	return systemUser, nil
}

func (s *StoreEntry) GetSystemUserInfo(ctx context.Context, userId int64) (*model.SystemUser, error) {
	systemUser := &model.SystemUser{
		Id: userId,
	}
	if err := s.DB.First(systemUser).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, xerr.Customf("该系统用户数据不存在: %d", userId)
		}
		return nil, xerr.WithStack(err)
	}
	return systemUser, nil
}

func (s *StoreEntry) SystemDeptList(ctx context.Context) ([]*model.SystemDept, error) {
	deptList := make([]*model.SystemDept, 0)
	if err := s.DB.Where("is_delete = ?", pasturePb.IsShow_Ok).Find(&deptList).Error; err != nil {
		return nil, xerr.WithStack(err)
	}
	return deptList, nil
}

func (s *StoreEntry) GetPenInfo(ctx context.Context, penId int64) (*model.Pen, error) {
	penData := &model.Pen{
		Id: penId,
	}
	if err := s.DB.First(penData).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, xerr.Customf("该栏舍数据不存在: %d", penId)
		}
		return nil, xerr.WithStack(err)
	}
	return penData, nil
}

func (s *StoreEntry) GetPenList(ctx context.Context) ([]*model.Pen, error) {
	penList := make([]*model.Pen, 0)
	if err := s.DB.Where("is_delete = ?", pasturePb.IsShow_Ok).Find(&penList).Error; err != nil {
		return nil, xerr.WithStack(err)
	}
	return penList, nil
}

func (s *StoreEntry) GetCowList(ctx context.Context) ([]*model.Cow, error) {
	cowList := make([]*model.Cow, 0)
	if err := s.DB.Where("is_remove = ?", pasturePb.IsShow_Ok).Find(&cowList).Error; err != nil {
		return nil, xerr.WithStack(err)
	}
	return cowList, nil
}

func (s *StoreEntry) GetCowInfo(ctx context.Context, cowId int64) (*model.Cow, error) {
	cowData := &model.Cow{Id: cowId}
	if err := s.DB.First(cowData).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, xerr.Customf("该牛只数据不存在: %d", cowId)
		}
		return nil, xerr.WithStack(err)
	}
	return cowData, nil
}

func (s *StoreEntry) GetTransferReasonInfo(ctx context.Context, reasonId int64) (*model.ConfigTransferPenReason, error) {
	configTransferPenReasonData := &model.ConfigTransferPenReason{
		Id: reasonId,
	}
	if err := s.DB.First(configTransferPenReasonData).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, xerr.Customf("该转群原因数据不存在: %d", reasonId)
		}
		return nil, xerr.WithStack(err)
	}
	return configTransferPenReasonData, nil
}

func (s *StoreEntry) GetCowWeightByLastSecond(ctx context.Context, cowId, lastWeightAt int64) (*model.EventWeight, error) {
	cowWeightData := &model.EventWeight{}
	if err := s.DB.Where("cow_id = ?", cowId).Where("weight_at < ?", lastWeightAt).First(cowWeightData).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, xerr.Customf("该牛只体重数据不存在: %d", cowId)
		}
		return nil, xerr.WithStack(err)
	}
	return cowWeightData, nil
}

func (s *StoreEntry) DiseaseTypeList(ctx context.Context) ([]*model.ConfigDiseaseType, error) {
	diseaseTypeList := make([]*model.ConfigDiseaseType, 0)
	if err := s.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&diseaseTypeList).Error; err != nil {
		return nil, xerr.WithStack(err)
	}
	return diseaseTypeList, nil
}

func (s *StoreEntry) DrugsById(ctx context.Context, id int64) (*model.Drugs, error) {
	drugs := &model.Drugs{Id: id}
	if err := s.DB.First(drugs).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, xerr.Customf("该药品数据不存在: %d", id)
		}
		return nil, xerr.WithStack(err)
	}
	return drugs, nil
}

func (s *StoreEntry) DiseaseListByIds(ctx context.Context, ids []int32) ([]*model.Disease, error) {
	diseaseList := make([]*model.Disease, 0)
	if err := s.DB.Where("id IN ?", ids).Find(&diseaseList).Error; err != nil {
		return nil, xerr.WithStack(err)
	}
	return diseaseList, nil
}

func (s *StoreEntry) ImmunizationById(ctx context.Context, id int64) (*model.ImmunizationPlan, error) {
	immunizationPlan := &model.ImmunizationPlan{Id: id}
	if err := s.DB.First(immunizationPlan).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, xerr.Customf("该免疫计划不存在: %d", id)
		}
		return nil, xerr.WithStack(err)
	}
	return immunizationPlan, nil
}

func (s *StoreEntry) GetWorkOrderSubByWorkOrderId(ctx context.Context, workOrderId int64) ([]*model.WorkOrderSub, error) {
	workOrderSubList := make([]*model.WorkOrderSub, 0)
	if err := s.DB.Where("work_order_id = ?", workOrderId).Find(&workOrderSubList).Error; err != nil {
		return nil, xerr.WithStack(err)
	}
	return workOrderSubList, nil
}