package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.domain.HwFenceTarget;
import com.ruoyi.business.domain.VO.EleMonitorDeviceSumVo;
import com.ruoyi.business.domain.VO.ElectronicFenceVo;
import com.ruoyi.business.mapper.HwFenceTargetMapper;
import com.ruoyi.common.core.constant.HwDictConstants;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.HwElectronicFenceMapper;
import com.ruoyi.business.domain.HwElectronicFence;
import com.ruoyi.business.service.IHwElectronicFenceService;

/**
 * 电子围栏Service业务层处理
 *
 * @author xins
 * @date 2023-09-04
 */
@Service
public class HwElectronicFenceServiceImpl implements IHwElectronicFenceService {
    @Autowired
    private HwElectronicFenceMapper hwElectronicFenceMapper;

    @Autowired
    private HwFenceTargetMapper hwFenceTargetMapper;

    /**
     * 查询电子围栏
     *
     * @param electronicFenceId 电子围栏主键
     * @return 电子围栏
     */
    @Override
    public HwElectronicFence selectHwElectronicFenceByElectronicFenceId(Long electronicFenceId) {
        return hwElectronicFenceMapper.selectHwElectronicFenceByElectronicFenceId(electronicFenceId);
    }

    @Override
    public List<HwElectronicFence> selectElectronicByScene(Long sceneId) {
        return hwElectronicFenceMapper.selectElectronicByScene(sceneId);
    }

    @Override
    public List<ElectronicFenceVo> selectEleTypeNums(Long sceneId) {
        List<ElectronicFenceVo> electronicFenceVos = new ArrayList<ElectronicFenceVo>();
        HwElectronicFence queryElectronicFence = new HwElectronicFence();
        queryElectronicFence.setSceneId(sceneId);
        List<HwElectronicFence> electronicFenceList = hwElectronicFenceMapper.selectHwElectronicFenceList(queryElectronicFence);
        electronicFenceList.forEach(electronicFence -> {
            ElectronicFenceVo e = new ElectronicFenceVo();
            String fenceType = electronicFence.getFenceType();
            String areaShapeFlag = electronicFence.getAreaShapeFlag();
            String areaRange = electronicFence.getAreaRange();
            Integer deviceCount = 0;
            if (fenceType.equals(HwDictConstants.FENCE_TYPE_DEVICE)
                    || fenceType.equals(HwDictConstants.FENCE_TYPE_MONITOR_UNIT)) {
                deviceCount = hwElectronicFenceMapper.selectDeviceCount(electronicFence.getElectronicFenceId());
            }

            if (fenceType.equals(HwDictConstants.FENCE_TYPE_MONITOR_UNIT)) {
                deviceCount = hwElectronicFenceMapper.selectDeviceCountByMonitorUnit(electronicFence.getElectronicFenceId());

            } else if (fenceType.equals(HwDictConstants.FENCE_TYPE_SCENE)) {
                deviceCount = hwElectronicFenceMapper.selectDeviceCountByScene(electronicFence.getSceneId());
            }

            if (areaShapeFlag.equals(HwDictConstants.AREA_SHAPE_FLAG_POLYGN)) {
                String[] areaRangeArr = areaRange.split("_");
                List<ElectronicFenceVo.AreaPoint> points = new ArrayList<ElectronicFenceVo.AreaPoint>();
                for (String areaR : areaRangeArr) {
                    String[] areaRArr = areaR.split(",");
                    ElectronicFenceVo.AreaPoint p = new ElectronicFenceVo.AreaPoint();
                    p.setLongitude(Double.valueOf(areaRArr[0]));
                    p.setLatitude(Double.valueOf(areaRArr[1]));
                    points.add(p);
                }
                e.setAreaPoints(points);

            } else if (areaShapeFlag.equals(HwDictConstants.AREA_SHAPE_FLAG_CIRCULA)) {
                String[] areaRangeArr = areaRange.split(",");
                ElectronicFenceVo.AreaPoint p = new ElectronicFenceVo.AreaPoint();
                p.setLongitude(Double.valueOf(areaRangeArr[0]));
                p.setLatitude(Double.valueOf(areaRangeArr[1]));

                e.setCenterPoint(p);
                e.setRadius(Double.valueOf(areaRangeArr[2]));
            }

            e.setAbnormalCount(2);//todo 异常数量获取
            e.setAreaShapeFlag(areaShapeFlag);
            e.setDeviceCount(deviceCount);
            electronicFenceVos.add(e);
        });

        return electronicFenceVos;
    }

    /**
     * 查询电子围栏列表
     *
     * @param hwElectronicFence 电子围栏
     * @return 电子围栏
     */
    @Override
    public List<HwElectronicFence> selectHwElectronicFenceList(HwElectronicFence hwElectronicFence) {
        return hwElectronicFenceMapper.selectHwElectronicFenceList(hwElectronicFence);
    }

    /**
     * 新增电子围栏
     *
     * @param hwElectronicFence 电子围栏
     * @return 结果
     */
    @Override
    public int insertHwElectronicFence(HwElectronicFence hwElectronicFence) {
        hwElectronicFence.setCreateTime(DateUtils.getNowDate());
        return hwElectronicFenceMapper.insertHwElectronicFence(hwElectronicFence);
    }

    /**
     * 修改电子围栏
     *
     * @param hwElectronicFence 电子围栏
     * @return 结果
     */
    @Override
    public int updateHwElectronicFence(HwElectronicFence hwElectronicFence) {
        hwElectronicFence.setUpdateTime(DateUtils.getNowDate());
        return hwElectronicFenceMapper.updateHwElectronicFence(hwElectronicFence);
    }

    /**
     * 批量删除电子围栏
     *
     * @param electronicFenceIds 需要删除的电子围栏主键
     * @return 结果
     */
    @Override
    public int deleteHwElectronicFenceByElectronicFenceIds(Long[] electronicFenceIds) {
        return hwElectronicFenceMapper.deleteHwElectronicFenceByElectronicFenceIds(electronicFenceIds);
    }

    /**
     * 删除电子围栏信息
     *
     * @param electronicFenceId 电子围栏主键
     * @return 结果
     */
    @Override
    public int deleteHwElectronicFenceByElectronicFenceId(Long electronicFenceId) {
        return hwElectronicFenceMapper.deleteHwElectronicFenceByElectronicFenceId(electronicFenceId);
    }
}
