package com.chinaitop.depot.intelligent.basicdata.service.impl;

import com.chinaitop.depot.intelligent.SendMessagePT.SendMessagePT;
import com.chinaitop.depot.intelligent.SendMessagePT.SmsDataVO;
import com.chinaitop.depot.intelligent.basicdata.model.BasicStorehouse;
import com.chinaitop.depot.intelligent.basicdata.model.MonitorAlarmPhone;
import com.chinaitop.depot.intelligent.basicdata.service.AlarmService;
import com.chinaitop.depot.intelligent.basicdata.service.BasicStorehouseService;
import com.chinaitop.depot.intelligent.basicdata.service.ResolutionToSendAlarmService;
import com.chinaitop.depot.intelligent.grainsituation.model.TWarningThresholdHistory;
import com.chinaitop.depot.intelligent.grainsituation.service.TWarningThresholdHistoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ResolutionToSendAlarmServiceimpl implements ResolutionToSendAlarmService {

    @Resource
    private BasicStorehouseService basicStorehouseService;
    @Resource
    private AlarmService alarmService;
    @Resource
    private TWarningThresholdHistoryService tWarningThresholdHistoryService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean getAlarmDetails(List<TWarningThresholdHistory> list) {
        if (list != null) {
            StringBuilder sms = new StringBuilder();
            String dateString = "";
            String orgId = "";
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            boolean tepm = true;
            for (TWarningThresholdHistory tWarningThresholdHistory : list) {
                orgId = tWarningThresholdHistory.getOrgId();//获取库点id
                String cfCode = tWarningThresholdHistory.getvCfCode();//获取仓房code
                BasicStorehouse basicStorehouse = basicStorehouseService.queryOrgNameOrCode(orgId, cfCode);
                if (basicStorehouse == null){
                    return false;
                }
                if (tepm){
                    sms.append(basicStorehouse.getDepotName());
                    Date detectioTtime = tWarningThresholdHistory.getwTime();//获取检测时间
                    dateString = formatter.format(detectioTtime);
                    tepm = false;
                }
                String storehouseName = basicStorehouse.getStorehouseName();
                sms.append(storehouseName);
                sms.append(tWarningThresholdHistory.getwRemark()).append(";");
                tWarningThresholdHistory.setIsSend("0");
                tWarningThresholdHistoryService.updateById(tWarningThresholdHistory);
            }
            this.sendMsg(sms.toString(), "检测时间：" + dateString, orgId);
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void sendMsg(String cameraAlias, String time, String orgid) {
        SendMessagePT sendMessagePT = new SendMessagePT();//--短信平台
        String sendMsg = "";
        //如果有报警信息
        if (!cameraAlias.isEmpty()) {
            sendMsg += "报警：" + cameraAlias;
            if (sendMsg.length() > 300){
                sendMsg = sendMsg.substring(0,300);
                sendMsg += "【信息过长,详细情况请登录系统查看】......";
            }
            sendMsg += time;
            System.out.println("信息内容：" + sendMsg);
            //查询出手机号码
            List<MonitorAlarmPhone> MonitorAlarmPhone = alarmService.queryAlarmPhoneByOrgid(Integer.valueOf(orgid));
            //发送短信
            if (MonitorAlarmPhone != null && MonitorAlarmPhone.size() > 0) {
                for (int i = 0; i < MonitorAlarmPhone.size(); i++) {
                    SmsDataVO smsDataVO = new SmsDataVO();
                    MonitorAlarmPhone alarmPhone = MonitorAlarmPhone.get(i);
                    smsDataVO.setSdst(alarmPhone.getAlarmPhone());
                    smsDataVO.setSmsg(sendMsg);
                    try {
                        sendMessagePT.sendingSMS(smsDataVO);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
