package cn.iocoder.yudao.module.system.controller.admin.nursing;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
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.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.CheckInWaitNurseLevelSimpleRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ItemPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.NurseItemImportExcel;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.NurseItemDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.NurseLevelConfigDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.NurseLevelDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.NursingCategoryDO;
import cn.iocoder.yudao.module.system.service.nursing.NurseItemService;
import cn.iocoder.yudao.module.system.service.nursing.NurseLevelService;
import com.alibaba.fastjson.JSONArray;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 护理等级")
@RestController
@RequestMapping("/nurse")
public class NurseLevelController {

    @Resource
    private NurseLevelService nurseLevelService;

    @Resource
    private NurseItemService nurseItemService;


    @PostMapping("/item/addNurseItem")
    @Operation(summary = "新增护理项目")
    @TenantIgnore
    public CommonResult<Boolean> addNurseItem(@RequestBody NurseItemDO itemDO) {
        itemDO.setTenantId(itemDO.getTenantId() == null ? TenantContextHolder.getTenantId() : itemDO.getTenantId());
        nurseItemService.addNurseItem(itemDO);
        return CommonResult.success(true);
    }


    @PostMapping("/item/updateNurseItem")
    @Operation(summary = "编辑护理项目")
    @TenantIgnore
    public CommonResult<Boolean> editNurseItem(@RequestBody NurseItemDO nurseItemDO) {
        nurseItemService.editNurseItem(nurseItemDO);
        return CommonResult.success(true);
    }


    @GetMapping("/item/getDetail")
    @Operation(summary = "获取护理项目详情")
    @Parameter(name = "itemId", description = "护理项目ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<NurseItemDO> getDetail(@RequestParam("itemId") Long itemId) {
        return CommonResult.success(nurseItemService.getDetail(itemId));
    }


    @GetMapping("/item/queryPage")
    @Operation(summary = "护理项目列表")
    @TenantIgnore
    public CommonResult<PageResult<NurseItemDO>> queryPage(@Valid ItemPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds()) ? (Long[]) Arrays.asList(TenantContextHolder.getTenantId()).toArray() : pageReqVO.getTenantIds());
        PageResult<NurseItemDO> pageResult = nurseItemService.queryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, NurseItemDO.class));
    }


    @DeleteMapping("/item/delete")
    @Operation(summary = "删除护理项目")
    @Parameter(name = "itemId", description = "护理项目ID", required = true, example = "1024")
    public CommonResult<Boolean> delete(@RequestParam("itemId") Long itemId) {
        nurseItemService.delete(itemId);
        return CommonResult.success(true);
    }

    @PostMapping("/move")
    @Operation(summary = "移动护理项目")
    public CommonResult<Boolean> move(@RequestBody NurseItemDO nurseItemDO) {
        nurseItemService.move(nurseItemDO);
        return success(true);
    }


    @GetMapping("/item/getParentCategoryList")
    @Operation(summary = "获取一级分类下所有护理项目列表")
    @TenantIgnore
    public CommonResult<List<NursingCategoryDO>> getParentCategoryList(@RequestParam(required = false) Long tenantId) {
        tenantId =  tenantId == null ? TenantContextHolder.getTenantId() : tenantId;
        return success(nurseItemService.getParentCategoryList(tenantId));
    }


    @PostMapping("/nurseLeve/addNurseLevel")
    @Operation(summary = "新增护理等级")
    @TenantIgnore
    public CommonResult<Boolean> addNurseLevel(@RequestBody NurseLevelDO nurseLevel) {
        nurseLevel.setTenantId(nurseLevel.getTenantId() == null ? TenantContextHolder.getTenantId() : nurseLevel.getTenantId());
        nurseLevelService.addNurseLevel(nurseLevel);
        return success(true);
    }

    @PostMapping("/nurseLeve/editNurseLevel")
    @Operation(summary = "编辑护理等级")
    public CommonResult<Boolean> editNurseLevel(@RequestBody NurseLevelDO nurseLevel) {
        nurseLevelService.editNurseLevel(nurseLevel);
        return success(true);
    }

    @GetMapping("/nurseLeve/findDetail")
    @Operation(summary = "获取护理等级详情")
    public CommonResult<NurseLevelDO> findDetail(@RequestParam("id") Long id) {
        return success(nurseLevelService.getDetail(id));
    }


    @DeleteMapping("/nurseLeve/deleteById")
    @Operation(summary = "删除护理等级")
    public CommonResult<Boolean> deleteById(@RequestParam("id") Long id) {
        nurseLevelService.deleteById(id);
        return success(true);
    }


    @GetMapping("/nurseLeve/findPage")
    @Operation(summary = "护理等级列表")
    @TenantIgnore
    public CommonResult<PageResult<NurseLevelDO>> findPage(@Valid ItemPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds()) ? (Long[]) Arrays.asList(TenantContextHolder.getTenantId()).toArray() : pageReqVO.getTenantIds());
        PageResult<NurseLevelDO> pageResult = nurseLevelService.findPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, NurseLevelDO.class));
    }

    @GetMapping("/nurseLeve/simple-list")
    @Operation(summary = "根据租户查询护理等级简单列表")
    @TenantIgnore
    @PermitAll
    public CommonResult<List<CheckInWaitNurseLevelSimpleRespVO>> getSimpleNurseLevelList(
            @RequestParam("tenantId") Long tenantId) {
        return success(nurseLevelService.getNurseLevelListByTenantId(tenantId));
    }


    @GetMapping("/nurseLeve/getListByElderId")
    @Operation(summary = "根据长者ID查询护理等级项目列表")
    public CommonResult<List<NurseLevelConfigDO>> getListByElderId(@RequestParam("elderId") Long elderId,@RequestParam(value = "categoryTypeName",required = false)  String categoryTypeName) {
        return success(nurseLevelService.getListByElderId(elderId,categoryTypeName));
    }


    @GetMapping("/findNurseItemListByName")
    @Operation(summary = "根据护理项名称查询列表")
    public CommonResult<List<NurseItemDO>> findNurseItemListByName(@RequestParam("nurseItemName") String nurseItemName, @RequestParam("categoryTypeName") String categoryTypeName) {
        return success(nurseLevelService.findNurseItemListByName(nurseItemName, categoryTypeName));
    }


    @PostMapping("/importNurseItem")
    @Operation(summary = "导入护理项目")
    public CommonResult<Map<String, Object>> importNurseItem(@RequestBody MultipartFile file) throws IOException {
        List<NurseItemImportExcel> dataList = ExcelUtils.read(file, NurseItemImportExcel.class);
        return success(nurseLevelService.importNurseItem(dataList));
    }

    @PostMapping("/downErrorExcel")
    @Operation(summary = "导出失败的护理excel")
    public void downErrorExcel(@RequestBody List<NurseItemImportExcel> payload, HttpServletResponse response) throws IOException {
        List<NurseItemImportExcel> data = JSONArray.parseArray(payload.toString(), NurseItemImportExcel.class);
        ExcelUtils.write(response, "护理项目.xls", "失败数据", NurseItemImportExcel.class, data);
    }


    @GetMapping("/downloadExcel")
    @Operation(summary = "下载护理项目导入模板")
    public void downloadFile(HttpServletResponse response) throws IOException {
        String filePath = "files/护理项目导入模板.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(filePath);
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=护理项目导入模板");
        IOUtils.copy(classPathResource.getInputStream(), response.getOutputStream());
        response.flushBuffer();
    }
}
