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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.entity.pldoor.PlDoorUserPcCapturePhotos;
import com.yuchen.saas.park.service.service.pldoor.PlDoorUserPcCapturePhotosService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 人员登记PC端拍照采集人脸 前端控制器
 * </p>
 *
 * @author miaoqs
 * @since 2024-10-30
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/plDoorUserPcCapturePhotos")
public class PlDoorUserPcCapturePhotosController {

    private final PlDoorUserPcCapturePhotosService plDoorUserPcCapturePhotosService;

    /**
     * 人员登记PC端拍照采集人脸 新增或修改
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "人员登记PC端拍照采集人脸 新增或修改", notes = "人员登记PC端拍照采集人脸 新增或修改")
    public R saveOrUpdate(@Valid @RequestBody PlDoorUserPcCapturePhotos plDoorUserPcCapturePhotos) {
        if(Func.isEmpty(plDoorUserPcCapturePhotos)) {
            return R.error("人员登记PC端拍照采集人脸不能为空");
        }
        return R.data(plDoorUserPcCapturePhotosService.saveOrUpdate(plDoorUserPcCapturePhotos));
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "人员登记PC端拍照采集人脸 详情", notes = "人员登记PC端拍照采集人脸详情")
    public R detail(Long id) {
        return R.data(plDoorUserPcCapturePhotosService.getById(id));
    }

    /**
     * 根据主表id删除人员登记PC端拍照采集人脸
     * @param plDoorUserPcCapturePhotos
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主表id 人员登记PC端拍照采集人脸 删除", notes = "根据主表id 人员登记PC端拍照采集人脸 删除")
    public R removeId(@RequestBody PlDoorUserPcCapturePhotos plDoorUserPcCapturePhotos) {
        plDoorUserPcCapturePhotosService.removeById(plDoorUserPcCapturePhotos.getId());
        return R.success();
    }

    /**
     * 分页 人员登记PC端拍照采集人脸
     */
    @GetMapping("/pageByparam")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "分页 人员登记PC端拍照采集人脸", notes = "分页 人员登记PC端拍照采集人脸")
    public R<NestPage<PlDoorUserPcCapturePhotos>> pageByparam(PlDoorUserPcCapturePhotos plDoorUserPcCapturePhotos,@RequestBody Query query) {
        IPage<PlDoorUserPcCapturePhotos> pages = plDoorUserPcCapturePhotosService.page(Condition.getPage(query),
                new LambdaQueryWrapper<PlDoorUserPcCapturePhotos>()
                        .eq(Func.isNotEmpty(plDoorUserPcCapturePhotos.getLoginUserId()),PlDoorUserPcCapturePhotos::getLoginUserId,
                                plDoorUserPcCapturePhotos.getLoginUserId())
                        .orderByDesc(PlDoorUserPcCapturePhotos::getCreateTime));
        return  R.data(NestPage.of(pages));
    }

    /**
     * 根据人员id获取3分钟内最后一条记录
     * @param userId
     * @return
     */
    @GetMapping("/getLastOneByUserId")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "根据人员id获取最后一条记录", notes = "根据人员id获取最后一条记录")
    public R getLastOneByUserId(Long userId) {
        // 获取当前时间
        Date now = new Date();
        // 计算三分钟之前的时间
        Date threeMinutesAgo = new Date(now.getTime() - 3 * 60 * 1000);
        // 设置查询条件，创建时间在三分钟之内
        PlDoorUserPcCapturePhotos capturePhotos = plDoorUserPcCapturePhotosService.getOne(new LambdaQueryWrapper<PlDoorUserPcCapturePhotos>()
                .eq(PlDoorUserPcCapturePhotos::getLoginUserId, userId)
                .between(PlDoorUserPcCapturePhotos::getCreateTime, threeMinutesAgo, now)
                .orderByDesc(PlDoorUserPcCapturePhotos::getCreateTime)
                .last("limit 1"));
        return R.data(capturePhotos);
    }

}
