package com.tzld.piaoquan.api.job.wecom.thirdpart;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.tzld.piaoquan.api.common.enums.wecom.ThirdPartWeComStaffStatusEnum;
import com.tzld.piaoquan.api.component.WeComThirdPartyApiClient;
import com.tzld.piaoquan.api.dao.mapper.wecom.thirdpart.ThirdPartWeComStaffMapper;
import com.tzld.piaoquan.api.model.param.wecom.thirdpart.CommonResponse;
import com.tzld.piaoquan.api.model.param.wecom.thirdpart.LoginInfo;
import com.tzld.piaoquan.api.model.param.wecom.thirdpart.UuidRequest;
import com.tzld.piaoquan.api.model.po.wecom.thirdpart.ThirdPartWeComStaff;
import com.tzld.piaoquan.api.model.po.wecom.thirdpart.ThirdPartWeComStaffExample;
import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
import com.tzld.piaoquan.growth.common.utils.RedisUtils;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Slf4j
@Component
public class WeComAccountJob {

    @Autowired
    ThirdPartWeComStaffMapper thirdPartWeComStaffMapper;
    @Autowired
    WeComThirdPartyApiClient apiClient;

    @Autowired
    private RedisUtils redisUtils;

    @XxlJob("checkAccountOnline")
    public ReturnT<String> checkAccountOnline(String param) {
        List<ThirdPartWeComStaff> activeStaffList = getCheckOnlineStaffList();
        for (ThirdPartWeComStaff staff : activeStaffList) {
            String uuid = staff.getThirdUuid();
            String offLineKey = "wecom:thirdpart:offline:" + uuid;
            if (redisUtils.containsKey(offLineKey)) {
                continue;
            }
            String response = apiClient.getRunClientByUuid(new UuidRequest(uuid));
            CommonResponse<LoginInfo> commonResponse =
                    JSONObject.parseObject(response, new TypeReference<CommonResponse<LoginInfo>>() {});
            if (commonResponse.getErrcode() != 0
                    || Objects.isNull(commonResponse.getData().getUser_info())
                    || Objects.isNull(commonResponse.getData().getUser_info().getObject())) {
                redisUtils.set(offLineKey, "1", 30 * 60);
                LarkRobotUtil.sendWeComThirdPartMessage(
                        "【账号掉线检测通知】\n" +
                                "账号名称：" + staff.getName() + "\n" +
                                "账号VID：" + staff.getThirdStaffId() + "\n" +
                                "账号UUID：" + uuid + "\n" +
                                "账号状态：已下线");
                if (staff.getStatus() == ThirdPartWeComStaffStatusEnum.NORMAL.getVal()) {
                    staff.setStatus(ThirdPartWeComStaffStatusEnum.OFFLINE.getVal());
                    thirdPartWeComStaffMapper.updateByPrimaryKeySelective(staff);
                }
            }
        }
        return ReturnT.SUCCESS;
    }

    private List<ThirdPartWeComStaff> getCheckOnlineStaffList() {
        List<Integer> statusList = Arrays.asList(ThirdPartWeComStaffStatusEnum.NORMAL.getVal(),
                ThirdPartWeComStaffStatusEnum.OFFLINE.getVal());
        ThirdPartWeComStaffExample example = new ThirdPartWeComStaffExample();
        example.createCriteria().andStatusIn(statusList);
        return thirdPartWeComStaffMapper.selectByExample(example);
    }
}
