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.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableDataPageInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.ibs.grid.domain.entity.GridShapeCountLingshou;
import com.ruoyi.ibs.list.service.ISignedProductsService;
import com.ruoyi.ibs.task.domain.dto.AppointmentDTO;
import com.ruoyi.ibs.task.domain.dto.CustDTO;
import com.ruoyi.ibs.task.domain.entity.AppointmentInfo;
import com.ruoyi.ibs.task.domain.entity.SecondVisit;
import com.ruoyi.ibs.task.domain.vo.SecondVisitVO;
import com.ruoyi.ibs.task.service.AppointmentService;
import com.ruoyi.system.domain.SysOperLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

@Api(tags = "走访接口")
@RestController
@RequestMapping("/appointment")
public class AppointmentController extends BaseController {

    @Resource
    private AppointmentService appointmentService;

    @GetMapping("/list")
    @Log(title = "走访-查询预约客户列表")
    @ApiOperation("我预约的客户")
    public TableDataInfo appointmentCustList(){
        startPage();
        List<AppointmentInfo> appointmentInfoList = appointmentService.appointmentCustList();
        return getDataTable(appointmentInfoList);
    }

    @GetMapping("/claimList")
    @Log(title = "走访-查询认领客户列表")
    @ApiOperation("我认领的客户")
    public TableDataInfo claimCustList(){
        startPage();
        List<AppointmentInfo> appointmentInfos = appointmentService.claimCustList();
        return getDataTable(appointmentInfos);
    }

    @GetMapping("/searchCust")
    @Log(title = "走访-搜索预约客户")
    @ApiOperation("搜索要预约的客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "custIdc", value = "客户身份证号/统信码", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult searchAppointmentCust(String custIdc){
        return AjaxResult.success(appointmentService.searchAppointmentCust(custIdc));
    }

    @GetMapping("/searchClaimCust")
    @Log(title = "走访-搜索认领客户")
    @ApiOperation("搜索要认领的客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "custIdc", value = "客户身份证号/统信码", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult searchClaimCustList(String custIdc){
        return AjaxResult.success(appointmentService.searchClaimCustList(custIdc));
    }

    @PostMapping("/cust")
    @Log(title = "走访-预约客户")
    @ApiOperation("预约客户")
    public AjaxResult appointmentCust(@Validated @RequestBody AppointmentDTO appointmentDTO){
        return toAjax(appointmentService.appointmentCust(appointmentDTO));
    }

    @PostMapping("/claimCust")
    @Log(title = "走访-认领客户")
    @ApiOperation("认领客户")
    public AjaxResult claimCust(@Validated @RequestBody CustDTO custDTO){
        return AjaxResult.success(appointmentService.claimCust(custDTO.getCustIdc()));
    }

    @GetMapping("/cust/detail")
    @Log(title = "走访-已被认领的产品")
    @ApiOperation("已被认领的产品")
    public AjaxResult custProdDetail(String custIdc){
        return AjaxResult.success(appointmentService.getCustProdDetail(custIdc));
    }

    @GetMapping("/allProds")
    @Log(title = "走访-查询所有产品")
    @ApiOperation("查询所有产品")
    public AjaxResult getAllProds(){
        return AjaxResult.success(appointmentService.getAllProds());
    }

    @GetMapping("/claimCust/detail")
    @Log(title = "走访-认领客户详情")
    @ApiOperation("认领客户详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "custIdc", value = "客户身份证号/统信码", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult claimCustDetail(String custIdc){
        return AjaxResult.success(appointmentService.getclaimCustDetail(custIdc));
    }

    @PostMapping("/secondVisit")
    @Log(title = "走访-二次走访预约")
    @ApiOperation("二次走访预约")
    public AjaxResult secondVisit(@Validated @RequestBody AppointmentDTO appointmentDTO, @RequestParam @NotBlank(message = "预约时间不能为空") Date visitTime){
        return AjaxResult.success(appointmentService.secondVisit(appointmentDTO, visitTime));
    }

    @GetMapping("/secondVisit/list")
    @Log(title = "走访-查询今日二次走访提醒列表")
    @ApiOperation("查询今日二次走访提醒列表")
    public TableDataPageInfo<SecondVisitVO> getTodaySecondVisits() {
        Page<Object> page = startPage();
        List<SecondVisitVO> todayVisits = appointmentService.getTodaySecondVisits();
        return getDataTable(todayVisits, page);
    }

    @PostMapping("/resourceAssist")
    @Log(title = "走访-资源协助申请")
    @ApiOperation("资源协助申请")
    public AjaxResult resourceAssist(@Validated @RequestBody AppointmentDTO appointmentDTO,
                                     @RequestParam @NotBlank(message = "柜员号不能为空") String userName,
                                     @RequestParam @NotBlank(message = "用户名不能为空") String nickName){
        return AjaxResult.success(appointmentService.resourceAssist(appointmentDTO, userName, nickName));
    }

    @GetMapping("/resourceAssist/list")
    @Log(title = "走访-查询资源协助申请列表")
    @ApiOperation("查询资源协助申请列表")
    public TableDataPageInfo<SecondVisitVO> getResourceAssistList() {
        Page<Object> page = startPage();
        List<SecondVisitVO> resourceAssistList = appointmentService.getResourceAssistList();
        return getDataTable(resourceAssistList, page);
    }

    @PostMapping("/resourceAssist/feedback")
    @Log(title = "走访-资源协助申请反馈")
    @ApiOperation("资源协助申请反馈")
    public AjaxResult resourceAssistFeedback(@Validated @RequestBody SecondVisit secondVisit){
        return AjaxResult.success(appointmentService.resourceAssistFeedback(secondVisit));
    }


}
