package com.dcit.danger.controller;

import com.dcit.common.enums.ResultEnum;
import com.dcit.common.utils.BeanCopyUtil;
import com.dcit.common.utils.DateUtil;
import com.dcit.common.utils.KeyUtil;
import com.dcit.common.utils.ResultVOUtil;
import com.dcit.common.vo.ResultVO;
import com.dcit.danger.dto.THiddenTroubleDTO;
import com.dcit.danger.dto.form.THiddenTroubleForm;
import com.dcit.danger.exception.IOCException;
import com.dcit.danger.model.THiddenTrouble;
import com.dcit.danger.model.THiddenTroubleExample;
import com.dcit.danger.model.THiddenTroubleExample.Criteria;
import com.dcit.danger.service.THiddenTroubleService;
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;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(value="隐患信息API")
@RestController
@RequestMapping("hiddenTrouble")
@Slf4j
public class THiddenTroubleController {
	
	@Autowired
	private THiddenTroubleService hiddenTroubleService;

	@ApiOperation(value = "按企业统计隐患数量")
	@GetMapping("/getNumberByEnterprise")
	public ResultVO<Object> getNumberByEnterprise(){

		ModelMap modelMap = new ModelMap();
		List<Map<Object, Object>> newList = new ArrayList<>();

		List<Map<Object, Object>> list = hiddenTroubleService.countByEnterprise();
		if(list.size() == 0){
			log.error("【按企业统计隐患数量】出错");
			throw new IOCException(ResultEnum.OPERATION_FAIL);
		}

		//显示危险源数量最多的前5个企业，其余显示为其他
		if(list.size() > 6) {
			Integer count = 0,x = 0;

			for (Map<Object, Object> map : list) {

				if(x < 5){
					newList.add(map);
				}else{
					count += Integer.valueOf(map.get("number").toString());
				}

				x++;
			}

			Map<Object, Object> map = new HashMap<>();
			map.put("name", "其他");
			map.put("number", count);
			newList.add(map);
		}else{
			newList.addAll(list);
		}

		modelMap.addAttribute("result", newList);
		return ResultVOUtil.success(modelMap);
	}

	@ApiOperation(value = "查询危险源信息")
	@GetMapping("/getMapHiddenTroubleById/{id}")
	public ResultVO<Object> getMapHiddenTroubleById(@PathVariable String id){
		THiddenTrouble hiddenTroubleInfo = hiddenTroubleService.selectByPrimaryKey(id);

		THiddenTroubleDTO dto = new THiddenTroubleDTO();
		BeanUtils.copyProperties(hiddenTroubleInfo, dto, BeanCopyUtil.getNullOrIgnorePropertyNames(hiddenTroubleInfo, null));

		return ResultVOUtil.success(hiddenTroubleInfo);
	}

	@ApiOperation(value = "修改危险源信息")
	@PostMapping("/editMapHiddenTrouble")
	public ResultVO<Object> editMapHiddenTrouble(@RequestBody THiddenTroubleForm hiddenTroubleForm){
		THiddenTrouble hiddenTroubleInfo = hiddenTroubleService.selectByPrimaryKey(hiddenTroubleForm.getId());

		if(hiddenTroubleInfo != null){
			BeanUtils.copyProperties(hiddenTroubleForm, hiddenTroubleInfo,BeanCopyUtil.getNullOrIgnorePropertyNames(hiddenTroubleForm, null));
			hiddenTroubleService.updateByPrimaryKey(hiddenTroubleInfo);
		}
		return ResultVOUtil.success(hiddenTroubleInfo);
	}
}
