package models

import (
	"annual-lottery/models/model"
	"annual-lottery/utils"
	"strings"
)

// UserFilters 用户过滤
func UserFilters(lvl string) ([]model.TaUser, string) {
	cf := "./conf/test.conf"
	usrs := make([]model.TaUser, 0)
	notIn := make([]string, 0)

	if !utils.FileExists(cf) {
		return usrs, ""
	}

	conf, err := utils.GetConfig(cf)
	if err != nil {
		return usrs, ""
	}

	pls := conf.String("conf")
	if pls == "" {
		return usrs, ""
	}

	for _, pl := range strings.Split(pls, ",") {
		for _, us := range strings.Split(conf.DefaultString(pl+"::us", ""), ",") {
			if us == "" {
				continue
			}

			usArr := strings.Split(us, "-")
			if pl == lvl {
				usrs = append(usrs, model.TaUser{
					UserName: usArr[1],
					UserOrg:  usArr[0],
				})
			} else {
				notIn = append(notIn, "user_name = '"+usArr[1]+"' and user_org = '"+usArr[0]+"'")
			}
		}
	}

	notExists := ""
	if len(notIn) > 0 {
		notExists = "select * from ta_user o where o.user_id = t.user_id and ((" + strings.Join(notIn, ") or (") + "))"
	}

	return usrs, notExists
}
