package com.dcit.danger.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dcit.common.enums.ResultEnum;
import com.dcit.common.utils.DateUtil;
import com.dcit.common.utils.ResultVOUtil;
import com.dcit.common.vo.ResultVO;
import com.dcit.danger.dto.AlarmInfoDTO;
import com.dcit.danger.dto.query.AlarmInfoQuery;
import com.dcit.danger.exception.IOCException;
import com.dcit.danger.model.AlarmInfo;
import com.dcit.danger.model.AlarmInfoExample;
import com.dcit.danger.model.AlarmInfoExample.Criteria;
import com.dcit.danger.service.AlarmInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value="预警信息API")
@RestController
@RequestMapping("alarmInfo")
@Slf4j
public class AlarmInfoController {
	
	@Autowired
	private AlarmInfoService alarmInfoService;
	
	@ApiOperation(value = "查询预警列表")
	@PostMapping("/getAlarmInfoList")
	public ResultVO<Object> getAlarmInfoList(@RequestBody AlarmInfoQuery alarmInfoQuery){
		ModelMap modelMap = new ModelMap();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("enterpriseId", alarmInfoQuery.getEnterpriseId());
			map.put("alarmStatus", alarmInfoQuery.getAlarmStatus());
			
			PageHelper.offsetPage(alarmInfoQuery.getPage(), alarmInfoQuery.getSize());
			
			List<AlarmInfoDTO> dtoList = alarmInfoService.selectAlarmInfoList(map);
			
			PageInfo<AlarmInfoDTO> pageInfoList = new PageInfo<AlarmInfoDTO>(dtoList);
			modelMap.addAttribute("pageData", pageInfoList.getList());
			modelMap.addAttribute("total", pageInfoList.getTotal());
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error("【查询报警列表】操作出错,error={}",e.getMessage());
			throw new IOCException(ResultEnum.OPERATION_FAIL);
		}
		
		return ResultVOUtil.success(modelMap);
	}
	
	@ApiOperation(value = "统计预警数量")
	@GetMapping("/getAlarmNum")
	public ResultVO<Object> getAlarmNum(@RequestParam(required=false,defaultValue="") String enterpriseId){
		ModelMap modelMap = new ModelMap();
		try {
			
			AlarmInfoExample example = new AlarmInfoExample();
			Criteria criteria = example.createCriteria();
			if(!StringUtils.isEmpty(enterpriseId)){
				criteria.andEnterpriseIdEqualTo(enterpriseId);
			}
			List<AlarmInfo> alarmTotalList = alarmInfoService.selectByExample(example);
			
			criteria.andAlarmStatusEqualTo("0");
			
			List<AlarmInfo> alarmIngList = alarmInfoService.selectByExample(example);
			
			modelMap.addAttribute("alarmTotal", alarmTotalList.size());
			modelMap.addAttribute("alarmIng", alarmIngList.size());
			modelMap.addAttribute("alarmDone", alarmTotalList.size() - alarmIngList.size());
			modelMap.put("statisticsTime", DateUtil.getCurrentDateStr(DateUtil.C_TIME_PATTON_DEFAULT));
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error("【统计预警数量】操作出错,error={}",e.getMessage());
			throw new IOCException(ResultEnum.OPERATION_FAIL);
		}
		
		return ResultVOUtil.success(modelMap);
	}
	
	@ApiOperation(value = "手动处理预警信息")
	@GetMapping("/handleAlarmInfo/{id}")
	public ResultVO<Object> handleAlarmInfo(@PathVariable String id){
		try {
			AlarmInfo alarmInfo = alarmInfoService.selectByPrimaryKey(id);
			if(null != alarmInfo){
				alarmInfo.setAlarmStatus("1");
				alarmInfo.setHandleTime(new Date());
				alarmInfo.setHandleType("2");
				//alarmInfo.setHandleUserId(null);
				
				alarmInfoService.updateByPrimaryKeySelective(alarmInfo);
			}else{
				log.error("【处理预警信息】操作出错,预警信息不存在");
				throw new IOCException(ResultEnum.NOT_EXIST);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("【处理预警信息】操作出错,error={}",e.getMessage());
			throw new IOCException(ResultEnum.OPERATION_FAIL);
		}
		return ResultVOUtil.success();
	}

}
