package com.bonus.app.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bonus.app.entity.DeviceWarnEntity;
import com.bonus.app.entity.ViolationInfoEntity;
import com.bonus.app.mapper.WarnReminderMapper;
import com.bonus.app.service.IWarnReminderService;
import com.bonus.common.core.constant.HttpStatus;
import com.bonus.common.core.constant.SecurityConstants;
import com.bonus.common.core.domain.R;
import com.bonus.common.core.utils.encryption.Sm4Utils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.entity.app.AppParamsDto;
import com.bonus.common.entity.app.vo.SafetyHatVo;
import com.bonus.system.api.RemoteFileService;
import com.bonus.system.api.domain.SysFile;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * app 接口实现层
 */
@Service
@Slf4j
public class WarnReminderServiceImpl implements IWarnReminderService {

    @Autowired
    private WarnReminderMapper mapper;

    @Resource
    private RemoteFileService remoteFileService;

    /**
     * 查询预警提醒 设备预警列表
     * @param proId
     * @return
     */
    @Override
    public AjaxResult getWarnReminderList(Long proId) {
        Map<String,Object> map = new HashMap<>();
        List<ViolationInfoEntity> listVio = new ArrayList<>();
        List<DeviceWarnEntity> listDev = new ArrayList<>();
        try {
            listVio = mapper.getViolationInfoList(proId);
            for (ViolationInfoEntity vo : listVio) {
                vo = handleData(vo);
            }
            listDev = mapper.getDeviceInfoList(proId);
            map.put("ViolationInfoList",listVio);
            map.put("DeviceWarnList",listDev);
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return AjaxResult.success(map);
        }

    /**
     * 查询预警提醒 违章识别列表
     * @param proId
     * @return
     */
    @Override
    public PageInfo<DeviceWarnEntity> getDeviceWarnList(Long proId) {
        List<DeviceWarnEntity> listDev = new ArrayList<>();
        try {
            listDev = mapper.getDeviceInfoList(proId);
        } catch (Exception e) {
            log.error(e.toString(),e);
        }
        PageInfo<DeviceWarnEntity> pageInfo = new PageInfo<>(listDev);
        return pageInfo;
    }

    @Override
    public PageInfo<ViolationInfoEntity> getViolationInfoList(Long proId) {
        List<ViolationInfoEntity> listVio = new ArrayList<>();
        try {
            listVio = mapper.getViolationInfoList(proId);
            for (ViolationInfoEntity vo : listVio) {
                vo = handleData(vo);
            }
        } catch (Exception e) {
            log.error(e.toString(),e);
        }
        PageInfo<ViolationInfoEntity> pageInfo = new PageInfo<>(listVio);
        return pageInfo;
    }


    public ViolationInfoEntity handleData(ViolationInfoEntity vo) {
        R<SysFile> result = remoteFileService.getImgBase64(vo.getFilePath(), SecurityConstants.INNER);
        if (result != null && result.getCode() == HttpStatus.SUCCESS && result.getData() != null) {
            String jsonString = JSON.toJSONString(result.getData());
            JSONObject item = JSON.parseObject(jsonString);
            String base64 = item.getString("url");
            vo.setBase64Url(base64);
        }
        return vo;
    }

}
