Newer
Older
huludao / src / main / java / com / newfiber / api / pc / service / impl / SimulateAutoMeetManageServiceImpl.java
package com.newfiber.api.pc.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.newfiber.api.core.bean.ReqBodyObj;
import com.newfiber.api.core.commons.CustomException;
import com.newfiber.api.core.commons.ResultCode;
import com.newfiber.api.core.utils.Tools;
import com.newfiber.api.pc.model.meet.MeetPlanType;
import com.newfiber.api.pc.model.meet.SimulateAutoMeetManage;
import com.newfiber.api.pc.service.MeetPlanTypeService;
import com.newfiber.api.pc.service.SimulateAutoMeetManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模拟自动触发报警业务层实现类
 * @description: TODO
 * @author: 张鸿志
 * @date: 2020/12/23 10:07
 * @version: v1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SimulateAutoMeetManageServiceImpl implements SimulateAutoMeetManageService {
    @Autowired
    private MeetPlanTypeService meetPlanTypeService;
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    @Override
    public String simulateMeetAuto(SimulateAutoMeetManage simulateAutoMeetManage) {
        if(StringUtils.isEmpty(simulateAutoMeetManage)){
            throw new CustomException(ResultCode.PARAM_NULL);
        }
        if(!(StringUtils.isEmpty(simulateAutoMeetManage.getTriggerValue()) && StringUtils.isEmpty(simulateAutoMeetManage.getSetCondition()))){
            //由于Double类型高精度运算可能会有误差,所以转换成BigDecimal类型进行计算
            BigDecimal triggerValue = new BigDecimal(simulateAutoMeetManage.getTriggerValue());
            BigDecimal setCondition = new BigDecimal(simulateAutoMeetManage.getSetCondition());
            int compare = setCondition.compareTo(triggerValue);
            if(compare < 0){
                return "并未达到触发报警的条件,不会发送短信通知!";
            }

        }
        //构建短信通知消息Map
        Map<String,String> smsMap = new HashMap<>();
        //根据ASCII码查询类别数据
        List<MeetPlanType> meetPlanTypes = meetPlanTypeService.selectList(null);
        for(MeetPlanType meetPlanType : meetPlanTypes){
            List<String> asciis = JSON.parseArray(meetPlanType.getFactors(), String.class);
            int indexOf = asciis.indexOf(simulateAutoMeetManage.getCodeAscii());
            //由于 短信模板字符长度不能太长,这里就只取第一个(因为有可能有多个类型会有同一个因子ASCII码)
            if(indexOf != -1){
                smsMap.put("m_type_name",meetPlanType.getMTypeName());
            }
        }
        smsMap.put("area_name","模拟"+simulateAutoMeetManage.getAreaName());
        smsMap.put("site_name","模拟"+simulateAutoMeetManage.getSiteName());
        smsMap.put("m_level_name","模拟-紧急");
        Map<String,Object> map = new HashMap<>(16);
        map.put("receiveMobile",simulateAutoMeetManage.getPhone());
        map.put("sendType","1");
        map.put("platform","510");
        map.put("content",System.currentTimeMillis());
        map.put("templateType","1");
        map.put("timeStamp",sdf.format(System.currentTimeMillis()));
        map.put("signName","新烽光电");
        map.put("templateCode","SMS_206890324");
        String token = Integer.parseInt(Tools.nullToEmpty(map.get("sendType"))) * 8735697 + "" + map.get("timeStamp")
                + map.get("platform") + map.get("receiveMobile");
        map.put("token", SecureUtil.md5(token));
        map.put("variable",smsMap);
        ReqBodyObj req = new ReqBodyObj();
        req.setData(map);
        String s = Tools.callUrl("http://139.155.49.237:8999/sms_send/send", JSONObject.toJSONString(req));
        if(StringUtils.isEmpty(s)){
            return "发送短信失败,请稍后再试!";
        }
        return "短信发送成功,请查看!";
    }
}