package com.zehong.system.service.impl;

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

import com.zehong.common.utils.DateUtils;
import com.zehong.system.domain.TDetectorUserCount;
import com.zehong.system.domain.vo.TDetectorUserInspectVo;
import com.zehong.system.domain.vo.TDetectorUserVO;
import com.zehong.system.mapper.TDeviceInfoMapper;
import com.zehong.system.mapper.TSiteStationInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zehong.system.mapper.TDetectorUserMapper;
import com.zehong.system.domain.TDetectorUser;
import com.zehong.system.service.ITDetectorUserService;

/**
 * 燃气用户Service业务层处理
 *
 * @author zehong
 * @date 2022-02-07
 */
@Service
public class TDetectorUserServiceImpl implements ITDetectorUserService
{
    @Autowired
    private TDetectorUserMapper tDetectorUserMapper;
    @Autowired
    private TDeviceInfoMapper tDeviceInfoMapper;
    @Autowired
    private TSiteStationInfoMapper tSiteStationInfoMapper;


    /**
     * 查询探测器用户列表
     *
     * @return 探测器用户
     */
    @Override
    public List<TDetectorUserVO> detectorUserList(TDetectorUser tDetectorUser)
    {
        long startTime = System.currentTimeMillis();
        List<TDetectorUserVO> list = new ArrayList<>();
        List<TDetectorUserVO> tDetectorUserList = tDetectorUserMapper.countTDetectorUser(tDetectorUser);

        Map<Long, List<TDetectorUserVO>> collect = tDetectorUserList.stream().collect(Collectors.groupingBy(TDetectorUserVO::getUserId));

        for (Map.Entry<Long, List<TDetectorUserVO>> longListEntry : collect.entrySet()) {
            List<Object> initList = new ArrayList<>();
            List<TDetectorUserVO> value = longListEntry.getValue();
            if (value.size() > 0) {
                TDetectorUserVO userVO = new TDetectorUserVO();
                BeanUtils.copyProperties(value.get(0), userVO);
                for (TDetectorUserVO tDetectorUserVO : value) {
                    // 如果设备数量为0 则不显示
                    if (tDetectorUserVO.getDetectorCount() == 0) {
                        continue;
                    }
                    initList.add(tDetectorUserVO);
                }
                userVO.setDetectorCountList(initList);
                list.add(userVO);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("detectorUserList = " + (end - startTime));
        return list;
    }


    /**
     * 查询探测器用户列表 地图使用 LabelMarker 标注的方式
     * @param tDetectorUser tDetectorUser
     * @return r
     */
    @Override
    public   List<TDetectorUserVO> massMarksDetectorUserList(TDetectorUser tDetectorUser) {
        return tDetectorUserMapper.countTDetectorUserForMassMarks(tDetectorUser);
    }

    /**
     * 查询探测器用户列表 地图使用 labelsLayerMarks 标注的方式
     * @param tDetectorUser tDetectorUser
     * @return r
     */
    @Override
    public Map<String, List<TDetectorUserVO>> labelsLayerMarksDetectorUserList(TDetectorUser tDetectorUser) {

        Map<String, List<TDetectorUserVO>> collect ;
        List<TDetectorUserVO> list = new ArrayList<>();
        List<TDetectorUserVO> tDetectorUserList = tDetectorUserMapper.countTDetectorUser(tDetectorUser);

        if (tDetectorUserList.size() > 0) {
            for(TDetectorUserVO user : tDetectorUserList){
                TDetectorUserVO userVO = new TDetectorUserVO();
                BeanUtils.copyProperties(user, userVO);

                List<Object> newList = new ArrayList<>();
                newList.add(user);
                userVO.setDetectorCountList(newList);

                if("1".equals(userVO.getUserType())) {
                    userVO.setIconType("6");
                } else if ("2".equals(userVO.getUserType())) {
                    userVO.setIconType("61");
                } else if ("3".equals(userVO.getUserType())) {
                    userVO.setIconType("62");
                }
                list.add(userVO);
            }
            collect = list.stream().collect(Collectors.groupingBy(TDetectorUserVO::getIconType));
        } else {
            collect = new HashMap<>(0);
        }

        return collect;
    }

    /**
     * 查询探测器用户列表 地图使用 massMarks 标注的方式
     * @param tDetectorUser tDetectorUser
     * @return r
     */
/*
    @Override
    public Map<String, List<TMassMarksDetectorUserVO>> massMarksDetectorUserList(TDetectorUser tDetectorUser) {
        List<TDetectorUserVO> tDetectorUserList = tDetectorUserMapper.countTDetectorUserForMassMarks(tDetectorUser);
        Map<String, List<TMassMarksDetectorUserVO>> collect ;
        if (tDetectorUserList.size() > 0) {

            List<TMassMarksDetectorUserVO> massMarksDetectorUsers = new ArrayList<>();

            TMassMarksDetectorUserVO massMarksDetectorUser;
            for(TDetectorUserVO user : tDetectorUserList){

                BigDecimal longitude = user.getLongitude();
                BigDecimal latitude = user.getLatitude();

                BigDecimal[] lnglat = new BigDecimal[2];
                lnglat[1] = latitude;
                lnglat[0] = longitude;
                massMarksDetectorUser = new TMassMarksDetectorUserVO();
                lnglat[1] = latitude;
                lnglat[0] = longitude;
                massMarksDetectorUser.setLnglat(lnglat);
                if ("1".equals(user.getUserType())) {
                    massMarksDetectorUser.setIconType("6");
                } else if ("2".equals(user.getUserType())) {
                    massMarksDetectorUser.setIconType("61");
                }
                massMarksDetectorUser.setStyle(2);
                massMarksDetectorUsers.add(massMarksDetectorUser);
            }
            collect = massMarksDetectorUsers.stream().collect(Collectors.groupingBy(TMassMarksDetectorUserVO::getIconType));
        } else {
            collect = new HashMap<>();
        }
        return collect;
    }
*/


    /**
     * 查询探测器报警用户（前台调用）
     *
     * @return 探测器用户
     */
    @Override
    public List<Map> getDetectorUserAlarmList()
    {
        List<Map> list = new ArrayList<>();

        // 查询报警用户
        List<Map> userList = tDetectorUserMapper.getDetectorUserAlarmList();
        for(Map map : userList){
            map.put("iconType", "6");
            list.add(map);
        }
        // 查询报警设备
        List<Map> infoList = tDeviceInfoMapper.getDeviceInfoAlarmList();
        for(Map map : infoList){
            // deviceType：1阀门井 2调压箱
            if(map.get("deviceType").equals("1")){
                map.put("iconType", "3");
            } else if(map.get("deviceType").equals("2")){
                map.put("iconType", "2");
            }
            list.add(map);
        }
        // 查询报警场站
        List<Map> stationList = tSiteStationInfoMapper.getSiteStationAlarmList();
        for(Map map : stationList){
            map.put("iconType", "4");
            list.add(map);
        }

        return list;
    }

    /**
     * 查询探测器报警用户（后台推送）
     *
     * @param tDetectorUser 探测器用户
     * @return 探测器用户
     */
    @Override
    public List<TDetectorUserVO> detectorUserAlarmList(TDetectorUser tDetectorUser)
    {
        List<TDetectorUserVO> list = new ArrayList<>();
        List<TDetectorUserVO> tDetectorUserList = tDetectorUserMapper.countTDetectorUser(tDetectorUser);

        List<Object> initList = new ArrayList<>();
        for(TDetectorUserVO user : tDetectorUserList) {
            TDetectorUserVO userVO = new TDetectorUserVO();
            BeanUtils.copyProperties(user, userVO);
            initList.add(userVO);
            userVO.setDetectorCountList(initList);

            list.add(user);
        }

        return list;
    }

    /**
     * 泽宏云推送查询燃气用户
     *
     * @param tDetectorUser 燃气用户
     * @return 燃气用户
     */
    @Override
    public List<TDetectorUser> selectTDetectorUser(TDetectorUser tDetectorUser)
    {
        return tDetectorUserMapper.selectTDetectorUser(tDetectorUser);
    }

    /**
     * 查询燃气用户
     *
     * @param userId 燃气用户ID
     * @return 燃气用户
     */
    @Override
    public TDetectorUser selectTDetectorUserById(Long userId)
    {
        return tDetectorUserMapper.selectTDetectorUserById(userId);
    }

    /**
     * 查询燃气用户列表
     *
     * @param tDetectorUser 燃气用户
     * @return 燃气用户
     */
    @Override
    public List<TDetectorUser> selectTDetectorUserList(TDetectorUser tDetectorUser)
    {
        return tDetectorUserMapper.selectTDetectorUserList(tDetectorUser);
    }
    @Override
    public List<TDetectorUser> selectTDetectorListstatus(TDetectorUser tDetectorUser){
        return tDetectorUserMapper.selectTDetectorListstatus(tDetectorUser);
    }
    /**
     * 新增燃气用户
     *
     * @param tDetectorUser 燃气用户
     * @return 结果
     */
    @Override
    public int insertTDetectorUser(TDetectorUser tDetectorUser)
    {
        return tDetectorUserMapper.insertTDetectorUser(tDetectorUser);
    }

    /**
     * 修改燃气用户
     *
     * @param tDetectorUser 燃气用户
     * @return 结果
     */
    @Override
    public int updateTDetectorUser(TDetectorUser tDetectorUser)
    {
        tDetectorUser.setUpdateTime(DateUtils.getNowDate());
        return tDetectorUserMapper.updateTDetectorUser(tDetectorUser);
    }

    /**
     * 批量删除燃气用户
     *
     * @param userIds 需要删除的燃气用户ID
     * @return 结果
     */
    @Override
    public int deleteTDetectorUserByIds(Long[] userIds)
    {
        return tDetectorUserMapper.deleteTDetectorUserByIds(userIds);
    }

    /**
     * 删除燃气用户信息
     *
     * @param userId 燃气用户ID
     * @return 结果
     */
    @Override
    public int deleteTDetectorUserById(Long userId)
    {
        return tDetectorUserMapper.deleteTDetectorUserById(userId);
    }

    /**
     * 用户数量
     * @return
     */
    public Map<String,Object> selectUserNum(){
        return tDetectorUserMapper.selectUserNum();
    }

    /**
     * 查询用户统计信息
     * @return
     */
    @Override
    public TDetectorUserCount userStatistics(String id) {
        return tDetectorUserMapper.userStatistics(id);
    }

    /**
     * 获取用户安检信息
     * @param inspectVo
     * @return
     */
    @Override
    public List<TDetectorUserInspectVo> getDetectorUserInspectInfo(TDetectorUserInspectVo inspectVo){
        return tDetectorUserMapper.getDetectorUserInspectInfo(inspectVo);
    }
}
