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

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.biz.vo.ElderlyHealthFollowUpPageVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.HealthFollowUpPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.HealthFollowUpSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyHealthFollowUpDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.FollowUpConfigDO;
import cn.iocoder.yudao.module.system.service.biz.ElderlyHealthFollowUpService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;

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

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

import java.util.List;

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


@Tag(name = "管理后台 - 长者健康随访")
@RestController
@RequestMapping("/elderly/healthFollowUp")
@Validated
public class ElderlyHealthFollowUpController {

    @Resource
    private ElderlyHealthFollowUpService elderlyHealthFollowUpService;

    @PostMapping("/create")
    @Operation(summary = "创建长者健康随访")
    @TenantIgnore
    public CommonResult<Boolean> createHealthFollowUp(@Valid @RequestBody HealthFollowUpSaveReqVO createReqVO) {
        elderlyHealthFollowUpService.createHealthFollowUp(createReqVO);
        return success(true);
    }

    @PostMapping("/createHealthFollowUpGrade")
    @Operation(summary = "创建随访等级")
    @TenantIgnore
    public CommonResult<Boolean> createHealthFollowUpGrade(@RequestBody List<FollowUpConfigDO> configs) {
        elderlyHealthFollowUpService.createHealthFollowUpGrade(configs);
        return success(true);
    }


    @GetMapping("/getHealthFollowUpGrade")
    @Operation(summary = "获取随访等级列表")
    @TenantIgnore
    public CommonResult<List<FollowUpConfigDO>> getHealthFollowUpGrade(@RequestParam Long tenantId) {
        return success(elderlyHealthFollowUpService.getHealthFollowUpGrade(tenantId));
    }


    @PutMapping("/update")
    @Operation(summary = "更新长者健康随访")
    @TenantIgnore
    public CommonResult<Boolean> updateHealthFollowUp(@Valid @RequestBody HealthFollowUpSaveReqVO updateReqVO) {
        elderlyHealthFollowUpService.updateHealthFollowUp(updateReqVO);
        return success(true);
    }

    //    @DeleteMapping("/delete")
//    @Operation(summary = "删除长者健康随访")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('elderly:health-follow-up:delete')")
//    public CommonResult<Boolean> deleteHealthFollowUp(@RequestParam("id") Long id) {
//        healthFollowUpService.deleteHealthFollowUp(id);
//        return success(true);
//    }
//
    @GetMapping("/getHealthFollowUpByConditions")
    @Operation(summary = "获得长者健康随访列表")
    @TenantIgnore
    public CommonResult<List<ElderlyHealthFollowUpDO>> getHealthFollowUpByConditions(@Valid HealthFollowUpPageReqVO pageReqVO) {
        return success(elderlyHealthFollowUpService.getHealthFollowUpByConditions(pageReqVO));
    }


    @GetMapping("/getLastElderlyHealthFollowRecords")
    @Operation(summary = "获得近七条健康随访数据")
    @TenantIgnore
    public CommonResult<List<ElderlyHealthFollowUpDO>> getLastElderlyHealthFollowRecords(@RequestParam Long elderId){
        return success(elderlyHealthFollowUpService.getLastElderlyHealthFollowRecords(elderId));
    }


    @GetMapping("/getElderlyHealthFollowDetail")
    @Operation(summary = "获取健康随访详情")
    @TenantIgnore
    public CommonResult<ElderlyHealthFollowUpDO> getElderlyHealthFollowDetail(@RequestParam Long id){
        return success(elderlyHealthFollowUpService.getElderlyHealthFollowDetail(id));
    }


    @GetMapping("/page")
    @Operation(summary = "获得长者健康随访分页")
    @TenantIgnore
    public CommonResult<PageResult<ElderlyHealthFollowUpPageVO>> getHealthFollowUpPage(@Valid HealthFollowUpPageReqVO pageReqVO) {
        if (pageReqVO.getTenantIds() == null) {
            pageReqVO.setTenantIds(new Long[]{TenantContextHolder.getTenantId()});
        }
        PageResult<ElderlyHealthFollowUpPageVO> pageResult = elderlyHealthFollowUpService.getHealthFollowUpPage(pageReqVO);
        return success(pageResult);
    }


    @GetMapping("/updateFollowUpGrade")
    @Operation(summary = "修改长者随访等级")
    @TenantIgnore
    public CommonResult<Boolean> updateFollowUpGrade(@RequestParam("elderId") Long elderId,@RequestParam("followUpGradeId") Long followUpGradeId){
        elderlyHealthFollowUpService.updateFollowUpGrade(elderId,followUpGradeId);
        return success(true);
    }

//    public CommonResult<List<ElderlyHealthFollowUpPageVO>>
//
//    @GetMapping("/export-excel")
//    @Operation(summary = "导出长者健康随访 Excel")
//    @PreAuthorize("@ss.hasPermission('elderly:health-follow-up:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportHealthFollowUpExcel(@Valid HealthFollowUpPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<HealthFollowUpDO> list = healthFollowUpService.getHealthFollowUpPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "长者健康随访.xls", "数据", HealthFollowUpRespVO.class,
//                        BeanUtils.toBean(list, HealthFollowUpRespVO.class));
//    }

}