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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.paper.library.business.entity.PaperLibraryOther;
import com.qmth.paper.library.business.service.BasicFileTypeService;
import com.qmth.paper.library.business.service.PaperLibraryOtherService;
import com.qmth.paper.library.common.bean.params.BasicFileTypeParams;
import com.qmth.paper.library.common.bean.result.DBVerifyResult;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.entity.BasicFileType;
import com.qmth.paper.library.common.enums.ExceptionResultEnum;
import com.qmth.paper.library.common.enums.FileTypeEnum;
import com.qmth.paper.library.common.mapper.BasicFileTypeMapper;
import com.qmth.paper.library.common.util.ServletUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class BasicFileTypeServiceImpl extends ServiceImpl<BasicFileTypeMapper, BasicFileType> implements BasicFileTypeService {

    @Resource
    private PaperLibraryOtherService paperLibraryOtherService;

    @Override
    public IPage<BasicFileType> pageData(Integer pageNumber, Integer pageSize) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        LambdaQueryWrapper<BasicFileType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BasicFileType::getSchoolId, schoolId);
        queryWrapper.orderByAsc(BasicFileType::getCreateTime);
        return this.baseMapper.selectPage(new Page<>(pageNumber, pageSize), queryWrapper);
    }

    @Override
    @Transactional
    public Long saveBasicFileType(BasicFileTypeParams params) throws IllegalAccessException {
        DBVerifyResult dbVerifyResult = SystemConstant.verifyDBFields(params, params.getClass());
        if(!dbVerifyResult.getStatus()){
            throw ExceptionResultEnum.ERROR.exception(dbVerifyResult.getMessage());
        }

        Long id = params.getId();
        String name = params.getName();
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());

        //校验名称是否重复
        LambdaQueryWrapper<BasicFileType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BasicFileType::getName, name.trim());
        queryWrapper.eq(BasicFileType::getSchoolId, schoolId);
        BasicFileType basicFileType = this.getOne(queryWrapper);
        if (Objects.nonNull(basicFileType)) {
            throw ExceptionResultEnum.ERROR.exception("文件类型名称【" + name + "】重复");
        }

        //编辑
        if (SystemConstant.longNotNull(id)) {
            BasicFileType oldFileType = this.getById(id);
            if (Objects.isNull(oldFileType)) {
                throw ExceptionResultEnum.ERROR.exception("文件类型不存在");
            }
            LambdaUpdateWrapper<BasicFileType> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(BasicFileType::getName, name);
            updateWrapper.eq(BasicFileType::getId, id);
            this.update(updateWrapper);
        } else { //新增
            id = SystemConstant.getDbUuid();
            BasicFileType fileType = new BasicFileType();
            fileType.setId(id);
            fileType.setName(name);
            fileType.setSchoolId(schoolId);
            fileType.setType(FileTypeEnum.CUSTOMER.name());
            this.save(fileType);
        }
        return id;
    }

    @Override
    public boolean deleteBasicFileTypeById(Long id) {
        BasicFileType fileType = this.getById(id);
        if (Objects.isNull(fileType)) {
            throw ExceptionResultEnum.ERROR.exception("文件类型不存在");
        }
        if (!fileType.getType().equals(FileTypeEnum.CUSTOMER.name())) {
            throw ExceptionResultEnum.ERROR.exception("只能删除自定义的文件类型");
        }
        //存在扫描文件,不能删除
        LambdaQueryWrapper<PaperLibraryOther> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaperLibraryOther::getFileTypeId, id);
        int count = paperLibraryOtherService.count(queryWrapper);
        if (count > 0) {
            throw ExceptionResultEnum.ERROR.exception("该文件类型已有扫描文件,不能删除");
        }
        return this.removeById(id);
    }

    @Override
    public List<BasicFileType> listBasicFileType() {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        LambdaQueryWrapper<BasicFileType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BasicFileType::getSchoolId, schoolId);
        queryWrapper.orderByAsc(BasicFileType::getCreateTime);
        return this.list(queryWrapper);
    }

}
