package com.ruoyi.ibs.task.controller;

import com.github.pagehelper.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataPageInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ibs.task.domain.dto.TaskRecordDTO;
import com.ruoyi.ibs.task.domain.dto.TaskVisitCustAddressDTO;
import com.ruoyi.ibs.task.domain.vo.VisitRecordListVO;
import com.ruoyi.ibs.task.service.TaskVisitRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author 吴凯程
 * @Date 2025/3/3
 **/
@Api(tags = "走访任务接口")
@RestController
@RequestMapping("/visit")
public class VisitTaskController extends BaseController {

    @Resource
    private TaskVisitRecordService visitRecordService;

    @ApiOperation("获取当前签到签退记录")
    @Log(title = "走访-获取当前签到签退记录")
    @GetMapping("/target/sign")
    public AjaxResult getCurrentSign(@RequestParam String recordId) {
        return AjaxResult.success(visitRecordService.getCurrentSign(recordId));
    }

    @ApiOperation("添加客户地址")
    @Log(title = "走访-添加客户地址")
    @PostMapping("/target/address")
    public AjaxResult addCustAddress(@RequestBody TaskVisitCustAddressDTO taskVisitCustAddressDTO) {
        return AjaxResult.success(visitRecordService.addCustAddress(taskVisitCustAddressDTO));
    }

    @ApiOperation("获取走访记录列表")
    @Log(title = "走访-获取走访记录列表")
    @GetMapping("/record/list")
    public AjaxResult getVisitRecordList(@RequestParam String custId) {
        return AjaxResult.success(visitRecordService.getVisitRecordList(custId));
    }

    @ApiOperation("获取单次活动走访记录列表")
    @Log(title = "走访-获取走访记录列表")
    @GetMapping("/record/listByCampign")
    public AjaxResult getVisitRecordListByCampaign(@RequestParam String custId,@RequestParam String campaignId) {
        return AjaxResult.success(visitRecordService.getVisitRecordListByCampaign(custId,campaignId));
    }


    @ApiOperation("获取用户走访记录")
    @Log(title = "走访-获取用户走访记录")
    @GetMapping("/record/user")
    public TableDataPageInfo<VisitRecordListVO> getVisitRecordListByUserName(TaskRecordDTO taskRecordDTO) {
        Page<Object> pages = startPage();
        List<VisitRecordListVO> visitRecordListByUserName = visitRecordService.getVisitRecordListByUserName(taskRecordDTO);
        return getDataTable(visitRecordListByUserName, pages);
    }

//    @ApiOperation("用户走访记录导出")
//    @Log(title = "走访-用户走访记录导出", businessType = BusinessType.EXPORT)
//    @PostMapping("/record/user/export")
//    public void export(HttpServletResponse response) {
//        visitRecordService.export(response);
//    }

    @ApiOperation("用户走访记录导出-异步")
    @Log(title = "走访-用户走访记录导出-异步", businessType = BusinessType.EXPORT)
//    @PostMapping("/record/user/exportAsync")
    @PostMapping("/record/user/export")
    public AjaxResult exportAsync() {
        String taskId = visitRecordService.exportAsync();
        return AjaxResult.success("导出任务创建成功，后台正在处理...", taskId);
    }

    @ApiOperation("获取走访记录详情")
    @Log(title = "走访-获取走访记录详情")
    @GetMapping("/record/detail")
    public AjaxResult getVisitRecord(@RequestParam String recordId) {
        return AjaxResult.success(visitRecordService.getVisitRecord(recordId));
    }

}
