package com.qmth.paper.library.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.entity.BasicFileType;
import com.qmth.paper.library.common.enums.SystemFileTypeEnum;
import com.qmth.paper.library.common.mapper.BasicFileTypeMapper;
import com.qmth.paper.library.common.service.SystemFileTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class SystemFileTypeServiceImpl extends ServiceImpl<BasicFileTypeMapper, BasicFileType> implements SystemFileTypeService {

    private final Logger logger = LoggerFactory.getLogger(SystemFileTypeServiceImpl.class);

    @Override
    public void saveSystemFileType(Long schoolId) {
        SystemFileTypeEnum[] systemFileTypes = SystemFileTypeEnum.values();
        for (SystemFileTypeEnum systemFileType : systemFileTypes) {
            String name = systemFileType.getTitle();
            BasicFileType existFileType = getBasicFileType(schoolId, name);
            if (existFileType != null) {
                logger.warn(String.format("[初始化文件类型] 文件类型已经存在,school_id:%s,name:%s", schoolId, name));
            } else {
                BasicFileType fileType = new BasicFileType();
                fileType.setId(SystemConstant.getDbUuid());
                fileType.setName(name);
                fileType.setSchoolId(schoolId);
                fileType.setType(systemFileType.name());
                this.save(fileType);
            }
        }
    }

    private BasicFileType getBasicFileType(Long schoolId, String name) {
        LambdaQueryWrapper<BasicFileType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BasicFileType::getSchoolId, schoolId);
        queryWrapper.eq(BasicFileType::getName, name);
        return this.getOne(queryWrapper);
    }
}
