package com.td.boss.game.complayergoods.service;

import com.riven.redisson.annotation.RedissonListener;
import com.riven.redisson.message.RedissonHeaders;
import com.td.boss.common.service.*;
import com.td.boss.config.delay.pojo.PlayerGoods;
import com.td.boss.game.complayergoods.pojo.ComPlayerGoods;
import com.td.boss.game.complayergoods.vo.ComPlayerGoodsSimpleVo;
import com.td.boss.game.complayergoods.vo.ComPlayerGoodsTypeSumVo;
import com.td.boss.game.complayergoods.vo.ComPlayerGoodsVo;
import com.td.boss.game.complayergoods.repository.ComPlayerGoodsRepository;
import com.td.boss.util.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class ComPlayerGoodsServiceImpl extends CommonServiceImpl<ComPlayerGoodsVo, ComPlayerGoods, String> implements ComPlayerGoodsService {

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private ComPlayerGoodsRepository comPlayerGoodsRepository;


    @Override
    public ComPlayerGoods findByUserIdAndIndexAndType(String userId, Integer index, Integer type) {
        Optional<ComPlayerGoods> comPlayerGoods = comPlayerGoodsRepository.findByUserIdAndGoodsIndexAndGoodsType(userId, index, type);
        return comPlayerGoods.orElse(null);
    }

    @Override
    public List<ComPlayerGoods> findAllByUserId(String userId) {
        return comPlayerGoodsRepository.findAllByUserId(userId);
    }


    @Override
    public List<ComPlayerGoodsTypeSumVo> findAllGroupByGoodsType() {
        return comPlayerGoodsRepository.findAmountSumByGoodsType();
    }

    /**
     * 处理仓库断电损耗 延迟任务
     *
     * @param messageId           消息id
     * @param queue               队列
     * @param sendTimestamp       发送时间戳
     * @param expectedDelayMillis 预计延迟
     * @param myHeader            头
     * @param playerGoods         玩家物品
     */
    @RedissonListener(queues = "playerGoods", messageConverter = "playerGoodsMessageConverter")
    public void handler(@Header(value = RedissonHeaders.MESSAGE_ID, required = false) String messageId,
                        @Header(RedissonHeaders.DELIVERY_QUEUE_NAME) String queue,
                        @Header(RedissonHeaders.SEND_TIMESTAMP) long sendTimestamp,
                        @Header(RedissonHeaders.EXPECTED_DELAY_MILLIS) long expectedDelayMillis,
                        @Header(value = "my_header", required = false, defaultValue = "test") String myHeader,
                        @Payload PlayerGoods playerGoods) {
        /**
         *         1.注入
         *         @Autowired
         *         private RedissonTemplate redissonTemplate;
         *
         *         2.像队列中添加延迟任务
         *         PlayerGoods playerGoods = new PlayerGoods();
         *         playerGoods.setUserId("1002");
         *         redissonTemplate.sendWithDelay("playerGoods", playerGoods, 10000);
         */

        System.out.println(messageId);
        System.out.println(queue);
        System.out.println(myHeader);
        long actualDelay = System.currentTimeMillis() - (sendTimestamp + expectedDelayMillis);
        System.out.println("receive " + playerGoods + ", delayed " + actualDelay + " millis");

        List<ComPlayerGoods> allByUserId = comPlayerGoodsRepository.findAllByUserId(playerGoods.getUserId());
        System.out.println("查一下：" + allByUserId);
        System.out.println(allByUserId);

    }
}
