package com.zd.chemical.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.chemical.mapper.HxpSmartTerminalConfigMapper;
import com.zd.chemical.domain.HxpSmartTerminalConfig;
import com.zd.chemical.service.IHxpSmartTerminalConfigService;

/**
 * 报警硬件控制时长配置Service业务层处理
 *
 * @author liubo
 * @date 2022-06-28
 */
@Service
public class HxpSmartTerminalConfigServiceImpl implements IHxpSmartTerminalConfigService {
    @Autowired
    private HxpSmartTerminalConfigMapper hxpSmartTerminalConfigMapper;

    /**
     * 查询报警硬件控制时长配置
     *
     * @param id 报警硬件控制时长配置主键
     * @return 报警硬件控制时长配置
     */
    @Override
    public HxpSmartTerminalConfig selectHxpSmartTerminalConfigById(Long id) {
        return hxpSmartTerminalConfigMapper.selectHxpSmartTerminalConfigById(id);
    }

    /**
     * 查询报警硬件控制时长配置列表
     *
     * @param hxpSmartTerminalConfig 报警硬件控制时长配置
     * @return 报警硬件控制时长配置
     */
    @Override
    public List<HxpSmartTerminalConfig> selectHxpSmartTerminalConfigList(HxpSmartTerminalConfig hxpSmartTerminalConfig) {
        return hxpSmartTerminalConfigMapper.selectHxpSmartTerminalConfigList(hxpSmartTerminalConfig);
    }

    /**
     * 新增报警硬件控制时长配置
     *
     * @param hxpSmartTerminalConfig 报警硬件控制时长配置
     * @return 结果
     */
    @Override
    public int insertHxpSmartTerminalConfig(HxpSmartTerminalConfig hxpSmartTerminalConfig) {
        return hxpSmartTerminalConfigMapper.insertHxpSmartTerminalConfig(hxpSmartTerminalConfig);
    }

    /**
     * 修改报警硬件控制时长配置
     *
     * @param hxpSmartTerminalConfig 报警硬件控制时长配置
     * @return 结果
     */
    @Override
    public int updateHxpSmartTerminalConfig(HxpSmartTerminalConfig hxpSmartTerminalConfig) {

        List<HxpSmartTerminalConfig> configs = hxpSmartTerminalConfigMapper.selectHxpSmartTerminalConfigList(hxpSmartTerminalConfig);
        List<HxpSmartTerminalConfig> list = new ArrayList<>();
        hxpSmartTerminalConfig.getIds().forEach(terminalId -> {
            HxpSmartTerminalConfig data = new HxpSmartTerminalConfig();
            data.setRingTime(hxpSmartTerminalConfig.getRingTime());
            data.setDealTime(hxpSmartTerminalConfig.getDealTime());
            data.setTerminalId(terminalId);
            configs.forEach(c -> {
                if(c.getTerminalId().equals(terminalId)){
                    data.setId(c.getId());
                }
            });
            list.add(data);
        });


        return hxpSmartTerminalConfigMapper.insertOrUpdateBatch(list);
    }

    /**
     * 批量删除报警硬件控制时长配置
     *
     * @param ids 需要删除的报警硬件控制时长配置主键
     * @return 结果
     */
    @Override
    public int deleteHxpSmartTerminalConfigByIds(Long[] ids) {
        return hxpSmartTerminalConfigMapper.deleteHxpSmartTerminalConfigByIds(ids);
    }

    /**
     * 删除报警硬件控制时长配置信息
     *
     * @param id 报警硬件控制时长配置主键
     * @return 结果
     */
    @Override
    public int deleteHxpSmartTerminalConfigById(Long id) {
        return hxpSmartTerminalConfigMapper.deleteHxpSmartTerminalConfigById(id);
    }
}
