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.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.nursing.vo.NursingPlanPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.nursing.vo.NursingPlanLowFrequencyByFloorRespVO;
import cn.iocoder.yudao.module.system.controller.admin.nursing.vo.NursingPlanLogRespVO;
import cn.iocoder.yudao.module.system.controller.admin.nursing.vo.NursingPlanRespVO;
import cn.iocoder.yudao.module.system.controller.admin.nursing.vo.NursingPlanSaveReqVO;
import cn.iocoder.yudao.module.system.service.nursing.NursingPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;
import java.util.Objects;

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


@Tag(name = "管理后台 - 护理计划")
@RestController
@RequestMapping("/elderly/nursingPlan")
@Validated
public class NursingPlanController {

    @Resource
    private NursingPlanService nursingPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建护理计划")
    public CommonResult<Boolean> createNursingPlan(@Valid @RequestBody NursingPlanSaveReqVO createReqVO) {
        nursingPlanService.createNursingPlan(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新护理计划")
    public CommonResult<Boolean> updateNursingPlan(@Valid @RequestBody NursingPlanSaveReqVO updateReqVO) {
        nursingPlanService.updateNursingPlan(updateReqVO);
        return success(true);
    }

    //    @DeleteMapping("/delete")
//    @Operation(summary = "删除护理计划")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('elderly:nursing-plan:delete')")
//    public CommonResult<Boolean> deleteNursingPlan(@RequestParam("id") Long id) {
//        nursingPlanService.deleteNursingPlan(id);
//        return success(true);
//    }
//
    @GetMapping("/get")
    @Operation(summary = "获得护理计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<NursingPlanRespVO> getNursingPlan(@RequestParam("id") Long id) {
        NursingPlanRespVO nursingPlan = nursingPlanService.getNursingPlan(id);
        return success(nursingPlan);
    }

    @GetMapping("/page")
    @Operation(summary = "获得护理计划分页")
    @TenantIgnore
    public CommonResult<PageResult<NursingPlanRespVO>> getNursingPlanPage(@Valid NursingPlanPageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        PageResult<NursingPlanRespVO> pageResult = nursingPlanService.getNursingPlanPage(pageReqVO);
        return success(pageResult);
    }


    @PostMapping("/copyNursingPlan")
    @Operation(summary = "复制护理计划")
    public CommonResult<Boolean> copyNursingPlan(@RequestBody NursingPlanSaveReqVO saveReqVO) {
        nursingPlanService.copyNursingPlan(saveReqVO);
        return success(true);
    }


    @GetMapping("/getNursingPlanByElderId")
    @Operation(summary = "根据长者ID查询护理计划")
    @TenantIgnore
    @PermitAll
    public CommonResult<List<NursingPlanLogRespVO>> getNursingPlanByElderId(@RequestParam("elderId") Long elderId) {
        return success(nursingPlanService.getNursingPlanByElderId(elderId));
    }

    @GetMapping("/getLowFrequencyPlanItemsByFloorId")
    @Operation(summary = "根据楼层ID获取长者中/低频护理计划明细")
    @Parameter(name = "floorId", description = "楼层ID", required = true, example = "1024")
    @TenantIgnore
    @PermitAll
    public CommonResult<List<NursingPlanLowFrequencyByFloorRespVO>> getLowFrequencyPlanItemsByFloorId(@RequestParam("floorId") Long floorId,
                                                                                                      @RequestParam("tenantId") Long tenantId) {
        return success(nursingPlanService.getLowFrequencyPlanItemsByFloorId(floorId,tenantId));
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出护理计划 Excel")
//    @PreAuthorize("@ss.hasPermission('elderly:nursing-plan:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportNursingPlanExcel(@Valid NursingPlanPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<NursingPlanDO> list = nursingPlanService.getNursingPlanPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "护理计划.xls", "数据", NursingPlanRespVO.class,
//                        BeanUtils.toBean(list, NursingPlanRespVO.class));
//    }

}
