package jnpf.attendance.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jnpf.FeignConfig;
import jnpf.SocketApi;
import jnpf.attendance.annotation.Machine;
import jnpf.attendance.mapper.AttendanceUserFaceMapper;
import jnpf.attendance.service.MachineStrategy;
import jnpf.base.UserInfo;
import jnpf.entity.attendance.AttendanceUserFace;
import jnpf.enums.attendance.ActionEnum;
import jnpf.enums.attendance.MachineEnum;
import jnpf.permission.UserApi;
import jnpf.permission.model.user.PartUserInfoVo;
import jnpf.util.ConstantUtil;
import jnpf.utils.FeignHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 科密考勤策略模式
 *
 * @author yanwenfu
 * @create 2025-08-21
 */
@Slf4j
@Component(value = "machineKeMiStrategy")
public class MachineKeMiStrategyImpl implements MachineStrategy {

    @Autowired
    private SocketApi socketApi;
    @Autowired
    private UserApi userApi;
    @Resource
    private AttendanceUserFaceMapper attendanceUserFaceMapper;

    @Override
    public MachineEnum getMachine() {
        return MachineEnum.KE_MI;
    }

    @Machine(dealAction = ActionEnum.XIA_FA, factory = MachineEnum.KE_MI)
    @Override
    public void addUserToMachine(UserInfo userInfo, PartUserInfoVo user, String sn) {

        // 查询用户照片
        LambdaQueryWrapper<AttendanceUserFace> userFaceQueryWrapper = new LambdaQueryWrapper<AttendanceUserFace>()
                .eq(AttendanceUserFace::getUserId, user.getUserId())
                .eq(AttendanceUserFace::getDeleteMark, ConstantUtil.NUM_FALSE);
        AttendanceUserFace userFace = attendanceUserFaceMapper.selectOne(userFaceQueryWrapper);
        log.error("用户照片信息：{}", userFace);
        JSONObject j1 = new JSONObject();
        j1.set("cmd_code", "SET_USER_INFO");
        j1.set("uId", user.getUserId());
        JSONArray array = new JSONArray();
        JSONObject j2 = new JSONObject();
        j2.set("userId", user.getUserId());
        j2.set("name", user.getRealName());
        if (null != userFace) {
            j2.set("photo", userFace.getFaceData());
            j2.set("photoEnroll", 1);
        }
        array.set(j2);
        j1.set("users", array);
        log.error("FeignHolder: {}", FeignHolder.get());
        socketApi.sendMsg2ClientNoToken(getMachine().getValue(), sn, j1.toString(), userInfo.getTenantId());
    }

    @Machine(dealAction = ActionEnum.SHAN_CHU, factory = MachineEnum.KE_MI)
    @Override
    public void deleteUserList(UserInfo userInfo, List<String> userIds, String sn) {

        if (!userIds.isEmpty()) {
            JSONObject json = new JSONObject();
            json.set("cmd_code", "DELETE_USER");
            json.set("usersCount", userIds.size());
            json.set("usersId", userIds);
            socketApi.sendMsg2ClientNoToken(getMachine().getValue(), sn, json.toString(), userInfo.getTenantId());
        }
    }

    @Override
    public void syncMachineMember(String sn) {

    }
}
