package com.fzm.mall.server.admin.message.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.SystemClock;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.exception.MyException;
import com.fzm.mall.server.admin.home_page.service.IMerchantService;
import com.fzm.mall.server.admin.message.entity.dto.SmsBatchDTO;
import com.fzm.mall.server.admin.message.entity.dto.SmsSendDTO;
import com.fzm.mall.server.admin.message.entity.vo.SmsSendVO;
import com.fzm.mall.server.admin.message.entity.vo.SmsTemplateVO;
import com.fzm.mall.server.admin.message.enums.status.SmsSendStatusEnum;
import com.fzm.mall.server.admin.message.enums.status.SmsTemplateStatusEnum;
import com.fzm.mall.server.admin.message.enums.type.AddresseeTypeEnum;
import com.fzm.mall.server.admin.message.enums.type.SendTypeEnum;
import com.fzm.mall.server.admin.message.mapper.SmsSendMapper;
import com.fzm.mall.server.admin.message.model.SmsSend;
import com.fzm.mall.server.admin.message.model.SmsTemplate;
import com.fzm.mall.server.admin.message.service.ISmsSendService;
import com.fzm.mall.server.admin.mq.MsgProducer;
import com.fzm.mall.server.admin.mq.enums.QueueTTLTypeEnum;
import com.fzm.mall.server.admin.user_center.service.IUserService;
import com.fzm.mall.server.admin.util.AliCloudSmsUtil;
import lombok.RequiredArgsConstructor;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wangp
 * @date 2021/5/6 15:44
 * @description 服务实现类
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SmsSendServiceImpl extends ServiceImpl<SmsSendMapper, SmsSend> implements ISmsSendService {

    private final DozerBeanMapper beanMapper;
    private final IMerchantService merchantService;
    private final IUserService userService;
    private final MsgProducer producer;

    @Override
    public void send(SmsSendDTO dto) {
        SmsSend smsSend = beanMapper.map(dto, SmsSend.class);
        String phoneNumberJson = "";

        switch (AddresseeTypeEnum.getByType(smsSend.getAddresseeType())) {
            case SELF_DEFINE:
                phoneNumberJson = smsSend.getAddressee();
                break;
            case ALL_USER:
                phoneNumberJson = merchantService.getAllMerchantLinkPhone();
                break;
            case ALL_MERCHANT:
                phoneNumberJson = userService.getAllUserLinkPhone();
                break;
        }

        SmsBatchDTO smsBatchDTO = new SmsBatchDTO();
        smsBatchDTO.setTemplateCode(smsSend.getTemplateCode());
        smsBatchDTO.setPhoneNumberJson(phoneNumberJson);

        switch (SendTypeEnum.getByType(smsSend.getSendType())) {
            case SEND_NOW:
                AliCloudSmsUtil.sendBatchSms(smsBatchDTO);
                break;
            case SEND_TIMING:
                long time = dto.getSendTime() - SystemClock.now();
                producer.sendTTL(smsBatchDTO, QueueTTLTypeEnum.SEND_TIMING_SMS_TTL, time / 1000L);
                break;
        }

        if (!save(smsSend)) {
            throw new MyException(MallResponseError.SAVE_ERROR);
        }
    }

    @Override
    public IPage<SmsSendVO> page(String smsNo, String keyWord, Integer pageNum, Integer pageSize) {
        IPage<SmsSendVO> page = new Page<>(pageNum, pageSize);
        return baseMapper.page(page, smsNo, keyWord);
    }

    @Override
    public void delete(String smsNo) {
        LambdaUpdateWrapper<SmsSend> uw = new LambdaUpdateWrapper<>();
        uw.set(SmsSend::getStatus, SmsSendStatusEnum.DELETE.getStatus())
                .eq(SmsSend::getSmsNo, smsNo);
        if (!update(uw)) {
            throw new MyException(MallResponseError.UPDATE_ERROR);
        }
    }

    @Override
    public void reSend(String smsNo) {
        QueryWrapper<SmsSend> qw = new QueryWrapper<>();
        qw.lambda().eq(SmsSend::getSmsNo, smsNo);
        SmsSend smsSend = getOne(qw);
        SmsSendDTO sendDTO = beanMapper.map(smsSend, SmsSendDTO.class);
        send(sendDTO);
    }


}
