package com.ruoyi.ibs.task.controller;

import com.github.pagehelper.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataPageInfo;
import com.ruoyi.ibs.task.domain.dto.*;
import com.ruoyi.ibs.task.domain.vo.VisitRecordListVO;
import com.ruoyi.ibs.task.domain.vo.VisitTargetVO;
import com.ruoyi.ibs.task.service.TaskVisitRecordService;
import com.ruoyi.ibs.task.service.VisitTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

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

    @Resource
    private VisitTaskService visitTaskService;

    @Resource
    private TaskVisitRecordService visitRecordService;



    @ApiOperation("获取走访任务列表")
    @Log(title = "获取走访任务列表")
    @GetMapping("/list")
    public AjaxResult getVisitTaskList(VisitTaskListDTO visitTaskListDTO) {
        return AjaxResult.success(visitTaskService.getVisitTaskList(visitTaskListDTO));
    }

    @ApiOperation("获取走访目标列表")
    @Log(title = "获取走访目标列表")
    @GetMapping("/target/list")
    public TableDataPageInfo<VisitTargetVO> getVisitTargetList(VisitTargetDTO visitTargetDTO) {
        List<VisitTargetVO> visitTargetList = visitTaskService.getVisitTargetList(visitTargetDTO);
        TableDataPageInfo<VisitTargetVO> rspData = new TableDataPageInfo<>();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(visitTargetList);
        rspData.setTotal(visitTargetList.size());
        return rspData;
    }

    @ApiOperation("获取走访目标列表")
    @Log(title = "获取走访目标列表")
    @GetMapping("/target/appointmentList")
    public TableDataPageInfo<VisitTargetVO> getVisitTargetListByAppointment(@RequestParam String finishFlag) {
        //默认未走访 可以新写一个已走访接口，返回预约表里和task表里都有的数据，或者写在一起，根据判断返回两个list
        //---0806根据finishFlag查询complete时间是否符合，定义为未走访、已走访
        Page<Object> pages = startPage();
        List<VisitTargetVO> visitTargetListByAppointment = visitTaskService.getVisitTargetListByAppointment(finishFlag);
        return getDataTable(visitTargetListByAppointment,pages);
    }

    @ApiOperation("走访签到")
    @Log(title = "走访签到")
    @PostMapping("/target/sign/in")
    public AjaxResult signIn(TaskVisitRecordDTO taskVisitRecordDTO) {
        return AjaxResult.success(visitRecordService.addTaskVisitRecord(taskVisitRecordDTO));
    }

    @ApiOperation("走访签退")
    @Log(title = "走访签退")
    @PostMapping("/target/sign/out")
    public AjaxResult signOut(SignOutDTO signOutDTO) {
        return AjaxResult.success(visitRecordService.updateTaskVisitRecord(signOutDTO));
    }

    @ApiOperation("非实地走访签到")
    @Log(title = "非实地走访签到")
    @PostMapping("/target/sign/remote")
    public AjaxResult addRemoteVisitRecord(TaskRemoteVisitDTO taskRemoteVisitDTO){
        return AjaxResult.success(visitRecordService.addRemoteVisitRecord(taskRemoteVisitDTO));
    }

    @ApiOperation("获取当前签到签退记录")
    @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("获取走访记录详情")
    @GetMapping("/record/detail")
    public AjaxResult getVisitRecord(@RequestParam String recordId) {
        return AjaxResult.success(visitRecordService.getVisitRecord(recordId));
    }

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

    @ApiOperation("上传商户门头照片")
    @PostMapping("/cust/uploadImage")
    public AjaxResult uploadCustImagesToOss(List<MultipartFile> imageList, String custId) {
        visitRecordService.uploadCustImagesToOss(imageList,custId);
        return AjaxResult.success("上传成功");
    }

    @ApiOperation("查询商户门头照片")
    @GetMapping("/cust/downloadImage")
    public AjaxResult downloadCustImagesToOss(String custId) {
        List<String> list = visitRecordService.downloadCustImagesToOss(custId);
        return AjaxResult.success(list);
    }


}
