package com.sooka.sponest.monitor.broadcast.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.datascope.annotation.DataScope;
import com.ruoyi.common.datascope.annotation.DataScopeMutiDept;
import com.sooka.sponest.monitor.aidevicedept.domain.CenterdataTAidevicedept;
import com.sooka.sponest.monitor.aidevicedept.mapper.CenterdataTAidevicedeptMapper;
import com.sooka.sponest.monitor.broadcast.domain.CentermonitorTCloudBroadcastImg;
import com.sooka.sponest.monitor.camera.domain.CentermonitorTCamera;
import com.sooka.sponest.monitor.camera.domain.CentermonitorTCameraImg;
import com.sooka.sponest.monitor.camera.domain.CentermonitorTCameraType;
import com.sooka.sponest.monitor.camera.mapper.CentermonitorTCameraMapper;
import com.sooka.sponest.monitor.device.domain.CentermonitorTMonitoringDevice;
import com.sooka.sponest.monitor.device.mapper.CentermonitorTMonitoringDeviceMapper;
import com.sooka.sponest.monitor.remoteapi.service.center.system.RemoteService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sooka.sponest.monitor.broadcast.mapper.CentermonitorTCloudBroadcastMapper;
import com.sooka.sponest.monitor.broadcast.domain.CentermonitorTCloudBroadcast;
import com.sooka.sponest.monitor.broadcast.service.ICentermonitorTCloudBroadcastService;
import com.sooka.sponest.monitor.base.service.impl.BaseService;

import javax.annotation.Resource;

/**
 * 云广播设备Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-23
 */
@Service
public class CentermonitorTCloudBroadcastServiceImpl extends BaseService implements ICentermonitorTCloudBroadcastService {
    @Resource
    private CentermonitorTCloudBroadcastMapper centermonitorTCloudBroadcastMapper;

    @Resource
    private CenterdataTAidevicedeptMapper centerdataTAidevicedeptMapper;
    @Resource
    private CentermonitorTCameraMapper centermonitorTCameraMapper;

    @Resource
    private RemoteService remoteService;

    /**
     * 查询云广播设备
     *
     * @param id 云广播设备主键
     * @return 云广播设备
     */
    @Override
    public CentermonitorTCloudBroadcast selectCentermonitorTCloudBroadcastById(String id) {
        /**图片前缀**/
        List<String> imgList = new ArrayList<>();
        R<String> url = remoteService.selectConfigKey_fileUrl();
        CentermonitorTCloudBroadcast cloudBroadcast = centermonitorTCloudBroadcastMapper.selectCentermonitorTCloudBroadcastById(id);
        List<CentermonitorTCloudBroadcast> list = centermonitorTCloudBroadcastMapper.selectTypesss(cloudBroadcast.getId());
        cloudBroadcast.setBroadcastSystem(list);
        ArrayList<CentermonitorTCloudBroadcastImg> broadcastImg = centermonitorTCloudBroadcastMapper.selectCentermonitorTBroadcastImgByBroadcastId(id);
        for (CentermonitorTCloudBroadcastImg img : broadcastImg) {
            Map<String, String> map = new HashMap<>();
            String imgPath = img.getBroadcastImg();
            if (imgPath != null && !imgPath.isEmpty()) {
                img.setBroadcastImg(url.getData() + imgPath);
                imgList.add(url.getData() + imgPath);
            }
        }
        cloudBroadcast.setBroadcastImagesList(imgList);
        return cloudBroadcast;
    }

    /**
     * 查询云广播设备列表
     *
     * @param centermonitorTCloudBroadcast 云广播设备
     * @return 云广播设备
     */
    @Override
    @DataScopeMutiDept(deptAlias = "d")
    public List<CentermonitorTCloudBroadcast> selectCentermonitorTCloudBroadcastList(CentermonitorTCloudBroadcast centermonitorTCloudBroadcast) {
        setSookaDataBase(centermonitorTCloudBroadcast);
        return centermonitorTCloudBroadcastMapper.selectCentermonitorTCloudBroadcastList(centermonitorTCloudBroadcast);
    }

    /**
     * 查询云广播设备列表(所有)
     *
     * @param centermonitorTCloudBroadcast 监测设备
     * @return 监测设备
     */
    @Override
    @DataScopeMutiDept(deptAlias = "d")
    public List<CentermonitorTCloudBroadcast> selectCentermonitorTCloudBroadcastListAll(CentermonitorTCloudBroadcast centermonitorTCloudBroadcast)
    {
        setSookaDataBase(centermonitorTCloudBroadcast);
        return centermonitorTCloudBroadcastMapper.selectCentermonitorTCloudBroadcastListAll(centermonitorTCloudBroadcast);
    }

    /**
     * 查询部门数据列表
     *
     * @param centerdataTAidevicedept 部门数据列表
     * @return 监测设备集合
     */
    @Override
    public List<CenterdataTAidevicedept> selectDeptBroadcastData(CenterdataTAidevicedept centerdataTAidevicedept) {
        return centermonitorTCloudBroadcastMapper.selectDeptBroadcastData(centerdataTAidevicedept);
    }


    /**
     * 查询云广播设备列表根据部门id查询
     *
     * @param str 云广播设备
     * @return 云广播设备
     */
    @Override
    public List<CentermonitorTCloudBroadcast> selectListByDeptId(List<String> str) {
        return centermonitorTCloudBroadcastMapper.selectListByDeptId(str);
    }

    /**
     * 新增云广播设备
     *
     * @param centermonitorTCloudBroadcast 云广播设备
     * @return 结果
     */
    @Override
    public int insertCentermonitorTCloudBroadcast(CentermonitorTCloudBroadcast centermonitorTCloudBroadcast) {
        for (int i = 0; i < centermonitorTCloudBroadcast.getBroadcastSystem().size(); i++) {
            CentermonitorTCameraType type = new CentermonitorTCameraType();
            type.setId(IdUtils.simpleUUID());
            type.setCameraId(centermonitorTCloudBroadcast.getId().toString());
            type.setType(centermonitorTCloudBroadcast.getBroadcastSystem().get(i).getType());
            type.setTypeName(centermonitorTCloudBroadcast.getBroadcastSystem().get(i).getTypeName());
            centermonitorTCloudBroadcastMapper.insertCenterBroadcastTCameraType(type);
        }
        R<String> url = remoteService.selectConfigKey_fileUrl();
        List<CentermonitorTCloudBroadcastImg> images = new ArrayList<>();
        for (String file : centermonitorTCloudBroadcast.getBroadcastImages()) {
            CentermonitorTCloudBroadcastImg image = new CentermonitorTCloudBroadcastImg();
            image.setBroadcastImg(file.substring(file.indexOf("group1")));
            image.setBroadcastId(centermonitorTCloudBroadcast.getId());
            images.add(image);
        }
        if (!images.isEmpty()) {
            centermonitorTCloudBroadcastMapper.insertCentermonitorTBroadcastImg(images);
        }
        return centermonitorTCloudBroadcastMapper.insertCentermonitorTCloudBroadcast(centermonitorTCloudBroadcast);
    }

    /**
     * 修改云广播设备
     *
     * @param centermonitorTCloudBroadcast 云广播设备
     * @return 结果
     */
    @Override
    public int updateCentermonitorTCloudBroadcast(CentermonitorTCloudBroadcast centermonitorTCloudBroadcast) {
        centermonitorTCloudBroadcastMapper.deleteBroadcastTCameraTypeById(centermonitorTCloudBroadcast.getId());
        for (int i = 0; i < centermonitorTCloudBroadcast.getBroadcastSystem().size(); i++) {
            CentermonitorTCameraType type = new CentermonitorTCameraType();
            type.setId(IdUtils.simpleUUID());
            type.setCameraId(centermonitorTCloudBroadcast.getId());
            type.setType(centermonitorTCloudBroadcast.getBroadcastSystem().get(i).getType());
            type.setTypeName(centermonitorTCloudBroadcast.getBroadcastSystem().get(i).getTypeName());
            centermonitorTCloudBroadcastMapper.insertCenterBroadcastTCameraType(type);
        }
        List<CentermonitorTCloudBroadcastImg> imagesList = new ArrayList<>();
        List<String> broadcastImagesList = centermonitorTCloudBroadcast.getBroadcastImages();
        centermonitorTCloudBroadcastMapper.deleteImageByBroadcastId(centermonitorTCloudBroadcast.getId());
        for (String image : broadcastImagesList) {
            if (image != null && image.contains("group")) {
                // 修改中间表记录的 cameraImg 字段
                CentermonitorTCloudBroadcastImg img = new CentermonitorTCloudBroadcastImg();
                img.setBroadcastImg(image.substring(image.indexOf("group1")));
                img.setBroadcastId(centermonitorTCloudBroadcast.getId());
                imagesList.add(img);
            }
        }
        if (!imagesList.isEmpty()) {
            centermonitorTCloudBroadcastMapper.insertCentermonitorTBroadcastImg(imagesList);
        }
        return centermonitorTCloudBroadcastMapper.updateCentermonitorTCloudBroadcast(centermonitorTCloudBroadcast);
    }

    /**
     * 批量删除云广播设备
     *
     * @param ids 需要删除的云广播设备主键
     * @return 结果
     */
    @Override
    public int deleteCentermonitorTCloudBroadcastByIds(String[] ids) {
        centerdataTAidevicedeptMapper.deleteCenterdataTAidevicedeptByCameraIds(ids);
        for (int i = 0; i < ids.length; i++) {
            centermonitorTCameraMapper.deleteCentermonitorTCameraTypeById(ids[i]);
        }
        return centermonitorTCloudBroadcastMapper.deleteCentermonitorTCloudBroadcastByIds(ids);
    }

    /**
     * 删除云广播设备信息
     *
     * @param id 云广播设备主键
     * @return 结果
     */
    @Override
    public int deleteCentermonitorTCloudBroadcastById(String id) {
        centermonitorTCameraMapper.deleteCentermonitorTCameraTypeById(id);
        return centermonitorTCloudBroadcastMapper.deleteCentermonitorTCloudBroadcastById(id);
    }

    /**
     * 查询附近云广播列表
     */
    @Override
    public List<CentermonitorTCloudBroadcast> selectNearHorn(String longitude,String latitude,String broadcastUse,String distance,String type){
        CentermonitorTCloudBroadcast centermonitorTCloudBroadcast = new CentermonitorTCloudBroadcast();
        centermonitorTCloudBroadcast.setLongitude(longitude);
        centermonitorTCloudBroadcast.setLatitude(latitude);
        centermonitorTCloudBroadcast.setBroadcastUse(broadcastUse);
        centermonitorTCloudBroadcast.setDistance(distance);
        centermonitorTCloudBroadcast.setType(type);
        setSookaDataBase(centermonitorTCloudBroadcast);
        return centermonitorTCloudBroadcastMapper.selectNearHorn(centermonitorTCloudBroadcast);
    }
}
