package com.huiju.estateagents.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.entity.SysSmsEvent;
import com.huiju.estateagents.entity.SysSmsTemplate;
import com.huiju.estateagents.mapper.SysSmsEventMapper;
import com.huiju.estateagents.mapper.SysSmsSettingMapper;
import com.huiju.estateagents.mapper.SysSmsTemplateMapper;
import com.huiju.estateagents.service.ISysSmsEventService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yansen
 * @since 2020-12-21
 */
@Service
public class SysSmsEventServiceImpl extends ServiceImpl<SysSmsEventMapper, SysSmsEvent> implements ISysSmsEventService {

    @Autowired
    SysSmsSettingMapper sysSmsSettingMapper;

    @Autowired
    SysSmsTemplateMapper sysSmsTemplateMapper;

    @Autowired
    com.huiju.estateagents.common.smsService.Service smsService;

    @Override
    public boolean sendSMSOfRepairTicket(String to, String lifeConsultant, String phone, String propPhone) {
        List<SysSmsTemplate> sysSmsTemplateByBizType = getSysSmsTemplateByBizType(CommConstant.SMS_BIZ_REPAIRE_TICKET);
        if (null == sysSmsTemplateByBizType || sysSmsTemplateByBizType.size() < 1) {
            return false;
        }

        Map<String, String> content = new HashMap<String, String>(){{
            put("name", lifeConsultant);
            put("phone", phone);
            put("phone2", propPhone);
        }};

        com.huiju.estateagents.common.smsService.Service.Message message = new com.huiju.estateagents.common.smsService.Service.Message();
        message.setTel(to)
                .setCode(sysSmsTemplateByBizType.get(0).getTemplateCode())
                .setSign(sysSmsTemplateByBizType.get(0).getSign())
                .setContent(JSONObject.toJSONString(content));

        try {
            smsService.sendMessage(message);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    private List<SysSmsTemplate> getSysSmsTemplateByBizType(String bizType) {
        return sysSmsTemplateMapper.getSysSmsTemplateByBizType(bizType);
    }
}
