package com.zcloud.controller.app;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.PageData;
import com.zcloud.entity.alarm.DtoData;
import com.zcloud.service.alarm.DBAlarmService;
import com.zcloud.service.alarm.DevNoticeService;
import com.zcloud.service.alarm.DeviceCorpService;
import com.zcloud.service.corp.CorpInfoService;
import com.zcloud.service.notice.NoticeCorpService;
import com.zcloud.service.notice.NoticeCorpUserService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller
@RequestMapping("/app/AppAlarm")
public class AppAlarmDataReception extends BaseController{
	@Autowired
	private DBAlarmService dbalarmService;
	@Autowired
	private DeviceCorpService deviceCorpService;

	@Autowired
	private NoticeCorpService noticecorpService;
	@Autowired
	private NoticeCorpUserService noticecorpuseridService;
	@Autowired
    private UsersService usersService;
	@Autowired
	private DevNoticeService devnoticeService;
	@Autowired
	private CorpInfoService corpInfoService;


	@RequestMapping(value="/getAlarmInfo")
	public void getAlarmInfo(@RequestBody DtoData dto) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
 		PageData pd = new PageData();
		pd = this.getPageData();
		System.out.println("---------------------------调用成功");
		if(null!=dto) {
			pd.put("DBALARM_ID", dto.getDBAlarmID());
			pd.put("ALARMID", dto.getAlarmID());
			pd.put("SCANNERALARMID", dto.getScannerAlarmID());
			pd.put("INTELLEXALARMID", dto.getIntellexAlarmID());
			pd.put("SCANNERCUSTOMERNUMBER", dto.getScannerCustomerNumber());
			pd.put("SCANNERFEEDNUMBER", dto.getScannerFeedNumber());
			pd.put("SCANNERIP", dto.ScannerIP);
			pd.put("VIDEOSOURCESYSTEMID", dto.getVideoSourceSystemID());
			pd.put("VIDEOSOURCECHANNELID", dto.getVideoSourceChannelID());
			pd.put("ALARMTYPE", dto.getAlarmType());
			pd.put("MANAGERSTARTTIME", dto.getManagerStartTime());
			pd.put("MANAGERALARMTIME",dto.getManagerAlarmTime());
			pd.put("MANAGERENDTIME", dto.getManagerEndTime());
			pd.put("VIDEOSOURCEALARMSTARTTIME", dto.getVideoSourceAlarmStartTime());
			pd.put("VIDEOSOURCEALARMTIME",  dto.getVideoSourceAlarmTime());

			pd.put("VIDEOSOURCEALARMENDTIME", dto.getVideoSourceAlarmEndTime());
			pd.put("JPBPATH", dto.getJPBPath());
			pd.put("THUMBNAIL", dto.getThumbnail());
			pd.put("THUMBNAILROWS", dto.getThumbnailRows());
			pd.put("THUMBNAILCOLS", dto.getThumbnailCols());
			pd.put("THUMBNAILSIZE", dto.getThumbnailSize());
			pd.put("THUMBNAILPATH", dto.getThumbnailPath());
			pd.put("SCANNERNAME", dto.getScannerName());
			pd.put("SPAREINT1", dto.getSpareInt1());
			pd.put("SPAREINT2", dto.getSpareInt2());
			pd.put("SPAREINT3", dto.getSpareInt3());
			pd.put("SPARESTR32_1", dto.getSpareStr32_1());
			pd.put("SPARESTR32_2", dto.getSpareStr32_2());
			pd.put("SPARESTR64_1", dto.getSpareStr64_1());
			pd.put("SPARESTR64_2", dto.getSpareStr64_2());
			pd.put("RULENAME", dto.getRuleName());
			pd.put("ALARMLEVELID", dto.getAlarmLevelID());
			pd.put("USERID", dto.getUserID());
			pd.put("ACKTIME", dto.getAckTime());
			pd.put("RULENAMESTR", dto.getRuleNameStr());
			pd.put("PTZNUMBER", dto.getPTZNumber());
			pd.put("SPECIESID", dto.getSpeciesID());
			pd.put("DVUID", dto.getDVUID());
			pd.put("DEVICEID", dto.getDeviceID());
			PageData corp = deviceCorpService.findByDevId(pd);
			pd.put("CORPINFO_ID", corp.get("CORPINFO_ID"));

			dbalarmService.save(pd);

			List<PageData> devnotice = devnoticeService.listAll(pd);
			if(devnotice != null && devnotice.size() == 1 && devnotice.get(0).get("ADDRESSEE") != null
					&& Tools.notEmpty(devnotice.get(0).get("ADDRESSEE").toString())) {
				String[] userList = devnotice.get(0).get("ADDRESSEE").toString().split(",");
				this.sendMes(userList, pd);
			}
		}
	}
	public Object sendMes(String[] userIds, PageData al) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("NOTICECORP_ID", this.get32UUID());	//主键
		pd.put("ISDELETE", "0");	//是否删除
		pd.put("ISTOP", "0");	//是否置顶（0否 1是）
		pd.put("STATUS", "0");	//状态（0：未发布   1：发布）
		PageData p = corpInfoService.findById(al);
		pd.put("USERNAME", p.getString("CORP_NAME"));					//用户名
		System.out.println(p.getString("CORP_NAME"));
		pd.put("CREATOR", p.getString("CORP_NAME"));						//发布人
		pd.put("CREATTIME", DateUtil.date2Str(new Date()));					//发布时间
		pd.put("CORP_INFO_ID", al.get("CORPINFO_ID"));
		pd.put("ISALL", "no");

		if(userIds.length>0) {
			List<Map<String, Object>> list = new ArrayList<>();
			Map<String , Object> map2 = new HashMap<>();
			for (int i = 0; i < userIds.length; i++) {
				map2 = new HashMap<>();
				map2.put("NOTICECORP_ID", pd.get("NOTICECORP_ID"));
				map2.put("USER_ID", userIds[i]);
				map2.put("NOTICECORPUSERID_ID", this.get32UUID());
				list.add(map2);
			}
			PageData pd2 = new PageData();
			pd2.put("list", list);
			noticecorpuseridService.saveAll(pd2);
		}
		pd.put("SYNOPSIS", "风险预警");
		pd.put("CONTENT", "<p>"+al.getString("SPARESTR64_1")+"于"+al.getString("MANAGERALARMTIME")+"触发"+al.getString("RULENAME")+"</p>");
		pd.put("DBALARM_ID",al.get("DBALARM_ID"));
		noticecorpService.save(pd);
		map.put("result", errInfo);
		return map;
	}
}
