package service

import (
	"fmt"
	"os"

	"be.ems/src/framework/utils/file"
	"be.ems/src/modules/ne/model"
	"be.ems/src/modules/ne/repository"
)

// 实例化服务层 NeSoftware 结构体
var NewNeSoftware = &NeSoftware{
	neSoftwareRepository: repository.NewNeSoftware,
	neVersionService:     NewNeVersion,
}

// NeSoftware 网元软件包信息 服务层处理
type NeSoftware struct {
	neSoftwareRepository *repository.NeSoftware // 网元软件包信息
	neVersionService     *NeVersion             // 网元版本信息服务
}

// FindByPage 分页查询列表数据
func (r NeSoftware) FindByPage(query map[string]string) ([]model.NeSoftware, int64) {
	return r.neSoftwareRepository.SelectByPage(query)
}

// Find 查询列表
func (r NeSoftware) Find(neSoftware model.NeSoftware) []model.NeSoftware {
	return r.neSoftwareRepository.Select(neSoftware)
}

// FindById 通过ID查询
func (r NeSoftware) FindById(id int64) model.NeSoftware {
	if id <= 0 {
		return model.NeSoftware{}
	}
	neHosts := r.neSoftwareRepository.SelectByIds([]int64{id})
	if len(neHosts) > 0 {
		return neHosts[0]
	}
	return model.NeSoftware{}
}

// Insert 新增信息
func (r NeSoftware) Insert(neSoftware model.NeSoftware) int64 {
	inserId := r.neSoftwareRepository.Insert(neSoftware)
	if inserId > 0 {
		// 更新同类型的新包版本
		neVersions := r.neVersionService.Find(model.NeVersion{NeType: neSoftware.NeType}, false)
		if len(neVersions) > 0 {
			for _, neVersion := range neVersions {
				neVersion.NewName = neSoftware.Name
				neVersion.NewVersion = neSoftware.Version
				neVersion.NewPath = neSoftware.Path
				neVersion.Status = "3"
				neVersion.UpdateBy = neSoftware.CreateBy
				r.neVersionService.Update(neVersion)
			}
		}
	}
	return inserId
}

// Update 修改信息
func (r NeSoftware) Update(neSoftware model.NeSoftware) int64 {
	rows := r.neSoftwareRepository.Update(neSoftware)
	if rows > 0 {
		// 更新同类型的新包版本
		neVersions := r.neVersionService.Find(model.NeVersion{
			NeType: neSoftware.NeType,
			Status: "3",
		}, false)
		if len(neVersions) > 0 {
			for _, neVersion := range neVersions {
				neVersion.NewName = neSoftware.Name
				neVersion.NewVersion = neSoftware.Version
				neVersion.NewPath = neSoftware.Path
				neVersion.Status = "3"
				neVersion.UpdateBy = neSoftware.UpdateBy
				r.neVersionService.Update(neVersion)
			}
		}
	}
	return rows
}

// DeleteByIds 批量删除信息
func (r NeSoftware) DeleteByIds(ids []int64, delFile bool) (int64, error) {
	// 检查是否存在
	rows := r.neSoftwareRepository.SelectByIds(ids)
	if len(rows) <= 0 {
		return 0, fmt.Errorf("neSoftware.noData")
	}

	if len(rows) == len(ids) {
		if delFile {
			// 遍历软件包列表进行文件删除
			for _, row := range rows {
				// 检查文件是否存在
				filePath := file.ParseUploadFileAbsPath(row.Path)
				if _, err := os.Stat(filePath); err != nil {
					continue
				}
				os.Remove(filePath)
			}
		}

		rows := r.neSoftwareRepository.DeleteByIds(ids)
		return rows, nil
	}

	// 删除信息失败！
	return 0, fmt.Errorf("delete fail")
}

// CheckUniqueTypeAndNameAndVersion 校验网元类型和文件名版本是否唯一
func (r NeSoftware) CheckUniqueTypeAndNameAndVersion(neType, name, version string, id int64) bool {
	uniqueId := r.neSoftwareRepository.CheckUnique(model.NeSoftware{
		NeType:  neType,
		Name:    name,
		Version: version,
	})
	if uniqueId == id {
		return true
	}
	return uniqueId == 0
}

// UpdateVersions 更新软件包对应网元的新版本
func (r NeSoftware) UpdateVersions(neSoftware model.NeSoftware, neVersion model.NeVersion) int64 {
	var rows int64 = 0
	// 更新同类型的新包版本
	neVersions := r.neVersionService.Find(neVersion, false)
	if len(neVersions) > 0 {
		for _, v := range neVersions {
			v.NewName = neSoftware.Name
			v.NewVersion = neSoftware.Version
			v.NewPath = neSoftware.Path
			v.Status = "3"
			v.UpdateBy = neVersion.UpdateBy
			rows += r.neVersionService.Update(v)
		}
	}
	return rows
}
