package com.ningdatech.pmapi.filemanage.manage;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.basic.function.VUtils;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.file.entity.File;
import com.ningdatech.file.service.FileService;
import com.ningdatech.pmapi.filemanage.model.dto.DocumentationGroupSaveDTO;
import com.ningdatech.pmapi.filemanage.model.dto.DocumentationSaveDTO;
import com.ningdatech.pmapi.filemanage.model.entity.Documentation;
import com.ningdatech.pmapi.filemanage.model.entity.DocumentationGroup;
import com.ningdatech.pmapi.filemanage.model.param.DocumentationListParam;
import com.ningdatech.pmapi.filemanage.model.vo.DocumentationGroupVO;
import com.ningdatech.pmapi.filemanage.model.vo.DocumentationVO;
import com.ningdatech.pmapi.filemanage.service.IDocumentationGroupService;
import com.ningdatech.pmapi.filemanage.service.IDocumentationService;
import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails;
import com.ningdatech.pmapi.user.util.LoginUserUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Classname DocumentationManage
 * @Description
 * @Date 2023/6/6 16:04
 * @Author PoffyZhang
 */
@Component
@AllArgsConstructor
@Slf4j
public class DocumentationManage {

    private final IDocumentationService documentationService;

    private final IDocumentationGroupService documentationGroupService;

    private final FileService fileService;

    /**
     *
     * @return
     */
    public List<DocumentationGroupVO> groupList() {
        List<DocumentationGroup> list = documentationGroupService.list();

        return list.stream().map(g -> BeanUtil.copyProperties(g,DocumentationGroupVO.class)).collect(Collectors.toList());
    }

    /**
     * 文档列表
     * @param param
     * @return
     */
    public PageVo<DocumentationVO> list(DocumentationListParam param) {
        Page<Documentation> page = param.page();
        documentationService.page(page, Wrappers.lambdaQuery(Documentation.class)
                .eq(Objects.nonNull(param.getGroupId()), Documentation::getGroupId, param.getGroupId()));

        if(0L == page.getTotal()){
            return PageVo.empty();
        }

        List<DocumentationVO> vos = page.getRecords().stream()
                .map(d -> BeanUtil.copyProperties(d,DocumentationVO.class))
                .collect(Collectors.toList());

        return PageVo.of(vos,page.getTotal());
    }

    /**
     * 增加分组
     * @param dto
     * @return
     */
    public String addGroup(DocumentationGroupSaveDTO dto) {
        UserInfoDetails user = LoginUserUtil.loginUserDetail();
        if(documentationGroupService.count(Wrappers.lambdaQuery(DocumentationGroup.class)
                .eq(DocumentationGroup::getName,dto.getName())) > 0){
            throw new BizException("此分组名 已经存在");
        }

        DocumentationGroup documentationGroup = BeanUtil.copyProperties(dto, DocumentationGroup.class);
        documentationGroup.setCreateBy(user.getRealName());
        documentationGroup.setCreateOn(LocalDateTime.now());
        if(documentationGroupService.save(documentationGroup)){
            return "添加成功";
        }
        return "添加失败";
    }

    /**
     * 删除分组 同时删除文档
     * @param id
     * @return
     */
    @Transactional
    public String deleteGroup(Long id) {
        DocumentationGroup group = documentationGroupService.getById(id);
        VUtils.isTrue(Objects.isNull(group)).throwMessage("该分组不存在!");

        //
        documentationService.remove(Wrappers.lambdaQuery(Documentation.class)
                .eq(Documentation::getGroupId, id));
        documentationGroupService.removeById(id);

        return "删除成功";
    }

    /**
     * 上传文档
     * @param dto
     * @return
     */
    public String addDoc(DocumentationSaveDTO dto) {
        UserInfoDetails user = LoginUserUtil.loginUserDetail();

        Long fileId = dto.getFileId();
        File file = fileService.getById(fileId);

        VUtils.isTrue(Objects.isNull(file)).throwMessage("该文件不存在!");

        DocumentationGroup group = documentationGroupService.getById(dto.getGroupId());

        VUtils.isTrue(Objects.isNull(group)).throwMessage("分组不存在!");

        Documentation documentation = new Documentation();
        documentation.setCreateBy(user.getRealName());
        documentation.setCreateOn(LocalDateTime.now());
        documentation.setSize(Objects.nonNull(file.getSize()) ? file.getSize()/1000L : 0L);
        documentation.setFileId(fileId);
        documentation.setGroupId(dto.getGroupId());
        documentation.setFileName(file.getOriginalFileName());
        if(documentationService.save(documentation)){
            documentation.setSort(documentation.getId().intValue());
            documentationService.updateById(documentation);
        }

        return "添加成功";
    }

    /**
     * 删除文档
     * @param id
     * @return
     */
    public String deleteDoc(Long id) {
        Documentation doc = documentationService.getById(id);
        VUtils.isTrue(Objects.isNull(doc)).throwMessage("该文档不存在!");

        if(documentationService.removeById(id)){
            return "删除成功";
        }
        return "删除失败";
    }

    public DocumentationVO detail(Long id) {
        Documentation doc = documentationService.getById(id);
        if(Objects.isNull(doc)){
            return null;
        }
        return BeanUtil.copyProperties(doc,DocumentationVO.class);
    }
}
