package conv

import (
	"encoding/json"
	"gadmin/package/gmdata"
	"gadmin/utility"
	"github.com/sirupsen/logrus"
)

var Role = new(cRole)

type cRole struct {
	version string
	lst     []*gmdata.Role
}

func (j *cRole) Extract(version string) {
	logrus.Info("cRole Extract...")
	j.setVersion(version).convert().save()
}

func (j *cRole) setVersion(version string) *cRole {
	j.version = version
	return j
}

func (j *cRole) convert() *cRole {
	b, err := GetUrlBody(j.version, "Role.json")
	if err != nil {
		logrus.Fatalf("GetUrlBody err:%+v", err)
	}

	if err := json.Unmarshal(b, &j.lst); err != nil {
		logrus.Fatalf("err=%v", err)
	}

	return j
}

func (j *cRole) save() {
	filePath := utility.LocalJsonPath(j.version, "Role.json")
	SaveJsonPath(filePath, j.lst)
}
