package jnpf.attendance.service.impl;

import cn.hutool.json.JSONObject;
import jnpf.SocketApi;
import jnpf.attendance.service.IsPerfMachineService;
import jnpf.enums.attendance.MachineEnum;
import jnpf.util.ConstantUtil;
import jnpf.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * KIPS服务实现
 *
 * @author yanwenfu
 * @create 2024-04-01
 */
@Slf4j
@Service
public class IsPerfMachineServiceImpl implements IsPerfMachineService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private SocketApi socketApi;

    @Override
    public void welcome() {

        try {
            // new NettyServer(nettyPort).bind();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    @Override
    public void updateApp(Map<String, Object> params) {

        String apkUrl = params.get("apkUrl").toString();
        String md5 = params.get("md5").toString();
        JSONObject json = new JSONObject();
        json.set("method", "updateApp");
        json.set("timestamp", System.currentTimeMillis());
        JSONObject body = new JSONObject();
        body.set("apkUrl", apkUrl);
        body.set("md5", md5);
        json.set("body", body);
        List<String> macList = redisUtil.getHashKeys(String.format(ConstantUtil.ONLINE_MAC, MachineEnum.KAI_JIA_YI.getValue()));
        if (!macList.isEmpty()) {
            macList.forEach(mac -> {
                socketApi.sendMsg2Client(MachineEnum.KAI_JIA_YI.getValue(), mac, json.toString());
            });
        }
    }

    @Override
    public List<String> getAllOnlineClient() {

        return redisUtil.getHashKeys(String.format(ConstantUtil.ONLINE_MAC, MachineEnum.KAI_JIA_YI.getValue()));
    }
}
