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;


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

        TDetectorInfo detector = new TDetectorInfo();
        for(TDetectorUser user : tDetectorUserList){
            TDetectorUserVO userVO = new TDetectorUserVO();
            BeanUtils.copyProperties(user, userVO);

            detector.setUserId(user.getUserId());
            detector.setIsDel("0");
            List<TDetectorInfo> tDetectorInfoList = tDetectorInfoMapper.selectTDetectorInfoList(detector);

            int onLineNum = 0;
            int offLineNum = 0;
            int cancelAlarmNum = 0;
            int historyAlarmNum = 0;

            if(tDetectorInfoList.size() > 0){
                for(TDetectorInfo info : tDetectorInfoList){
                    if(!StringUtils.isEmpty(info.getDetectorStatus()) && info.getDetectorStatus().equals("0")){
                        ++onLineNum;
                    }else{
                        ++offLineNum;
                    }
                }

                List<TDetectorReportData> dataList = tDetectorReportDataMapper.countDetectorByUserId(detector);
                historyAlarmNum = dataList.size();

                for(TDetectorReportData data : dataList){
                    if(!StringUtils.isEmpty(data.getIsCancelAlarm()) && data.getIsCancelAlarm().equals("1")){
                        ++cancelAlarmNum;
                    }
                }
            }

            userVO.setDetectorCount(tDetectorInfoList.size());
            userVO.setOnLineNum(onLineNum);
            userVO.setOffLineNum(offLineNum);
            userVO.setHistoryAlarmNum(historyAlarmNum);
            userVO.setCancelAlarmNum(cancelAlarmNum);
            userVO.setProcessingAlarmNum(Math.abs(historyAlarmNum - cancelAlarmNum));
            userVO.setIconType("6");
            userVO.setDetectorType("气体探测器");

            list.add(userVO);
        }

        return list;
    }

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