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.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.EmergencyTrackingPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.EmergencyTrackingRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.EmergencyTrackingSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyEmergencyTrackingDO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import cn.iocoder.yudao.module.system.service.biz.BuildService;
import cn.iocoder.yudao.module.system.service.biz.EmergencyTrackingService;
import cn.iocoder.yudao.module.system.service.tenant.TenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.security.access.prepost.PreAuthorize;
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 static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 长者应急跟踪")
@RestController
@RequestMapping("/elderly/emergencyTracking")
@Validated
public class EmergencyTrackingController {

    @Resource
    private EmergencyTrackingService emergencyTrackingService;
    @Resource
    private TenantService tenantService;
    @Resource
    private BuildService buildService;

    @PostMapping("/create")
    @Operation(summary = "创建长者应急跟踪")
    @TenantIgnore
    public CommonResult<Long> createEmergencyTracking(@Valid @RequestBody EmergencyTrackingSaveReqVO createReqVO) {
        return success(emergencyTrackingService.createEmergencyTracking(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新长者应急跟踪")
    @TenantIgnore
    public CommonResult<Boolean> updateEmergencyTracking(@Valid @RequestBody EmergencyTrackingSaveReqVO updateReqVO) {
        emergencyTrackingService.updateEmergencyTracking(updateReqVO);
        return success(true);
    }
//
//    @DeleteMapping("/delete")
//    @Operation(summary = "删除长者应急跟踪")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('elderly:emergency-tracking:delete')")
//    public CommonResult<Boolean> deleteEmergencyTracking(@RequestParam("id") Long id) {
//        emergencyTrackingService.deleteEmergencyTracking(id);
//        return success(true);
//    }
//
    @GetMapping("/get")
    @Operation(summary = "获得长者应急跟踪详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<EmergencyTrackingRespVO> getEmergencyTracking(@RequestParam("id") Long id) {
        ElderlyEmergencyTrackingDO emergencyTracking = emergencyTrackingService.getEmergencyTracking(id);
        EmergencyTrackingRespVO bean = BeanUtils.toBean(emergencyTracking, EmergencyTrackingRespVO.class);
        bean.setBedName(buildService.getFullBedNameByBedId(bean.getBedId()));
        return success(bean);
    }

    @GetMapping("/page")
    @Operation(summary = "获得长者应急跟踪分页")
    @TenantIgnore
    public CommonResult<PageResult<EmergencyTrackingRespVO>> getEmergencyTrackingPage(@Valid EmergencyTrackingPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(pageReqVO.getTenantIds() == null ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<ElderlyEmergencyTrackingDO> pageResult = emergencyTrackingService.getEmergencyTrackingPage(pageReqVO);
        PageResult<EmergencyTrackingRespVO> pageBean = BeanUtils.toBean(pageResult, EmergencyTrackingRespVO.class);
        for (EmergencyTrackingRespVO emergencyTrackingRespVO : pageBean.getList()) {
            TenantDO tenant = tenantService.getTenant(emergencyTrackingRespVO.getTenantId());
            emergencyTrackingRespVO.setTenantName(tenant == null ? null : tenant.getName());
        }
        return success(pageBean);
    }
//
//    @GetMapping("/export-excel")
//    @Operation(summary = "导出长者应急跟踪 Excel")
//    @PreAuthorize("@ss.hasPermission('elderly:emergency-tracking:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportEmergencyTrackingExcel(@Valid EmergencyTrackingPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<EmergencyTrackingDO> list = emergencyTrackingService.getEmergencyTrackingPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "长者应急跟踪.xls", "数据", EmergencyTrackingRespVO.class,
//                        BeanUtils.toBean(list, EmergencyTrackingRespVO.class));
//    }

}