package cn.pluss.platform.impl;

import cn.pluss.platform.PushService;
import cn.pluss.platform.entity.MerchantStore;
import cn.pluss.platform.entity.PushAll;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.PushAllMapper;
import cn.pluss.platform.merchantStore.MerchantStoreService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.user.impl.GeneralPushUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 发送推送的服务实现类
 * @author DJH
 */
@Service
public class PushServiceImpl extends ServiceImpl<PushAllMapper, PushAll> implements PushService {

    public static final int MAX_BROADCAST_COUNT = 5;

    @Setter(onMethod_ = {@Autowired})
    private MerchantStoreService merchantStoreService;

    @Setter(onMethod_ = {@Autowired,@Lazy})
    private UserAppService userAppService;

    @Autowired
    private GeneralPushUtil generalPushUtil;

    @Override
    public void pushMsg(String userId, String type) {
        List<String> alias = new ArrayList<>();
        alias.add(userId);

        switch (type) {
            case MAKE_ORDER:
                generalPushUtil.sendAllPlatByAlias(alias, "收银呗", "收银呗线上店订单来了请及时处理", "3");
                break;
            case URGE_ORDER:
                generalPushUtil.sendAllPlatByAlias(alias, "收银呗", "您有一笔催单请及时处理", "3");
                break;
            case REQUEST_SERVICE:
                generalPushUtil.sendAllPlatByAlias(alias, "收银呗", "您有一个新的呼叫服务请及时处理", "3");
                break;
            case REFUND_ORDER:
                generalPushUtil.sendAllPlatByAlias(alias, "收银呗", "您有一笔未接单的线上店订单，客户已申请退款。", "2");
                break;
            default:
        }
    }

    @Override
    public void pushMsg(Integer storeId, String type) {
        MerchantStore merchantStore = new MerchantStore();
        merchantStore.setId(storeId);
        merchantStore = merchantStoreService.queryMerchantStore(merchantStore);

        if (merchantStore == null) {
            throw new MsgException("未找到对应店铺");
        }

        UserApp userApp = new UserApp().setMerchantCode(merchantStore.getMerchantCode());
        userApp.setUserType("promoter");
        userApp = userAppService.queryUserApp(userApp);

        pushMsg(userApp.getUserId() + "", type);
    }

    @Override
    public void pushAll(String title, String msg) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = sdf.format(new Date());
        QueryWrapper<PushAll> qWrapper = new QueryWrapper<>();
        qWrapper.likeRight("createTime", currentDate);

        Integer count = baseMapper.selectCount(qWrapper);
        if (count >= MAX_BROADCAST_COUNT) {
            throw new MsgException("当天发送广播次数已经达到最大次数" + MAX_BROADCAST_COUNT + "次");
        }

        PushAll entity = new PushAll();
        entity.setTitle(title);
        entity.setContent(msg);
        save(entity);

        generalPushUtil.sendAllPlatAll(title, msg, "1");
    }
}
