package com.openhis.web.document.controller;

import com.core.common.core.domain.R;
import com.openhis.common.enums.DocPermissionEnum;
import com.openhis.common.enums.DocTypeEnum;
import com.openhis.common.enums.DocUseRangeEnum;
import com.openhis.web.document.appservice.IDocDefinitionAppService;
import com.openhis.web.document.dto.DocDefinitionDto;
import com.openhis.web.document.dto.DocDefinitonParam;
import com.openhis.web.document.util.EnumUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文书定义  controller
 *
 * @author wanghaiming
 * @date 2025-08-12
 */
@RestController
@RequestMapping("/document/definition")
@Slf4j
@AllArgsConstructor
public class DocDefinitionController {
    private final IDocDefinitionAppService iDocDefinitionAppService;

    /**
     * 初始化文书定义
     *
     * @return
     */
    @GetMapping("/init")
    public R<?> init() {
        Map<String, Object> map = new HashMap<>();

        // 获取文档类型枚举列表
        map.put("docTypes", EnumUtil.toMapList(DocTypeEnum.class));

        // 获取权限枚举列表
        map.put("permissions", EnumUtil.toMapList(DocPermissionEnum.class));
        //使用范围枚举
        List<String> noInUseCodes=new ArrayList<>();
        noInUseCodes.add(DocUseRangeEnum.USE_BY_SELF.getCode());
        map.put("useRanges", EnumUtil.toMapListFilter(DocUseRangeEnum.class, noInUseCodes));
        return R.ok(map);
    }

    /**
     * 新增文书定义
     *
     * @param definitionDto
     * @return
     */
    @PostMapping("/add")

    public R<?> addDefinition(@RequestBody DocDefinitionDto definitionDto) {
        return iDocDefinitionAppService.addDefinition(definitionDto);
    }

    /**
     * 修改文书定义
     *
     * @param definitionDto
     * @return
     */
    @PostMapping("/update")
    public R<?> updateDefinition(@RequestBody DocDefinitionDto definitionDto) {
        return iDocDefinitionAppService.updateDefinition(definitionDto);
    }

    /**
     * 获取文书定义详情
     *
     * @param id 文书定义ID
     * @return 文书定义详情
     */
    @GetMapping("/{id}")
    public R<?> getDefinitionDetailById(@PathVariable Long id) {
        log.info("获取文书定义详情 ID={}", id);
        return iDocDefinitionAppService.getDefinitionDetailById(id);
    }

    /**
     * 删除文书定义
     *
     * @param id 文书定义ID
     * @return 文书定义详情
     */
    @DeleteMapping("/delete/{id}")
    public R<?> deleteDefinition(@PathVariable("id") Long id) {
        log.info("删除文书定义，ID={}", id);
        // 调用删除服务（注意：方法名应改为删除相关，如removeById）
        return iDocDefinitionAppService.deleteDefinition(id);
    }

    /**
     * 查询获取文书定义列表 树形结构
     *
     * @return
     */
    @GetMapping("/treeList")
    public R<?> getTreeList(DocDefinitonParam docDefinitonParam) {
        return iDocDefinitionAppService.getTreeList(docDefinitonParam);
    }

}
