package com.usky.meeting.controller.web;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.meeting.annotation.FaceLog;
import com.usky.meeting.domain.MeetingFace;
import com.usky.meeting.service.MeetingFaceService;
import com.usky.meeting.service.vo.FaceResultVO;
import com.usky.meeting.service.vo.MeetingFaceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyj
 * @since 2024-03-08
 */
@RestController
@RequestMapping("/meetingFace")
@Api("人脸验证接口")
@Slf4j
public class MeetingFaceController {
    @Autowired
    MeetingFaceService faceService;

    @PostMapping("/vef")
    @ApiOperation(value="人脸验证", notes="根据传入的base64编码和数据的base64编码进行对比")
    @FaceLog
    public MeetingFaceVO faceVef(@RequestBody String imageBase){
        return faceService.vef(imageBase);
    }

    @GetMapping("/faceList")
    @ApiOperation(value = "人脸列表",notes = "查询所有的人脸信息")
    public ApiResult<CommonPage<MeetingFace>> faceList(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam(required = false) String faceName,
            @RequestParam(required = false) String faceStatus
    ){
        IPage<MeetingFace> page = faceService.page(new Page(current, size), new QueryWrapper<MeetingFace>()
                .like(StrUtil.isNotBlank(faceName),"face_name",faceName)
                .like(StrUtil.isNotBlank(faceStatus),"face_status",faceStatus)
                .eq("tenant_id", SecurityUtils.getTenantId()));
        return ApiResult.success(new CommonPage<>(page.getRecords(),page.getTotal(),size,current));
    }


    @GetMapping("/faceDelete/{fid}")
    @ApiOperation(value = "删除人脸",notes = "根据id进行删除")
    public FaceResultVO faceDelete(@PathVariable Integer fid){
        faceService.removeById(fid);
        return FaceResultVO.success("删除成功");
    }

    @GetMapping("/info/{fid}")
    @ApiOperation(value = "查询方法",notes = "根据id查询人脸信息")
    public FaceResultVO info(@PathVariable Integer fid){
        return FaceResultVO.success(faceService.getById(fid));
    }

    @PostMapping("/save")
    @ApiOperation(value = "添加")
    public FaceResultVO save(@RequestBody MeetingFace face){
        face.setVefNum(0);
        face.setCreateTime(LocalDateTime.now());
        face.setTenantId(SecurityUtils.getTenantId());
        faceService.save(face);
        return FaceResultVO.success("添加成功");
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public FaceResultVO update(@RequestBody MeetingFace face){
        faceService.updateById(face);
        return FaceResultVO.success("修改成功");
    }
}

