package com.rongwei.bscommon.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwei.bscommon.sys.dao.KnowledgebaseCommentsDao;
import com.rongwei.bscommon.sys.service.KnowledgebaseCommentsService;
import com.rongwei.bscommon.sys.utils.KnowledgeSystemUtils;
import com.rongwei.bsentity.domain.KnowledgebaseCommentsDo;
import com.rongwei.bsentity.enums.KnowledgeInfo;
import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ChenGuangYu
 * @version 1.0
 * @email chenguangyu@irongwei.com
 * @date 2024/1/11 09:16
 */
@Service
public class KnowledgebaseCommentsServiceImpl extends ServiceImpl<KnowledgebaseCommentsDao, KnowledgebaseCommentsDo> implements KnowledgebaseCommentsService {

    @Autowired
    private KnowledgebaseCommentsDao knowledgebaseCommentsDao;

    @Autowired
    private KnowledgebaseCommentsService knowledgebaseCommentsService;

    public static final String NOTIFICATION_TITLE = "您收到一条【%s】新点评";


    @Override
    public R KnowledgebaseCommentsSave(Map<String, Object> objectMap) {


        String commentid = (String) objectMap.get("COMMENTID");//评论数据 id
        String commentcontent = (String) objectMap.get("COMMENTCONTENT");//评论内容
        String commenterid = (String) objectMap.get("COMMENTERID");//评论人id
        String commenteremployeeid = knowledgebaseCommentsDao.QueryJobNumber(commenterid);//获取评论人工号

        KnowledgeInfo knowledgeInfo = knowledgebaseCommentsDao.knowledgeInfo(commentid);
        String authorid = knowledgeInfo.getAuthorid();//获取作者
        String title = knowledgeInfo.getTitle();//获取数据标题
        String authornotification = (String) objectMap.get("AUTHORNOTIFICATION");//作者通知状态

        KnowledgebaseCommentsDo knowCommentInfo = new KnowledgebaseCommentsDo();
        knowCommentInfo.setId(SecurityUtil.getUUID());
        knowCommentInfo.setCommentid(commentid);
        knowCommentInfo.setCommentauthor(authorid);
        knowCommentInfo.setCommentcontent(commentcontent);
        knowCommentInfo.setCommenterid(commenterid);
        knowCommentInfo.setCommenteremployeeid(commenteremployeeid);
        Date currentDate = new Date();
        knowCommentInfo.setCreatedate(currentDate);
        knowCommentInfo.setCreateuserid(commenterid);
        knowledgebaseCommentsService.save(knowCommentInfo);
        if ("y".equals(authornotification)) {//系统消息通知作者
            List<String> personnelIdList = new ArrayList<>();
            personnelIdList.add(authorid);
            KnowledgeSystemUtils.sendNotify(String.format(NOTIFICATION_TITLE,title), commentcontent,
                    "", personnelIdList, "", "knowledge");
        }
        return R.ok();
    }
}
