package com.yx.face.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yx.face.boot.uitls.OkHttpUtils;
import com.yx.face.service.TbDeviceWhiteListService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @ProjectName: face-server
 * @Package: com.yx.face.service
 * @ClassName: TbDeviceWhiteListService
 * @Author: 崔哥
 * @Description:
 * @Date: 2021/12/7 15:34
 * @Version: 1.0
 */
@Slf4j
@Service
public class TbDeviceWhiteListServiceImpl implements TbDeviceWhiteListService {

    @Value("${tb.jkm.device.white.list.url}")
    private String tbDeviceWhiteListUrl;


    @Override
    public Boolean addOrDeleteTbDeviceWhiteList(String sn, String type) {
        JSONObject json = new JSONObject();
        json.put("sn", sn);
        OkHttpUtils sync = OkHttpUtils.builder().url(tbDeviceWhiteListUrl);
        json.put("type", type);
        String addOrDelete = "";
        //type	操作类型	是	00：新增；01：删除；10：查询
        if (StringUtils.equals("00", type)) {
            //status	int	Fou	白名单状态00：禁用；01：启用
            json.put("status", "01");
            log.info("添加 {}", json.toJSONString());
            addOrDelete = sync.localPost(json.toJSONString()).sync();
        }
        if (StringUtils.equals("01", type)) {
            json.put("status", "00");
            log.info("删除白名单 {}", json.toJSONString());
            addOrDelete = sync.localPost(json.toJSONString()).sync();
        }
        if (StringUtils.isBlank(addOrDelete)) {
            log.info("天波健康码启用停用接口返回值为addOrDelete为空{}", sn, addOrDelete);
            return true;
        }
        JSONObject addOrDeleteJsonObject = JSONObject.parseObject(addOrDelete);
        log.info("返回值；{}", addOrDelete);
        if (addOrDeleteJsonObject == null) {
            return true;
        }
        Integer code = addOrDeleteJsonObject.getInteger("code");
        if (code == null) {
            return false;
        }
        if (code == 200) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String s = selectDeviceWhiteList("111", "11");
        System.out.println(s);
    }

    public static String selectDeviceWhiteList(String sn, String type) {
        JSONObject json = new JSONObject();
        json.put("sn", "611F010T00100124");
        json.put("type", "10");
        String sync = OkHttpUtils.builder().url("http://183.237.179.218:1891/Hanghui/deviceWhiteList").localPost(json.toJSONString()).sync();
        return sync;
    }
}
