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.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.finance_center.entity.vo.IntegralListVO;
import com.fzm.mall.server.admin.message.entity.dto.ModifySmsTemplateDTO;
import com.fzm.mall.server.admin.message.entity.dto.SmsTemplateDTO;
import com.fzm.mall.server.admin.message.entity.vo.SmsTemplateVO;
import com.fzm.mall.server.admin.message.enums.status.SmsTemplateStatusEnum;
import com.fzm.mall.server.admin.message.mapper.SmsTemplateMapper;
import com.fzm.mall.server.admin.message.model.SmsTemplate;
import com.fzm.mall.server.admin.message.service.ISmsTemplateService;
import com.fzm.mall.server.admin.mq.MsgProducer;
import com.fzm.mall.server.admin.mq.enums.QueueTTLTypeEnum;
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 SmsTemplateServiceImpl extends ServiceImpl<SmsTemplateMapper, SmsTemplate> implements ISmsTemplateService {

    private final DozerBeanMapper beanMapper;
    private final MsgProducer producer;

    @Override
    public void add(SmsTemplateDTO dto) {
        SmsTemplate smsTemplate = beanMapper.map(dto, SmsTemplate.class);
        //TODO SHIBAI-EXCEPTION
        AliCloudSmsUtil.addSmsTemplate(dto);

        if (false) {
            smsTemplate.setStatus(SmsTemplateStatusEnum.DELETE.getStatus());
            smsTemplate.setReason("xxx");
        } else {
            smsTemplate.setStatus(SmsTemplateStatusEnum.CHECKING.getStatus());
            smsTemplate.setTemplateCode("xxx");
        }

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

        //异步发行token
//        producer.sendTTL(new JSONObject().put("templateCode", "xxx"), QueueTTLTypeEnum.ISSUE_INTEGRAL_TTL, 2 * 60 * 1000);
    }

    @Override
    public IPage<SmsTemplateVO> page(String templateCode, String templateName, Integer pageNum, Integer pageSize) {
        IPage<SmsTemplateVO> page = new Page<>(pageNum, pageSize);
        return baseMapper.page(page, templateCode, templateName);
    }

    @Override
    public void delete(String templateCode) {
        QueryWrapper<SmsTemplate> qw = new QueryWrapper<>();
        qw.lambda().ne(SmsTemplate::getStatus, SmsTemplateStatusEnum.DELETE.getStatus())
                .eq(SmsTemplate::getTemplateCode, templateCode);
        SmsTemplate smsTemplate = getOne(qw);

        AliCloudSmsUtil.deleteSmsTemplate(templateCode);

        if (false) {
            smsTemplate.setReason("xxx");
        } else {
            smsTemplate.setStatus(SmsTemplateStatusEnum.DELETE.getStatus());
        }

        if (!updateById(smsTemplate)) {
            throw new MyException(MallResponseError.UPDATE_ERROR);
        }
    }

    @Override
    public void modify(ModifySmsTemplateDTO dto) {
        SmsTemplate smsTemplate = beanMapper.map(dto, SmsTemplate.class);
        //TODO SHIBAI-EXCEPTION
        AliCloudSmsUtil.modifySmsTemplate(dto);

        if (false) {
            smsTemplate.setReason("xxx");
        } else {
            smsTemplate.setStatus(SmsTemplateStatusEnum.CHECKING.getStatus());
        }

        UpdateWrapper<SmsTemplate> uw = new UpdateWrapper<>();
        uw.lambda().eq(SmsTemplate::getTemplateCode, smsTemplate.getTemplateCode());
        if (!update(smsTemplate, uw)) {
            throw new MyException(MallResponseError.SAVE_ERROR);
        }

//        producer.sendTTL(new JSONObject().put("templateCode", "xxx"), QueueTTLTypeEnum.ISSUE_INTEGRAL_TTL, 2 * 60 * 1000);
    }

    @Override
    public void updateCheckResult(String templateCode, Integer status, String reason) {
        UpdateWrapper<SmsTemplate> uw = new UpdateWrapper<>();
        uw.lambda()
                .set(SmsTemplate::getStatus, status)
                .set(SmsTemplate::getReason, reason)
                .eq(SmsTemplate::getTemplateCode, templateCode);
        update(uw);
    }
}
