package com.yx.face.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yx.face.boot.uitls.FileUploadUtil;
import com.yx.face.boot.uitls.OkHttpUtils;
import com.yx.face.dao.PushDao;
import com.yx.face.entity.Admin;
import com.yx.face.entity.SystemPushConfig;
import com.yx.face.service.PushService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * @ProjectName: face-server-netty
 * @Package: com.yx.face.service.impl
 * @ClassName: PushServiceImpl
 * @Author: 崔哥
 * @Description: 数据推送
 * @Date: 2021/11/22 15:05
 * @Version: 1.0
 */
@Slf4j
@Service
public class PushServiceImpl implements PushService {

    @Resource
    private PushDao pushDao;

    @Override
    public void push(String sn, String faceLog) {
        log.info("sn================" + sn);
        List<SystemPushConfig> list = pushDao.selectList(1);
        if (list.isEmpty()) {
            return;
        }
        JSONObject jsonObject = JSONObject.parseObject(faceLog);
        String sfzPhoto = jsonObject.getString("sfzPhoto");
        if (StringUtils.isNotBlank(sfzPhoto)){
            String sfzImgBase64 = getImgBase64(sfzPhoto);
            jsonObject.put("sfzPhoto", sfzImgBase64);
        }
        String photo = jsonObject.getString("photo");
        if (StringUtils.isNotBlank(photo)){
            String imgBase64 = getImgBase64(photo);
            jsonObject.put("photo", imgBase64);
        }
        Integer adminId = jsonObject.getInteger("adminId");
        jsonObject.remove("adminId");
        jsonObject.remove("userId");
        log.info("数据推送前；{}", jsonObject);
        JSONObject json = new JSONObject();
        json.put("data", jsonObject.toJSONString());
        log.info("数据推送后；{}", json);
        list.forEach(systemPushConfig -> {
            log.info("systemPushConfig================" + systemPushConfig);
            Admin admin = pushDao.getAdmin(systemPushConfig.getAccountId());
            if (admin.getType() == 5) {
                if (systemPushConfig.getAccountId() != adminId) {
                    log.info("数据不相等不能推；adminId {} AccountId {}  ", adminId, systemPushConfig.getAccountId());
                    return;
                }
            }
            log.info("admin" + admin);
            List<String> mapList = pushDao.checkSn(admin, sn);
            log.info("mapList================" + mapList);
            if (!mapList.isEmpty()) {
                if (StringUtils.isNotBlank(systemPushConfig.getPushUrl())) {
                    log.info("systemPushConfig.getPushUrl================" + systemPushConfig.getPushUrl());
                    String sync = OkHttpUtils.builder().url(systemPushConfig.getPushUrl()).localPost(json.toJSONString()).sync();
                    log.info("推送返回结果；{}", sync);
                }
            }
        });
    }


    @Override
    public void pushVisitor(String userVisitor) {
        log.info("sn================" + userVisitor);
        List<SystemPushConfig> list = pushDao.selectList(2);
        if (list.isEmpty()) {
            return;
        }
        log.info("数据推送前；{}", userVisitor);
        JSONObject jsonObject = JSONObject.parseObject(userVisitor);
        Integer adminId = jsonObject.getInteger("adminId");
        jsonObject.remove("adminId");
        String phone = jsonObject.getString("phone");
        String avatar = jsonObject.getString("avatar");
        if (StringUtils.isNotBlank(avatar)){
            String imgBase64 = getImgBase64(avatar);
            jsonObject.put("avatar", imgBase64);
        }
        JSONObject json = new JSONObject();
        json.put("data", jsonObject);
        log.info("数据推送后；{}", json);
        list.forEach(systemPushConfig -> {
            log.info("systemPushConfig；{}", systemPushConfig);
            Admin admin = pushDao.getAdmin(systemPushConfig.getAccountId());
            log.info("admin" + admin);
            if (admin.getType() == 5) {
                if (systemPushConfig.getAccountId() != adminId) {
                    log.info("数据不相等不能推；adminId {} AccountId {}  ", adminId, systemPushConfig.getAccountId());
                    return;
                }
            }
            List<String> mapList = pushDao.checkPhone(admin, phone);
            if (!mapList.isEmpty()) {
                log.info("mapList================" + mapList);
                if (StringUtils.isNotBlank(systemPushConfig.getPushUrl())) {
                    String sync = OkHttpUtils.builder().url(systemPushConfig.getPushUrl()).localPost(json.toJSONString()).sync();
                    log.info("推送返回结果；{}", sync);
                }
            }
        });
    }

    private String getImgBase64(String imgUrl) {
        String base64 = FileUploadUtil.downloadFile(imgUrl);
        if (StringUtils.isBlank(base64)) {
            return "";
        }
        return base64;
    }

}
