

package com.tmerclub.cloud.biz.feign;

import com.tmerclub.cloud.api.biz.feign.NotifyFeignClient;
import com.tmerclub.cloud.biz.controller.app.SmsController;
import com.tmerclub.cloud.biz.service.NotifyLogService;
import com.tmerclub.cloud.biz.service.NotifyTemplateTagService;
import com.tmerclub.cloud.biz.service.SmsLogService;
import com.tmerclub.cloud.common.cache.util.RedisUtil;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.SendTypeEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.PrincipalUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;


/**
 * @author lhd
 * @date 2020/12/30
 */
@DubboService
public class NotifyFeignController implements NotifyFeignClient {

    @Autowired
    private SmsLogService smsLogService;
    @Autowired
    private NotifyLogService notifyLogService;
    @Autowired
    private NotifyTemplateTagService notifyTemplateTagService;

    @Override
    public ServerResponseEntity<Boolean> checkValidCode(String validAccount, String validCode, SendTypeEnum type) {
        if (PrincipalUtil.isMobile(validAccount)) {
            return ServerResponseEntity.success(smsLogService.checkValidCode(validAccount, validCode, type));
        }
        return ServerResponseEntity.success(false);
    }

    @Override
    public ServerResponseEntity<Boolean> checkValidCodeByFlag(String mobile, String checkRegisterSmsFlag) {
        String redisMobile = RedisUtil.get(SmsController.CHECK_REGISTER_SMS_FLAG + checkRegisterSmsFlag);
        return ServerResponseEntity.success(Objects.equals(redisMobile, mobile));
    }

    @Override
    public ServerResponseEntity<Integer> getUnreadMsg(Long userId) {
        return ServerResponseEntity.success(notifyLogService.countUnreadBySendTypeAndRemindType(userId, Constant.MSG_TYPE));
    }

    @Override
    public ServerResponseEntity<Void> deleteTagByTagId(Long tagId) {
        notifyTemplateTagService.deleteByTagId(tagId);
        return ServerResponseEntity.success();
    }

}
