package com.yc.ship.module.trade.service.order.impl;



import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.module.trade.convert.order.TradeOrderConvert;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderLogDO;
import com.yc.ship.module.trade.dal.mysql.order.TradeOrderLogMapper;
import com.yc.ship.module.trade.dal.mysql.order.TradeOrderMapper;
import com.yc.ship.module.trade.enums.TradeOrderOperateTypeEnum;
import com.yc.ship.module.trade.service.order.TradeOrderLogService;
import com.yc.ship.module.trade.service.order.bo.TradeOrderLogCreateReqBO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 交易下单日志 Service 接口
 *
 * @author qsl
 */
@Service
public class TradeOrderLogServiceImpl implements TradeOrderLogService {

    @Resource
    private TradeOrderLogMapper tradeOrderLogMapper;

    @Resource
    private TradeOrderMapper tradeOrderMapper;

    @Override
    public void createOrderLog(TradeOrderLogCreateReqBO logDTO) {
        tradeOrderLogMapper.insert(TradeOrderConvert.INSTANCE.convert(logDTO));
    }

    @Override
    public List<TradeOrderLogDO> getOrderLogListByOrderId(String orderNo) {
        List<Long> orderIds = tradeOrderMapper.selectOrderIdByOrderNo(orderNo);
        if(orderIds.isEmpty()) {
            return Collections.emptyList();
        }
        LambdaQueryWrapperX<TradeOrderLogDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.in(TradeOrderLogDO::getOrderId, orderIds);
        queryWrapper.orderByDesc(TradeOrderLogDO::getCreateTime);
        return tradeOrderLogMapper.selectList(queryWrapper);
    }

    @Override
    public void delLog(Long orderId, TradeOrderOperateTypeEnum...operateType) {
        LambdaQueryWrapperX<TradeOrderLogDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(TradeOrderLogDO::getOrderId, orderId);
        queryWrapper.in(TradeOrderLogDO::getOperateType, Arrays.stream(operateType).map(TradeOrderOperateTypeEnum::getType).toArray());
        tradeOrderLogMapper.delete(queryWrapper);
    }
}
