package road

import (
	"context"
	"time"

	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/core/logs"
)

type RoadPoolInfo struct {
	Id           int
	Status       string
	RoadPoolName string
	RoadPoolCode string
	RoadUidPool  string
	UpdateTime   time.Time
	CreateTime   time.Time
}

const ROAD_POOL_INFO = "road_pool_info"

func InsertRoadPool(ctx context.Context, roadPool RoadPoolInfo) bool {
	o := orm.NewOrm()
	_, err := o.InsertWithCtx(ctx, &roadPool)
	if err != nil {
		logs.Error("insert road pool fail: ", err)
		return false
	}
	return true
}

func GetRoadPoolLenByMap(ctx context.Context, params map[string]string) int {
	o := orm.NewOrm()
	qs := o.QueryTable(ROAD_POOL_INFO)
	for k, v := range params {
		if len(v) > 0 {
			qs = qs.Filter(k, v)
		}
	}
	cnt, err := qs.Limit(-1).CountWithCtx(ctx)
	if err != nil {
		logs.Error("get road pool len by map fail: ", err)
	}
	return int(cnt)
}

func GetRoadPoolByMap(ctx context.Context, params map[string]string, displayCount, offset int) []RoadPoolInfo {
	o := orm.NewOrm()
	var roadPoolList []RoadPoolInfo
	qs := o.QueryTable(ROAD_POOL_INFO)
	for k, v := range params {
		if len(v) > 0 {
			qs.Filter(k, v)
		}
	}
	_, err := qs.Limit(displayCount, offset).OrderBy("-update_time").AllWithCtx(ctx, &roadPoolList)
	if err != nil {
		logs.Error("get road pool by map fail: ", err)
	}
	return roadPoolList
}

func GetRoadPoolByRoadPoolCode(ctx context.Context, roadPoolCode string) RoadPoolInfo {
	o := orm.NewOrm()
	var roadPoolInfo RoadPoolInfo
	_, err := o.QueryTable(ROAD_POOL_INFO).Filter("road_pool_code", roadPoolCode).Limit(1).AllWithCtx(ctx, &roadPoolInfo)

	if err != nil {
		logs.Error("get road pool info by road pool code fail: ", err)
	}

	return roadPoolInfo
}

func GetAllRollPool(ctx context.Context, params map[string]string) []RoadPoolInfo {
	o := orm.NewOrm()
	var roadPoolList []RoadPoolInfo
	qs := o.QueryTable(ROAD_POOL_INFO)
	for k, v := range params {
		if len(v) > 0 {
			qs = qs.Filter(k, v)
		}
	}
	_, err := qs.Limit(-1).AllWithCtx(ctx, &roadPoolList)
	if err != nil {
		logs.Error("get all roll pool fail: ", err)
	}
	return roadPoolList
}

func GetRoadPoolByName(ctx context.Context, roadPoolName string) RoadPoolInfo {
	o := orm.NewOrm()
	var roadPoolInfo RoadPoolInfo
	_, err := o.QueryTable(ROAD_POOL_INFO).Filter("road_pool_name", roadPoolName).Limit(1).AllWithCtx(ctx, &roadPoolInfo)
	if err != nil {
		logs.Error("get road pool by name fail: ", err)
	}
	return roadPoolInfo
}

func DeleteRoadPoolByCode(ctx context.Context, roadPoolCode string) bool {
	o := orm.NewOrm()
	_, err := o.QueryTable(ROAD_POOL_INFO).Filter("road_pool_code", roadPoolCode).DeleteWithCtx(ctx)
	if err != nil {
		logs.Error("delete road pool by code fail: ", err)
		return false
	}
	return true
}

func UpdateRoadPool(ctx context.Context, roadPool RoadPoolInfo) bool {
	o := orm.NewOrm()
	_, err := o.UpdateWithCtx(ctx, &roadPool)

	if err != nil {
		logs.Error("update road pool fail: ", err)
		return false
	}
	return true
}
