package com.zanxiang.game.platform.serve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.game.platform.serve.dao.mapper.PlatformDeYangOrderMapper;
import com.zanxiang.game.platform.serve.enums.GamePlatformEnum;
import com.zanxiang.game.platform.serve.pojo.dto.PlatformDeYangOrderDTO;
import com.zanxiang.game.platform.serve.pojo.entity.PlatformDeYangOrder;
import com.zanxiang.game.platform.serve.service.IPlatformDeYangOrderService;
import com.zanxiang.module.redis.annotation.Lock;
import com.zanxiang.module.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author : lingfeng
 * @time : 2023-05-22
 * @description : 德扬订单
 */
@Slf4j
@Service
public class PlatformDeYangOrderServiceImpl extends ServiceImpl<PlatformDeYangOrderMapper, PlatformDeYangOrder> implements IPlatformDeYangOrderService {

    @Override
    public Boolean addOrUpdate(PlatformDeYangOrderDTO orderDTO) {
        return super.saveOrUpdate(PlatformDeYangOrder.builder()
                .orderId(orderDTO.getOrderid())
                .platformKey(GamePlatformEnum.DE_YANG.getPlatformKey())
                .uid(orderDTO.getUid())
                .mpOpenId(orderDTO.getWecha_id())
                .gameName(orderDTO.getGame_name())
                .payStatus(orderDTO.getPay_status())
                .price(orderDTO.getPrice())
                .addTime(orderDTO.getAddtime() == null ? null : DateUtil.secondToLocalDateTime(orderDTO.getAddtime()))
                .payType(orderDTO.getPay_type())
                .payTime(Strings.isBlank(orderDTO.getPay_time()) ? null : DateUtil.parseLocalDateTime(orderDTO.getPay_time()))
                .roleName(orderDTO.getRole_name())
                .roleArea(orderDTO.getRole_area())
                .tunnelId(orderDTO.getTunnel_id())
                .productName(orderDTO.getProduct_name())
                .touFangBack(orderDTO.getToufang_back())
                .agentId(orderDTO.getAgent_id2())
                .bankOrderNo(orderDTO.getBankOrderNo())
                .build());
    }

    @Lock(prefix = "platform:deYangOrderService:saveOrUpdate", waitTime = 3, leaseTime = 3, timeUnit = TimeUnit.MINUTES)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(List<PlatformDeYangOrder> orderList) {
        if (CollectionUtils.isEmpty(orderList)) {
            return true;
        }
        Map<String, PlatformDeYangOrder> oldMap = list(new LambdaQueryWrapper<PlatformDeYangOrder>()
                .in(PlatformDeYangOrder::getOrderId, orderList.stream().map(PlatformDeYangOrder::getOrderId).collect(Collectors.toList()))
        ).stream().collect(Collectors.toMap(PlatformDeYangOrder::getOrderId, Function.identity()));
        List<PlatformDeYangOrder> addList = new ArrayList<>(orderList.size());
        for (PlatformDeYangOrder order : orderList) {
            PlatformDeYangOrder old = oldMap.remove(order.getOrderId());
            if (old == null) {
                addList.add(order);
            } else {
                order.setOrderId(old.getOrderId());
                if (!order.equals(old)) {
                    updateById(order);
                }
            }
        }
        if (CollectionUtils.isNotEmpty(addList)) {
            saveBatch(addList);
        }
        return true;
    }
}
