package com.red.circle.other.app.service;

import com.red.circle.other.app.command.redpacket.GrabRoomRedPacketCmdExe;
import com.red.circle.other.app.command.redpacket.SendRoomRedPacketCmdExe;
import com.red.circle.other.app.command.redpacket.query.QueryRoomRedPacketDetailQryExe;
import com.red.circle.other.app.command.redpacket.query.QueryRoomRedPacketListQryExe;
import com.red.circle.other.app.dto.clientobject.GrabRoomRedPacketResultCO;
import com.red.circle.other.app.dto.clientobject.RoomRedPacketCO;
import com.red.circle.other.app.dto.clientobject.RoomRedPacketDetailCO;
import com.red.circle.other.app.dto.cmd.GrabRoomRedPacketCmd;
import com.red.circle.other.app.dto.cmd.QueryRoomRedPacketDetailCmd;
import com.red.circle.other.app.dto.cmd.QueryRoomRedPacketListCmd;
import com.red.circle.other.app.dto.cmd.SendRoomRedPacketCmd;
import com.red.circle.other.app.util.DistributedLockUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 房间红包服务实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RoomRedPacketServiceImpl implements RoomRedPacketService {

    private final SendRoomRedPacketCmdExe sendRoomRedPacketCmdExe;
    private final GrabRoomRedPacketCmdExe grabRoomRedPacketCmdExe;
    private final QueryRoomRedPacketListQryExe queryRoomRedPacketListQryExe;
    private final QueryRoomRedPacketDetailQryExe queryRoomRedPacketDetailQryExe;
    private final DistributedLockUtil distributedLockUtil;

    @Override
    public RoomRedPacketCO sendRedPacket(SendRoomRedPacketCmd cmd) {
        return sendRoomRedPacketCmdExe.execute(cmd);
    }

    @Override
    public RoomRedPacketCO sendPlatformRedPacket(SendRoomRedPacketCmd cmd) {
        return sendRoomRedPacketCmdExe.executePlatform(cmd);
    }

    @Override
    public GrabRoomRedPacketResultCO grabRedPacket(GrabRoomRedPacketCmd cmd) {
        String key = "room:red_packet:" + cmd.getPacketId() + ":" + cmd.getReqUserId();
        return distributedLockUtil.executeWithLock(key, () -> grabRoomRedPacketCmdExe.execute(cmd));
    }

    @Override
    public List<RoomRedPacketCO> queryRoomRedPacketList(QueryRoomRedPacketListCmd cmd) {
        return queryRoomRedPacketListQryExe.execute(cmd);
    }

    @Override
    public RoomRedPacketDetailCO queryRedPacketDetail(QueryRoomRedPacketDetailCmd cmd) {
        return queryRoomRedPacketDetailQryExe.execute(cmd);
    }
}
