package com.xjrsoft.module.personnel.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.module.hikvision.mapper.HikvisionDataMapper;
import com.xjrsoft.module.hikvision.util.ApiUtil;
import com.xjrsoft.module.personnel.entity.FaceManagement;
import com.xjrsoft.module.personnel.entity.TeacherFaceProcess;
import com.xjrsoft.module.personnel.mapper.TeacherFaceProcessMapper;
import com.xjrsoft.module.personnel.service.IFaceManagementService;
import com.xjrsoft.module.personnel.service.ITeacherFaceProcessService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @title: 人脸信息审核
* @Author dzx
* @Date: 2024-05-10
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class TeacherFaceProcessServiceImpl extends MPJBaseServiceImpl<TeacherFaceProcessMapper, TeacherFaceProcess> implements ITeacherFaceProcessService {
    private final HikvisionDataMapper hikvisionDataMapper;

    private final IFaceManagementService faceManagementService;


    //删除人脸后，移除海康那边的人脸
    @Override
    public Boolean removeByIds(List<Long> ids) {
        List<TeacherFaceProcess> list = this.baseMapper.selectBatchIds(ids);
        ApiUtil apiUtil = new ApiUtil();
        String apiPath = "/api/resource/v1/face/single/delete";
        JsonParser parser = new JsonParser();
        for (TeacherFaceProcess process : list) {
            String studentHikvisionId = hikvisionDataMapper.getTeacherHikvisionId(process.getUserId());

            String apiUrl = "/api/resource/v1/person/condition/personInfo";
            JsonObject paramsJson = new JsonObject();
            paramsJson.addProperty("paramName", "personId");
            JsonArray array = new JsonArray();
            array.add(studentHikvisionId);
            paramsJson.add("paramValue", array);
            String personInfoRes = apiUtil.doPost(apiUrl, paramsJson.toString(), null);
            String faceId = null;
            JsonObject personInfoData = parser.parse(personInfoRes).getAsJsonObject();
            if("0".equals(personInfoData.get("code").getAsString()) && "success".equals(personInfoData.get("msg").getAsString())){
                JsonObject dataJson = personInfoData.get("data").getAsJsonObject();
                if(dataJson.get("total").getAsInt() > 0){
                    JsonArray personPhoto = dataJson.get("list").getAsJsonArray().get(0)
                            .getAsJsonObject().get("personPhoto").getAsJsonArray();
                    if(personPhoto.size() > 0){
                        faceId = personPhoto.get(0).getAsJsonObject().get("personPhotoIndexCode").getAsString();
                    }

                }
            }


            JsonObject paramJson = new JsonObject();
            paramJson.addProperty("faceId", faceId);

            String result = apiUtil.doPost(apiPath, paramJson.toString(), null);
            process.setExamStatus(2);
            process.setHikvisionResult(result);
            process.setDeleteMark(DeleteMark.DELETED.getCode());
            process.setModifyUserId(StpUtil.getLoginIdAsLong());
            process.setModifyDate(new Date());
            this.baseMapper.updateById(process);

            FaceManagement faceManagement = faceManagementService.getOne(
                    new QueryWrapper<FaceManagement>().lambda()
                            .eq(FaceManagement::getUserId, process.getUserId())
                            .eq(FaceManagement::getFileId, process.getFacePhoto())
            );
            if(faceManagement != null){
                faceManagement.setRegisterBase64(null);
                faceManagementService.update(faceManagement);
            }

        }
        return true;
    }
}
