package com.zehong.system.service.impl;

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

import com.zehong.system.domain.vo.TDetectorUserVO;
import com.zehong.system.mapper.TDetectorInfoMapper;
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)
    {
        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);

            TDetectorUserVO temp = new TDetectorUserVO();
            if(user.getUserId().equals(temp.getUserId())){
                initList.add(user);
                userVO.setDetectorCountList(initList);
                userVO.setIconType("6");
            } else {
                BeanUtils.copyProperties(user, temp);
                List<Object> newList = new ArrayList<>();
                newList.add(temp);
                userVO.setDetectorCountList(newList);
                userVO.setIconType("6");
                list.add(userVO);
            }
        }

        return list;
    }

    /**
     * 查询探测器报警用户（前台调用）
     *
     * @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);
    }

    /**
     * 新增燃气用户
     * 
     * @param tDetectorUser 燃气用户
     * @return 结果
     */
    @Override
    public int insertTDetectorUser(TDetectorUser tDetectorUser)
    {
        return tDetectorUserMapper.insertTDetectorUser(tDetectorUser);
    }

    /**
     * 修改燃气用户
     * 
     * @param tDetectorUser 燃气用户
     * @return 结果
     */
    @Override
    public int updateTDetectorUser(TDetectorUser tDetectorUser)
    {
        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();
    }
}
