package com.citygis.web.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.citygis.common.core.domain.AjaxResult;
import com.citygis.web.domain.TabFileParh;
import com.citygis.web.domain.TabSystemSpace;
import com.citygis.web.mapper.TabSystemSpaceMapper;
import com.citygis.web.service.ITabFileParhService;
import com.citygis.web.service.ITabSystemSpaceService;
import com.citygis.web.utils.FileUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统空间 服务实现类
 * </p>
 *
 * @author citygis
 * @since 2025-01-17
 */
@Service
public class TabSystemSpaceServiceImpl extends ServiceImpl<TabSystemSpaceMapper, TabSystemSpace> implements ITabSystemSpaceService {

    @Resource
    ITabFileParhService iTabFileParhService;

    @Resource
    TabSystemSpaceMapper tabSystemSpaceMapper;

    @Override
    public List<TabSystemSpace> getSpace(TabSystemSpace tabSystemSpace) {
        List<TabSystemSpace> space = tabSystemSpaceMapper.getSpace(tabSystemSpace);
        return space;
    }

    @Override
    public List<TabSystemSpace> getSpaceDetailByFilepath(TabSystemSpace tabSystemSpace) {
        return tabSystemSpaceMapper.getSpaceDetailByFilepath(tabSystemSpace);
    }

    @Override
    public void insertTabSystemSpace() {

        Snowflake snowflake = IdUtil.getSnowflake(1, 1);

        List<TabFileParh> list = iTabFileParhService.list();
        List<TabSystemSpace> tabSystemSpaceList = new ArrayList<>();
        list.forEach(tabFileParh -> {
            TabSystemSpace tabSystemSpace = new TabSystemSpace();

            File file = new File(tabFileParh.getFilepath());

            tabSystemSpace.setId(snowflake.nextId());
            tabSystemSpace.setFilepath(tabFileParh.getFilepath());
            tabSystemSpace.setType(tabFileParh.getType());
            tabSystemSpace.setIp(file.getAbsolutePath());
            tabSystemSpace.setFileSpace(FileUtils.getFilesLengthDesc(FileUtils.getFilesLengthB(file)));
            tabSystemSpace.setUsableSpace(FileUtils.getFilesLengthDesc(file.getUsableSpace()));
            tabSystemSpace.setFileTotalSpace(FileUtils.getFilesLengthDesc(file.getTotalSpace()));
            tabSystemSpace.setCreateTime(new Date());

            tabSystemSpaceList.add(tabSystemSpace);
        });
        this.saveBatch(tabSystemSpaceList);
    }

    @Override
    public AjaxResult getLinkTitle(TabSystemSpace tabSystemSpace) {
        return AjaxResult.success(tabSystemSpaceMapper.getLinkTitle(tabSystemSpace));
    }
}
