package com.yanzhu.jh.video.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.enums.PublicStateEnum;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.yanzhu.jh.video.domain.DevAiProjectConfigPassage;
import com.yanzhu.jh.video.mapper.DevAiProjectConfigMapper;
import com.yanzhu.jh.video.domain.DevAiProjectConfig;
import com.yanzhu.jh.video.service.IDevAiProjectConfigService;
import com.ruoyi.common.utils.SecurityUtils;
/**
 * 设备配置Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2023-10-03
 */
@Service
public class DevAiProjectConfigServiceImpl implements IDevAiProjectConfigService 
{
    @Autowired
    private DevAiProjectConfigMapper devAiProjectConfigMapper;

    /**
     * 查询设备配置
     * 
     * @param id 设备配置主键
     * @return 设备配置
     */
    @Override
    public DevAiProjectConfig selectDevAiProjectConfigById(Long id)
    {
        DevAiProjectConfig devAiProjectConfig = devAiProjectConfigMapper.selectDevAiProjectConfigById(id);
        for(DevAiProjectConfigPassage devAiProjectConfigPassage:devAiProjectConfig.getDevAiProjectConfigPassageList()){
            if(devAiProjectConfigPassage.getPassageType()!=null){
                devAiProjectConfigPassage.setPassageTypes(Arrays.asList(devAiProjectConfigPassage.getPassageType().split(",")));
            }
        }
        return devAiProjectConfig;
    }

    /**
     * 查询设备配置列表
     * 
     * @param devAiProjectConfig 设备配置
     * @return 设备配置
     */
    @Override
    public List<DevAiProjectConfig> selectDevAiProjectConfigList(DevAiProjectConfig devAiProjectConfig)
    {
        return devAiProjectConfigMapper.selectDevAiProjectConfigList(devAiProjectConfig);
    }

    /**
     * 新增设备配置
     * 
     * @param devAiProjectConfig 设备配置
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDevAiProjectConfig(DevAiProjectConfig devAiProjectConfig)
    {
        devAiProjectConfig.setCreateBy(SecurityUtils.getUsername());
        devAiProjectConfig.setCreateTime(DateUtils.getNowDate());
        int rows = devAiProjectConfigMapper.insertDevAiProjectConfig(devAiProjectConfig);
        insertDevAiProjectConfigPassage(devAiProjectConfig);
        return rows;
    }

    /**
     * 修改设备配置
     * 
     * @param devAiProjectConfig 设备配置
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDevAiProjectConfig(DevAiProjectConfig devAiProjectConfig)
    {
        devAiProjectConfig.setUpdateBy(SecurityUtils.getUsername());
        devAiProjectConfig.setUpdateTime(DateUtils.getNowDate());
        devAiProjectConfigMapper.deleteDevAiProjectConfigPassageByAiboxId(devAiProjectConfig.getId());
        insertDevAiProjectConfigPassage(devAiProjectConfig);
        return devAiProjectConfigMapper.updateDevAiProjectConfig(devAiProjectConfig);
    }

    /**
     * 批量删除设备配置
     * 
     * @param ids 需要删除的设备配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDevAiProjectConfigByIds(Long[] ids)
    {
        devAiProjectConfigMapper.deleteDevAiProjectConfigPassageByAiboxIds(ids);
        return devAiProjectConfigMapper.deleteDevAiProjectConfigByIds(ids);
    }

    /**
     * 删除设备配置信息
     * 
     * @param id 设备配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDevAiProjectConfigById(Long id)
    {
        devAiProjectConfigMapper.deleteDevAiProjectConfigPassageByAiboxId(id);
        return devAiProjectConfigMapper.deleteDevAiProjectConfigById(id);
    }

    /**
     * 新增设备配置通道信息
     * 
     * @param devAiProjectConfig 设备配置对象
     */
    public void insertDevAiProjectConfigPassage(DevAiProjectConfig devAiProjectConfig)
    {
        List<DevAiProjectConfigPassage> devAiProjectConfigPassageList = devAiProjectConfig.getDevAiProjectConfigPassageList();
        Long id = devAiProjectConfig.getId();
        if (StringUtils.isNotNull(devAiProjectConfigPassageList))
        {
            List<DevAiProjectConfigPassage> list = new ArrayList<DevAiProjectConfigPassage>();
            for (DevAiProjectConfigPassage devAiProjectConfigPassage : devAiProjectConfigPassageList)
            {
                devAiProjectConfigPassage.setAiboxId(id);
                devAiProjectConfigPassage.setIsDel(PublicStateEnum.OK.getCode());
                devAiProjectConfigPassage.setPassageType(String.join(",", devAiProjectConfigPassage.getPassageTypes()));
                if(devAiProjectConfigPassage.getPassageType().equals("")){
                    devAiProjectConfigPassage.setPassageType(null);
                }
                list.add(devAiProjectConfigPassage);
            }
            if (list.size() > 0)
            {
                devAiProjectConfigMapper.batchDevAiProjectConfigPassage(list);
            }
        }
    }

}
