package com.zehong.system.service.impl;

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

import com.zehong.system.domain.TDetectorInfo;
import com.zehong.system.domain.TDetectorReportData;
import com.zehong.system.domain.vo.TDetectorUserVO;
import com.zehong.system.mapper.TDetectorInfoMapper;
import com.zehong.system.mapper.TDetectorReportDataMapper;
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;
import org.springframework.util.StringUtils;

/**
 * 燃气用户Service业务层处理
 * 
 * @author zehong
 * @date 2022-02-07
 */
@Service
public class TDetectorUserServiceImpl implements ITDetectorUserService 
{
    @Autowired
    private TDetectorUserMapper tDetectorUserMapper;
    @Autowired
    private TDetectorInfoMapper tDetectorInfoMapper;
    @Autowired
    private TDetectorReportDataMapper tDetectorReportDataMapper;


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

        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");
                userVO.setUserStatus("1");
            } else {
                BeanUtils.copyProperties(user, temp);
                List<Object> newList = new ArrayList<>();
                newList.add(temp);
                userVO.setDetectorCountList(newList);
                userVO.setIconType("6");
                userVO.setUserStatus("1");
                list.add(userVO);
            }
        }

        return list;
    }

    /**
     * 查询探测器报警用户（前台调用）
     *
     * @return 探测器用户
     */
    @Override
    public List<TDetectorUserVO> getDetectorUserAlarmList()
    {
        List<TDetectorUserVO> list = new ArrayList<>();
        TDetectorInfo tDetectorInfo = new TDetectorInfo();
        tDetectorInfo.setDetectorStatus("2");
        // 查询报警的数据
        List<TDetectorUserVO> tDetectorUserList = tDetectorUserMapper.countTDetectorUser(tDetectorInfo);

        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");
                userVO.setUserStatus("2");
            } else {
                BeanUtils.copyProperties(user, temp);
                List<Object> newList = new ArrayList<>();
                newList.add(temp);
                userVO.setDetectorCountList(newList);
                userVO.setIconType("6");
                userVO.setUserStatus("2");
                list.add(userVO);
            }
        }

        return list;
    }

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

        TDetectorUserVO userVO = new TDetectorUserVO();
        BeanUtils.copyProperties(user, userVO);
        list.add(user);
        userVO.setDetectorCountList(list);

        return userVO;
    }

    /**
     * 泽宏云推送查询燃气用户
     *
     * @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);
    }
}
