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.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.VisitRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.VisitRecordRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.reservation.VisitRecordDO;
import cn.iocoder.yudao.module.system.dal.dataobject.reservation.VisitRecordDetailDO;
import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper;
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.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 java.util.Objects;

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


@Tag(name = "管理后台 - 客户回访记录")
@RestController
@RequestMapping("/return/visitRecord")
@Validated
public class VisitRecordController {

    @Resource
    private VisitRecordService visitRecordService;
    @Resource
    private TenantService tenantService;

//
//    @PostMapping("/create")
//    @Operation(summary = "创建客户回访记录")
//    @PreAuthorize("@ss.hasPermission('return:visit-record:create')")
//    public CommonResult<Long> createVisitRecord(@Valid @RequestBody VisitRecordSaveReqVO createReqVO) {
//        return success(visitRecordService.createVisitRecord(createReqVO));
//    }
//
    @PutMapping("/update")
    @Operation(summary = "更新客户回访记录")
    @TenantIgnore
    public CommonResult<Boolean> updateVisitRecord(@Valid @RequestBody VisitRecordDetailDO updateReqVO) {
        visitRecordService.updateVisitRecordDetail(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除客户回访记录")
    @TenantIgnore
    public CommonResult<Boolean> deleteVisitRecord(@RequestBody Long[] ids) {
        visitRecordService.deleteVisitRecord(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户回访记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<VisitRecordRespVO> getVisitRecord(@RequestParam("id") Long id) {
        VisitRecordRespVO visitRecord = visitRecordService.getVisitRecord(id);
        return success(visitRecord);
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户回访记录分页")
    @TenantIgnore
    public CommonResult<PageResult<VisitRecordRespVO>> getVisitRecordPage(@Valid VisitRecordPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(pageReqVO.getTenantIds() == null ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<VisitRecordDO> pageResult = visitRecordService.getVisitRecordPage(pageReqVO);
        PageResult<VisitRecordRespVO> pageBean = BeanUtils.toBean(pageResult, VisitRecordRespVO.class);
        for (VisitRecordRespVO respVO : pageBean.getList()) {
            respVO.setTenantName(tenantService.getTenant(respVO.getTenantId()).getName());
        }
        return success(pageBean);
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出客户回访记录 Excel")
//    @PreAuthorize("@ss.hasPermission('return:visit-record:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportVisitRecordExcel(@Valid VisitRecordPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<VisitRecordDO> list = visitRecordService.getVisitRecordPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "客户回访记录.xls", "数据", VisitRecordRespVO.class,
//                        BeanUtils.toBean(list, VisitRecordRespVO.class));
//    }

}