package com.zehong.system.service.impl;

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

import com.zehong.common.core.domain.entity.SysUser;
import com.zehong.common.utils.DateUtils;
import com.zehong.system.domain.TDeviceInfo;
import com.zehong.system.domain.TPipe;
import com.zehong.system.domain.vo.HiddenTroubleVo;
import com.zehong.system.mapper.SysUserMapper;
import com.zehong.system.mapper.TDeviceInfoMapper;
import com.zehong.system.mapper.TPipeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zehong.system.mapper.THiddenTroubleMapper;
import com.zehong.system.domain.THiddenTrouble;
import com.zehong.system.service.ITHiddenTroubleService;

/**
 * 隐患信息Service业务层处理
 * 
 * @author zehong
 * @date 2021-07-21
 */
@Service
public class THiddenTroubleServiceImpl implements ITHiddenTroubleService 
{
    @Autowired
    private THiddenTroubleMapper tHiddenTroubleMapper;
    @Autowired
    private TDeviceInfoMapper tDeviceInfoMapper;
    @Autowired
    private TPipeMapper tPipeMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询隐患信息
     * 
     * @param troubleId 隐患信息ID
     * @return 隐患信息
     */
    @Override
    public HiddenTroubleVo selectTHiddenTroubleById(int troubleId)
    {
        HiddenTroubleVo hiddenTroubleVo = new HiddenTroubleVo();
        THiddenTrouble tHiddenTrouble = tHiddenTroubleMapper.selectTHiddenTroubleById(troubleId);
        BeanUtils.copyProperties(tHiddenTrouble, hiddenTroubleVo);

        if(tHiddenTrouble.getDeviceId() != null){
            if("0".equals(tHiddenTrouble.getDeviceType())){
                TPipe pipe = tPipeMapper.selectTPipeById(tHiddenTrouble.getDeviceId());
                hiddenTroubleVo.setDeviceName(pipe.getPipeName());
            } else {
                TDeviceInfo device = tDeviceInfoMapper.selectTDeviceInfoById(tHiddenTrouble.getDeviceId());
                hiddenTroubleVo.setDeviceName(device.getDeviceName());
            }
        }
        return hiddenTroubleVo;
    }

    /**
     * 查询隐患信息列表
     * 
     * @param tHiddenTrouble 隐患信息
     * @return 隐患信息
     */
    @Override
    public List<HiddenTroubleVo> selectTHiddenTroubleList(THiddenTrouble tHiddenTrouble)
    {
        List<HiddenTroubleVo> list = new ArrayList<>();
        List<THiddenTrouble> tHiddenTroubleList = tHiddenTroubleMapper.selectTHiddenTroubleList(tHiddenTrouble);

        if(tHiddenTroubleList.size() != 0) {
            for (THiddenTrouble trouble : tHiddenTroubleList) {
                HiddenTroubleVo hiddenTroubleVo = new HiddenTroubleVo();
                BeanUtils.copyProperties(trouble, hiddenTroubleVo);

                SysUser sysUser = sysUserMapper.selectUserById(trouble.getReportMan());
                hiddenTroubleVo.setReportManName(sysUser.getNickName());

                list.add(hiddenTroubleVo);
            }
        }
        return list;
    }

    /**
     * 新增隐患信息
     * 
     * @param tHiddenTrouble 隐患信息
     * @return 结果
     */
    @Override
    public int insertTHiddenTrouble(THiddenTrouble tHiddenTrouble)
    {
        tHiddenTrouble.setCreateTime(DateUtils.getNowDate());
        return tHiddenTroubleMapper.insertTHiddenTrouble(tHiddenTrouble);
    }

    /**
     * 修改隐患信息
     * 
     * @param tHiddenTrouble 隐患信息
     * @return 结果
     */
    @Override
    public int updateTHiddenTrouble(THiddenTrouble tHiddenTrouble)
    {
        tHiddenTrouble.setUpdateTime(DateUtils.getNowDate());
        return tHiddenTroubleMapper.updateTHiddenTrouble(tHiddenTrouble);
    }

    /**
     * 批量删除隐患信息
     * 
     * @param troubleIds 需要删除的隐患信息ID
     * @return 结果
     */
    @Override
    public int deleteTHiddenTroubleByIds(int[] troubleIds)
    {
        return tHiddenTroubleMapper.deleteTHiddenTroubleByIds(troubleIds);
    }

    /**
     * 删除隐患信息信息
     * 
     * @param troubleId 隐患信息ID
     * @return 结果
     */
    @Override
    public int deleteTHiddenTroubleById(int troubleId)
    {
        return tHiddenTroubleMapper.deleteTHiddenTroubleById(troubleId);
    }
}
