/**
 * Copyright &copy; 2021-2026 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.jeeplus.aop.demo.annotation.DemoMode;
import com.jeeplus.common.constant.CommonConstants;
import com.jeeplus.core.dto.DragNode;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.sys.domain.Office;
import com.jeeplus.sys.service.OfficeService;
import com.jeeplus.sys.service.dto.OfficeDTO;
import com.jeeplus.sys.service.mapstruct.OfficeWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 机构Controller
 *
 * @author jeeplus
 * @version 2021-5-15
 */
@RestController
@RequestMapping("/sys/office")
public class OfficeController {

    @Autowired
    private OfficeService officeService;

    @Autowired
    private OfficeWrapper officeWrapper;

    @ApiLog("获取二级节点数据信息")
    @GetMapping("getSecondLevelData")
    public ResponseEntity getSecondLevelData() {
        List<OfficeDTO> list = officeService.getSecondLevelData ();
        return ResponseEntity.ok (list);
    }

    /**
     * 根据id查询部门
     *
     * @param id
     * @return
     */
    @ApiLog("查询部门")
    // @PreAuthorize("hasAnyAuthority('sys:office:view','sys:office:add','sys:office:edit')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam String id) {
        OfficeDTO officeDTO = officeWrapper.toDTO ( officeService.getById ( id ) );
        return ResponseEntity.ok ( officeDTO );
    }

    /**
     * 保存或者修改部门
     *
     * @param officeDTO
     * @return
     */
    @DemoMode
    @ApiLog("保存部门")
    @PreAuthorize("hasAnyAuthority('sys:office:add','sys:office:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody OfficeDTO officeDTO) {
        Office office = officeWrapper.toEntity ( officeDTO );
        officeService.saveOrUpdate ( office );
        return ResponseEntity.ok ( office );
    }

    /**
     * 移动机构节点
     *
     * @param dragDTO
     * @return
     */
    @PostMapping("drag")
    public ResponseEntity drag(@Valid @RequestBody DragNode <OfficeDTO> dragDTO) {
        officeService.sortList ( officeWrapper.toEntity ( dragDTO ) );
        return ResponseEntity.ok ( "移动节点成功!" );
    }


    /**
     * 删除部门
     *
     * @param ids
     * @return
     */
    @DemoMode
    @ApiLog("删除部门")
    @PreAuthorize("hasAuthority('sys:office:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        String idArray[] = ids.split ( "," );
        officeService.removeWithChildrenByIds ( Lists.newArrayList ( idArray ) );
        return ResponseEntity.ok ( "删除成功！" );
    }


    /**
     * 获取机构JSON数据。
     *
     * @param extId   排除的ID
     * @param type    类型（1：公司；2：部门）
     * @param showAll 是否显示不可用数据 1 显示 0 隐藏
     * @return
     */
    @ApiLog("获取部门数据")
    @GetMapping("treeData")
    public ResponseEntity <List <OfficeDTO>> treeData(@RequestParam(required = false) String tenantId,
                                                      @RequestParam(required = false) String parentId,
                                                      @RequestParam(required = false) String extId,
                                                      @RequestParam(required = false) String type,
                                                      @RequestParam(required = false, defaultValue = CommonConstants.NO) String showAll) {
        List <OfficeDTO> list = officeService.findList ( tenantId, parentId );
        //List <OfficeDTO> rootTree = officeService.getRootTree (list, extId, type, showAll,"1" );
        List <OfficeDTO> rootTree = officeService.getOldRootTree (parentId,list, extId, type, showAll );
        return ResponseEntity.ok ( rootTree );
    }

    /**
     * 获取机构JSON数据(评估,会计)。
     *
     * @param extId   排除的ID
     * @param type    类型（1：公司；2：部门）
     * @param showAll 是否显示不可用数据 1 显示 0 隐藏
     * @return
     */
    @ApiLog("获取部门数据(评估,会计)")
    @GetMapping("treeDataPgAndKj")
    public ResponseEntity <List <OfficeDTO>> treeDataPgAndKj(@RequestParam(required = false) String tenantId,
                                                      @RequestParam(required = false) String parentId,
                                                      @RequestParam(required = false) String extId,
                                                      @RequestParam(required = false) String type,
                                                      @RequestParam(required = false, defaultValue = CommonConstants.NO) String showAll) {
        List <OfficeDTO> list = officeService.findListPgAndKj ( tenantId, parentId );
        //List <OfficeDTO> rootTree = officeService.getRootTree (list, extId, type, showAll,"1" );
        List <OfficeDTO> rootTree = officeService.getOldRootTree (parentId,list, extId, type, showAll );
        return ResponseEntity.ok ( rootTree );
    }

    /**
     * 获取机构JSON数据(评估,会计,项目)。
     *
     * @param extId   排除的ID
     * @param type    类型（1：公司；2：部门）
     * @param showAll 是否显示不可用数据 1 显示 0 隐藏
     * @return
     */
    @ApiLog("获取部门数据(评估,会计,项目)")
    @GetMapping("treeDataPgAndKjAndXm")
    public ResponseEntity <List <OfficeDTO>> treeDataPgAndKjAndXm(@RequestParam(required = false) String tenantId,
                                                             @RequestParam(required = false) String parentId,
                                                             @RequestParam(required = false) String extId,
                                                             @RequestParam(required = false) String type,
                                                             @RequestParam(required = false, defaultValue = CommonConstants.NO) String showAll) {
        List <OfficeDTO> list = officeService.treeDataPgAndKjAndXm ( tenantId, parentId );
        //List <OfficeDTO> rootTree = officeService.getRootTree (list, extId, type, showAll,"1" );
        List <OfficeDTO> rootTree = officeService.getOldRootTree (parentId,list, extId, type, showAll );
        return ResponseEntity.ok ( rootTree );
    }

    /**
     * 获取机构JSON数据。
     *
     * @param extId   排除的ID
     * @param type    类型（1：公司；2：部门）
     * @param showAll 是否显示不可用数据 1 显示 0 隐藏
     * @return
     */
    @ApiLog("获取部门数据")
    @GetMapping("allTreeData")
    public ResponseEntity <List <OfficeDTO>> allTreeData(@RequestParam(required = false) String tenantId,
                                                      @RequestParam(required = false) String parentId,
                                                      @RequestParam(required = false) String extId,
                                                      @RequestParam(required = false) String type,
                                                      @RequestParam(required = false, defaultValue = CommonConstants.NO) String showAll) {
        List <OfficeDTO> list = officeService.findAllList ( tenantId, parentId );
        //List <OfficeDTO> rootTree = officeService.getRootTree (list, extId, type, showAll,"1" );
        List <OfficeDTO> rootTree = officeService.getOldRootTree (parentId,list, extId, type, showAll );
        return ResponseEntity.ok ( rootTree );
    }

    /**
     * 获取机构JSON数据。
     *
     * @param extId   排除的ID
     * @param type    类型（1：公司；2：部门）
     * @param showAll 是否显示不可用数据 1 显示 0 隐藏
     * @return
     */
    @ApiLog("获取签字注师2部门数据")
    @GetMapping("qzTreeData")
    public ResponseEntity <List <OfficeDTO>> qzTreeData(@RequestParam(required = false) String tenantId,
                                                      @RequestParam(required = false) String parentId,
                                                      @RequestParam(required = false) String extId,
                                                      @RequestParam(required = false) String type,
                                                      @RequestParam(required = false, defaultValue = CommonConstants.NO) String showAll) {
        List <OfficeDTO> list = officeService.findList2 ( tenantId, parentId );
        //List <OfficeDTO> rootTree = officeService.getRootTree (list, extId, type, showAll,"1" );
        List <OfficeDTO> rootTree = officeService.getOldRootTree (parentId,list, extId, type, showAll );
        return ResponseEntity.ok ( rootTree );
    }
    /**
     * 根据签字注师岗位获取部门数据。
     *
     * @param extId   排除的ID
     * @param type    类型（1：公司；2：部门）
     * @param showAll 是否显示不可用数据 1 显示 0 隐藏
     * @return
     */
    @ApiLog("根据签字注师岗位获取部门数据")
    @GetMapping("accessTreeData")
    public ResponseEntity <List <OfficeDTO>> accessTreeData(@RequestParam(required = false) String tenantId,
                                                      @RequestParam(required = false) String parentId,
                                                      @RequestParam(required = false) String extId,
                                                      @RequestParam(required = false) String type,
                                                      @RequestParam(required = false, defaultValue = CommonConstants.NO) String showAll) {
        //根据签字注师2岗位用户所在部门查部门数据
        List <OfficeDTO> list = officeService.findAccessList ( tenantId, parentId );
        //List <OfficeDTO> rootTree = officeService.getRootTree (list, extId, type, showAll,"1" );
        List <OfficeDTO> rootTree = officeService.getOldRootTree (parentId,list, extId, type, showAll );
        return ResponseEntity.ok ( rootTree );
    }

    /**
     * 根据用户查询出部门组织。
     *
     * @param extId   排除的ID
     * @param type    类型（1：公司；2：部门）
     * @param showAll 是否显示不可用数据 1 显示 0 隐藏
     * @return
     */
    @ApiLog("根据用户查询出部门组织")
    @GetMapping("treeDataByUser")
    public ResponseEntity <List <OfficeDTO>> treeDataByUser(@RequestParam(required = false) String tenantId,
                                                            @RequestParam(required = false) String parentId,
                                                            @RequestParam(required = false) String extId,
                                                            @RequestParam(required = false) String type,
                                                            @RequestParam(required = false, defaultValue = CommonConstants.NO) String showAll) {
        //根据签字注师2岗位用户所在部门查部门数据
        List <OfficeDTO> list = officeService.findListByUser ( tenantId, parentId );
        //List <OfficeDTO> rootTree = officeService.getRootTree (list, extId, type, showAll,"1" );
        List <OfficeDTO> rootTree = officeService.getOldRootTree (parentId,list, extId, type, showAll );

        return ResponseEntity.ok ( rootTree );
    }

    /**
     * 获取机构JSON数据。
     *
     * @param extId 排除的ID
     * @param type  类型（1：公司；2：部门）
     * @param showAll 是否显示不可用数据 1 显示 0 隐藏
     * @return
     */
    @ApiLog("获取部门数据")
    @GetMapping("treeData2")
    public ResponseEntity treeData2(@RequestParam(required = false) String extId, @RequestParam(required = false) String type, @RequestParam(required = false, defaultValue = CommonConstants.NO) String showAll) {
        List<OfficeDTO> list = officeWrapper.toDTO (officeService.list ());
        List rootTree = officeService.getRootTree2 (list, extId, type, showAll);
        return ResponseEntity.ok (rootTree);
    }


    /**
     * 获取机构JSON数据。
     *
     * @param extId 排除的ID
     * @param type  类型（1：公司；2：部门）
     * @param showAll 是否显示不可用数据 1 显示 0 隐藏
     * @return
     */
    @ApiLog("获取部门数据")
    @GetMapping("treeData3")
    public ResponseEntity treeData3(@RequestParam(required = false) String extId, @RequestParam(required = false) String type, @RequestParam(required = false, defaultValue = CommonConstants.NO) String showAll) {
        List<OfficeDTO> list = officeWrapper.toDTO (officeService.list (new QueryWrapper<Office>().lambda().eq(Office::getDelFlag,"0")));
        List rootTree = officeService.getRootTree (list, extId, type, showAll, "1");
        return ResponseEntity.ok (rootTree);
    }

}
