package com.fzm.mall.server.admin.mq.consumer.order_center;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.SystemClock;
import com.fzm.mall.server.admin.order.model.Delivery;
import com.fzm.mall.server.admin.order.service.IDeliveryService;
import com.fzm.mall.server.admin.user.model.Asset;
import com.fzm.mall.server.admin.user.service.IAssetService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author wangp
 * @date 2021/3/10 17:40
 * @description 数字货币发行
 * @since JDK 1.8
 */
@Component
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OrderConsumer {

    private final IDeliveryService deliveryService;
    private final IAssetService assetService;

    /**
     * 已结束的提货订单的状态
     */
    private static final List<Integer> DELIVERY_STATE_CLOSED = Arrays.asList(Delivery.DELIVERY_STATE_REFUNDED,
            Delivery.DELIVERY_STATE_CLOSED, Delivery.DELIVERY_STATE_REFUSED);

    @RabbitListener(queues = "ly.order.delivery.queue")
    public void changeOrderDeliveryState(Map<String, Object> obj) {
        /**
         * 更改提货单状态
         */
        String orderDeliveryId = (String) obj.get("orderDeliveryId");

        QueryWrapper<Delivery> queryWrapper = new QueryWrapper<Delivery>().eq("order_delivery_id", orderDeliveryId)
                .eq("is_delete", false);
        Delivery deliveryData = deliveryService.getOne(queryWrapper);

        /**
         * 提货单未关闭的，到设置时间关闭
         */
        if(!DELIVERY_STATE_CLOSED.contains(deliveryData.getDeliveryState())){

            deliveryData.setDeliveryState(Delivery.DELIVERY_STATE_CLOSED);
            deliveryData.setFinishTime(SystemClock.now());

            UpdateWrapper<Delivery> updateWrapper = new UpdateWrapper<Delivery>().eq("order_delivery_id", orderDeliveryId);
            boolean updateDe = deliveryService.update(deliveryData, updateWrapper);
            if(!updateDe){
                log.error("update delivery fail, order_delivery_id:" + orderDeliveryId);
            }
        }

        /**
         * 更改用户冻结资产
         */

        QueryWrapper<Asset> wrapper = new QueryWrapper<Asset>().eq("uid", deliveryData.getUid())
                .eq("coin", deliveryData.getCoin());
        Asset asset = assetService.getOne(wrapper);
        if(null == asset){
            log.error("query asset fail, uid:" + deliveryData.getUid() + "coin:" + deliveryData.getCoin());
            return;
        }
        BigDecimal number = new BigDecimal(deliveryData.getNumber());
        BigDecimal frozen = asset.getFrozen();
        BigDecimal result = frozen.subtract(number);
        frozen = result.compareTo(BigDecimal.ZERO) > 0 ? result : BigDecimal.ZERO;
        asset.setFrozen(frozen);
        UpdateWrapper assetUpdateWrapper = new UpdateWrapper<Asset>().eq("uid", asset.getUid())
                .eq("coin", asset.getCoin())
                .eq("update_time", asset.getUpdateTime());
        boolean update = assetService.update(asset, assetUpdateWrapper);
        if(!update){
            log.error("update asset fail, uid:" + deliveryData.getUid() + "coin:" + deliveryData.getCoin() + "update_time:" + asset.getUpdateTime());
        }
    }
}
