package service

import (
	"fmt"
	"strconv"
	"strings"

	"be.ems/features/udm_user/model"
	"be.ems/features/udm_user/repo"
)

// 实例化服务层 ServiceUdmSubUser 结构体
var NewServiceUdmSubUser = &ServiceUdmSubUser{
	repoSunUser: *repo.NewRepoUdmSubUser,
}

// ServiceUdmSubUser UDM签约用户 服务层处理
type ServiceUdmSubUser struct {
	repoSunUser repo.RepoUdmSubUser
}

// Save UDM签约用户-获取全部保存数据库
func (r *ServiceUdmSubUser) Save(neID string) int64 {
	var num int64 = 0
	subArr := redisUdmSubUserList()
	// 有数据才清空
	if len(subArr) == 0 {
		return num
	}
	go r.repoSunUser.ClearAndInsert(neID, subArr)
	return int64(len(subArr))
}

// Page UDM签约用户-分页查询数据库
func (r *ServiceUdmSubUser) Page(query map[string]any) map[string]any {
	return r.repoSunUser.SelectPage(query)
}

// List UDM签约用户-查询数据库
func (r *ServiceUdmSubUser) List(subUser model.UdmSubUser) []model.UdmSubUser {
	return r.repoSunUser.SelectList(subUser)
}

// Insert UDM签约用户-新增单个
// imsi长度15，ki长度32，opc长度0或者32
func (r *ServiceUdmSubUser) Insert(neID string, subUser model.UdmSubUser) int64 {
	subUser.NeID = neID
	return r.repoSunUser.Insert(subUser)
}

// Insert UDM签约用户-批量添加
func (r *ServiceUdmSubUser) Inserts(neID string, subUser model.UdmSubUser, num string) int64 {
	var arr []model.UdmSubUser

	imsiVlen := len(subUser.Imsi)
	imsiV, err := strconv.Atoi(subUser.Imsi)
	if err != nil {
		return 0
	}
	msisdnVlen := len(subUser.Msisdn)
	msisdnV, err := strconv.Atoi(subUser.Msisdn)
	if err != nil {
		return 0
	}

	numV, err := strconv.Atoi(num)
	if err != nil {
		return 0
	}

	subUser.NeID = neID
	for i := 0; i < numV; i++ {
		msisdn := fmt.Sprint(msisdnV + i)
		if len(msisdn) < msisdnVlen {
			msisdn = fmt.Sprintf("%0*s", msisdnVlen, msisdn)
		}
		subUser.Msisdn = msisdn

		imsi := fmt.Sprint(imsiV + i)
		if len(imsi) < imsiVlen {
			imsi = fmt.Sprintf("%0*s", imsiVlen, imsi)
		}
		subUser.Imsi = imsi

		// IP会自动递增
		if len(subUser.StaticIp) > 5 {
			parts := strings.Split(subUser.StaticIp, ".")
			lastPart := parts[3]
			lastNum, _ := strconv.Atoi(lastPart)
			lastNum += i
			newLastPart := strconv.Itoa(lastNum)
			parts[3] = newLastPart
			newIP := strings.Join(parts, ".")
			subUser.StaticIp = newIP
		}

		subUser.EpsDat = fmt.Sprintf("%s,%s,%s,%s,%s,%s,2,%s,%s", subUser.EpsFlag, subUser.EpsOdb, subUser.HplmnOdb, subUser.Ard, subUser.Epstpl, subUser.ContextId, subUser.ApnContext, subUser.StaticIp)

		arr = append(arr, subUser)
	}

	return r.repoSunUser.Inserts(arr)
}

// InsertCSV UDM签约用户-批量添加
func (r *ServiceUdmSubUser) InsertCSV(neID string, data []map[string]string) int64 {
	var arr []model.UdmSubUser
	for _, v := range data {
		var subUser model.UdmSubUser
		subUser.NeID = neID
		if s, ok := v["imsi"]; ok {
			subUser.Imsi = s
		}
		if s, ok := v["msisdn"]; ok {
			subUser.Msisdn = s
		}
		if s, ok := v["ambr"]; ok {
			subUser.Ambr = s
		}
		if s, ok := v["nssai"]; ok {
			subUser.Nssai = s
		}
		if s, ok := v["arfb"]; ok {
			subUser.Arfb = s
		}
		if s, ok := v["sar"]; ok {
			subUser.Sar = s
		}
		if s, ok := v["rat"]; ok {
			subUser.Rat = s
		}
		if s, ok := v["cn"]; ok {
			subUser.Cn = s
		}
		if s, ok := v["smf_sel"]; ok {
			subUser.SmfSel = s
		}
		if s, ok := v["sm_dat"]; ok {
			subUser.SmData = s
		}
		if s, ok := v["eps_dat"]; ok {
			subUser.EpsDat = s
			arr := strings.Split(s, ",")
			if len(arr) == 8 {
				subUser.EpsFlag = arr[0]
				subUser.EpsOdb = arr[1]
				subUser.HplmnOdb = arr[2]
				subUser.Ard = arr[3]
				subUser.Epstpl = arr[4]
				subUser.ContextId = arr[5]
				subUser.ApnContext = arr[6]
				subUser.StaticIp = arr[7]
			}
		}

		if subUser.Imsi == "" {
			continue
		}
		arr = append(arr, subUser)
	}
	return r.repoSunUser.Inserts(arr)
}

// InsertTxt UDM签约用户-批量添加
func (r *ServiceUdmSubUser) InsertTxt(neID string, data [][]string) int64 {
	var arr []model.UdmSubUser
	for _, v := range data {
		if len(v) < 10 {
			continue
		}
		var subUser model.UdmSubUser
		subUser.NeID = neID
		subUser.Imsi = v[0]
		subUser.Msisdn = v[1]
		subUser.Ambr = v[2]
		subUser.Nssai = v[3]
		subUser.Arfb = v[4]
		subUser.Sar = v[5]
		subUser.Rat = v[6]
		subUser.Cn = v[7]
		subUser.SmfSel = v[8]
		subUser.SmData = v[9]
		if len(v) > 17 {
			subUser.EpsFlag = v[10]
			subUser.EpsOdb = v[11]
			subUser.HplmnOdb = v[12]
			subUser.Ard = v[13]
			subUser.Epstpl = v[14]
			subUser.ContextId = v[15]
			subUser.ApnContext = v[16]
			subUser.StaticIp = v[17]
			subUser.EpsDat = strings.Join(v[10:], ",")
		}

		if subUser.Imsi == "" {
			continue
		}
		arr = append(arr, subUser)
	}
	return r.repoSunUser.Inserts(arr)
}

// Insert UDM签约用户-批量添加4G用户
func (r *ServiceUdmSubUser) Insert4G(neID string, subUser model.UdmSubUser) int64 {
	return r.repoSunUser.Insert4G(neID, subUser)
}

// Insert UDM签约用户-修改更新
func (r *ServiceUdmSubUser) Update(neID string, subUser model.UdmSubUser) int64 {
	return r.repoSunUser.Update(neID, subUser)
}

// Update4GIP UDM签约用户-批量修改4G IP
func (r *ServiceUdmSubUser) Update4GIP(neID string, subUser model.UdmSubUser) int64 {
	return r.repoSunUser.Update4GIP(neID, subUser)
}

// Update4GIP UDM签约用户-批量修改sm-data
func (r *ServiceUdmSubUser) UpdateSmData(neID string, subUser model.UdmSubUser) int64 {
	return r.repoSunUser.UpdateSmData(neID, subUser)
}

// Insert UDM签约用户-删除单个
func (r *ServiceUdmSubUser) Delete(neID, imsi string) int64 {
	return r.repoSunUser.Delete(neID, imsi)
}

// Insert UDM签约用户-删除范围
func (r *ServiceUdmSubUser) Deletes(neID, imsi, num string) int64 {
	return r.repoSunUser.Deletes(neID, imsi, num)
}
