package org.eco.als.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.eco.als.domain.AtlasFile;
import org.eco.als.domain.bo.AtlasFileBo;
import org.eco.als.domain.vo.AtlasFileImportVo;
import org.eco.als.domain.vo.AtlasFileVo;
import org.eco.als.listener.AtlasFileImportListener;
import org.eco.als.mapper.AtlasFileMapper;
import org.eco.als.service.IAtlasFileService;
import org.eco.common.core.core.domain.model.LoginUser;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.core.utils.MapstructUtils;
import org.eco.common.excel.entity.ExcelResultRes;
import org.eco.common.excel.service.IExcelService;
import org.eco.common.orm.core.page.PageQuery;
import org.eco.common.orm.core.service.impl.BaseServiceImpl;
import org.eco.system.service.IImportExportService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static org.eco.als.domain.table.AtlasFileTableDef.ATLAS_FILE;

/**
 * 文件管理Service业务层处理
 *
 * @author wgk
 * @date 2024-11-27
 */
@Service
@Slf4j
public class AtlasFileServiceImpl extends BaseServiceImpl<AtlasFileMapper, AtlasFile> implements IAtlasFileService {
    @Resource
    private AtlasFileMapper atlasFileMapper;

    @Resource
    private IExcelService excelService;

    @Resource
    private IImportExportService importExportService;

    @Override
    public QueryWrapper query() {
        return super.query().from(ATLAS_FILE);
    }

    private QueryWrapper buildQueryWrapper(AtlasFileBo atlasFileBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(ATLAS_FILE.FILE_NAME.like
            (atlasFileBo.getFileName()));
        queryWrapper.and(ATLAS_FILE.FILE_TYPE.eq
            (atlasFileBo.getFileType()));
        queryWrapper.and(ATLAS_FILE.OSS_ID.eq
            (atlasFileBo.getOssId()));
        queryWrapper.and(ATLAS_FILE.STATUS.eq
            (atlasFileBo.getStatus()));
        queryWrapper.and(ATLAS_FILE.REMARKS.eq
            (atlasFileBo.getRemarks()));

        return queryWrapper;
    }

    /**
     * 查询文件管理
     *
     * @param id 文件管理主键
     * @return 文件管理
     */
    @Override
    public AtlasFileVo selectById(Long id) {
        return this.getOneAs(query().where(ATLAS_FILE.ID.eq(id)), AtlasFileVo.class);

    }


    /**
     * 查询文件管理列表
     *
     * @param atlasFileBo 文件管理Bo
     * @return 文件管理集合
     */
    @Override
    public List<AtlasFileVo> selectList(AtlasFileBo atlasFileBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(atlasFileBo);
        return this.listAs(queryWrapper, AtlasFileVo.class);
    }

    /**
     * 分页查询文件管理列表
     *
     * @param atlasFileBo 文件管理Bo
     * @return 分页文件管理集合
     */
    @Override
    public PageResult<AtlasFileVo> selectPage(AtlasFileBo atlasFileBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(atlasFileBo);
        Page<AtlasFileVo> page = this.pageAs(PageQuery.build(), queryWrapper, AtlasFileVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增文件管理
     *
     * @param atlasFileBo 文件管理Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(AtlasFileBo atlasFileBo) {
        AtlasFile atlasFile = MapstructUtils.convert(atlasFileBo, AtlasFile.class);

        return this.save(atlasFile);//使用全局配置的雪花算法主键生成器生成ID值
    }

    /**
     * 新增文件管理，前台提供主键值，一般用于导入的场合
     *
     * @param atlasFileBo 文件管理Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(AtlasFileBo atlasFileBo) {
        AtlasFile atlasFile = MapstructUtils.convert(atlasFileBo, AtlasFile.class);


        return atlasFileMapper.insertWithPk(atlasFile) > 0;//前台传来主键值
    }

    /**
     * 修改文件管理
     *
     * @param atlasFileBo 文件管理Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(AtlasFileBo atlasFileBo) {
        AtlasFile atlasFile = MapstructUtils.convert(atlasFileBo, AtlasFile.class);
        if (ObjectUtil.isNotNull(atlasFile) && ObjectUtil.isNotNull(atlasFile.getId())) {
            boolean updated = this.updateById(atlasFile);
            return updated;
        }
        return false;
    }

    @Override
    public void asyncImportData(MultipartFile file, boolean updateSupport, LoginUser loginUser) {
        ExcelResultRes result;
        try {
            String name = file.getOriginalFilename();
            result = excelService.importExcel(file.getInputStream(), name, AtlasFileImportVo.class, new AtlasFileImportListener(updateSupport, loginUser));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        boolean flag = importExportService.saveInfo(result, loginUser, "0");
        if (flag) {
            log.info("异步导入日志写入成功");
        }
    }

    @Override
    public void asyncExport(List<AtlasFileVo> listVo, String sheetName, LoginUser loginUser) {
        ExcelResultRes result = excelService.exportExcel(listVo, sheetName, AtlasFileVo.class);
        boolean flag = importExportService.saveInfo(result, loginUser, "1");
        if (flag) {
            log.info("异步导出日志写入成功");
        }
    }

    /**
     * 批量删除文件管理
     *
     * @param ids 需要删除的文件管理主键集合
     * @return 结果:true 删除成功，false 删除失败
     */
    @Transactional
    @Override
    public boolean deleteByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

}
