package com.dcit.danger.controller;

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

import com.dcit.danger.model.*;
import com.dcit.danger.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
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.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.EnterpriseRegDTO;
import com.dcit.danger.dto.query.EnterpriseInfoQuery;
import com.dcit.danger.exception.IOCException;
import com.dcit.danger.model.EnterpriseGoodsInfoExample.Criteria;

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

@Api(value="数据统计API")
@RestController
@RequestMapping("dataStatistics")
@Slf4j
public class DataStatisticsController {
	
	@Autowired
	private EnterpriseInfoService enterpriseInfoService;
	
	@Autowired
	private EnterpriseGoodsService enterpriseGoodsService;
	
	@Autowired
	private SafetyDeviceInfoService safetyDeviceInfoService;

	@Autowired
	private THiddenTroubleService tHiddenTroubleService;
	
	@Autowired
	private AlarmInfoService alarmInfoService;
	
	
	@ApiOperation(value = "管理端数据统计")
	@PostMapping("/managerDataStatistics")
	public ResultVO<Object> managerDataStatistics(@RequestBody EnterpriseInfoQuery enterpriseInfoQuery){
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			List<EnterpriseRegDTO> regDtoList = enterpriseInfoService.selectEnterpriseRegInfo(enterpriseInfoQuery);
			
			int regNum = 0;
			int checkIngNum = 0;
			//int totalNum = 0;
//			List<String> list = new ArrayList<String>();
			List<String> regList = new ArrayList<String>();
			for(EnterpriseRegDTO dto : regDtoList){
				if(dto.getAccountStatus().equals("0") && dto.getIsEnable().equals("0")){
					regNum++;
					regList.add(dto.getEnterpriseId());
				}
				if(dto.getAccountStatus().equals("3")){
					checkIngNum++;
				}
				
//				list.add(dto.getEnterpriseId());
			}

			EnterpriseBasicInfoExample enterpriseBasicInfoExample = new EnterpriseBasicInfoExample();
			com.dcit.danger.model.EnterpriseBasicInfoExample.Criteria enterpriseBasicInfoCriteria = enterpriseBasicInfoExample.createCriteria();
			if(regList.size()>0){
				enterpriseBasicInfoCriteria.andIdIn(regList);
			}

			List<EnterpriseBasicInfo> enterpriseList = enterpriseInfoService.selectEnterpriseInfoByExample(enterpriseBasicInfoExample);

			EnterpriseGoodsInfoExample example = new EnterpriseGoodsInfoExample();
			Criteria criteria = example.createCriteria();
			//criteria.andIsBigDangerEqualTo("1");
			if(regList.size()>0){
				criteria.andEnterpriseIdIn(regList);
			}
			
			long dangerNum = enterpriseGoodsService.countByExample(example);
			List<EnterpriseGoodsInfo> goodsList = enterpriseGoodsService.selectByExample(example);
			
			SafetyDeviceInfoExample safetyDeviceInfoExample = new SafetyDeviceInfoExample();
			com.dcit.danger.model.SafetyDeviceInfoExample.Criteria safetyDeviceCriteria = safetyDeviceInfoExample.createCriteria();
			safetyDeviceCriteria.andDataTypeEqualTo("1").andIsDelEqualTo("0");
			if(regList.size()>0){
				safetyDeviceCriteria.andEnterpriseIdIn(regList);
			}
			
			long cameraNum = safetyDeviceInfoService.countByExample(safetyDeviceInfoExample);
			List<SafetyDeviceInfo> cameraList = safetyDeviceInfoService.selectByExample(safetyDeviceInfoExample);

			THiddenTroubleExample tHiddenTroubleExample = new THiddenTroubleExample();
			if(regList.size()>0){
				tHiddenTroubleExample.createCriteria().andEnterpriseIdIn(regList);
			}
			long troubleNum = tHiddenTroubleService.countByExample(tHiddenTroubleExample);
			List<THiddenTrouble> troubleList = tHiddenTroubleService.selectByExample(tHiddenTroubleExample);

			AlarmInfoExample alarmInfoExample = new AlarmInfoExample();
			com.dcit.danger.model.AlarmInfoExample.Criteria alarmInfoCriteria = alarmInfoExample.createCriteria();
			if(regList.size()>0){
				alarmInfoCriteria.andEnterpriseIdIn(regList);
			}
			
			long alarmTotal = alarmInfoService.countByExample(alarmInfoExample);
			
			alarmInfoCriteria.andAlarmStatusEqualTo("0");
			long alarmingNum = alarmInfoService.countByExample(alarmInfoExample);
			
			map.put("enterpriseRegNum", regNum);
			map.put("enterpriseCheckIngNum", checkIngNum);
			map.put("dangerNum", dangerNum);
			map.put("cameraNum", cameraNum);
			map.put("troubleNum", troubleNum);
			map.put("alarmTotal", alarmTotal);
			map.put("alarmingNum", alarmingNum);
			map.put("handleAlarmNum", alarmTotal - alarmingNum);
			map.put("statisticsTime", DateUtil.getCurrentDateStr(DateUtil.C_TIME_PATTON_DEFAULT));
			map.put("enterpriseList", enterpriseList);
			map.put("goodsList", goodsList);
			map.put("cameraList", cameraList);
			map.put("troubleList", troubleList);

		} catch (Exception e) {
			e.printStackTrace();
			log.error("【管理端数据统计】操作出错,error={}",e.getMessage());
			throw new IOCException(ResultEnum.OPERATION_FAIL);
		}
		
		
		return ResultVOUtil.success(map);
	}

	@ApiOperation(value = "数据统计")
	@PostMapping("/getDataStatistics")
	public ResultVO<Object> getDataStatistics(@RequestBody EnterpriseInfoQuery enterpriseInfoQuery){
		ModelMap modelMap = new ModelMap();
		try {
			List<EnterpriseRegDTO> list = new ArrayList<>();
			List<EnterpriseRegDTO> regDtoList = enterpriseInfoService.selectEnterpriseRegInfo(enterpriseInfoQuery);

			for(EnterpriseRegDTO dto : regDtoList){
				if(dto.getAccountStatus().equals("0") && dto.getIsEnable().equals("0")){
					EnterpriseGoodsInfoExample example = new EnterpriseGoodsInfoExample();
					example.createCriteria().andEnterpriseIdEqualTo(dto.getEnterpriseId());
					long dangerNum = enterpriseGoodsService.countByExample(example);
					dto.setDangerNum(dangerNum);

					THiddenTroubleExample troubleExample = new THiddenTroubleExample();
					troubleExample.createCriteria().andEnterpriseIdEqualTo(dto.getEnterpriseId());
					long troubleNum = tHiddenTroubleService.countByExample(troubleExample);
					dto.setTroubleNum(troubleNum);

					SafetyDeviceInfoExample safetyDeviceInfoExample = new SafetyDeviceInfoExample();
					safetyDeviceInfoExample.createCriteria().andEnterpriseIdEqualTo(dto.getEnterpriseId()).andDataTypeEqualTo("1").andIsDelEqualTo("0");
					long camera = safetyDeviceInfoService.countByExample(safetyDeviceInfoExample);
					dto.setCameraNum(camera);

					list.add(dto);
				}
			}
			PageInfo<EnterpriseRegDTO> pageInfoList = new PageInfo<EnterpriseRegDTO>(list);

			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("/enterpriseDataStatistics/{enterpriseId}")
	public ResultVO<Object> enterpriseDataStatistics(@PathVariable String enterpriseId){
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			EnterpriseGoodsInfoExample example = new EnterpriseGoodsInfoExample();
			Criteria criteria = example.createCriteria();
			criteria.andEnterpriseIdEqualTo(enterpriseId);
			
			long dangerNum = enterpriseGoodsService.countByExample(example);
			
			SafetyDeviceInfoExample safetyDeviceInfoExample = new SafetyDeviceInfoExample();
			safetyDeviceInfoExample.createCriteria().andDataTypeEqualTo("1").andEnterpriseIdEqualTo(enterpriseId);;
			
			long cameraNum = safetyDeviceInfoService.countByExample(safetyDeviceInfoExample);
			
			AlarmInfoExample alarmInfoExample = new AlarmInfoExample();
			alarmInfoExample.createCriteria().andEnterpriseIdEqualTo(enterpriseId);
			
			long alarmTotal = alarmInfoService.countByExample(alarmInfoExample);
			alarmInfoExample.clear();
			alarmInfoExample.createCriteria().andEnterpriseIdEqualTo(enterpriseId).andAlarmStatusEqualTo("0");
			long alarmingNum = alarmInfoService.countByExample(alarmInfoExample);
			
			map.put("dangerNum", dangerNum);
			map.put("cameraNum", cameraNum);
			map.put("alarmTotal", alarmTotal);
			map.put("alarmingNum", alarmingNum);
			map.put("handleAlarmNum", alarmTotal - alarmingNum);
			map.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(map);
	}

}
