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

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.reservation.vo.AppointmentPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.AppointmentRespVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.AppointmentSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.excel.AppointmentExportExcelVO;
import cn.iocoder.yudao.module.system.dal.dataobject.reservation.VisitAppointmentDO;
import cn.iocoder.yudao.module.system.dal.dataobject.reservation.VisitRecordDO;
import cn.iocoder.yudao.module.system.service.biz.BuildService;
import cn.iocoder.yudao.module.system.service.reservation.VisitAppointmentService;
import cn.iocoder.yudao.module.system.service.reservation.VisitRecordService;
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.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

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

import java.io.IOException;
import java.util.List;

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


@Tag(name = "管理后台 - 长者来访预约")
@RestController
@RequestMapping("/visit/appointment")
@Validated
public class VisitAppointmentController {

    @Resource
    private VisitAppointmentService visitAppointmentService;
    @Resource
    private VisitRecordService visitRecordService;
    @Resource
    private BuildService buildService;
    @Resource
    private TenantService tenantService;


    @PostMapping("/create")
    @Operation(summary = "创建长者来访预约")
    @TenantIgnore
    public CommonResult<Long> createAppointment(@Valid @RequestBody AppointmentSaveReqVO createReqVO) {
        createReqVO.setTenantId(createReqVO.getTenantId() == null ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(visitAppointmentService.createAppointment(createReqVO));
    }


    @PutMapping("/update")
    @Operation(summary = "更新长者来访预约")
    @TenantIgnore
    public CommonResult<Boolean> updateAppointment(@Valid @RequestBody AppointmentSaveReqVO updateReqVO) {
        visitAppointmentService.updateAppointment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/close")
    @Operation(summary = "关闭长者来访预约")
    @Parameter(name = "id", description = "编号", required = true)
    @TenantIgnore
    public CommonResult<Boolean> closeAppointment(@RequestParam("id") Long id) {
        visitAppointmentService.closeAppointment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得长者来访预约详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<AppointmentRespVO> getAppointment(@RequestParam("id") Long id) {
        AppointmentRespVO appointment = visitAppointmentService.getAppointment(id);
        return success(appointment);
    }

    @GetMapping("/page")
    @Operation(summary = "获得长者来访预约分页")
    @TenantIgnore
    public CommonResult<PageResult<AppointmentRespVO>> getAppointmentPage(@Valid AppointmentPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(pageReqVO.getTenantIds() == null ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<VisitAppointmentDO> pageResult = visitAppointmentService.getAppointmentPage(pageReqVO);
        PageResult<AppointmentRespVO> pageBean = BeanUtils.toBean(pageResult, AppointmentRespVO.class);
        for (AppointmentRespVO respVO : pageBean.getList()) {
            respVO.setFullBedName(buildService.getFullBedNameByBedId(respVO.getBedId()));
            respVO.setTenantName(tenantService.getTenant(respVO.getTenantId()).getName());
        }
        return success(pageBean);
    }


    @GetMapping("/exportAppointmentExcel")
    @Operation(summary = "导出长者来访预约 Excel")
    public void exportAppointmentExcel(@Valid AppointmentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VisitAppointmentDO> list = visitAppointmentService.getAppointmentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "长者来访预约.xls", "数据", AppointmentExportExcelVO.class,
                        BeanUtils.toBean(list, AppointmentExportExcelVO.class));
    }

    @PostMapping("/createReturnVisitRecord")
    @Operation(summary = "创建回访记录")
    @TenantIgnore
    public CommonResult<Boolean> createReturnVisitRecord(@RequestBody VisitRecordDO visitRecordDO){
        visitRecordDO.setTenantId(visitRecordDO.getTenantId() == null ? TenantContextHolder.getTenantId() : visitRecordDO.getTenantId());
        visitRecordDO.setSourceType(1); //1.预约 2：登记
        visitRecordService.createReturnVisitRecord(visitRecordDO);
        return success(true);
    }


}