package com.xjrsoft.module.personnel.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.personnel.dto.FaceIdentifyRecordsPageDto;
import com.xjrsoft.module.personnel.dto.FaceIdentifyRecordsPageUserDto;
import com.xjrsoft.module.personnel.entity.FaceIdentifyRecords;
import com.xjrsoft.module.personnel.entity.FaceManagement;
import com.xjrsoft.module.personnel.service.IFaceIdentifyRecordsService;
import com.xjrsoft.module.personnel.vo.FaceIdentifyRecordsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/personnel" + "/faceidentifyrecords")
@Api(value = "/personnel" + "/faceidentifyrecords", tags = "人脸识别记录")
@AllArgsConstructor
public class FaceIdentifyRecordsController {
    private final IFaceIdentifyRecordsService faceIdentifyRecordsService;

    @GetMapping(value = "/page")
    @ApiOperation(value = "人脸识别记录(分页)")
    @SaCheckPermission("faceidentifyrecords:detail")
    public RT<PageOutput<FaceIdentifyRecordsVo>> Page(@Valid FaceIdentifyRecordsPageDto dto) {
        MPJLambdaWrapper<FaceIdentifyRecords> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .eq(StrUtil.isNotBlank(dto.getDeviceName()), FaceIdentifyRecords::getDeviceName, dto.getDeviceName())
                .like(StrUtil.isNotBlank(dto.getLocation()), FaceIdentifyRecords::getLocation, dto.getDeviceName())
                .like(StrUtil.isNotBlank(dto.getName()), FaceIdentifyRecords::getName, dto.getName())
                .eq(ObjectUtil.isNotNull(dto.getInoutDirection()), FaceIdentifyRecords::getInoutDirection, dto.getInoutDirection())
                .eq(ObjectUtil.isNotNull(dto.getFaceCode()) && dto.getFaceCode() != null, FaceIdentifyRecords::getFaceCode, dto.getFaceCode());

        if (ObjectUtil.isNotNull(dto.getStartTime()) && ObjectUtil.isNotNull(dto.getEndTime())) {
            queryWrapper.ge(FaceIdentifyRecords::getVerifyTime, dto.getStartTime());
            queryWrapper.le(FaceIdentifyRecords::getVerifyTime, dto.getEndTime());
        }
        queryWrapper.select(FaceIdentifyRecords.class, x -> VoToColumnUtil.fieldsToColumns(FaceIdentifyRecordsVo.class).contains(x.getProperty()))
                .select(FaceIdentifyRecords::getId)
                .orderByDesc(FaceIdentifyRecords::getVerifyTime);

        IPage<FaceIdentifyRecords> page = faceIdentifyRecordsService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<FaceIdentifyRecordsVo> pageOutput = ConventPage.getPageOutput(page, FaceIdentifyRecordsVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/user/page")
    @ApiOperation(value = "用户人脸识别记录(分页)")
    public RT<PageOutput<FaceIdentifyRecordsVo>> userPage(@Valid FaceIdentifyRecordsPageUserDto dto) {
        if (dto.getUserId() == null) {
            dto.setUserId(StpUtil.getLoginIdAsLong());
        }
        MPJLambdaWrapper<FaceIdentifyRecords> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .innerJoin(FaceManagement.class, FaceManagement::getFaceCode, FaceIdentifyRecords::getFaceCode)
                .eq(FaceManagement::getUserId, dto.getUserId())
                .eq(ObjectUtil.isNotEmpty(dto.getInoutDirection()) && dto.getInoutDirection() > 0, FaceIdentifyRecords::getInoutDirection, dto.getInoutDirection())
                .select(FaceIdentifyRecords.class, x -> VoToColumnUtil.fieldsToColumns(FaceIdentifyRecordsVo.class).contains(x.getProperty()))
                .orderByDesc(FaceIdentifyRecords::getVerifyTime);

        IPage<FaceIdentifyRecords> page = faceIdentifyRecordsService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<FaceIdentifyRecordsVo> pageOutput = ConventPage.getPageOutput(page, FaceIdentifyRecordsVo.class);
        return RT.ok(pageOutput);
    }
}
