package cn.chnmuseum.party.service.impl;

import cn.chnmuseum.party.common.enums.*;
import cn.chnmuseum.party.common.mvc.InterfaceException;
import cn.chnmuseum.party.mapper.AssetMapper;
import cn.chnmuseum.party.mapper.AuditMapper;
import cn.chnmuseum.party.model.*;
import cn.chnmuseum.party.service.*;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <pre>
 * 学习内容信息 服务实现类
 * </pre>
 *
 * @author Danny Lee
 * @since 2021-03-26
 */
@Slf4j
@Service
public class AuditServiceImpl extends ServiceImpl<AuditMapper, Audit> implements AuditService {

    @Resource
    private AuditMapper auditMapper;

    @Resource
    private AssetMapper assetMapper;

    @Resource
    private TUserServiceImpl userService;

    @Resource
    private VideoContentService videoContentService;

    @Resource
    private ExhibitionBoardService exhibitionBoardService;

    @Resource
    private LearningContentService learningContentService;

    @Resource
    private LearningContentBoardService learningContentBoardService;

    @Resource
    private LearningContentBoardCatService learningContentBoardCatService;

    @Resource
    private LearningContentCopyrightOwnerService learningContentCopyrightOwnerService;

    @Override
    public Page<Audit> getUserList(Page<Audit> page, TUser user) {
        return page.setRecords(auditMapper.getUserList(page, user));
    }

    /**
     * 分页查询
     */
    @Override
    public Page<Audit> pageList(String name, AuditStatusEnum status, AuditStatusEnum auditStatusLevel, AuditTypeEnum type, Page<Object> page) {
        //分页
        Page<Audit> auditPage = new Page<>();
        auditPage.setCurrent(page.getCurrent());
        auditPage.setSize(page.getSize());
        //条件
        QueryWrapper<Audit> ew = new QueryWrapper<Audit>()
                //暂时注掉
//                .eq("a.level", auditStatusLevel.name())
                .eq("a.type", type.name())
//                .eq("b.is_deleted", 0)
//                .eq("c.is_deleted", 0)
                .orderByDesc("a.create_time", "a.id");
        //
        if (status != null) {
            ew.eq(status != null, "a.status", status.name());
        }
        return pageByType(ew, name, type, auditPage);
    }

    /**
     * 分别查询
     */
    private Page<Audit> pageByType(QueryWrapper<Audit> ew, String name,
                                   AuditTypeEnum type, Page<Audit> auditPage) {
        Page<Audit> selectPage = null;
        if (StringUtils.isNotBlank(name)) {
            ew.and(i -> i.like("b.name", name).or().like("c.user_name", name));
        }
        switch (type) {
            case LEARNING_CONTENT:
                selectPage = auditMapper.getLearningContentPage(auditPage, ew);
                break;
            case EXHIBITION_BOARD:
                selectPage = auditMapper.getExhibitionBoardPage(auditPage, ew);
                break;
            case ACCOUNT:
                break;
            case VIDEO_CONTENT:
                selectPage = auditMapper.getVideoContentPage(auditPage, ew);
            default:
        }
        //
        return selectPage;
    }

    /**
     * 根据id更新审核信息
     */
    @Override
    public boolean updateAuditAllById(Audit audit) {
        //审核层级,初审 和 复审
        AuditStatusEnum auditStatusLevel = AuditStatusEnum.valueOf(audit.getLevel());
        boolean update;
        switch (auditStatusLevel) {
            case TBC:
                update = updateOnTBC(audit);
                return update;
            case TBCA:
                update = updateOnTBCA(audit);
                return update;
            default:
                throw new InterfaceException("level参数不正确");
        }
    }

    @Override
    public Audit selectOne(String id, String type) {
        UpdateWrapper<Audit> wrapper = new UpdateWrapper<>();
        if (StringUtils.isNotBlank(id)) {
            wrapper.eq("ref_item_id", id);
        }
        if (StringUtils.isNotBlank(type)) {
            wrapper.eq("type", type);
        }
        wrapper.orderByDesc("create_time").last("limit 1");
        return getOne(wrapper);
    }

    /**
     * 初审级别的修改情况
     */
    private boolean updateOnTBC(Audit audit) {
        audit.setFirstTime(LocalDateTime.now());
        String status = audit.getStatus();
        AuditStatusEnum auditStatusEnum = AuditStatusEnum.valueOf(status);
        //初审 通过时,修改状态为待复审
        if (AuditStatusEnum.APPROVED_FINAL.equals(auditStatusEnum)) {
            audit.setStatus(AuditStatusEnum.TBCA.name());
            audit.setLevel(AuditStatusEnum.TBCA.name());
        }
        if (!AuditStatusEnum.REFUSED.equals(auditStatusEnum)) {
            audit.setFirstRemarks("");
        }
        audit.setSecondRemarks("");
        //通过与不通过时 都修改
        boolean updateRefItemByRefItemId = updateRefItemByRefItemId(audit);
        int update = auditMapper.updateById(audit);
        return updateRefItemByRefItemId && update >= 1;
        //初审驳回时,不做状态修改
//        int update = auditMapper.updateById(audit);
//        return update >= 1;
    }

    /**
     * 复审级别的修改情况
     */
    private boolean updateOnTBCA(Audit audit) {
        audit.setSecondTime(LocalDateTime.now());
        String status = audit.getStatus();
        AuditStatusEnum auditStatusEnum = AuditStatusEnum.valueOf(status);
        if (!AuditStatusEnum.REFUSED.equals(auditStatusEnum)) {
            audit.setSecondRemarks("");
        }
        audit.setFirstRemarks("");
        //复审通过时,
//        boolean updateRefItemByRefItemId = true;
//        if (AuditStatusEnum.APPROVED_FINAL.equals(auditStatusEnum)) {
//            //修改对应审核项中的信息
//            updateRefItemByRefItemId = updateRefItemByRefItemId(audit);
//        }

        //通过与不通过时 都修改
        boolean updateRefItemByRefItemId = updateRefItemByRefItemId(audit);
        int update = auditMapper.updateById(audit);
        return updateRefItemByRefItemId && update >= 1;
    }

    /**
     * 根据审核项ID 修改 审核项表中的信息
     */
    private boolean updateRefItemByRefItemId(Audit audit) {
        String type = audit.getType();
        AuditTypeEnum auditTypeEnum = AuditTypeEnum.valueOf(type);
        boolean update;
        switch (auditTypeEnum) {
            case VIDEO_CONTENT:
                update = fillVideoContentByAudit(audit);
                break;
//            case ACCOUNT:
//                break;
            case EXHIBITION_BOARD:
                update = fillExhibitionBoardByAudit(audit);
                break;
            case LEARNING_CONTENT:
                update = fillLearningContentByAudit(audit);
                break;
            default:
                throw new InterfaceException("type参数不正确");
        }
        return update;
    }

    /**
     * 根据审核操作 填充VideoContent属性用于更改
     */
    public boolean fillVideoContentByAudit(Audit audit) {
        final String videoContentId = audit.getRefItemId();
        VideoContent videoContent = new VideoContent();
        videoContent.setId(videoContentId);
        videoContent.setAuditStatus(audit.getStatus());
        //当审核级别为复审,审核状态为通过是,会修改审核项其它表中的 发布与删除字段,不是此情况下是直接修改审核状态
        boolean continueFill = AuditStatusEnum.APPROVED_FINAL.name().equals(audit.getStatus()) && AuditStatusEnum.TBCA.name().equals(audit.getLevel());
        if (!continueFill) {
            return this.videoContentService.updateById(videoContent);
        }
        String operation = audit.getOperation();
        AuditOperationEnum auditOperationEnum = AuditOperationEnum.valueOf(operation);
        boolean update = false;
        switch (auditOperationEnum) {
            case ENABLE:
                videoContent.setPublished(true);
                update = this.videoContentService.updateById(videoContent);
                break;
            case DISABLE:
                videoContent.setPublished(false);
                update = this.videoContentService.updateById(videoContent);
                break;
            case REMOVE:
                final LambdaQueryWrapper<ExhibitionBoard> queryWrapper = Wrappers.<ExhibitionBoard>lambdaQuery().eq(ExhibitionBoard::getVideoContentId, videoContentId).select(ExhibitionBoard::getId);
                final List<String> exhibitionBoardIdList = this.exhibitionBoardService.listObjs(queryWrapper, Object::toString);
                if (exhibitionBoardIdList != null && !exhibitionBoardIdList.isEmpty()) {
                    this.exhibitionBoardService.removeByIds(exhibitionBoardIdList);
                    this.assetMapper.delete(Wrappers.<Asset>lambdaUpdate().in(Asset::getRefItemId, exhibitionBoardIdList));

                    final LambdaQueryWrapper<LearningContentBoard> learningContentBoardLambdaQueryWrapper = Wrappers.<LearningContentBoard>lambdaQuery().in(LearningContentBoard::getExhibitionBoardCatId, exhibitionBoardIdList);
                    final List<LearningContentBoard> learningContentBoardList = this.learningContentBoardService.list(learningContentBoardLambdaQueryWrapper);
                    if (learningContentBoardList != null && !learningContentBoardList.isEmpty()) {
                        final Map<String, List<String>> collect = learningContentBoardList.stream().collect(Collectors.groupingBy(LearningContentBoard::getLearningContentId, Collectors.mapping(LearningContentBoard::getExhibitionBoardId, Collectors.toList())));
                        collect.forEach((k, v) -> {
                            if (v.size() == 1) {
                                this.learningContentService.removeById(k);
                                this.learningContentBoardService.remove(Wrappers.<LearningContentBoard>lambdaUpdate().eq(LearningContentBoard::getLearningContentId, k));
                                this.learningContentBoardCatService.remove(Wrappers.<LearningContentBoardCat>lambdaUpdate().eq(LearningContentBoardCat::getLearningContentId, k));
                                this.learningContentCopyrightOwnerService.remove(Wrappers.<LearningContentCopyrightOwner>lambdaUpdate().eq(LearningContentCopyrightOwner::getLearningContentId, k));
                            }
                        });

                        LambdaUpdateWrapper<LearningContentBoard> deleteWrapper1 = Wrappers.<LearningContentBoard>lambdaUpdate().in(LearningContentBoard::getExhibitionBoardId, exhibitionBoardIdList);
                        this.learningContentBoardService.remove(deleteWrapper1);
                    }
                }
                this.assetMapper.delete(Wrappers.<Asset>lambdaUpdate().in(Asset::getRefItemId, videoContentId));
                update = this.videoContentService.removeById(videoContentId);
                break;
            case ADD:
                this.assetMapper.update(Asset.builder().published(true).build(), Wrappers.<Asset>lambdaUpdate().eq(Asset::getRefItemId, videoContentId));
                videoContent.setPublished(true);
                update = this.videoContentService.updateById(videoContent);
                break;
            case EDIT:
                final String data = this.auditMapper.selectById(audit.getId()).getModelData();
                final VideoContent one = JSONObject.parseObject(data, VideoContent.class);
                final List<String> videoFileIdList = one.getVideoFileIdList();
                if (videoFileIdList != null && !videoFileIdList.isEmpty()) {
                    final LambdaQueryWrapper<Asset> assetQueryWrapper = Wrappers.<Asset>lambdaQuery().eq(Asset::getRefItemId, videoContentId);
                    assetQueryWrapper.eq(Asset::getPublished, true);
                    final List<Asset> assetList = this.assetMapper.selectList(assetQueryWrapper);
                    final Map<String, String> collect = assetList.stream().collect(Collectors.toMap(Asset::getId, Asset::getFileUrl));
                    for (String videoFileId : videoFileIdList) {
                        final Asset asset = this.assetMapper.selectById(videoFileId);
                        asset.setThumbnail(videoContent.getThumbnail());
                        asset.setFileType(FileTypeEnum.VIDEO.name());
                        asset.setFileCat(FileCatEnum.VIDEO_CONTENT.name());
                        asset.setRefItemId(videoContentId);
                        asset.setPublished(true);
                        this.assetMapper.updateById(asset);
                        if (StringUtils.isBlank(videoContent.getName())) {
                            videoContent.setName(asset.getVideoContentName());
                            this.videoContentService.updateById(videoContent);
                        }
                        collect.remove(videoFileId);
                    }
                    this.assetMapper.deleteBatchIds(collect.keySet());
                }
                one.setAuditStatus(audit.getStatus());
                one.setPublished(true);
                update = this.videoContentService.updateById(one);
                break;
            default:
        }
        return update;
    }

    /**
     * 根据审核操作 填充ExhibitionBoard属性用于更改
     */
    public boolean fillExhibitionBoardByAudit(Audit audit) {
        final String exhibitionBoardId = audit.getRefItemId();
        ExhibitionBoard exhibitionBoard = new ExhibitionBoard();
        exhibitionBoard.setId(exhibitionBoardId);
        exhibitionBoard.setAuditStatus(audit.getStatus());
        //当审核级别为复审,审核状态为通过是,会修改审核项其它表中的 发布与删除字段,不是此情况下是直接修改审核状态
        boolean continueFill = AuditStatusEnum.APPROVED_FINAL.name().equals(audit.getStatus()) && AuditStatusEnum.TBCA.name().equals(audit.getLevel());
        if (!continueFill) {
            return this.exhibitionBoardService.updateById(exhibitionBoard);
        }
        String operation = audit.getOperation();
        AuditOperationEnum auditOperationEnum = AuditOperationEnum.valueOf(operation);
        boolean update = false;
        switch (auditOperationEnum) {
            case UPPER:
                exhibitionBoard.setPublished(true);
                update = this.exhibitionBoardService.updateById(exhibitionBoard);
                break;
            case LOWER:
                exhibitionBoard.setPublished(false);
                update = this.exhibitionBoardService.updateById(exhibitionBoard);
                break;
            case REMOVE:
                final LambdaQueryWrapper<LearningContentBoard> learningContentBoardLambdaQueryWrapper = Wrappers.<LearningContentBoard>lambdaQuery().eq(LearningContentBoard::getExhibitionBoardCatId, exhibitionBoardId);
                final List<LearningContentBoard> learningContentBoardList = this.learningContentBoardService.list(learningContentBoardLambdaQueryWrapper);
                if (learningContentBoardList != null && !learningContentBoardList.isEmpty()) {
                    final Map<String, List<String>> collect = learningContentBoardList.stream().collect(Collectors.groupingBy(LearningContentBoard::getLearningContentId, Collectors.mapping(LearningContentBoard::getExhibitionBoardId, Collectors.toList())));
                    collect.forEach((k, v) -> {
                        if (v.size() == 1) {
                            this.learningContentService.removeById(k);
                            this.learningContentBoardService.remove(Wrappers.<LearningContentBoard>lambdaUpdate().eq(LearningContentBoard::getLearningContentId, k));
                            this.learningContentBoardCatService.remove(Wrappers.<LearningContentBoardCat>lambdaUpdate().eq(LearningContentBoardCat::getLearningContentId, k));
                            this.learningContentCopyrightOwnerService.remove(Wrappers.<LearningContentCopyrightOwner>lambdaUpdate().eq(LearningContentCopyrightOwner::getLearningContentId, k));
                        }
                    });
                    LambdaUpdateWrapper<LearningContentBoard> learningContentBoardLambdaUpdateWrapper = Wrappers.<LearningContentBoard>lambdaUpdate().eq(LearningContentBoard::getExhibitionBoardId, exhibitionBoardId);
                    this.learningContentBoardService.remove(learningContentBoardLambdaUpdateWrapper);
                }
                this.assetMapper.delete(Wrappers.<Asset>lambdaUpdate().eq(Asset::getRefItemId, exhibitionBoardId));
                update = this.exhibitionBoardService.removeById(exhibitionBoardId);
                break;
            case ADD:
                this.assetMapper.update(Asset.builder().published(true).build(), Wrappers.<Asset>lambdaUpdate().eq(Asset::getRefItemId, exhibitionBoardId));
                exhibitionBoard.setPublished(true);
                update = this.exhibitionBoardService.updateById(exhibitionBoard);
                break;
            case EDIT:
                final String data = this.auditMapper.selectById(audit.getId()).getModelData();
                final ExhibitionBoard one = JSONObject.parseObject(data, ExhibitionBoard.class);
                final List<String> audioIdList = one.getAudioIdList();
                if (audioIdList != null && !audioIdList.isEmpty()) {
                    final LambdaQueryWrapper<Asset> assetQueryWrapper = Wrappers.<Asset>lambdaQuery().eq(Asset::getRefItemId, exhibitionBoardId);
                    assetQueryWrapper.eq(Asset::getFileCat, FileCatEnum.EXHIBITION_BOARD_AUDIO.name());
                    assetQueryWrapper.eq(Asset::getPublished, true);
                    final List<Asset> assetList = this.assetMapper.selectList(assetQueryWrapper);
                    final Map<String, String> collect = assetList.stream().collect(Collectors.toMap(Asset::getId, Asset::getFileUrl));
                    for (String audioId : audioIdList) {
                        final Asset asset = this.assetMapper.selectById(audioId);
                        asset.setFileType(FileTypeEnum.AUDIO.name());
                        asset.setFileCat(FileCatEnum.EXHIBITION_BOARD_AUDIO.name());
                        asset.setRefItemId(exhibitionBoardId);
                        asset.setPublished(true);
                        this.assetMapper.updateById(asset);
                        collect.remove(audioId);
                    }
                    this.assetMapper.deleteBatchIds(collect.keySet());
                }
                final List<String> datumIdList = one.getDatumIdList();
                if (datumIdList != null && !datumIdList.isEmpty()) {
                    final LambdaQueryWrapper<Asset> assetQueryWrapper = Wrappers.<Asset>lambdaQuery().eq(Asset::getRefItemId, exhibitionBoardId);
                    assetQueryWrapper.eq(Asset::getFileCat, FileCatEnum.EXHIBITION_BOARD_DATUM.name());
                    assetQueryWrapper.eq(Asset::getPublished, true);
                    final List<Asset> assetList = this.assetMapper.selectList(assetQueryWrapper);
                    final Map<String, String> collect = assetList.stream().collect(Collectors.toMap(Asset::getId, Asset::getFileUrl));
                    for (String datumId : datumIdList) {
                        final Asset asset = this.assetMapper.selectById(datumId);
                        asset.setFileCat(FileCatEnum.EXHIBITION_BOARD_DATUM.name());
                        asset.setRefItemId(exhibitionBoardId);
                        asset.setPublished(true);
                        this.assetMapper.updateById(asset);
                        collect.remove(datumId);
                    }
                    this.assetMapper.deleteBatchIds(collect.keySet());
                }
                one.setAuditStatus(audit.getStatus());
                one.setPublished(true);
                update = this.exhibitionBoardService.updateById(one);
                break;
            default:
        }
        return update;
    }

    /**
     * 根据审核操作 填充LearningContent属性用于更改
     */
    public boolean fillLearningContentByAudit(Audit audit) {
        final String learningContentId = audit.getRefItemId();
        LearningContent learningContent = new LearningContent();
        learningContent.setAuditStatus(audit.getStatus());
        learningContent.setId(learningContentId);
        //当审核级别为复审,审核状态为通过是,会修改审核项其它表中的 发布与删除字段,不是此情况下是直接修改审核状态
        boolean continueFill = AuditStatusEnum.APPROVED_FINAL.name().equals(audit.getStatus()) && AuditStatusEnum.TBCA.name().equals(audit.getLevel());
        if (!continueFill) {
            return this.learningContentService.updateById(learningContent);
        }
        String operation = audit.getOperation();
        AuditOperationEnum auditOperationEnum = AuditOperationEnum.valueOf(operation);
        boolean update = false;
        switch (auditOperationEnum) {
            case ENABLE:
                learningContent.setPublished(true);
                update = this.learningContentService.updateById(learningContent);
                break;
            case DISABLE:
                learningContent.setPublished(false);
                update = this.learningContentService.updateById(learningContent);
                break;
            case REMOVE:
                this.learningContentBoardService.remove(Wrappers.<LearningContentBoard>lambdaUpdate().eq(LearningContentBoard::getLearningContentId, learningContentId));
                this.learningContentBoardCatService.remove(Wrappers.<LearningContentBoardCat>lambdaUpdate().eq(LearningContentBoardCat::getLearningContentId, learningContentId));
                this.learningContentCopyrightOwnerService.remove(Wrappers.<LearningContentCopyrightOwner>lambdaUpdate().eq(LearningContentCopyrightOwner::getLearningContentId, learningContentId));
                update = this.learningContentService.removeById(learningContentId);
                break;
            case ADD:
                learningContent.setPublished(true);
                update = this.learningContentService.updateById(learningContent);
                break;
            case EDIT:
                final String data = this.auditMapper.selectById(audit.getId()).getModelData();
                final LearningContent one = JSONObject.parseObject(data, LearningContent.class);
                final List<String> exhibitionBoardCatIdList = one.getExhibitionBoardCatIdList();
                if (exhibitionBoardCatIdList != null && !exhibitionBoardCatIdList.isEmpty()) {
                    LambdaUpdateWrapper<LearningContentBoardCat> lambdaUpdateWrapper = Wrappers.<LearningContentBoardCat>lambdaUpdate().eq(LearningContentBoardCat::getLearningContentId, learningContentId);
                    this.learningContentBoardCatService.remove(lambdaUpdateWrapper);

                    for (String exhibitionBoardCatId : exhibitionBoardCatIdList) {
                        LearningContentBoardCat learningContentBoardCat = LearningContentBoardCat.builder().exhibitionBoardCatId(exhibitionBoardCatId).learningContentId(learningContentId).build();
                        this.learningContentBoardCatService.save(learningContentBoardCat);
                    }
                }
                final List<String> copyrightOwnerIdList = one.getCopyrightOwnerIdList();
                if (copyrightOwnerIdList != null && !copyrightOwnerIdList.isEmpty()) {
                    LambdaUpdateWrapper<LearningContentCopyrightOwner> lambdaUpdateWrapper = Wrappers.<LearningContentCopyrightOwner>lambdaUpdate().eq(LearningContentCopyrightOwner::getLearningContentId, learningContentId);
                    this.learningContentCopyrightOwnerService.remove(lambdaUpdateWrapper);

                    for (String copyrightOwnerId : copyrightOwnerIdList) {
                        LearningContentCopyrightOwner contentCopyrightOwner = LearningContentCopyrightOwner.builder().copyrightOwnerId(copyrightOwnerId).learningContentId(learningContentId).build();
                        this.learningContentCopyrightOwnerService.save(contentCopyrightOwner);
                    }
                }
                final List<String> exhibitionBoardIdList = one.getExhibitionBoardIdList();
                if (exhibitionBoardIdList != null && !exhibitionBoardIdList.isEmpty()) {
                    LambdaUpdateWrapper<LearningContentBoard> lambdaUpdateWrapper = Wrappers.<LearningContentBoard>lambdaUpdate().eq(LearningContentBoard::getLearningContentId, learningContentId);
                    this.learningContentBoardService.remove(lambdaUpdateWrapper);

                    for (String exhibitionBoardId : exhibitionBoardIdList) {
                        LearningContentBoard learningContentBoard = LearningContentBoard.builder()
                                .learningContentId(learningContentId)
                                .exhibitionBoardCatId(this.exhibitionBoardService.getById(exhibitionBoardId).getExhibitionBoardCatId())
                                .exhibitionBoardId(exhibitionBoardId)
                                .build();
                        this.learningContentBoardService.save(learningContentBoard);
                    }
                }
                one.setAuditStatus(audit.getStatus());
                one.setPublished(true);
                update = this.learningContentService.updateById(one);
                break;
            default:
        }
        return update;
    }

    /**
     * 根据审核项数据插入审核记录
     *
     * @param refItemId     审核项ID
     * @param content       审核内容
     * @param typeEnum      审核类型
     * @param operationEnum 操作类型
     */
    @Override
    public boolean saveByRefItemInfo(String refItemId, String content, AuditTypeEnum typeEnum, AuditOperationEnum operationEnum) {
        Audit audit = new Audit();
        //
        audit.setRefItemId(refItemId);
        audit.setContent(content);
        audit.setType(typeEnum.name());
        audit.setOperation(operationEnum.name());
        //
        audit.setLevel(AuditStatusEnum.TBC.name());
        audit.setStatus(AuditStatusEnum.TBC.name());
        audit.setCreateTime(LocalDateTime.now());
        //
        Object principal = SecurityUtils.getSubject().getPrincipal();
        if (principal instanceof TUser) {
            TUser user = (TUser) principal;
            audit.setUserId(user.getId());
            audit.setUserName(user.getUserName());
            audit.setOrganId(user.getOrgId());
            audit.setOrgName(user.getOrgName());
        }
        //保存
        int insert = auditMapper.insert(audit);
        return insert >= 1;
    }

}