/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.security.trans.basic.controller;

import io.swagger.annotations.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.security.ht.entity.basic.Department;
import org.security.system.entity.Dept;
import org.security.system.vo.DeptVO;
import org.security.trans.basic.dto.DepartmentDTO;
import org.security.ht.vo.basic.DeptDataVO;
import org.security.ht.vo.basic.MapDeptDataVO;
import org.security.ht.vo.basic.RoleDataVO;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.security.ht.vo.basic.DepartmentVO;
import org.security.trans.basic.service.IDepartmentService;
import org.springblade.core.boot.ctrl.BladeController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * 基础信息-组织机构 控制器
 *
 * @author BladeX
 * @since 2022-03-07
 */
@RestController
@AllArgsConstructor
@RequestMapping("/department")
@Api(value = "基础信息-组织机构", tags = "基础信息-组织机构接口")
public class DepartmentController extends BladeController {

	private final IDepartmentService departmentService;

	/**
	 * 详情
	 * create:qx
	 */
	@GetMapping("/detail/{id}")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入department")
	public R<DeptDataVO> detail(@PathVariable Long id) {
		return R.data(departmentService.getDepartmentById(id));
	}

	/**
	 * 自定义分页 基础信息-组织机构
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入department")
	public R<IPage<DepartmentVO>> page(DepartmentVO department, Query query) {
		IPage<DepartmentVO> pages = departmentService.selectDepartmentPage(Condition.getPage(query), department);
		return R.data(pages);
	}

	/**
	 * 修改 基础信息-组织机构(完善组织机构信息)
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入departmentDTO")
	public R update(@Valid @RequestBody DepartmentDTO departmentDTO) {
		return R.status(departmentService.updateDeptData(departmentDTO));
	}
	/**
	 * 新增 基础信息-组织机构(用于新增自助行)
	 */
	@PostMapping("/addSelfBank")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "新增", notes = "传入departmentDTO")
	public R addSelfBank(@Valid @RequestBody DepartmentDTO departmentDTO) {
		return departmentService.addSelfBank(departmentDTO);
	}

	/**
	 * 删除 基础信息-组织机构
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(departmentService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 懒加载列表
	 * create:qx
	 */
	@GetMapping("/lazy-list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "deptName", value = "部门名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "deptLevel", value = "部门层级", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "懒加载列表", notes = "传入dept")
	public R<List<DepartmentVO>> lazyList(@ApiIgnore @RequestParam Map<String, Object> dept, String parentId) {
		List<DepartmentVO> list = departmentService.lazyList(dept, parentId);
		return R.data(list);
	}

	/**
	 * 联级选择器树形结构
	 *create:qx
	 * @return
	 */
	@GetMapping("/cascade")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	public R<List<DepartmentVO>> cascadeSelection() {
		List<DepartmentVO> list = departmentService.cascadeSelection();
		return R.data(list);
	}


	/**
	 * 联级选择器树形结构
	 *create:qx
	 * @return
	 */
	@GetMapping("/child-dept-tree")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	public R<List<DepartmentVO>> getChildDeptTree(String topDeptId) {
		List<DepartmentVO> list = departmentService.getChildDeptTree(topDeptId);
		return R.data(list);
	}

	/**
	 * 部门树
	 * create:qx
	 */
	@GetMapping("/tree")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "部门树", notes = "传入dept")
	public R<List<DepartmentVO>> tree(DepartmentDTO departmentDTO) {
		List<DepartmentVO> list = departmentService.tree(departmentDTO);
		return R.data(list);
	}

	/**
	 * 根据部门id获取相关角色id
	 */
	@GetMapping("/data")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "根据部门id获取相关角色id", notes = "传入deptId")
	public R<List<RoleDataVO>> detail(String deptId) {
		return R.data(departmentService.getData(deptId));
	}


	/**
	 * 获取机构地图数据
	 */
	@GetMapping("/map")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "获取机构地图数据", notes = "获取机构地图数据")
	public R<List<MapDeptDataVO>> getDeptMapDetail() {
		return R.data(departmentService.getDeptMapDetail());
	}

	/**
	 * 消息总线信息同步，更新机构信息
	 */
	@PostMapping("/syn/save/department")
	public R saveDepartmentData(@RequestBody List<Department> departmentList){
		return R.status(departmentService.saveDepartmentData(departmentList));
	}

	/**
	 * 懒加载列表 for下发任务
	 */
	@GetMapping("/lazy-list-for-task")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "deptName", value = "部门名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "deptLevel", value = "部门层级", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "懒加载列表", notes = "传入dept")
	public R<List<DepartmentVO>> lazyListForTask(@ApiIgnore @RequestParam Map<String, Object> dept, String parentId) {
		List<DepartmentVO> list = departmentService.lazyListForTask(dept, parentId);
		return R.data(list);
	}

	/**
	 * 自助银行列表
	 */
	@GetMapping("/self-bank-list")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "自助银行列表", notes = "")
	public R<List<DepartmentVO>> getSelfBankData() {
		return R.data(departmentService.getSelfBankData());
	}


	/**
	 * 获取总行营业部室----项目预算总行管理部门
	 */
	@GetMapping("/budget-dept")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "获取总行营业部室----项目预算总行管理部门", notes = "")
	public R<List<Department>> getBudgetDept() {
		return R.data(departmentService.getBudgetDept());
	}

}
