package com.fastbee.breakdown.service.impl;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fastbee.breakdown.domain.dto.IotDeviceBreakdown;
import com.fastbee.common.utils.DateUtils;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.breakdown.mapper.DeviceBreakdownMapper;
import com.fastbee.breakdown.domain.DeviceBreakdown;
import com.fastbee.breakdown.service.IDeviceBreakdownService;

/**
 * 故障Service业务层处理
 * 
 * @author kerwincui
 * @date 2024-11-27
 */
@Service
public class DeviceBreakdownServiceImpl implements IDeviceBreakdownService 
{
    @Autowired
    private DeviceBreakdownMapper deviceBreakdownMapper;

    /**
     * 查询故障
     * 
     * @param id 故障主键
     * @return 故障
     */
    @Override
    public DeviceBreakdown selectDeviceBreakdownById(Long id)
    {
        return deviceBreakdownMapper.selectDeviceBreakdownById(id);
    }

    /**
     * 查询故障列表
     * 
     * @param deviceBreakdown 故障
     * @return 故障
     */
    @Override
    public List<DeviceBreakdown> selectDeviceBreakdownList(DeviceBreakdown deviceBreakdown)
    {
        return deviceBreakdownMapper.selectDeviceBreakdownList(deviceBreakdown);
    }

    /**
     * 新增故障
     * 
     * @param deviceBreakdown 故障
     * @return 结果
     */
    @Override
    public int insertDeviceBreakdown(DeviceBreakdown deviceBreakdown)
    {
        deviceBreakdown.setCreateTime(DateUtils.getNowDate());
        return deviceBreakdownMapper.insertDeviceBreakdown(deviceBreakdown);
    }

    /**
     * 修改故障
     * 
     * @param deviceBreakdown 故障
     * @return 结果
     */
    @Override
    public int updateDeviceBreakdown(DeviceBreakdown deviceBreakdown)
    {
        deviceBreakdown.setUpdateTime(DateUtils.getNowDate());
        return deviceBreakdownMapper.updateDeviceBreakdown(deviceBreakdown);
    }

    /**
     * 批量删除故障
     * 
     * @param ids 需要删除的故障主键
     * @return 结果
     */
    @Override
    public int deleteDeviceBreakdownByIds(Long[] ids)
    {
        return deviceBreakdownMapper.deleteDeviceBreakdownByIds(ids);
    }

    /**
     * 删除故障信息
     * 
     * @param id 故障主键
     * @return 结果
     */
    @Override
    public int deleteDeviceBreakdownById(Long id)
    {
        return deviceBreakdownMapper.deleteDeviceBreakdownById(id);
    }

    /**
     * 统计故障的分组数量
     * @param TypeValue
     * @param CreateTimeValue
     * @return
     */
    @Override
    public List<IotDeviceBreakdown> getFaultStatistics(String TypeValue, String CreateTimeValue,String EndTimeValue){
        QueryWrapper<DeviceBreakdown> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Type", TypeValue)
                .ge("create_time",CreateTimeValue)
                .le("create_time",EndTimeValue)
                .select("count(*) as id", "fault_name", "create_time","Type")
                .groupBy("fault_name");
        List<IotDeviceBreakdown> breakdown = new ArrayList<>();
        QueryWrapper<DeviceBreakdown> query = new QueryWrapper<>();
        query.eq("Type",TypeValue);
        QueryWrapper<DeviceBreakdown> queryTime = new QueryWrapper<>();
        queryTime.le("create_time",EndTimeValue).ge("create_time",CreateTimeValue);
        if ((deviceBreakdownMapper.selectCount(query) <=0)||(deviceBreakdownMapper.selectCount(queryTime)<=0)) {
            return breakdown;
        }
        System.err.println(deviceBreakdownMapper.selectList(queryWrapper));
       for (var item : deviceBreakdownMapper.selectList(queryWrapper))
       {
           IotDeviceBreakdown breakdownItem = new IotDeviceBreakdown();
           breakdownItem.setCounts(item.getId());
           breakdownItem.setFault_name(item.getFaultName());
           breakdownItem.setType(item.getType());
           breakdown.add(breakdownItem);
       }
         return breakdown;
    }
}
