package com.xueyi.system.pass.controller.api;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xueyi.common.core.constant.basic.SecurityConstants;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.system.api.device.domain.vo.DeviceTenantSourceMergeVo;
import com.xueyi.system.api.pass.domain.dto.DmRecognizedMultiRecordsDto;
import com.xueyi.system.api.pass.domain.dto.DmRecognizedRecordsDto;
import com.xueyi.system.api.pass.domain.po.DmRecognizedRecordsPo;
import com.xueyi.system.api.pass.feign.RemoteRecognizedRecordsService;
import com.xueyi.system.pass.mapper.DmRecognizedRecordsMapper;
import com.xueyi.system.pass.service.impl.DmRecognizedRecordsServiceImpl;
import com.xueyi.system.resource.controller.api.BaseApiController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 人员识别管理 业务处理
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/pass/api")
public class DmRecognizedRecordsApiController extends BaseApiController {
    private static final Logger log = LoggerFactory.getLogger(DmRecognizedRecordsApiController.class);
    @Autowired
    private RemoteRecognizedRecordsService recognizedRecordsFeign;

    @Autowired
    private DmRecognizedRecordsServiceImpl recognizedRecordsService;

    @ResponseBody
    @PostMapping(value = "/upload-pass", produces = "application/json;charset=UTF-8")
    public JSONObject upload(@RequestBody DmRecognizedRecordsDto recognizedRecordsDto){
        log.info("upload-pass devId={}", recognizedRecordsDto.getDevId());
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(recognizedRecordsDto.getDevId());


        return recognizedRecordsFeign.uploadPass(recognizedRecordsDto, vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);
    }

    @ResponseBody
    @PostMapping(value = "/upload-multi-pass", produces = "application/json;charset=UTF-8")
    public JSONObject uploads(@RequestBody DmRecognizedMultiRecordsDto recognizedMultiRecordsDto){
        log.info("upload-multi-pass devId={}", recognizedMultiRecordsDto.getDevId());
        DeviceTenantSourceMergeVo vo = super.getDeviceTenantSourceMergeVo(recognizedMultiRecordsDto.getDevId());
        if (recognizedMultiRecordsDto.getRecords().size() > 0)
        recognizedRecordsService.setRedisRecognizedCount(recognizedMultiRecordsDto.getRecords().size());

        return recognizedRecordsFeign.uploadMultiPass(recognizedMultiRecordsDto, vo.getTenantId(), vo.getSourceSlave(), SecurityConstants.INNER);
    }

    @Autowired
    private DmRecognizedRecordsMapper recordsMapper;

//    @Autowired
//    private RecognizedRecordsRepository recordsRepository;

    @GetMapping("/syncEs")
    public AjaxResult sync() {
        List<DmRecognizedRecordsPo> lists = recordsMapper.selectList(new QueryWrapper<>());
//        lists.stream().map(f->recordsRepository.save((EsRecognizedRecordsPo) f));
        return AjaxResult.success();
    }
}
