package dto

import (
	"cold-delivery/common/global"
	"fmt"
	"gogs.baozhida.cn/zoie/OAuth-core/tools/search"
	"gorm.io/gorm"
	"strings"
)

type GeneralDelDto struct {
	Id  int   `uri:"id" json:"id" validate:"required"`
	Ids []int `json:"ids"`
}

func (g GeneralDelDto) GetIds() []int {
	ids := make([]int, 0)
	if g.Id != 0 {
		ids = append(ids, g.Id)
	}
	if len(g.Ids) > 0 {
		for _, id := range g.Ids {
			if id > 0 {
				ids = append(ids, id)
			}
		}
	} else {
		if g.Id > 0 {
			ids = append(ids, g.Id)
		}
	}
	if len(ids) <= 0 {
		//方式全部删除
		ids = append(ids, 0)
	}
	return ids
}

type GeneralGetDto struct {
	Id int `uri:"id" json:"id" validate:"required"`
}

func MakeCondition(q interface{}) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		condition := &search.GormCondition{
			GormPublic: search.GormPublic{},
			Join:       make([]*search.GormJoin, 0),
		}
		search.ResolveSearchQuery(global.Driver, q, condition)
		for _, join := range condition.Join {
			if join == nil {
				continue
			}
			db = db.Joins(join.JoinOn)
			for k, v := range join.Where {
				db = db.Where(k, v...)
			}
			for k, v := range join.Or {
				db = db.Or(k, v...)
			}
			for _, o := range join.Order {
				db = db.Order(o)
			}
		}
		for k, v := range condition.Where {
			db = db.Where(k, v...)
		}

		for k, v := range condition.Or {
			db = db.Or(k, v...)
		}
		var orContains string
		for k, v := range condition.OrContains {
			orContains += fmt.Sprintf(" OR %v '%v'", k, v[0])
		}
		if len(orContains) > 0 {
			db = db.Where(strings.TrimLeft(orContains, " OR"))
		}
		for _, o := range condition.Order {
			db = db.Order(o)
		}
		return db
	}
}

//func MakeCondition(q interface{}) func(db *gorm.DB) *gorm.DB {
//	return func(db *gorm.DB) *gorm.DB {
//		// 检查是否是WaybillGetPageReq类型
//		req, ok := q.(*dto2.WaybillGetPageReq)
//		if !ok {
//			// 如果不是WaybillGetPageReq类型，则继续正常的处理逻辑
//			return makeNormalCondition(q)(db)
//		}
//
//		// 特别处理Status字段
//		if req.Status == 1 {
//			// 添加一个额外的条件来匹配status=7
//			db = db.Or("status = ?", model.WaybillStatusoverrule)
//		}
//
//		// 继续使用search.ResolveSearchQuery来处理其他条件
//		condition := &search.GormCondition{
//			GormPublic: search.GormPublic{},
//			Join:       make([]*search.GormJoin, 0),
//		}
//		search.ResolveSearchQuery(global.Driver, q, condition)
//
//		for _, join := range condition.Join {
//			if join == nil {
//				continue
//			}
//			db = db.Joins(join.JoinOn)
//			for k, v := range join.Where {
//				db = db.Where(k, v...)
//			}
//			for k, v := range join.Or {
//				db = db.Or(k, v...)
//			}
//			for _, o := range join.Order {
//				db = db.Order(o)
//			}
//		}
//		for k, v := range condition.Where {
//			db = db.Where(k, v...)
//		}
//		for k, v := range condition.Or {
//			db = db.Or(k, v...)
//		}
//		var orContains string
//		for k, v := range condition.OrContains {
//			orContains += fmt.Sprintf(" OR %v '%v'", k, v[0])
//		}
//		if len(orContains) > 0 {
//			db = db.Where(strings.TrimLeft(orContains, " OR"))
//		}
//		for _, o := range condition.Order {
//			db = db.Order(o)
//		}
//		return db
//	}
//}
//
//// 辅助函数：用于处理非WaybillGetPageReq类型的正常情况
//func makeNormalCondition(q interface{}) func(db *gorm.DB) *gorm.DB {
//	return func(db *gorm.DB) *gorm.DB {
//		condition := &search.GormCondition{
//			GormPublic: search.GormPublic{},
//			Join:       make([]*search.GormJoin, 0),
//		}
//		search.ResolveSearchQuery(global.Driver, q, condition)
//
//		for _, join := range condition.Join {
//			if join == nil {
//				continue
//			}
//			db = db.Joins(join.JoinOn)
//			for k, v := range join.Where {
//				db = db.Where(k, v...)
//			}
//			for k, v := range join.Or {
//				db = db.Or(k, v...)
//			}
//			for _, o := range join.Order {
//				db = db.Order(o)
//			}
//		}
//		for k, v := range condition.Where {
//			db = db.Where(k, v...)
//		}
//		for k, v := range condition.Or {
//			db = db.Or(k, v...)
//		}
//		var orContains string
//		for k, v := range condition.OrContains {
//			orContains += fmt.Sprintf(" OR %v '%v'", k, v[0])
//		}
//		if len(orContains) > 0 {
//			db = db.Where(strings.TrimLeft(orContains, " OR"))
//		}
//		for _, o := range condition.Order {
//			db = db.Order(o)
//		}
//		return db
//	}
//}

func Paginate(pageSize, pageIndex int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		offset := (pageIndex - 1) * pageSize
		if offset < 0 {
			offset = 0
		}
		return db.Offset(offset).Limit(pageSize)
	}
}
