package repository

import (
	"fmt"
	"time"

	"be.ems/src/framework/database/db"
	"be.ems/src/framework/logger"
	"be.ems/src/modules/ne/model"
)

// 实例化数据层 NeVersion 结构体
var NewNeVersion = &NeVersion{}

// NeVersion 网元版本信息 数据层处理
type NeVersion struct{}

// SelectByPage 分页查询集合
func (r NeVersion) SelectByPage(query map[string]string) ([]model.NeVersion, int64) {
	tx := db.DB("").Model(&model.NeVersion{})
	// 查询条件拼接
	if v, ok := query["neType"]; ok && v != "" {
		tx = tx.Where("ne_type = ?", v)
	}
	if v, ok := query["neId"]; ok && v != "" {
		tx = tx.Where("ne_id = ?", v)
	}
	if v, ok := query["version"]; ok && v != "" {
		tx = tx.Where("version like ?", fmt.Sprintf("%s%%", v))
	}
	if v, ok := query["path"]; ok && v != "" {
		tx = tx.Where("path like ?", fmt.Sprintf("%s%%", v))
	}

	// 查询结果
	var total int64 = 0
	rows := []model.NeVersion{}

	// 查询数量为0直接返回
	if err := tx.Count(&total).Error; err != nil || total <= 0 {
		return rows, total
	}

	// 查询数据分页
	pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"])
	tx = tx.Limit(pageSize).Offset(pageSize * pageNum)
	tx = tx.Order("update_time desc")
	err := tx.Find(&rows).Error
	if err != nil {
		logger.Errorf("query find err => %v", err.Error())
		return rows, total
	}
	return rows, total
}

// Select 查询集合
func (r NeVersion) Select(param model.NeVersion) []model.NeVersion {
	tx := db.DB("").Model(&model.NeVersion{})
	// 查询条件拼接
	if param.NeType != "" {
		tx = tx.Where("ne_type = ?", param.NeType)
	}
	if param.NeId != "" {
		tx = tx.Where("ne_id = ?", param.NeId)
	}
	if param.Version != "" {
		tx = tx.Where("version like ?", fmt.Sprintf("%s%%", param.Version))
	}
	if param.Path != "" {
		tx = tx.Where("path like ?", fmt.Sprintf("%s%%", param.Path))
	}
	if param.Status != "" {
		tx = tx.Where("status = ?", param.Status)
	}

	// 查询数据
	rows := []model.NeVersion{}
	if err := tx.Order("id asc").Find(&rows).Error; err != nil {
		logger.Errorf("query find err => %v", err.Error())
		return rows
	}
	return rows
}

// SelectByIds 通过ID查询
func (r NeVersion) SelectByIds(ids []int64) []model.NeVersion {
	rows := []model.NeVersion{}
	if len(ids) <= 0 {
		return rows
	}
	tx := db.DB("").Model(&model.NeVersion{})
	// 构建查询条件
	tx = tx.Where("id in ?", ids)
	// 查询数据
	if err := tx.Find(&rows).Error; err != nil {
		logger.Errorf("query find err => %v", err.Error())
		return rows
	}
	return rows
}

// Insert 新增信息
func (r NeVersion) Insert(param model.NeVersion) int64 {
	if param.CreateBy != "" {
		ms := time.Now().UnixMilli()
		param.CreateTime = ms
		param.UpdateTime = ms
		param.UpdateBy = param.CreateBy
	}
	tx := db.DB("").Create(&param)
	if err := tx.Error; err != nil {
		logger.Errorf("CreateInBatches err => %v", err)
	}
	return param.ID
}

// Update 修改信息
func (r NeVersion) Update(param model.NeVersion) int64 {
	if param.ID <= 0 {
		return 0
	}
	if param.UpdateBy != "" {
		param.UpdateTime = time.Now().UnixMilli()
	}
	param.UpdateTime = time.Now().UnixMilli()
	tx := db.DB("").Model(&model.NeVersion{})
	// 构建查询条件
	tx = tx.Where("id = ?", param.ID)
	tx = tx.Omit("id", "create_by", "create_time")
	// 执行更新
	if err := tx.Updates(param).Error; err != nil {
		logger.Errorf("update err => %v", err.Error())
		return 0
	}
	return tx.RowsAffected
}

// DeleteByIds 批量删除信息
func (r NeVersion) DeleteByIds(ids []int64) int64 {
	if len(ids) <= 0 {
		return 0
	}
	tx := db.DB("").Where("id in ?", ids)
	if err := tx.Delete(&model.NeVersion{}).Error; err != nil {
		logger.Errorf("delete err => %v", err.Error())
		return 0
	}
	return tx.RowsAffected
}
