package com.yunzhupaas.flowable.controller;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.UserInfo;
import com.yunzhupaas.base.controller.SuperController;
import com.yunzhupaas.base.vo.PageListVO;
import com.yunzhupaas.base.vo.PaginationVO;
import com.yunzhupaas.constant.MsgCode;
import com.yunzhupaas.exception.WorkFlowException;
import com.yunzhupaas.flowable.entity.CommentEntity;
import com.yunzhupaas.flowable.entity.TaskEntity;
import com.yunzhupaas.flowable.entity.TemplateNodeEntity;
import com.yunzhupaas.flowable.enums.NodeEnum;
import com.yunzhupaas.flowable.model.comment.CommentCrForm;
import com.yunzhupaas.flowable.model.comment.CommentListVO;
import com.yunzhupaas.flowable.model.comment.CommentPagination;
import com.yunzhupaas.flowable.model.templatenode.nodejson.NodeModel;
import com.yunzhupaas.flowable.service.CommentService;
import com.yunzhupaas.flowable.service.TaskService;
import com.yunzhupaas.flowable.service.TemplateNodeService;
import com.yunzhupaas.flowable.util.ServiceUtil;
import com.yunzhupaas.permission.entity.UserEntity;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.StringUtil;
import com.yunzhupaas.util.UploaderUtil;
import com.yunzhupaas.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
 * 流程评论
 *
 * @author 云筑产品开发平台组
 * @version V3.1.0
 * @copyright 深圳市乐程软件有限公司
 */
@Tag(name = "流程评论", description = "Comment")
@RestController
@RequestMapping("/api/workflow/comment")
public class CommentController extends SuperController<CommentService, CommentEntity> {

    @Autowired
    private ServiceUtil serviceUtil;
    
    @Autowired
    private CommentService commentService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private TemplateNodeService templateNodeService;

    /**
     * 获取流程评论列表
     *
     * @param pagination 分页模型
     * @return
     */
    @Operation(summary = "获取流程评论列表")
    @GetMapping
    public ActionResult<PageListVO<CommentListVO>> list(CommentPagination pagination) {
        List<CommentEntity> list = commentService.getlist(pagination);
        List<String> replyId = list.stream().filter(t -> StringUtil.isNotEmpty(t.getReplyId())).map(CommentEntity::getReplyId).collect(Collectors.toList());
        List<CommentEntity> replyList = commentService.getlist(replyId);
        List<CommentListVO> listVO = JsonUtil.getJsonToList(list, CommentListVO.class);
        List<String> userId = list.stream().map(t -> t.getCreatorUserId()).collect(Collectors.toList());
        userId.addAll(replyList.stream().map(CommentEntity::getCreatorUserId).collect(Collectors.toList()));
        UserInfo userInfo = UserProvider.getUser();
        List<UserEntity> userName = serviceUtil.getUserName(userId);
        for (CommentListVO commentModel : listVO) {
            UserEntity userEntity = userName.stream().filter(t -> t.getId().equals(commentModel.getCreatorUserId())).findFirst().orElse(null);
            if (commentModel.getCreatorUserId().equals(userInfo.getUserId()) && !"1".equals(String.valueOf(commentModel.getDeleteShow()))) {
                commentModel.setIsDel(1); //1-删除按钮显示
            } else if ("1".equals(String.valueOf(commentModel.getDeleteShow()))) {
                commentModel.setIsDel(2); //1-删除按钮显示
                commentModel.setText("该评论已被删除");
            } else {
                commentModel.setIsDel(0);
            }
            commentModel.setCreatorUser(userEntity != null ? userEntity.getRealName() + "/" + userEntity.getAccount() : "");
            commentModel.setCreatorUserHeadIcon(userEntity != null ? UploaderUtil.uploaderImg(userEntity.getHeadIcon()) : commentModel.getCreatorUserHeadIcon());
            List<CommentEntity> collect = replyList.stream().filter(t -> t.getId().equals(commentModel.getReplyId())).collect(Collectors.toList());
            StringJoiner name = new StringJoiner(",");
            StringJoiner text = new StringJoiner(",");
            for (CommentEntity replyEntity : collect) {
                if (null != replyEntity) {
                    UserEntity user = userName.stream().filter(t -> t.getId().equals(replyEntity.getCreatorUserId())).findFirst().orElse(null);
                    if (user != null) {
                        name.add(user.getRealName() + "/" + user.getAccount());
                    }
                    String resText = ("1".equals(String.valueOf(replyEntity.getDeleteShow())) || "1".equals(String.valueOf(replyEntity.getDeleteMark()))) ? "该评论已被删除" : replyEntity.getText();
                    text.add(resText);
                }
            }
            commentModel.setReplyText(text.toString());
            commentModel.setReplyUser(name.toString());
            CommentEntity entity = list.stream().filter(e -> ObjectUtil.equals(e.getId(), commentModel.getId())).findFirst().orElse(null);
            if (null != entity) {
                commentModel.setFile(entity.getFiles());
            }
        }
        PaginationVO vo = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
        return ActionResult.page(listVO, vo);
    }

    /**
     * 新建流程评论
     *
     * @param commentForm 流程评论模型
     * @return
     */
    @Operation(summary = "新建流程评论")
    @PostMapping
    @Parameters({
            @Parameter(name = "commentForm", description = "流程评论模型", required = true),
    })
    public ActionResult create(@RequestBody @Valid CommentCrForm commentForm) throws WorkFlowException {
        CommentEntity entity = JsonUtil.getJsonToBean(commentForm, CommentEntity.class);
        entity.setFiles(commentForm.getFile());
        commentService.create(entity);
        return ActionResult.success(MsgCode.SU002.get());
    }

    /**
     * 删除流程评论
     *
     * @param id 主键
     * @return
     */
    @Operation(summary = "删除流程评论")
    @DeleteMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
    })
    public ActionResult delete(@PathVariable("id") String id) {
        CommentEntity entity = commentService.getInfo(id);
        if (entity.getCreatorUserId().equals(UserProvider.getUser().getUserId())) {
            boolean delComment = false;
            TaskEntity task = taskService.getInfoSubmit(entity.getTaskId(), TaskEntity::getId, TaskEntity::getFlowId);
            if (task != null) {
                TemplateNodeEntity nodeEntity = templateNodeService.getList(task.getFlowId()).stream().filter(e -> StringUtil.equals(NodeEnum.global.getType(), e.getNodeType())).findFirst().orElse(null);
                if (nodeEntity != null) {
                    NodeModel nodeModel = JsonUtil.getJsonToBean(nodeEntity.getNodeJson(), NodeModel.class);
                    delComment = nodeModel.getHasCommentDeletedTips();
                }
            }
            commentService.delete(entity, delComment);
            return ActionResult.success(MsgCode.SU003.get());
        }
        return ActionResult.success(MsgCode.FA003.get());
    }

}

