package com.liquidnet.service.kylin.service.impl;

import com.liquidnet.service.kylin.dto.vo.groupbuy.KylinGroupBuyOrderVo;
import com.liquidnet.service.kylin.service.GroupBuyNotifyService;
import com.liquidnet.service.kylin.utils.GroupBuySmsSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;

@Service
@Slf4j
public class GroupBuyNotifyServiceImpl implements GroupBuyNotifyService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private GroupBuySmsSender groupBuySmsSender;

    @Override
    public void notifySuccess(String groupOrderId) {
        KylinGroupBuyOrderVo group = load(groupOrderId);
        if (group == null) {
            return;
        }
        groupBuySmsSender.notifySuccess(group);
    }

    @Override
    public void notifyFailed(String groupOrderId) {
        KylinGroupBuyOrderVo group = load(groupOrderId);
        if (group == null) {
            return;
        }
        groupBuySmsSender.notifyFailed(group);
    }

    @Override
    public void notifyRemind1h(String groupOrderId) {
        KylinGroupBuyOrderVo group = load(groupOrderId);
        if (group == null) {
            return;
        }
        groupBuySmsSender.notifyRemind1h(group);
    }

    private KylinGroupBuyOrderVo load(String groupOrderId) {
        if (groupOrderId == null) {
            return null;
        }
        return mongoTemplate.findOne(
                Query.query(Criteria.where("groupOrderId").is(groupOrderId)),
                KylinGroupBuyOrderVo.class, KylinGroupBuyOrderVo.class.getSimpleName());
    }
}
