/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.criterion.web;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.criterion.entity.EvaluationCriterion;
import com.jeeplus.modules.criterion.service.EvaluationCriterionService;

/**
 * criterionController
 * @author 邱悦
 * @version 2021-12-28
 */
@RestController
@RequestMapping(value = "/criterion/evaluationCriterion")
public class EvaluationCriterionController extends BaseController {

	@Autowired
	private EvaluationCriterionService evaluationCriterionService;

	@ModelAttribute
	public EvaluationCriterion get(@RequestParam(required=false) String id) {
		EvaluationCriterion entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = evaluationCriterionService.get(id);
		}
		if (entity == null){
			entity = new EvaluationCriterion();
		}
		return entity;
	}


	/**
	 * 评价标准树表数据
	 */
	@RequiresPermissions("criterion:evaluationCriterion:list")
	@GetMapping("list")
	public AjaxJson list(EvaluationCriterion evaluationCriterion) {
		return AjaxJson.success().put("list", evaluationCriterionService.findList(evaluationCriterion));
	}

	/**
	 * 根据Id获取评价标准数据
	 */
	@RequiresPermissions(value={"criterion:evaluationCriterion:view","criterion:evaluationCriterion:add","criterion:evaluationCriterion:edit"},logical=Logical.OR)
	@GetMapping("queryById")
	public AjaxJson queryById(EvaluationCriterion evaluationCriterion) {
		return AjaxJson.success().put("evaluationCriterion", evaluationCriterion);
	}

	/**
	 * 保存评价标准
	 */
	@RequiresPermissions(value={"criterion:evaluationCriterion:add","criterion:evaluationCriterion:edit"},logical=Logical.OR)
	@PostMapping("save")
	public AjaxJson save(EvaluationCriterion evaluationCriterion, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(evaluationCriterion);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		evaluationCriterionService.save(evaluationCriterion);//保存
		return AjaxJson.success("保存评价标准成功");
	}

	/**
	 * 删除评价标准
	 */
	@RequiresPermissions("criterion:evaluationCriterion:del")
	@DeleteMapping("delete")
	public AjaxJson delete(EvaluationCriterion evaluationCriterion) {
		evaluationCriterionService.delete(evaluationCriterion);
		return AjaxJson.success("删除评价标准成功");
	}

	/**
	     * 获取JSON树形数据。
	     * @param extId 排除的ID
	     * @return
	*/
	@RequiresPermissions("user")
	@GetMapping("treeData")
	public AjaxJson treeData(@RequestParam(required = false) String extId) {
		List<EvaluationCriterion> list = evaluationCriterionService.findList(new EvaluationCriterion());
		List rootTree = getEvaluationCriterionTree(list, extId);
		return AjaxJson.success().put("treeData", rootTree);
	}

	private List<EvaluationCriterion> getEvaluationCriterionTree(List<EvaluationCriterion> list, String extId) {
		List<EvaluationCriterion> evaluationCriterions = Lists.newArrayList();
		List<EvaluationCriterion> rootTrees = evaluationCriterionService.getChildren("0");
		for (EvaluationCriterion root : rootTrees) {
		    if (StringUtils.isBlank(extId) ||  !extId.equals(root.getId())) {
		        evaluationCriterions.add(getChildOfTree(root, list, extId));
		    }
		}
		return evaluationCriterions;
	}

	private EvaluationCriterion getChildOfTree(EvaluationCriterion evaluationCriterion, List<EvaluationCriterion> evaluationCriterionList, String extId) {
		evaluationCriterion.setChildNodes(Lists.newArrayList());
		for (EvaluationCriterion child : evaluationCriterionList) {
		    if (StringUtils.isBlank(extId) ||  (!extId.equals(child.getId()) && child.getParentIds().indexOf("," + extId + ",") == -1)) {
		        if (child.getParentId().equals(evaluationCriterion.getId())) {
		            evaluationCriterion.getChildNodes().add(getChildOfTree(child, evaluationCriterionList, extId));
		        }
		    }
		}
		return evaluationCriterion;
	}



}