package com.yuchen.saas.park.service.controller.pldoor;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.dto.pldoor.PlVisitorInfoDTO;
import com.yuchen.saas.park.api.entity.pldoor.PlVisitorInfo;
import com.yuchen.saas.park.api.vo.pldoor.*;
import com.yuchen.saas.park.api.vo.request.PlMyVisitorReq;
import com.yuchen.saas.park.api.vo.request.PlPropertyPassReq;
import com.yuchen.saas.park.api.vo.request.PlVisitorInfoReq;
import com.yuchen.saas.park.service.service.pldoor.PlVisitorInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;

/**
 * <p>
 * 保利访客信息表 前端控制器
 * </p>
 *
 * @author miaoqs
 * @since 2024-04-18
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/plVisitorInfo")
public class PlVisitorInfoController {

    private final PlVisitorInfoService plVisitorInfoService;


    /**
     * 访客信息 新增/修改
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "访客信息 新增/修改", notes = "访客信息 新增/修改")
    public R saveOrUpdate(@Valid @RequestBody PlVisitorInfoDTO plVisitorInfoDTO) {
        if(Func.isEmpty(plVisitorInfoDTO) || Func.isEmpty(plVisitorInfoDTO.getPlVisitorCrewList())) {
            return R.error("访客信息不能为空");
        }
        return R.data(plVisitorInfoService.saveOrUpdateByParam(plVisitorInfoDTO));
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "访客信息详情", notes = "访客信息详情")
    public R detail(Long id) {
        return R.data(plVisitorInfoService.detailById(id));
    }

    /**
     * 根据主表id删除访客信息
     * @param plVisitorInfoDTO
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主表id访客信息删除", notes = "根据主表id访客信息删除")
    public R removeId(@RequestBody PlVisitorInfoDTO plVisitorInfoDTO) throws Exception {
        return R.data(plVisitorInfoService.deleteById(plVisitorInfoDTO.getId()));
    }

    /**
     * 分页 访客信息
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "分页 访客登记", notes = "分页 访客登记")
    public R<NestPage<PlVisitorInfoVO>> page(PlVisitorInfoReq req) {
        Page<PlVisitorInfoReq> page = new Page<>();
        page.setCurrent(req.getCurrent());
        page.setSize(req.getSize());
        IPage<PlVisitorInfoVO> pages = plVisitorInfoService.pageByParam(page, req);
        if (Func.isEmpty(pages)){
            pages = new Page<>();
        }
        return  R.data(NestPage.of(pages));
    }

    @PostMapping("/updateVisitorState")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改到访状态", notes = "修改到访状态")
    public R updateVisitorState(@Valid @RequestBody PlVisitorInfo plVisitorInfo) {
        if(Func.isEmpty(plVisitorInfo) || Func.isEmpty(plVisitorInfo.getVisitorState())) {
            return R.error("访客信息不能为空");
        }
        return R.data(plVisitorInfoService.saveOrUpdate(plVisitorInfo));
    }

    @GetMapping("/getTodayAppointmentInfo")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "今日预约情况", notes = "今日预约情况")
    public R<PlTodayAppointmentInfoVO> getTodayAppointmentInfo(Long parkId) {
        PlTodayAppointmentInfoVO plTodayAppointmentInfoVO = plVisitorInfoService.getTodayAppointmentInfo(parkId);
        return R.data(plTodayAppointmentInfoVO);
    }

    /**
     * 分页 访客信息
     */
    @GetMapping("/pageByTodayWillVisitParam")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "分页 即将到访访客", notes = "分页 即将到访访客")
    public R<NestPage<PlTodayWillVisitVO>> pageByTodayWillVisitParam(PlVisitorInfoReq req) {
        Page<PlVisitorInfoReq> page = new Page<>();
        page.setCurrent(req.getCurrent());
        page.setSize(req.getSize());
        IPage<PlTodayWillVisitVO> pages = plVisitorInfoService.pageByTodayWillVisitParam(page, req);
        if (Func.isEmpty(pages)){
            pages = new Page<>();
        }
        return  R.data(NestPage.of(pages));
    }

    /**
     * 我的预约/我的访客 分页
     */
    @GetMapping("/pageByMyVistorAppointment")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "我的预约/我的访客 分页", notes = "我的预约/我的访客 分页")
    public R<NestPage<PlVisitorInfoVO>> pageByMyVistorAppointment(PlMyVisitorReq req) {
        Page<PlMyVisitorReq> page = new Page<>();
        page.setCurrent(req.getCurrent());
        page.setSize(req.getSize());
        IPage<PlVisitorInfoVO> pages = plVisitorInfoService.pageByMyVistorAppointment(page, req);
        if (Func.isEmpty(pages)){
            pages = new Page<>();
        }
        return  R.data(NestPage.of(pages));
    }

    /**
     * 数量统计
     * @param parkId 项目id
     * @param statisticesType 1:人数;2:预约
     * @return
     */
    @GetMapping("/getQuantityStatistics")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "统计分析-访客数量统计", notes = "统计分析-访客数量统计")
    public R<PlVistorStatisticsVO> getQuantityStatistics(Long parkId,String statisticesType) {
        if(Func.isEmpty(parkId) || Func.isEmpty(statisticesType)){
            return R.error("项目id或者查询类型不能为空");
        }
        PlVistorStatisticsVO plVistorStatisticsVO = plVisitorInfoService.getQuantityStatistics(parkId, statisticesType);
        return  R.data(plVistorStatisticsVO);
    }

    /**
     * 统计分析-到访人数统计
     * @param parkId 项目id
     * @param dateType 时间类型 1:近7天;2:本月;3:近12月
     * @return
     */
    @GetMapping("/getVisitorNumStatistics")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "统计分析-到访人数统计", notes = "统计分析-到访人数统计")
    public R<PlVistorUserNumStatisticsVO> getVisitorNumStatistics(Long parkId, String dateType) {
        PlVistorUserNumStatisticsVO plVistorUserNumStatisticsVO = plVisitorInfoService.getVisitorNumStatistics(parkId, dateType);
        return  R.data(plVistorUserNumStatisticsVO);
    }

    /**
     * 统计分析-到访时段统计
     * @param parkId 项目id
     * @param dateType 时间类型 1:近7天;2:本月;3:近12月
     * @return
     */
    @GetMapping("/getVisitorHourStatistics")
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "统计分析-到访时段统计", notes = "统计分析-到访时段统计")
    public R<PlVistorUserNumStatisticsVO> getVisitorHourStatistics(Long parkId, String dateType) {
        PlVistorUserNumStatisticsVO plVistorUserNumStatisticsVO = plVisitorInfoService.getVisitorHourStatistics(parkId, dateType);
        return  R.data(plVistorUserNumStatisticsVO);
    }


    /**
     * 访客登记-门禁权限手动下发
     * @param visitId 访客登记id
     * @return
     */
    @GetMapping("/sendVisitorPower")
    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "访客登记-门禁权限手动下发", notes = "访客登记-门禁权限手动下发")
    public R sendVisitorPower(Long visitId) {
        return R.data(plVisitorInfoService.sendVisitorPower(visitId));
    }

    @GetMapping("/getVisitorQrcode")
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "获取访客门禁权限二维码", notes = "获取访客门禁权限二维码")
    public R getVisitorQrcode(Long visitorId) throws UnsupportedEncodingException {
        return R.data(plVisitorInfoService.getVisitorQrcode(visitorId));
    }

    @PostMapping("/exportVisitor")
    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "访客导出", notes = "访客导出")
    public void exportVisitor(@RequestBody PlVisitorInfoReq req, HttpServletResponse response) {
        plVisitorInfoService.exportVisitor(req, response);
    }


    @GetMapping("/getVisitorByParkIdCarNo")
    @ApiOperationSupport(order = 15)
    @ApiOperation(value = "根据项目和车牌号获取被访者信息", notes = "根据项目和车牌号获取被访者信息")
    public R getVisitorByParkIdCarNo(Long parkId, String carNo) {
        return R.data(plVisitorInfoService.getVisitorByParkIdCarNo(parkId,carNo));
    }

}
