package org.springblade.flow.permission.controller;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import org.springblade.flow.base.ActionResult;
import org.springblade.flow.base.vo.ListVO;
import org.springblade.flow.database.exception.DataException;
import org.springblade.flow.permission.entity.OrganizeEntity;
import org.springblade.flow.permission.entity.PositionEntity;
import org.springblade.flow.permission.model.position.PosOrgModel;
import org.springblade.flow.permission.model.position.PositionInfoVO;
import org.springblade.flow.permission.model.position.PositionListAllVO;
import org.springblade.flow.permission.model.position.PositionSelectorVO;
import org.springblade.flow.permission.service.OrganizeService;
import org.springblade.flow.permission.service.PositionService;
import org.springblade.flow.util.JsonUtil;
import org.springblade.flow.util.JsonUtilEx;
import org.springblade.flow.util.treeutil.SumTree;
import org.springblade.flow.util.treeutil.newtreeutil.TreeDotUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 *  数据处理
 *
 * @author william
 * @date   2022-07-26 16:35:54
 */
@Api(tags = "岗位管理", value = "Position")
@RestController
@RequestMapping("/paas/permission/Position")
public class PositionController {
	@Autowired
	private PositionService positionService;
	@Autowired
	private OrganizeService organizeService;

	@ApiOperation("列表")
	@GetMapping("/All")
	public ActionResult<ListVO<PositionListAllVO>> listAll() {
		List<PositionEntity> list = positionService.getList().stream()
				.filter(t -> "1".equals(String.valueOf(t.getEnabledMark()))).collect(Collectors.toList());
		List<PositionListAllVO> vos = JsonUtil.getJsonToList(list, PositionListAllVO.class);
		ListVO<PositionListAllVO> vo = new ListVO<>();
		vo.setList(vos);
		return ActionResult.success(vo);
	}

	/**
	 * 树形（机构+岗位）
	 *
	 * @return
	 */
	@ApiOperation("获取岗位下拉列表（公司+部门+岗位）")
	@GetMapping("/Selector")
	public ActionResult selector() {
		List<PositionEntity> positionData = positionService.getList().stream()
				.filter(t -> "1".equals(String.valueOf(t.getEnabledMark()))).collect(Collectors.toList());
		List<OrganizeEntity> allOrganizeData = organizeService.getList();
		List<OrganizeEntity> organizeData = organizeService.getList().stream()
				.filter(t -> "0".equals(String.valueOf(t.getIsDeleted()))).collect(Collectors.toList());
		List<PosOrgModel> posOrgModels = JsonUtil.getJsonToList(organizeData, PosOrgModel.class);
		for (PosOrgModel entity1 : posOrgModels) {
			if ("department".equals(entity1.getType())) {
				entity1.setIcon("icon-ym icon-ym-tree-department1");
			} else if ("company".equals(entity1.getType())) {
				entity1.setIcon("icon-ym icon-ym-tree-organization3");
			}
		}
		for (PositionEntity entity : positionData) {
			PosOrgModel posOrgModel = JsonUtil.getJsonToBean(entity, PosOrgModel.class);
			posOrgModel.setParentId(entity.getId());
			posOrgModel.setType("position");
			posOrgModel.setIcon("icon-ym icon-ym-tree-position1");
			posOrgModels.add(posOrgModel);
		}
		List<SumTree<PosOrgModel>> trees = TreeDotUtils.convertListToTreeDot(posOrgModels);
		List<PositionSelectorVO> listVO = JsonUtil.getJsonToList(trees, PositionSelectorVO.class);
		List<OrganizeEntity> entities = allOrganizeData.stream()
				.filter(t -> "1".equals(String.valueOf(t.getIsDeleted())) && "0".equals(t.getParentId()))
				.collect(Collectors.toList());
		Iterator<PositionSelectorVO> iterator = listVO.iterator();
		while (iterator.hasNext()) {
			PositionSelectorVO positionSelectorVO = iterator.next();
			for (OrganizeEntity entity : entities) {
				if (entity.getId().equals(positionSelectorVO.getParentId())) {
					// 使用迭代器的删除方法删除
					iterator.remove();
				}
			}
		}
		ListVO vo = new ListVO();
		vo.setList(listVO);
		return ActionResult.success(vo);
	}

	/**
	 * 获取岗位管理信息
	 *
	 * @param id 主键值
	 * @return
	 */
	@ApiOperation("获取岗位管理信息")
	@GetMapping("/{id}")
	public ActionResult<PositionInfoVO> getInfo(@PathVariable("id") String id) throws DataException {
		PositionEntity entity = positionService.getInfo(id);
		PositionInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, PositionInfoVO.class);
		return ActionResult.success(vo);
	}

}
