package com.tmerclub.cloud.order.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.order.bo.OrderVirtualInfoLogBO;
import com.tmerclub.cloud.order.bo.mongo.MongoOrderVirtualInfoLogBO;
import com.tmerclub.cloud.order.service.OrderVirtualInfoLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author TRACK
 */
@Service
public class OrderVirtualInfoLogServiceImpl implements OrderVirtualInfoLogService {
    private static final Logger logger = LoggerFactory.getLogger(OrderVirtualInfoLogServiceImpl.class);

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveLog(MongoOrderVirtualInfoLogBO mongoOrderVirtualInfoLogBO) {
        logger.info("保存核销记录到mongodb{}", mongoOrderVirtualInfoLogBO);
        mongoTemplate.insert(mongoOrderVirtualInfoLogBO);
    }

    @Override
    public void saveBatch(List<MongoOrderVirtualInfoLogBO> mongoOrderVirtualInfoLogBOList) {
        logger.info("插入核销日志：{}", Json.toJsonString(mongoOrderVirtualInfoLogBOList));
        if (CollUtil.isEmpty(mongoOrderVirtualInfoLogBOList)) {
            return;
        }
        // 批量操作
        try {
            mongoTemplate.insert(mongoOrderVirtualInfoLogBOList, MongoOrderVirtualInfoLogBO.class);
        } catch (DuplicateKeyException e) {
            logger.info("重复核销数据插入异常：{}", Json.toJsonString(mongoOrderVirtualInfoLogBOList));
        }
    }

    @Override
    public List<OrderVirtualInfoLogBO> getByOrderId(Long orderId) {
        Criteria criteria = new Criteria();
        if (Objects.nonNull(orderId)) {
            criteria.and("orderId").is(orderId);
        }
        Query query = new Query(criteria);
        logger.info("getByOrderId()的mongodb查询语句:{}", query);
        List<MongoOrderVirtualInfoLogBO> orderVirtualInfoLogList = mongoTemplate.find(query, MongoOrderVirtualInfoLogBO.class);
        List<OrderVirtualInfoLogBO> orderVirtualInfoLogBOList = BeanUtil.mapAsList(orderVirtualInfoLogList, OrderVirtualInfoLogBO.class);
        return orderVirtualInfoLogBOList;
    }

    @Override
    public List<OrderVirtualInfoLogBO> getByWriteOffCode(String writeOffCode) {
        Criteria criteria = new Criteria();
        if (StrUtil.isNotBlank(writeOffCode)) {
            criteria.and("writeOffCode").is(writeOffCode);
        }
        Query query = new Query(criteria);
        logger.info("getByWriteOffCode()的mongodb查询语句:{}", query);
        List<MongoOrderVirtualInfoLogBO> orderVirtualInfoLogList = mongoTemplate.find(query, MongoOrderVirtualInfoLogBO.class);
        List<OrderVirtualInfoLogBO> orderVirtualInfoLogBOList = BeanUtil.mapAsList(orderVirtualInfoLogList, OrderVirtualInfoLogBO.class);
        return orderVirtualInfoLogBOList;
    }
}
