package org.springblade.system.service.impl;

import jakarta.annotation.Resource;
import org.springblade.common.core.domain.R;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.system.domain.FilesClassifyMarkDto;
import org.springblade.system.mapper.FilesClassifyMarkMapper;
import org.springblade.system.service.FilesClassifyMarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;



/**
 * @Author：liang.chao
 * @Date：2025/9/9 - 17:04
 */

@Service
public class FilesClassifyMarkServiceImpl implements FilesClassifyMarkService {

    @Resource
    private FilesClassifyMarkMapper filesClassifyMarkMapper;

    @Override
    public List<FilesClassifyMarkDto> list(FilesClassifyMarkDto dto) {
        return filesClassifyMarkMapper.list(dto);
    }

    @Override
    public R add(FilesClassifyMarkDto dto) {
        List<FilesClassifyMarkDto> list = filesClassifyMarkMapper.listByName(dto);
        if (list.size() > 0) {
            return R.fail("该分类标记已存在");
        }
        dto.setCreateUserId(AuthUtil.getUser().getUserId().toString());
        dto.setCreateUserName(AuthUtil.getUser().getUserName());
        dto.setUpdateUserId(AuthUtil.getUser().getUserId().toString());
        dto.setUpdateUserName(AuthUtil.getUser().getUserName());
        Integer add = filesClassifyMarkMapper.add(dto);
        if (add <= 0) {
            return R.fail("添加失败");
        }
        return R.ok();
    }

    @Override
    public R edit(FilesClassifyMarkDto dto) {
        Integer isExit = filesClassifyMarkMapper.isExit(dto);
        if (isExit > 0) {
            return R.fail("文件分类标记名称已存在");
        }
        dto.setUpdateUserId(AuthUtil.getUser().getUserId().toString());
        dto.setUpdateUserName(AuthUtil.getUser().getUserName());
        Integer edit = filesClassifyMarkMapper.edit(dto);
        if (edit > 0) {
            return R.ok();
        } else {
            return R.fail("修改失败");
        }
    }

    @Override
    public Integer del(FilesClassifyMarkDto dto) {
        return filesClassifyMarkMapper.del(dto);
    }

    @Override
    public Integer isUseByFilesContents(String id) {
        Integer useByFilesContents = filesClassifyMarkMapper.getUseByFilesContents(id);
        Integer useByProFilesContents = filesClassifyMarkMapper.getUseByProFilesContents(id);
        if (useByFilesContents > 0 || useByProFilesContents > 0) {
            return 1;
        } else {
            return 0;
        }
    }
}
