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

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.common.mqtt.EmqClient;
import com.xjrsoft.common.utils.ImageUtil;
import com.xjrsoft.module.personnel.entity.FaceManagement;
import com.xjrsoft.module.personnel.mapper.FaceManagementMapper;
import com.xjrsoft.module.personnel.service.IFaceManagementService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@AllArgsConstructor
@Slf4j
public class FaceManagementServiceImpl extends MPJBaseServiceImpl<FaceManagementMapper, FaceManagement> implements IFaceManagementService {
    private final FaceManagementMapper faceManagementMapper;


    private EmqClient emqClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(FaceManagement faceManagement) {
        FaceManagement old = faceManagementMapper.selectOne(Wrappers.<FaceManagement>query().lambda().eq(FaceManagement::getFaceCode, faceManagement.getFaceCode()));
        if (old != null) {
            throw new MyException(String.format("人脸编码【%s】已存在！", faceManagement.getFaceCode()));
        }

        faceManagement.setVerifyStatus(2); // 自动通过
        faceManagementMapper.insert(faceManagement);

        // 推送数据客户端
//        JSONObject jsonObject = JSONUtil.createObj()
//                .put("id", faceManagement.getId().toString())
//                .put("register_base64", ImageUtil.base64RemovePrefix(faceManagement.getRegisterBase64()));
//
//        try {
//            emqClient.publish("/face/device/personnel/image/verify", jsonObject);
//        } catch (MqttException e) {
//            log.error("mqtt", "publish", "/face/device/personnel/image/verify", e.getMessage());
//        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(FaceManagement faceManagement) {
        faceManagement.setVerifyStatus(1);
        faceManagement.setSyncStatus(1);
        if (faceManagement.getRegisterBase64() == null) {
            FaceManagement old = faceManagementMapper.selectById(faceManagement.getId());
            faceManagement.setRegisterBase64(old.getRegisterBase64());
        }
        faceManagementMapper.updateById(faceManagement);

        // 推送数据客户端
        JSONObject jsonObject = JSONUtil.createObj()
                .put("id", faceManagement.getId().toString())
                .put("register_base64", ImageUtil.base64RemovePrefix(faceManagement.getRegisterBase64()));

        try {
            emqClient.publish("/face/device/personnel/image/verify", jsonObject);
        } catch (MqttException e) {
            log.error("mqtt", "publish", "/face/device/personnel/image/verify", e.getMessage());
        }

        return true;
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        LambdaQueryWrapper<FaceManagement> queryWrapper = new LambdaQueryWrapper<FaceManagement>().in(FaceManagement::getId, ids);
        List<FaceManagement> oldCars = faceManagementMapper.selectList(queryWrapper);
        // 删除数据
        faceManagementMapper.deleteBatchIds(ids);

        for (FaceManagement item : oldCars) {
            // 推送数据客户端
            JSONObject jsonObject = JSONUtil.createObj()
                    .put("employee_number", item.getFaceCode());

            try {
                emqClient.publish("/face/device/personnel/delete", jsonObject);
            } catch (MqttException e) {
                log.error("mqtt", "publish", "/face/device/personnel/delete", e.getMessage());
            }
        }

        return true;
    }
}
