package com.ruoyi.asset.service.impl;

import java.util.List;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.asset.mapper.TbAssetInformationMapper;
import com.ruoyi.asset.domain.TbAssetInformation;
import com.ruoyi.asset.service.ITbAssetInformationService;

import javax.annotation.Resource;

/**
 * 资产信息Service业务层处理
 * 
 * @author 原动力
 * @date 2023-05-15
 */
@Service
public class TbAssetInformationServiceImpl implements ITbAssetInformationService 
{
    @Autowired
    private TbAssetInformationMapper tbAssetInformationMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询资产信息
     * 
     * @param id 资产信息主键
     * @return 资产信息
     */
    @Override
    public TbAssetInformation selectTbAssetInformationById(Long id)
    {
        return tbAssetInformationMapper.selectTbAssetInformationById(id);
    }

    @Override
    public TbAssetInformation selectTbAssetAdjustmentByAssetNumber(String assetNumber) {
        return tbAssetInformationMapper.selectTbAssetInformationByAssetNumber(assetNumber);
    }

    /**
     * 查询资产信息列表
     * 
     * @param tbAssetInformation 资产信息
     * @return 资产信息
     */
    @Override
    public List<TbAssetInformation> selectTbAssetInformationList(TbAssetInformation tbAssetInformation)
    {
        return tbAssetInformationMapper.selectTbAssetInformationList(tbAssetInformation);
    }

    /**
     * 新增资产信息
     * 
     * @param tbAssetInformation 资产信息
     * @return 结果
     */
    @Override
    public int insertTbAssetInformation(TbAssetInformation tbAssetInformation)
    {
        tbAssetInformation.setCreateTime(DateUtils.getNowDate());
        tbAssetInformation.setCreateBy(SecurityUtils.getUsername());
        return tbAssetInformationMapper.insertTbAssetInformation(tbAssetInformation);
    }

    /**
     * 修改资产信息
     * 
     * @param tbAssetInformation 资产信息
     * @return 结果
     */
    @Override
    public int updateTbAssetInformation(TbAssetInformation tbAssetInformation)
    {
        tbAssetInformation.setUpdateTime(DateUtils.getNowDate());
        return tbAssetInformationMapper.updateTbAssetInformation(tbAssetInformation);
    }

    /**
     * 批量删除资产信息
     * 
     * @param ids 需要删除的资产信息主键
     * @return 结果
     */
    @Override
    public int deleteTbAssetInformationByIds(Long[] ids)
    {
        return tbAssetInformationMapper.deleteTbAssetInformationByIds(ids);
    }

    /**
     * 删除资产信息信息
     * 
     * @param id 资产信息主键
     * @return 结果
     */
    @Override
    public int deleteTbAssetInformationById(Long id)
    {
        return tbAssetInformationMapper.deleteTbAssetInformationById(id);
    }

    @Override
    public boolean setDiy(List<String> labels) {
        String key = "func:assetInfo:" + SecurityUtils.getUserId();
        String jsonStr = JSONUtil.toJsonStr(labels);
        stringRedisTemplate.opsForValue().set(key, jsonStr);
        return true;
    }

    @Override
    public List<String> getDiy(){
        String json = stringRedisTemplate.opsForValue().get("func:assetInfo:" + SecurityUtils.getUserId());
        List<String> labels = JSONUtil.toList(json, String.class);
        return labels;
    }
}
