package jnpf.attendance.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import jnpf.SocketApi;
import jnpf.attendance.annotation.Machine;
import jnpf.attendance.service.AttendanceUserFaceService;
import jnpf.attendance.service.MachineStrategy;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.enums.attendance.ActionEnum;
import jnpf.enums.attendance.MachineEnum;
import jnpf.model.attendance.vo.UserFaceVo;
import jnpf.permission.V2UserApi;
import jnpf.permission.model.user.PartUserInfoVo;
import jnpf.permission.vo.v2.user.UserBoundVO;
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 2024-04-03
 */
@Slf4j
@Component(value = "machineKaiJiaYiStrategy")
public class MachineKaiJiaYiStrategyImpl implements MachineStrategy {

    @Autowired
    private AttendanceUserFaceService attendanceUserFaceService;

    @Resource
    private V2UserApi v2UserApi;

    @Autowired
    private SocketApi socketApi;

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

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

        // 查询用户人脸数据
        UserFaceVo userFace = attendanceUserFaceService.getUserFace(user.getUserId());
        if (null == userFace) {
            log.error("用户暂未录入人脸: {}", user.getUserId());
            return;
        }
        JSONObject json = new JSONObject();
        json.set("method", "person/create");
        json.set("timestamp", System.currentTimeMillis());
        JSONArray array = new JSONArray();
        JSONObject userJson = new JSONObject();
        userJson.set("age", 0);
        userJson.set("name", user.getRealName());
        userJson.set("imgUrl", userFace.getFaceData());
        Integer sex = user.getGender();
        if (null != sex) {
            if (2 == sex) {
                sex = 0;
            }
            if (3 == sex) {
                sex = 1;
            }
        } else {
            sex = 1;
        }
        userJson.set("sex", sex);
        userJson.set("type", 3);
        userJson.set("vipId", user.getUserNo());
        JSONObject extraJson = new JSONObject();
        extraJson.set("tenantId", userInfo.getTenantId());
        extraJson.set("userId", user.getUserId());
        userJson.set("extra", extraJson);
        array.add(userJson);
        json.set("body", array);
        socketApi.sendMsg2Client(getMachine().getValue(), sn, json.toString());
    }

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

        JSONObject json = new JSONObject();
        json.set("method", "person/delete");
        json.set("timestamp", System.currentTimeMillis());
        JSONObject jsonBody = new JSONObject();
        StringBuilder sb = new StringBuilder();
        // 根据userIds查询userNo

        ActionResult<List<UserBoundVO>> allUserInfoBatch = v2UserApi.getAllUserInfoBatch(userIds, null);
        if (200 == allUserInfoBatch.getCode() && !allUserInfoBatch.getData().isEmpty()) {
            allUserInfoBatch.getData().forEach(n -> sb.append(n.getUserNo()).append(","));
            jsonBody.set("deleteId", sb.substring(0, sb.length() - 1));
            json.set("body", jsonBody);
            socketApi.sendMsg2Client(getMachine().getValue(), sn, json.toString());
        }
        //        List<UserNoInfoVo> noList = userApi.getUserNoByIds(userIds);
//        if (null != noList && !noList.isEmpty()) {
//            noList.forEach(n -> sb.append(n.getUserNo()).append(","));
//            jsonBody.set("deleteId", sb.substring(0, sb.length() - 1));
//            json.set("body", jsonBody);
//            socketApi.sendMsg2Client(getMachine().getValue(), sn, json.toString());
//        }
    }

    @Override
    public void syncMachineMember(String sn) {

        JSONObject json = new JSONObject();
        json.set("method", "person/list");
        json.set("timestamp", System.currentTimeMillis());
        JSONObject body = new JSONObject();
        body.set("current", 1);
        body.set("size", 999);
        body.set("needImage", false);
        json.set("body", body);
        socketApi.sendMsg2Client(getMachine().getValue(), sn, json.toString());
    }
}
