package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
	plantModel "sundynix-micro-go/app/plant/model"
	"sundynix-micro-go/app/plant/rpc/internal/svc"
	"sundynix-micro-go/app/plant/rpc/plant"
)

type DeleteWikiLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDeleteWikiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteWikiLogic {
	return &DeleteWikiLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
}

func (l *DeleteWikiLogic) DeleteWiki(in *plant.IdsReq) (*plant.CommonResp, error) {
	if err := l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("id IN ?", in.Ids).Delete(&plantModel.Wiki{}).Error; err != nil {
			return err
		}
		if err := tx.Where("wiki_id IN ?", in.Ids).Delete(&plantModel.WikiClassRelation{}).Error; err != nil {
			return err
		}
		if err := tx.Where("wiki_id IN ?", in.Ids).Delete(&plantModel.WikiOss{}).Error; err != nil {
			return err
		}
		return tx.Where("wiki_id IN ? OR related_wiki_id IN ?", in.Ids, in.Ids).Delete(&plantModel.WikiRelated{}).Error
	}); err != nil {
		return nil, err
	}
	return &plant.CommonResp{Code: 0, Msg: "ok"}, nil
}
