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.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.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.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.PlanInfoDTO;
import com.dcit.danger.dto.form.PlanInfoForm;
import com.dcit.danger.dto.query.PlanInfoQuery;
import com.dcit.danger.exception.IOCException;
import com.dcit.danger.model.PlanInfo;
import com.dcit.danger.model.PlanInfoExample;
import com.dcit.danger.model.PlanInfoExample.Criteria;
import com.dcit.danger.service.PlanInfoService;
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("planInfo")
@Slf4j
public class PlanInfoController {
	
	@Autowired
	private PlanInfoService planInfoService;
	
	@ApiOperation(value = "查询企业预案列表")
	@PostMapping("/selectPlanInfoList")
	public ResultVO<Object> selectPlanInfoList(@RequestBody PlanInfoQuery planInfoQuery){
		
		ModelMap modelMap = new ModelMap();
		try {
			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("enterpriseId", planInfoQuery.getEnterpriseId());
			map.put("planLevel", planInfoQuery.getPlanLevel());
			map.put("planType", planInfoQuery.getPlanType());
			map.put("planTitle", planInfoQuery.getPlanTitle());
			map.put("isReported", planInfoQuery.getIsReported());
			
			
			PageHelper.offsetPage(planInfoQuery.getPage(), planInfoQuery.getSize());
			
			/*PlanInfoExample planInfoExample = new PlanInfoExample();
			Criteria criteria = planInfoExample.createCriteria();
			
			if(!StringUtils.isEmpty(planInfoQuery.getEnterpriseId())){
				criteria.andEnterpriseIdEqualTo(planInfoQuery.getEnterpriseId());
			}
			
			if(!StringUtils.isEmpty(planInfoQuery.getPlanTitle())){
				criteria.andPlanTitleLike("%"+ planInfoQuery.getPlanTitle() +"%");
			}
			
			if(!StringUtils.isEmpty(planInfoQuery.getIsReported())){
				criteria.andIsReportedEqualTo(planInfoQuery.getIsReported());
			}
			
			List<PlanInfo> planInfoList = planInfoService.selectByExample(planInfoExample);*/
			
			List<PlanInfoDTO> dtoList = planInfoService.selectPlanInfoList(map);
			
			PageInfo<PlanInfoDTO> pageList = new PageInfo<PlanInfoDTO>(dtoList);
			
			
			
			modelMap.addAttribute("pageData", pageList.getList());
			modelMap.addAttribute("total", pageList.getTotal());
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error("【查询企业预案列表】操作出错,error={}",e.getMessage());
			throw new IOCException(ResultEnum.OPERATION_FAIL);
		}
		
		return ResultVOUtil.success(modelMap);
		
	}
	
	@ApiOperation(value = "查询企业预案信息")
	@GetMapping("/getPlanInfoById/{id}")
	public ResultVO<Object> getPlanInfoById(@PathVariable String id){
		PlanInfo planInfo = planInfoService.selectByPrimaryKey(id);
		
		PlanInfoDTO dto = new PlanInfoDTO();
		BeanUtils.copyProperties(planInfo, dto, BeanCopyUtil.getNullOrIgnorePropertyNames(planInfo, null)); 
		
		return ResultVOUtil.success(dto);
	}
	
	@ApiOperation(value = "新增编辑企业预案")
	@PostMapping("/addOrEditPlanInfo")
	public ResultVO<Object> addOrEditPlanInfo(@RequestBody PlanInfoForm planInfoForm, BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			log.error("【新增编辑企业预案】参数不正确, planInfoForm={}",planInfoForm);
			throw new IOCException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
		}
		PlanInfo planInfo = null;
		
		PlanInfoDTO dto = new PlanInfoDTO();
		
		try {
			if(StringUtils.isEmpty(planInfoForm.getId())){
				planInfo = new PlanInfo();
				BeanUtils.copyProperties(planInfoForm, planInfo, BeanCopyUtil.getNullOrIgnorePropertyNames(planInfoForm, null));
				
				planInfo.setId(KeyUtil.getUUIDKey());
				
				if(!StringUtils.isEmpty(planInfoForm.getReportedTime())){
					planInfo.setReportedTime(DateUtil.parseDate(DateUtil.C_TIME_PATTON_DEFAULT, planInfoForm.getReportedTime()));
				}
				if(!StringUtils.isEmpty(planInfoForm.getCheckTime())){
					planInfo.setCheckTime(DateUtil.parseDate(DateUtil.C_TIME_PATTON_DEFAULT, planInfoForm.getCheckTime()));
				}
				
				planInfoService.insertSelective(planInfo);
			}else{
				planInfo = planInfoService.selectByPrimaryKey(planInfoForm.getId());
				if(null != planInfo){
					if("2".equals(planInfo.getCheckStatus())){
						planInfo.setCheckStatus("0");
						planInfo.setRemarks("");
					}
					BeanUtils.copyProperties(planInfoForm, planInfo, BeanCopyUtil.getNullOrIgnorePropertyNames(planInfoForm, null));
					
					if(!StringUtils.isEmpty(planInfoForm.getReportedTime())){
						planInfo.setReportedTime(DateUtil.parseDate(DateUtil.C_TIME_PATTON_DEFAULT, planInfoForm.getReportedTime()));
					}
					if(!StringUtils.isEmpty(planInfoForm.getCheckTime())){
						planInfo.setCheckTime(DateUtil.parseDate(DateUtil.C_TIME_PATTON_DEFAULT, planInfoForm.getCheckTime()));
					}
					planInfo.setUpdateTime(new Date());
					
					planInfoService.updateByPrimaryKeySelective(planInfo);
				}else{
					log.error("【编辑企业预案】操作出错,企业预案不存在");
					throw new IOCException(ResultEnum.OPERATION_FAIL);
				}
			}
			
			BeanUtils.copyProperties(planInfo, dto, BeanCopyUtil.getNullOrIgnorePropertyNames(planInfo, null));
		} catch (Exception e) {
			e.printStackTrace();
			log.error("【新增编辑企业预案】操作出错,error={}",e.getMessage());
			throw new IOCException(ResultEnum.OPERATION_FAIL);
		}
		return ResultVOUtil.success(dto);
	}
	
	
	@ApiOperation(value = "删除企业预案")
	@GetMapping("/deletePlanInfoById/{id}")
	public ResultVO<Object> deletePlanInfoById(@PathVariable String id){
		try {
			planInfoService.deleteByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("【删除企业预案】操作出错,error={}",e.getMessage());
			throw new IOCException(ResultEnum.OPERATION_FAIL);
		}
		
		return ResultVOUtil.success();
	}

}
