package Wx

import (
	"context"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"wechat-api/ent/wx"
	"wechat-api/internal/utils/dberrorhandler"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

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

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

func (l *UpdateBlockAndAllowListLogic) UpdateBlockAndAllowList(req *types.UpdateBlockAndAllowListReq) (resp *types.BaseMsgResp, err error) {
	err = l.svcCtx.DB.Wx.UpdateOneID(*req.Id).
		SetNotNilAllowList(req.AllowList).
		SetNotNilGroupAllowList(req.GroupAllowList).
		SetNotNilBlockList(req.BlockList).
		SetNotNilGroupBlockList(req.GroupBlockList).
		Exec(l.ctx)

	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	wxinfo, err := l.svcCtx.DB.Wx.Query().
		Where(
			wx.IDEQ(*req.Id),
		).
		WithAgent().
		Only(l.ctx)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}
	l.svcCtx.Rds.HDel(l.ctx, "wx_info", wxinfo.Wxid)
	return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
}
