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.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
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.ElderlySyntheticAbilityPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderSyntheticAbilitySignDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlySyntheticAbilityDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlySyntheticAbilityInfoDO;
import cn.iocoder.yudao.module.system.service.biz.ElderlySyntheticAbilityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

import java.util.List;
import java.util.Map;

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

@Tag(name = "管理后台 - 评估记录")
@RestController
@RequestMapping("/elderlySyntheticAbility")
public class ElderlySyntheticAbilityController {

    @Resource
    private ElderlySyntheticAbilityService elderlySyntheticAbilityService;


    @GetMapping("/queryPage")
    @Operation(summary = "评估记录分页")
//    @PreAuthorize("@ss.hasPermission('elderlySyntheticAbility:query')")
    @TenantIgnore
    public CommonResult<PageResult<ElderlySyntheticAbilityDO>> queryPage(@Valid ElderlySyntheticAbilityPageReqVO pageVO) {
        pageVO.setTenantIds(pageVO.getTenantIds() == null ? new Long[]{TenantContextHolder.getTenantId()} : pageVO.getTenantIds());
        PageResult<ElderlySyntheticAbilityDO> pageResult = elderlySyntheticAbilityService.queryPage(pageVO);
        return success(BeanUtils.toBean(pageResult, ElderlySyntheticAbilityDO.class));
    }


    @GetMapping("/getAdmissionAssessment")
    @Operation(summary = "获取入院评估详情（评估按钮）")
    @Parameter(name = "syntheticAbilityId", description = "评估记录ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<ElderlySyntheticAbilityDO> getAdmissionAssessment(@RequestParam("syntheticAbilityId") Long syntheticAbilityId) {
        ElderlySyntheticAbilityDO elderlySyntheticAbilityDO = elderlySyntheticAbilityService.getAdmissionAssessment(syntheticAbilityId);
        return success(elderlySyntheticAbilityDO);
    }


    @PostMapping("/addOrUpdate")
    @Operation(summary = "评估记录新增或修改")
    public CommonResult<Long> addOrUpdate(@RequestBody ElderlySyntheticAbilityDO syntheticAbilityDO) {
        Long id = elderlySyntheticAbilityService.addOrUpdate(syntheticAbilityDO);
        return success(id);
    }

    @GetMapping("/getSyntheticAbilityInfoById")
    @Operation(summary = "获取评估详情")
    @Parameter(name = "syntheticAbilityId", description = "评估记录ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<ElderlySyntheticAbilityDO> getSyntheticAbilityInfoById(@RequestParam("syntheticAbilityId") Long syntheticAbilityId) {
        return success(elderlySyntheticAbilityService.getSyntheticAbilityInfoById(syntheticAbilityId));
    }


    @GetMapping("/findElderlySyntheticAbilityInfo")
    @Operation(summary = "获取单个评估项详情")
    @Parameter(name = "syntheticAbilityInfoId", description = "评估记录项ID", required = true, example = "1024")
    public CommonResult<ElderlySyntheticAbilityInfoDO> findElderlySyntheticAbilityInfo(@RequestParam("syntheticAbilityInfoId") Long syntheticAbilityInfoId) {
        return success(elderlySyntheticAbilityService.findElderlySyntheticAbilityInfo(syntheticAbilityInfoId));
    }


    @GetMapping("/generateSignInfo")
    @Operation(summary = "生成签名信息")
    @Parameter(name = "syntheticAbilityId", description = "评估记录ID", required = true, example = "1024")
    public CommonResult<Map<String, Object>> generateSignInfo(@RequestParam("syntheticAbilityId") Long syntheticAbilityId) {
        return success(elderlySyntheticAbilityService.generateSignInfo(syntheticAbilityId));
    }


    @PostMapping("/addSignInfo")
    @Operation(summary = "新增签名")
    public CommonResult<Long> addSignInfo(@RequestBody ElderSyntheticAbilitySignDO signDO){
        return success(elderlySyntheticAbilityService.addSignInfo(signDO));
    }


    @GetMapping("/confirmSign")
    @Operation(summary = "保存确认签名")
    @Parameters({ @Parameter(name = "elderSyntheticAbilitySignId", description = "评估记录签名ID", required = true, example = "1024"),
            @Parameter(name = "randomStr", description = "随机字符串", required = true)})
    public CommonResult<Boolean> confirmSign(@RequestParam("elderSyntheticAbilitySignId") Long elderSyntheticAbilitySignId,@RequestParam("randomStr") String randomStr){
        elderlySyntheticAbilityService.confirmSign(elderSyntheticAbilitySignId,randomStr);
        return success(true);
    }

    @GetMapping("/getSignInfoById")
    @Operation(summary = "根据评估记录ID获取签名")
    @Parameter(name = "syntheticAbilityId", description = "评估记录ID", required = true, example = "1024")
    public CommonResult<ElderSyntheticAbilitySignDO> getSignInfoById(@RequestParam("syntheticAbilityId") Long syntheticAbilityId){
        return success(elderlySyntheticAbilityService.getSignInfoById(syntheticAbilityId));
    }


    @GetMapping("/getByElderId")
    @Operation(summary = "根据长者ID获取评估")
    public CommonResult<List<ElderlySyntheticAbilityDO>> getByElderId(@RequestParam("elderId") Long elderId,
                                                                      @RequestParam(value = "evaluationAgency",required = false) Integer evaluationAgencyType) {
        return success(elderlySyntheticAbilityService.getByElderId(elderId,evaluationAgencyType));
    }

    @GetMapping("/findLatestByElderId")
    @Operation(summary = "根据长者ID获取最新评估报告")
    public CommonResult<ElderlySyntheticAbilityDO> findLatestByElderId(@RequestParam("elderId") Long elderId){
        return success(elderlySyntheticAbilityService.findLatestByElderId(elderId));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评估记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        elderlySyntheticAbilityService.delete(id);
        return success(true);
    }
}
