package com.dcit.danger.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.authority.model.SysParam;
import com.dcit.authority.service.SysParamService;
import com.dcit.common.enums.ResultEnum;
import com.dcit.common.utils.BeanCopyUtil;
import com.dcit.common.utils.KeyUtil;
import com.dcit.common.utils.ResultVOUtil;
import com.dcit.common.vo.ResultVO;
import com.dcit.danger.dto.form.SysCodeForm;
import com.dcit.danger.dto.node.BuildCodeTreeNode;
import com.dcit.danger.dto.node.SysCodeTreeNode;
import com.dcit.danger.exception.IOCException;
import com.dcit.danger.model.SysCode;
import com.dcit.danger.model.SysCodeExample;
import com.dcit.danger.service.SystemConfigService;

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

@Api(value="系统配置相关接口")
@RestController
@RequestMapping("systemConfig")
@Slf4j
public class SystemConfigController {
	
	@Autowired
	private SystemConfigService systemConfigService;
	
	@Autowired
	private SysParamService sysParamService;
	
	
	@ApiOperation(value = "获取系统数据字典树")
    @GetMapping("/getSystemCodeTree")
	public ResultVO<Object> getSystemCodeTree(){
		
		List<SysCode> list = systemConfigService.selectSystemCodeByExample(null);
		
		List<SysCodeTreeNode> sysCodeTree = null;
		
		try {
			
			List<SysCodeTreeNode> sysCodeList = new ArrayList<SysCodeTreeNode>();
			
			SysCodeTreeNode sysCodeTreeNode = null;
			
			for(SysCode sysCode : list){
				sysCodeTreeNode = new SysCodeTreeNode();
				BeanUtils.copyProperties(sysCode, sysCodeTreeNode, BeanCopyUtil.getNullOrIgnorePropertyNames(sysCode, null));
				sysCodeList.add(sysCodeTreeNode);
			}
			
			sysCodeTree = BuildCodeTreeNode.buildTree(sysCodeList);
			
		} catch (Exception e) {
			log.error("【获取系统数据字典树】操作失败,e={}",e.getMessage());
			throw new IOCException(ResultEnum.OPERATION_FAIL);
		}
		
		//前端添加了
		SysCodeTreeNode topNode = new SysCodeTreeNode();
		topNode.setId("0");
		topNode.setParentId("-1");
		topNode.setType("1");
		topNode.setName("顶级");
		topNode.setCode("-1");
		topNode.setChildList(sysCodeTree);
		
		return ResultVOUtil.success(topNode);
		
	}
	
	@ApiOperation(value = "根据父类id查询数据字典列表")
    @GetMapping("/getSystemCodeByParentId/{parentId}")
	public ResultVO<Object> getSystemCodeByParentId(@PathVariable String parentId){
		SysCodeExample sysCodeExample = new SysCodeExample();
		sysCodeExample.createCriteria().andParentIdEqualTo(parentId);
		
		List<SysCode> sysCodeList = systemConfigService.selectSystemCodeByExample(sysCodeExample);
		
		return ResultVOUtil.success(sysCodeList);
		
	}
	
	@ApiOperation(value = "根据id获取系统数据字典详情")
    @GetMapping("/getSystemCodeById/{id}")
	public ResultVO<Object> getSystemCodeById(@PathVariable String id){
		
		SysCode sysCode = systemConfigService.selectSystemCodeById(id);
		
		return ResultVOUtil.success(sysCode);
		
	}
	
	
	@ApiOperation(value = "新增或编辑数据字典")
    @PostMapping("/addOrEditSystemCode")
	public ResultVO<Object> addOrEditSystemCode(@RequestBody SysCodeForm sysCodeForm, BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			log.error("【新增或编辑数据字典】参数不正确,sysCodeForm={}",sysCodeForm);
			throw new IOCException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
		}
		
		SysCode sysCode = null;
		
		try {
			if(StringUtils.isEmpty(sysCodeForm.getId())){
				sysCode = new SysCode();
				BeanUtils.copyProperties(sysCodeForm, sysCode, BeanCopyUtil.getNullOrIgnorePropertyNames(sysCodeForm, null));
				sysCode.setId(KeyUtil.getUUIDKey());
				
				systemConfigService.addSystemCode(sysCode);
			}else{
				sysCode = systemConfigService.selectSystemCodeById(sysCodeForm.getId());
				if(null != sysCode){
					BeanUtils.copyProperties(sysCodeForm, sysCode, BeanCopyUtil.getNullOrIgnorePropertyNames(sysCodeForm, null));
					
					systemConfigService.updateSystemCodeById(sysCode);
				}else{
					log.error("【编辑数据字典】操作出错,记录不存在,sysCode={}, sysCodeForm={}",sysCode, sysCodeForm);
					throw new IOCException(ResultEnum.NOT_EXIST);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("【新增编辑数据字典】操作出错,error={},sysCodeForm={}",e.getMessage(),sysCodeForm);
			throw new IOCException(ResultEnum.OPERATION_FAIL);
		}
		
		return ResultVOUtil.success(sysCode);
	}
	
	@ApiOperation(value = "根据id删除系统数据字典")
    @GetMapping("/delSystemCodeById/{id}")
	public ResultVO<Object> delSystemCodeById(@PathVariable String id){
		
		try {
			systemConfigService.deleteSystemCodeById(id);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("【删除系统数据字典】操作出错,error={}",e.getMessage());
			throw new IOCException(ResultEnum.OPERATION_FAIL);
		}
		
		return ResultVOUtil.success();
	}
	
	@ApiOperation(value = "查询系统名称")
	@GetMapping("/getSysTitle")
	public ResultVO<Object> getSysTitle(){
		SysParam sysParam = sysParamService.selectByName("systemName");
		return ResultVOUtil.success(sysParam);
	}
}
