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.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.RegistrationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.RegistrationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.RegistrationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.reservation.VisitRecordDO;
import cn.iocoder.yudao.module.system.service.reservation.VisitRecordService;
import cn.iocoder.yudao.module.system.service.reservation.VisitRegistrationService;
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.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("/visit/registration")
@Validated
public class VisitRegistrationController {

    @Resource
    private VisitRegistrationService registrationService;
    @Resource
    private VisitRecordService visitRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建来访登记")
    @TenantIgnore
    public CommonResult<Long> createRegistration(@Valid @RequestBody RegistrationSaveReqVO createReqVO) {
        createReqVO.setTenantId(createReqVO.getTenantId() == null ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(registrationService.createRegistration(createReqVO));
    }


    @PutMapping("/update")
    @Operation(summary = "更新来访登记")
    @TenantIgnore
    public CommonResult<Boolean> updateRegistration(@Valid @RequestBody RegistrationSaveReqVO updateReqVO) {
        registrationService.updateRegistration(updateReqVO);
        return success(true);
    }


    //    @DeleteMapping("/delete")
//    @Operation(summary = "删除来访登记")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('visit:registration:delete')")
//    public CommonResult<Boolean> deleteRegistration(@RequestParam("id") Long id) {
//        registrationService.deleteRegistration(id);
//        return success(true);
//    }
//
    @GetMapping("/get")
    @Operation(summary = "获得来访登记")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<RegistrationRespVO> getRegistration(@RequestParam("id") Long id) {
        RegistrationRespVO registration = registrationService.getRegistration(id);
        return success(registration);
    }

    @GetMapping("/page")
    @Operation(summary = "获得来访登记分页")
    @TenantIgnore
    public CommonResult<PageResult<RegistrationRespVO>> getRegistrationPage(@Valid RegistrationPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(pageReqVO.getTenantIds() == null ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<RegistrationRespVO> pageResult = registrationService.getRegistrationPage(pageReqVO);
        return success(pageResult);
    }


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



//    @GetMapping("/export-excel")
//    @Operation(summary = "导出来访登记 Excel")
//    @PreAuthorize("@ss.hasPermission('visit:registration:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportRegistrationExcel(@Valid RegistrationPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<RegistrationDO> list = registrationService.getRegistrationPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "来访登记.xls", "数据", RegistrationRespVO.class,
//                        BeanUtils.toBean(list, RegistrationRespVO.class));
//    }

}