package com.zcloud.service.video.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.video.VideoManagerMapper;
import com.zcloud.service.video.VideoManagerService;
import com.zcloud.util.Jurisdiction;
import com.zcloud.mapper.datasource.eightwork.EightWorkVideoManagerMapper;
import com.zcloud.util.PLSUtil;
import com.zcloud.util.ReturnMap;
import com.zcloud.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 说明：视频管理
 * 作者：luoxiaobao
 * 时间：2021-05-10
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class VideoManagerServiceImpl implements VideoManagerService {

    @Autowired
    private VideoManagerMapper videomanagerMapper;

    @Autowired
    private EightWorkVideoManagerMapper eightWorkVideoManagerMapper;

    /**
     * 新增
     *
     * @param pd
     * @throws Exception
     */
    public void save(PageData pd) throws Exception {
        videomanagerMapper.save(pd);
    }

    /**
     * 删除
     *
     * @param pd
     * @throws Exception
     */
    public void delete(PageData pd) throws Exception {
        videomanagerMapper.delete(pd);
    }

    @Override
    public void editZhiding(PageData pd) throws Exception {
        // TODO Auto-generated method stub
        videomanagerMapper.editZhiding(pd);
    }

    /**
     * 修改
     *
     * @param pd
     * @throws Exception
     */
    public void edit(PageData pd) throws Exception {
        videomanagerMapper.edit(pd);
    }

    public void editPls(PageData pd) throws Exception {
        videomanagerMapper.editPls(pd);
    }

    public void editIsShowBycorpinfoid(PageData pd) throws Exception {
        videomanagerMapper.editIsShowBycorpinfoid(pd);
    }

    public void editIsShow(PageData pd) throws Exception {
        videomanagerMapper.editIsShow(pd);
    }

    /**
     * 列表
     *
     * @param page
     * @throws Exception
     */
    public List<PageData> list(Page page) throws Exception {
        return videomanagerMapper.datalistPage(page);
    }

    /**
     * 列表(全部)
     *
     * @param pd
     * @throws Exception
     */
    public List<PageData> listAll(PageData pd) throws Exception {
        return videomanagerMapper.listAll(pd);
    }

    /**
     * 通过id获取数据
     *
     * @param pd
     * @throws Exception
     */
    public PageData findById(PageData pd) throws Exception {
        return videomanagerMapper.findById(pd);
    }

    /**
     * 批量删除
     *
     * @param ArrayDATA_IDS
     * @throws Exception
     */
    public void deleteAll(String[] ArrayDATA_IDS) throws Exception {
        videomanagerMapper.deleteAll(ArrayDATA_IDS);
    }

    @Override
    public PageData findByCode(PageData videoPd) {
        return videomanagerMapper.findByCode(videoPd);
    }

    @Override
    public List<PageData> getCameraAlarmCount(PageData pageData) {
        return videomanagerMapper.getCameraAlarmCount(pageData);
    }

    @Override
    public List<PageData> getCameraAlarmTypeCount(PageData pageData) {
        return videomanagerMapper.getCameraAlarmTypeCount(pageData);
    }

    @Override
    public PageData getCameraAlarmTimeCount(PageData pageData) {
        List<PageData> cameraAlarmTimeCount = videomanagerMapper.getCameraAlarmTimeCount(pageData);
        int leijicont = 0;
        // 月份集合
        List<String> monthStrList = new ArrayList<>();
        // 当月数据
        List<Integer> monthList = new ArrayList<>();
        // 累计数据
        List<Integer> accumulatedList = new ArrayList<>();
        for (PageData item : cameraAlarmTimeCount) {
            monthStrList.add(item.getString("MONTH"));
            monthList.add(Integer.parseInt(item.getString("MONTH_COUNT")));
            leijicont += Integer.parseInt(item.getString("MONTH_COUNT"));
            accumulatedList.add(leijicont);
        }
        PageData valuePd = new PageData();
        valuePd.put("monthStrList", monthStrList);
        valuePd.put("monthList", monthList);
        valuePd.put("accumulatedList", accumulatedList);
        return valuePd;
    }

    @Override
    public PageData getCameraTopToShow(PageData pageData) {
        return videomanagerMapper.getCameraTopToShow(pageData);
    }

    @Override
    public List<PageData> getCameraList(PageData pageData) throws Exception {
        pageData.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        JSONObject ryCameraList = PLSUtil.getCameraList(pageData.getString("CORPINFO_ID"));
        switch (pageData.getString("CAMREA_TYPE")) {
            case "workSafelyCamera":
                // 八项作业摄像头
                List<PageData> eightWorkVideoList = eightWorkVideoManagerMapper.listAll(pageData);
                return getMapCameraList(eightWorkVideoList, ryCameraList);
            case "videoAIAnalysis":
                // Ai 报警视频分析 全部的摄像头
                List<PageData> dataList = videomanagerMapper.listAll(pageData);
                return getMapCameraList(dataList, ryCameraList);
        }

        return new ArrayList<>();
    }

    private List<PageData> getMapCameraList(List<PageData> eightWorkVideoList, JSONObject ryCameraList) {
        HashMap<String, String> videoIds = new HashMap<>();
        HashMap<String, String> videolonlat = new HashMap<>();
        eightWorkVideoList.forEach(item -> {
                    videoIds.put(item.getString("PLS_ID"), item.getString("VIDEOMANAGER_ID"));
                    videoIds.put("NAME" + item.getString("PLS_ID"), item.getString("VIDEONAME"));
                    videoIds.put("URL" + item.getString("PLS_ID"), item.getString("VIDEOURL"));
                    videoIds.put("CODE" + item.getString("PLS_ID"), item.getString("CODE"));
                }
        );
        if (!"200".equals(ryCameraList.getString("code"))) {
            return null;
        }
        // 除去没有经纬度的摄像头。
        List<PageData> collect = JSON.parseArray(JSON.toJSONString(ryCameraList.getJSONArray("rows")), PageData.class).stream()
                .filter((item -> Tools.notEmpty(item.getString("lon")) && videoIds.containsKey(item.getString("camId")))).collect(Collectors.toList());
        collect
                .forEach(item -> {
                    item.put("VIDEOMANAGER_ID", videoIds.get(item.getString("camId")));
                    item.put("VIDEONAME", videoIds.get("NAME" + item.getString("camId")));
                    item.put("VIDEOURL", videoIds.get("URL" + item.getString("camId")));
                    item.put("CODE", videoIds.get("CODE" + item.getString("camId")));
                    videolonlat.put(item.getString("camId"), item.getString("lon") + "," + item.getString("lat"));
                });
        return collect;
    }

	@Override
	public List<PageData> listApp(Page page) {
		return videomanagerMapper.dataApplistPage(page);
	}

	@Override
	public void updateAiByOUTSOURCED_ID(PageData pd) {
		videomanagerMapper.updateAiByOUTSOURCED_ID(pd);
	}

}

