package com.ruoyi.business.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.business.domain.HwSceneMode;
import com.ruoyi.business.domain.VO.HwSceneVo;
import com.ruoyi.common.core.constant.HwDictConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.datascope.annotation.DataScope;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.HwSceneMapper;
import com.ruoyi.business.domain.HwScene;
import com.ruoyi.business.service.IHwSceneService;

/**
 * 场景信息Service业务层处理
 *
 * @author xins
 * @date 2023-08-23
 */
@Service
public class HwSceneServiceImpl implements IHwSceneService {
    @Autowired
    private HwSceneMapper hwSceneMapper;
    @Autowired
    private RedisService redisService;


    /**
     * 查询场景信息
     *
     * @param sceneId 场景信息主键
     * @return 场景信息
     */
    @Override
    public HwScene selectHwSceneBySceneId(Long sceneId) {
        return hwSceneMapper.selectHwSceneBySceneId(sceneId);
    }

    @Override
    public List<HwScene> selectHwSceneBySceneModeId(Long sceneModeId) {
        List<HwScene> hwScenes = hwSceneMapper.selectHwSceneBySceneModeId(sceneModeId);

        return hwScenes;
    }

    /**
     * 查询场景信息列表
     *
     * @param hwScene 场景信息
     * @return 场景信息
     */
    @Override
    @DataScope(tenantAlias = "hs")
    public List<HwScene> selectHwSceneList(HwScene hwScene) {
        List<HwScene> scenes = hwSceneMapper.selectHwSceneList(hwScene);
        boolean defaultFlag = false;
        for (HwScene scene : scenes) {
            if (defaultFlag) {
                scene.setDefaultFlag(HwDictConstants.DEVICE_DEFAULT_FLAG_NO);
            } else {
                if (scene.getDefaultFlag().equalsIgnoreCase(HwDictConstants.DEVICE_DEFAULT_FLAG_YES)) {
                    defaultFlag = true;
                }
            }
        }
        return scenes;
    }

    @Override
    public List<HwSceneVo> selectHwSceneList2(HwSceneVo hwScene) {
        return hwSceneMapper.selectHwSceneVoList(hwScene);
    }


    /**
     * 新增场景信息
     *
     * @param hwScene 场景信息
     * @return 结果
     */
    @Override
    public int insertHwScene(HwScene hwScene) {
        if (hwScene.getDefaultFlag() == "1") {
            hwSceneMapper.updateDefaultFlag(hwScene.getTenantId());
        }
        hwScene.setDefaultFlag("1");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        hwScene.setSceneStatus("1");
        Long lon = new Long(10);
        hwScene.setSceneId(lon);
        hwScene.setCreateBy(sysUser.getNickName());

        if (hwScene.getPreserveTime() == null) {
            hwScene.setPreserveTime(new BigDecimal(90));
        }
        if (hwScene.getTestPreserveTime() == null) {
            hwScene.setTestPreserveTime(new BigDecimal(30));
        }
        String randomKey = RandomStringUtils.random(12, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789");
        String randomSecret = RandomStringUtils.random(16, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789");
        hwScene.setModeKey(randomKey);
        hwScene.setModeSecret(randomSecret);
        hwScene.setCreateTime(DateUtils.getNowDate());
        List<HwScene> list = new ArrayList<HwScene>();
        list.add(hwScene);
        redisService.setCacheList(hwScene.getSceneId() + "", list);

        return hwSceneMapper.insertHwScene(hwScene);
    }

    /**
     * 修改场景信息
     *
     * @param hwScene 场景信息
     * @return 结果
     */
    @Override
    public int updateHwScene(HwScene hwScene) {
        if (hwScene.getDefaultFlag() == "1") {
            hwSceneMapper.updateDefaultFlag(hwScene.getTenantId());
        }
        hwScene.setDefaultFlag("1");
        hwScene.setUpdateTime(DateUtils.getNowDate());
        List<HwScene> list = new ArrayList<HwScene>();
        list.add(hwScene);
        redisService.setCacheList(hwScene.getSceneId() + "", list);
        return hwSceneMapper.updateHwScene(hwScene);
    }

    /**
     * 批量删除场景信息
     *
     * @param sceneIds 需要删除的场景信息主键
     * @return 结果
     */
    @Override
    public int deleteHwSceneBySceneIds(Long[] sceneIds) {
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < sceneIds.length; i++) {
            list.add(sceneIds[i]);
        }
        redisService.deleteObject(sceneIds + "");
        return hwSceneMapper.deleteHwSceneBySceneIds(sceneIds);
    }

    /**
     * 删除场景信息信息
     *
     * @param sceneId 场景信息主键
     * @return 结果
     */
    @Override
    public int deleteHwSceneBySceneId(Long sceneId) {
        redisService.deleteObject(sceneId + "");
        return hwSceneMapper.deleteHwSceneBySceneId(sceneId);
    }
}
