package com.ruoyi.system.service.impl;

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

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.TimeStampUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.AMapFenceVo;
import com.ruoyi.system.domain.vo.SearchLocationInfoVo;
import com.ruoyi.system.mapper.TElectronicFenceMapper;
import com.ruoyi.system.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TSearchLocationInfoMapper;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-16
 */
@Service
public class TSearchLocationInfoServiceImpl implements ITSearchLocationInfoService {
    private static final Logger log = LoggerFactory.getLogger(TSearchLocationInfoServiceImpl.class);

    @Autowired
    private TSearchLocationInfoMapper tSearchLocationInfoMapper;

    @Autowired
    private ITDeviceListService tDeviceListService;

    @Autowired
    private ITUserProfileService userProfileService;

    @Autowired
    private ITShouhuanInfoService shouhuanInfoService;
    @Autowired
    private TElectronicFenceMapper tElectronicFenceMapper;
    @Autowired
    private ITElectronicFenceKeyService iElectronicFenceKeyService;
    @Autowired
    private ITElectronicFenceServiceService iElectronicFenceServiceService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override

    public TSearchLocationInfo selectTSearchLocationInfoById(Long id) {
        return tSearchLocationInfoMapper.selectTSearchLocationInfoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param tSearchLocationInfo 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TSearchLocationInfo> selectTSearchLocationInfoList(TSearchLocationInfo tSearchLocationInfo) {
        return tSearchLocationInfoMapper.selectTSearchLocationInfoList(tSearchLocationInfo);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param tSearchLocationInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTSearchLocationInfo(TSearchLocationInfo tSearchLocationInfo) {
        tSearchLocationInfo.setCreateTime(DateUtils.getNowDate());
        return tSearchLocationInfoMapper.insertTSearchLocationInfo(tSearchLocationInfo);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param tSearchLocationInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTSearchLocationInfo(TSearchLocationInfo tSearchLocationInfo) {
        tSearchLocationInfo.setUpdateTime(DateUtils.getNowDate());
        return tSearchLocationInfoMapper.updateTSearchLocationInfo(tSearchLocationInfo);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTSearchLocationInfoByIds(Long[] ids) {
        return tSearchLocationInfoMapper.deleteTSearchLocationInfoByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTSearchLocationInfoById(Long id) {
        return tSearchLocationInfoMapper.deleteTSearchLocationInfoById(id);
    }

    @Override
    public List<SearchLocationInfoVo> selectSearchLocationInfoVoBySearchValue(TSearchLocationInfo tSearchLocationInfo) {
        //判断查询内容是否为空
        if (StringUtils.isEmpty(tSearchLocationInfo.getSearchValue())) {
            log.error("当前查询内容禁止为空,查询者id：{}", tSearchLocationInfo.getUserId());
            throw new ServiceException("当前查询内容禁止为空！");
        }
        //查询内容
        String searchValue = tSearchLocationInfo.getSearchValue();
        //查找所有搜索内容
        List<SearchLocationInfoVo> list = tSearchLocationInfoMapper.selectSearchLocationInfoVoBySearchValue(searchValue);
        if (list != null && !list.isEmpty()) {
            return list.stream().map(
                    mapper -> {
                        //配置设备最新定位（经纬度）
                        List<TShouhuanInfo> shouhuanInfos = shouhuanInfoService.selectTShouhuanInfoListBydeviceIdCode(mapper.getDeviceIdCode());
                        if (shouhuanInfos != null && !shouhuanInfos.isEmpty()) {
                            TShouhuanInfo shouhuanInfo = shouhuanInfos.get(0);
                            mapper.setGpsLong(shouhuanInfo.getUseGpsLong());
                            mapper.setGpsLat(shouhuanInfo.getUseGpsLat());
                        }
                        //配置设备电子围栏
                        mapper.setaMapFenceList(getAMapFenceByDeviceId(mapper.getDeviceId()));
                        return mapper;
                    }
            ).collect(Collectors.toList());
        }
        return list;
    }

    /**
     * 配置设备电子围栏
     *
     * @param deviceId
     * @return
     */
    private List<AMapFenceVo> getAMapFenceByDeviceId(Long deviceId) {
        List<TElectronicFence> tElectronicFences = tElectronicFenceMapper.selectTElectronicFenceListByDeviceId(deviceId);
        if (tElectronicFences != null && !tElectronicFences.isEmpty()) {
            return tElectronicFences.stream()
                    .map(tElectronicFence -> {
                        TElectronicFenceService fenceService = iElectronicFenceServiceService.selectTElectronicFenceServiceById(tElectronicFence.getServiceId());
                        TElectronicFenceKey tElectronicFenceKey = iElectronicFenceKeyService.selectTElectronicFenceKeyById(fenceService.getKeyId());
                        AMapFenceVo aMapFenceVo = new AMapFenceVo();
                        aMapFenceVo.setId(tElectronicFence.getId());
                        aMapFenceVo.setKey(tElectronicFenceKey.getAmapKey());
                        aMapFenceVo.setSid(fenceService.getAmapSid());
                        aMapFenceVo.setGfid(tElectronicFence.getGfid());
                        aMapFenceVo.setUserId(tElectronicFence.getUserId());
                        aMapFenceVo.setDeviceId(tElectronicFence.getDeviceId());
                        aMapFenceVo.setName(tElectronicFence.getName());
                        aMapFenceVo.setStartTime(TimeStampUtils.convertToTodayDateTimeToMillis(tElectronicFence.getStartTime().getTime()));
                        aMapFenceVo.setEndTime(TimeStampUtils.convertToTodayDateTimeToMillis(tElectronicFence.getEndTime().getTime()));
                        aMapFenceVo.setState(tElectronicFence.geteState());
                        aMapFenceVo.setType(tElectronicFence.geteType());
                        aMapFenceVo.setDesc(tElectronicFence.geteDesc());
                        return aMapFenceVo;
                    })
                    .collect(Collectors.toList());
        }
        return null;
    }
}
