package repository

import (
	"fmt"
	"time"

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

// 实例化数据层 NeLicense 结构体
var NewNeLicense = &NeLicense{}

// NeLicense 网元授权激活信息 数据层处理
type NeLicense struct{}

// SelectByPage 分页查询集合
func (r NeLicense) SelectByPage(query map[string]string) ([]model.NeLicense, int64) {
	tx := db.DB("").Model(&model.NeLicense{})
	// 查询条件拼接
	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["expiryDate"]; ok && v != "" {
		tx = tx.Where("expiry_date = ?", v)
	}
	if v, ok := query["serialNum"]; ok && v != "" {
		tx = tx.Where("serial_num = ?", v)
	}
	if v, ok := query["createBy"]; ok && v != "" {
		tx = tx.Where("create_by like ?", fmt.Sprintf("%s%%", v))
	}

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

	// 查询数量为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("id 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 NeLicense) Select(param model.NeLicense) []model.NeLicense {
	tx := db.DB("").Model(&model.NeLicense{})
	// 查询条件拼接
	if param.NeType != "" {
		tx = tx.Where("ne_type = ?", param.NeType)
	}
	if param.NeId != "" {
		tx = tx.Where("ne_id = ?", param.NeId)
	}
	if param.ExpiryDate != "" {
		tx = tx.Where("expiry_date = ?", param.ExpiryDate)
	}
	if param.CreateBy != "" {
		tx = tx.Where("create_by like ?", fmt.Sprintf("%s%%", param.CreateBy))
	}

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

// SelectByIds 通过ID查询
func (r NeLicense) SelectByIds(ids []int64) []model.NeLicense {
	rows := []model.NeLicense{}
	if len(ids) <= 0 {
		return rows
	}
	tx := db.DB("").Model(&model.NeLicense{})
	// 构建查询条件
	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 NeLicense) Insert(neInfo model.NeLicense) int64 {
	if neInfo.CreateBy != "" {
		ms := time.Now().UnixMilli()
		neInfo.CreateTime = ms
		neInfo.UpdateTime = ms
		neInfo.UpdateBy = neInfo.CreateBy
	}
	tx := db.DB("").Create(&neInfo)
	if err := tx.Error; err != nil {
		logger.Errorf("CreateInBatches err => %v", err)
	}
	return neInfo.ID
}

// Update 修改信息
func (r NeLicense) Update(param model.NeLicense) 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.NeLicense{})
	// 构建查询条件
	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 NeLicense) DeleteByIds(ids []int64) int64 {
	if len(ids) <= 0 {
		return 0
	}
	tx := db.DB("").Where("id in ?", ids)
	if err := tx.Delete(&model.NeLicense{}).Error; err != nil {
		logger.Errorf("delete err => %v", err.Error())
		return 0
	}
	return tx.RowsAffected
}
