package service

import (
	"errors"

	"github.com/go-admin-team/go-admin-core/sdk/service"
	"github.com/jinzhu/copier"
	"gorm.io/gorm"

	"go-admin/app/admin/models"
	"go-admin/app/admin/service/dto"
	"go-admin/common/actions"
	cDto "go-admin/common/dto"
	statuscode "go-admin/common/status_code"
)

type LineCoinnetwork struct {
	service.Service
}

// GetPage 获取LineCoinnetwork列表
func (e *LineCoinnetwork) GetPage(c *dto.LineCoinnetworkGetPageReq, p *actions.DataPermission, list *[]models.LineCoinnetwork, count *int64) error {
	var err error
	var data models.LineCoinnetwork

	err = e.Orm.Model(&data).
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
		).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("LineCoinnetworkService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

// Get 获取LineCoinnetwork对象
func (e *LineCoinnetwork) Get(d *dto.LineCoinnetworkGetReq, p *actions.DataPermission, model *models.LineCoinnetwork) error {
	var data models.LineCoinnetwork

	err := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).
		First(model, d.GetId()).Error
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		e.Log.Errorf("Service GetLineCoinnetwork error:%s \r\n", err)
		return err
	}
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err
	}
	return nil
}

// Insert 创建LineCoinnetwork对象
func (e *LineCoinnetwork) Insert(c *dto.LineCoinnetworkInsertReq) error {
	var err error
	var data models.LineCoinnetwork
	c.Generate(&data)
	var count int64
	e.Orm.Model(&data).Where("network_name =?").Count(&count)

	if count > 0 {
		return errors.New("网络已存在")
	}

	err = e.Orm.Create(&data).Error
	if err != nil {
		e.Log.Errorf("LineCoinnetworkService Insert error:%s \r\n", err)
		return err
	}
	return nil
}

// Update 修改LineCoinnetwork对象
func (e *LineCoinnetwork) Update(c *dto.LineCoinnetworkUpdateReq, p *actions.DataPermission) error {
	var err error
	var data = models.LineCoinnetwork{}
	var count int64
	e.Orm.Model(&data).Where("network_name =? AND id !=?", c.NetworkName, c.Id).Count(&count)

	if count > 0 {
		return errors.New("网络已存在")
	}

	e.Orm.Scopes(
		actions.Permission(data.TableName(), p),
	).First(&data, c.GetId())
	c.Generate(&data)

	db := e.Orm.Save(&data)
	if err = db.Error; err != nil {
		e.Log.Errorf("LineCoinnetworkService Save error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")
	}
	return nil
}

// Remove 删除LineCoinnetwork
func (e *LineCoinnetwork) Remove(d *dto.LineCoinnetworkDeleteReq, p *actions.DataPermission) error {
	var data models.LineCoinnetwork

	db := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).Delete(&data, d.GetId())
	if err := db.Error; err != nil {
		e.Log.Errorf("Service RemoveLineCoinnetwork error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权删除该数据")
	}
	return nil
}

// 获取区块链列表
// 状态码
func (e *LineCoinnetwork) GetOpenList(list *[]dto.LineCoinnetworkAppResp) int {
	var datas []models.LineCoinnetwork
	item := dto.LineCoinnetworkAppResp{}

	if err := e.Orm.Model(&models.LineCoinnetwork{}).Find(&datas).Error; err != nil {
		return statuscode.ServerError
	}

	for _, v := range datas {
		copier.Copy(&item, v)

		*list = append(*list, item)
	}
	return statuscode.OK
}
