package com.openhis.web.document.controller;

import com.core.common.core.domain.R;
import com.openhis.common.enums.DocStatisticsDefinitionTypeEnum;
import com.openhis.web.document.appservice.IDocStatisticsDefinitionAppService;
import com.openhis.web.document.dto.DocStatisticsDefinitionDto;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统计定义  controller
 *
 * @author wanghaiming
 * @date 2025-08-12
 */
@RestController
@RequestMapping("/document/statistics/definition")
@Slf4j
@AllArgsConstructor
public class DocStatisticsDefinitionController {

    private final IDocStatisticsDefinitionAppService docStatisticsDefinitionAppService;

    /**
     * 初始化页面数据
     *
     * @return
     */
    @GetMapping("/init")
    public R<?> init() {
        Map<String, Object> map = new HashMap<>();

        // 获取文档状态枚举
        map.put("doc_statistics_definition_type_enums", EnumUtil.toMapList(DocStatisticsDefinitionTypeEnum.class));
        return R.ok(map);
    }

//    @PostMapping("/createOrEdit")
//    public R<?> createOrEdit(@RequestBody DocStatisticsDefinitionDto docStatisticsDefinitionDto) {
//        return docStatisticsDefinitionAppService.createOrEdit(docStatisticsDefinitionDto);
//    }


    /**
     * 新增统计定义
     *
     * @param docStatisticsDefinitionDto
     * @return
     */
    @PostMapping("/add")
    public R<?> add(@RequestBody DocStatisticsDefinitionDto docStatisticsDefinitionDto) {
        return docStatisticsDefinitionAppService.add(docStatisticsDefinitionDto);
    }

    /**
     * 修改统计定义
     *
     * @param docStatisticsDefinitionDto
     * @return
     */
    @PostMapping("/update")
    public R<?> update(@RequestBody DocStatisticsDefinitionDto docStatisticsDefinitionDto) {
        return docStatisticsDefinitionAppService.update(docStatisticsDefinitionDto);
    }

    /**
     * 删除统计定义
     *
     * @param id
     */
    @DeleteMapping("/deleteDocStatisticsDefinition")
    public R<?> deleteDocStatisticsDefinition(Long id) {
        return docStatisticsDefinitionAppService.deleteDocStatisticsDefinition(id);
    }
    /**
     * 分页查询列表-不包含options
     *
     * @param pageNo
     * @param pageSize
     * @param searchKey
     * @param request
     * @return
     */
    @GetMapping("/getPageList")
    public R<?> getPageList(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                            @RequestParam(name = "searchKey", required = false) String searchKey, HttpServletRequest request) {
        return docStatisticsDefinitionAppService.getPageList(pageNo, pageSize, searchKey, request);
    }
    /**
     * 获取文档统计定义列表-不包含options，即字典数据
     *
     * @param isStatistics 文档统计定义是否启用   0：不统计  1：统计 可不传
     * @return
     */
    @GetMapping("/getList")
    public R<?> getList(Integer isStatistics){
        List<DocStatisticsDefinitionDto> list = docStatisticsDefinitionAppService.getList(isStatistics);
        return R.ok(list);
    }
    /**
     * 获取文档统计定义列表(包含options，即字典数据)
     *
     * @param isStatistics 文档统计定义是否启用   0：不统计  1：统计 可不传
     * @return
     */
    @GetMapping("/getListWithOptionList")
    public R<?>getListWithOptionList(Integer isStatistics){
        List<DocStatisticsDefinitionDto> list = docStatisticsDefinitionAppService.getListWithOptionList(isStatistics);
        return R.ok(list);
    }
    /**
     * 获取文档统计定义选项列表并按指定格式返回-已作废
     *
     * @return
     */
    @GetMapping("/getOptionList")
    public R<?> getOptionList() {
        return docStatisticsDefinitionAppService.getOptionList();
    }
}
