package com.openhis.web.document.controller;

import com.core.common.core.domain.R;
import com.openhis.common.enums.DocStatusEnum;
import com.openhis.common.enums.DocTypeEnum;
import com.openhis.web.document.appservice.IDocRecordAppService;
import com.openhis.web.document.dto.DocRecordDto;
import com.openhis.web.document.dto.DocRecordPatientQueryParam;
import com.openhis.web.document.dto.DocRecordQueryParam;
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
 */
@RestController
@RequestMapping("/document/record")
@Slf4j
@AllArgsConstructor
public class DocRecordController {

    private final IDocRecordAppService docRecordAppService;

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

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

    /**
     * 新增记录
     *
     * @param docRecordDto 文书记录信息
     * @return R
     */
    @PostMapping("/addRecord")
    public R<?> addRecord(@RequestBody DocRecordDto docRecordDto) {
        return docRecordAppService.addRecord(docRecordDto);
    }

    /**
     * 编辑记录 修改前先设置文书为编辑状态
     *
     * @param docRecordDto 文书记录信息
     * @return R
     */
    @PostMapping("/updateRecord")
    public R<?> updateRecord(@RequestBody DocRecordDto docRecordDto) {
        return docRecordAppService.updateRecord(docRecordDto);
    }

    /**
     * 设置文书为编辑状态
     *
     * @param recordId 文书记录ID
     * @return R
     */
    @PostMapping("/setIsEdit")
    public R<?> setIsEdit(Long recordId) {
        return docRecordAppService.setIsEdit(recordId);
    }

    /**
     * 根据患者ID或就诊ID获取文书记录列表,只针对不需返回患者具体信息的列表，体温单除外，单独处理
     * 需返回患者具体信息的列表，请使用getPatientRecordList
     *
     * @param docRecordQueryParam 文书记录查询参数
     * @param pageNo              页码
     * @param pageSize            页大小
     * @param searchKey           搜索关键字
     * @param request             请求
     *                            1.IsPage 是否分页 0:不分页 1:分页
     * @return R
     */
    @GetMapping("/getRecordByEncounterIdList")
    R<?> getRecordByEncounterIdList(DocRecordQueryParam docRecordQueryParam,
                                    @RequestParam(value = "isPage", defaultValue = "1") Integer isPage,
                                    @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    @RequestParam(name = "searchKey", required = false) String searchKey, HttpServletRequest request) {
        return docRecordAppService.getRecordByEncounterIdList(docRecordQueryParam, isPage, pageNo, pageSize, searchKey, request);
    }

    /**
     * 文书记录列表
     *
     * @param docRecordPatientQueryParam 文书记录查询参数
     * @param pageNo                     页码
     * @param pageSize                   页大小
     * @param searchKey                  搜索关键字
     * @param request                    请求
     * @return R
     */
    @GetMapping("/getRecordPageList")
    R<?> getRecordPageList(DocRecordPatientQueryParam docRecordPatientQueryParam,
                           @RequestParam(value = "primaryMenuEnumList", required = false) List<Integer> primaryMenuEnumList,
                           @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                           @RequestParam(name = "searchKey", required = false) String searchKey,
                           HttpServletRequest request) {
        return docRecordAppService.getRecordPageList(docRecordPatientQueryParam, primaryMenuEnumList, pageNo, pageSize, searchKey, request);
    }

    /**
     * 根据文书记录ID获取文书记录信息
     *
     * @param recordId 文书记录ID
     */
    @GetMapping("/getRecordById")
    R<?> getRecordById(@RequestParam(value = "recordId") Long recordId) {
        return docRecordAppService.getRecordById(recordId);
    }

    /**
     * 删除文书分配信息
     *
     * @param ids 删除文书分配信息ID列表
     */
    @DeleteMapping("/deleteRecord")
    R<?> deleteRecord(@RequestBody List<Long> ids) {
        return docRecordAppService.deleteRecord(ids);
    }

}
