package actions

import (
	"fmt"
	"strconv"
	"zzman/model"

	u "zzman/util"

	"gorm.io/gorm"
)

func GenConfig(tx *gorm.DB) error {

	gateways, err := FindGateways(tx)
	if err != nil {
		return err
	}

	cities, err := FindCitiesWithEdgesCount(tx)
	if err != nil {
		return err
	}

	configs := make([]model.ConfigCreate, len(gateways)*30)
	for iGateway := range gateways {
		for j := range 30 {
			n := iGateway*30 + j
			iCity := (iGateway*30 + j) % len(cities)
			gateway := gateways[iGateway]
			city := cities[iCity]

			configs[n] = model.ConfigCreate{
				Table:      u.P(strconv.Itoa(j + 221)),
				Cityhash:   &city.Hash,
				CityLabel:  &city.Label,
				Network:    u.P(fmt.Sprintf("172.30.168.%d", j+222)),
				GatewayMac: &gateway.Macaddr,
				User:       u.P(fmt.Sprintf("jdzz%ddt%d", iGateway+1, j+221)),
				InnerIp:    u.P(fmt.Sprintf("%s%d", gateway.ProxyIp, j+222)),
				EdgeMac:    u.P(""),
				IsChange:   u.P(1),
				IsOnline:   u.P(0),
			}
		}
	}

	println(fmt.Sprintf("生成配置：%d 条", len(configs)))
	err = CreateConfigs(tx, configs)
	if err != nil {
		return err
	}

	return nil
}
