package cn.iocoder.yudao.module.pms.controller.admin.qhse.file;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.file.vo.QhseFileTreePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.file.vo.QhseFileTreeRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.file.vo.QhseFileTreeSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotTreeListReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.file.QhseFileTreeDO;
import cn.iocoder.yudao.module.pms.service.qhse.file.QhseFileTreeService;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptSimpleRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - QHSE文件树")
@RestController
@RequestMapping("/rq/qhse-file-tree")
@Validated
public class QhseFileTreeController {

    @Resource
    private QhseFileTreeService qhseFileTreeService;

    @PostMapping("/create")
    @Operation(summary = "创建QHSE文件树")
    @PreAuthorize("@ss.hasPermission('rq:qhse-file-tree:create')")
    public CommonResult<Long> createQhseFileTree(@Valid @RequestBody QhseFileTreeSaveReqVO createReqVO) {
        return success(qhseFileTreeService.createQhseFileTree(createReqVO));
    }

    @PostMapping("/add-dept-deal")
    public void createDeptDeal(@RequestBody DeptDO dept) {
        qhseFileTreeService.addDeptDeal(dept);
    }
    @PostMapping("/update-dept-deal")
    public void updateDeptDeal(@RequestBody DeptDO dept) {
        qhseFileTreeService.updateDeptDeal(dept);
    }
    @PostMapping("/delete-dept-deal")
    public void updateDeptDeal(String id) {
        qhseFileTreeService.deleteDeptDeal(id);
    }


    @PutMapping("/update")
    @Operation(summary = "更新QHSE文件树")
    @PreAuthorize("@ss.hasPermission('rq:qhse-file-tree:update')")
    public CommonResult<Boolean> updateQhseFileTree(@Valid @RequestBody QhseFileTreeSaveReqVO updateReqVO) {
        qhseFileTreeService.updateQhseFileTree(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除QHSE文件树")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:qhse-file-tree:delete')")
    public CommonResult<Boolean> deleteQhseFileTree(@RequestParam("id") Long id) {
        qhseFileTreeService.deleteQhseFileTree(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得QHSE文件树")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:qhse-file-tree:query')")
    public CommonResult<QhseFileTreeRespVO> getQhseFileTree(@RequestParam("id") Long id) {
        QhseFileTreeDO qhseFileTree = qhseFileTreeService.getQhseFileTree(id);
        return success(BeanUtils.toBean(qhseFileTree, QhseFileTreeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得QHSE文件树分页")
    @PreAuthorize("@ss.hasPermission('rq:qhse-file-tree:query')")
    public CommonResult<PageResult<QhseFileTreeRespVO>> getQhseFileTreePage(@Valid QhseFileTreePageReqVO pageReqVO) {
        PageResult<QhseFileTreeDO> pageResult = qhseFileTreeService.getQhseFileTreePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QhseFileTreeRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取部门精简信息列表", description = "只包含被开启的部门，主要用于前端的下拉选项")
    @PermitAll
    public CommonResult<List<DeptSimpleRespVO>> getSimpleDeptList() {
        List<QhseFileTreeDO> list = qhseFileTreeService.getQhseTreeList(new IotTreeListReqVO());
        return success(BeanUtils.toBean(list, DeptSimpleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出QHSE文件树 Excel")
    @PreAuthorize("@ss.hasPermission('rq:qhse-file-tree:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQhseFileTreeExcel(@Valid QhseFileTreePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QhseFileTreeDO> list = qhseFileTreeService.getQhseFileTreePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "QHSE文件树.xls", "数据", QhseFileTreeRespVO.class,
                        BeanUtils.toBean(list, QhseFileTreeRespVO.class));
    }


    @GetMapping("/ids/{id}")
    @Operation(summary = "获取某个节点的所有上级节点")
    public CommonResult<LinkedList<Map<String, Object>>> getTreeIds(@PathVariable("id") Long id) {
        List<QhseFileTreeDO> allParentIds = qhseFileTreeService.getAllParentIds(id);
        LinkedList<Map<String, Object>> list = new LinkedList<>();
        allParentIds.forEach(e -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", e.getId());
            map.put("name", e.getName());
            if (e.getName().contains("科瑞石油技术")) {
                map.put("type", "root");
            }
            list.add(map);
        });
        QhseFileTreeDO iotTree = qhseFileTreeService.getQhseFileTree(id);
        Map<String, Object> map = new HashMap<>();
        map.put("id", iotTree.getId());
        map.put("name", iotTree.getName());
        if (iotTree.getName().contains("科瑞石油技术")){
            map.put("type", "root");
        }
        list.add(map);
        return success(list);
    }


    @GetMapping("/init/tree")
    @PermitAll
    public void initTree() {
        qhseFileTreeService.initTree();
    }


}