package com.lz.modules.sys.service.app.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lz.common.utils.StringUtil;
import com.lz.modules.app.entity.StaffEntity;
import com.lz.modules.app.req.ResultRecordReq;
import com.lz.modules.app.service.StaffService;
import com.lz.modules.flow.entity.FlowRecord;
import com.lz.modules.flow.service.FlowRecordService;
import com.lz.modules.sys.dao.app.ResultCommentMapper;
import com.lz.modules.sys.entity.app.ResultComment;
import com.lz.modules.sys.service.app.ResultCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* <p>
* 业绩评论表 服务类
* </p>
*
* @author quyixiao
* @since 2020-08-10
*/

@Service
public class ResultCommentServiceImpl extends ServiceImpl<ResultCommentMapper, ResultComment> implements ResultCommentService {


    @Autowired
	private ResultCommentMapper resultCommentMapper;

    @Autowired
	private ResultCommentService resultCommentService;
    @Autowired
	private StaffService staffService;

    @Autowired
	private FlowRecordService flowRecordService;


	@Override
	public ResultComment selectResultCommentById(Long id){
		return resultCommentMapper.selectResultCommentById(id);
	}



	@Override
	public Long insertResultComment(ResultComment resultComment){
		return resultCommentMapper.insertResultComment(resultComment);
	}



	@Override
	public int updateResultCommentById(ResultComment resultComment){
		return resultCommentMapper.updateResultCommentById(resultComment);
	}



	@Override
	public int updateCoverResultCommentById(ResultComment resultComment){
		return resultCommentMapper.updateCoverResultCommentById(resultComment);
	}



	@Override
	public int deleteResultCommentById(Long id){
		return resultCommentMapper.deleteResultCommentById(id);
	}

	@Override
	public List<ResultComment> selectByRecordId(Long recordId) {
		return resultCommentMapper.selectByRecordId(recordId);
	}

	@Override
	public ResultComment selectLastComment(Long recordId) {
		return resultCommentMapper.selectLastComment(recordId);
	}

	@Override
	public void addOrUpdateComment(ResultRecordReq req, Long staffId, int status,FlowRecord lastUsedFlowRecord) {
		ResultComment comment = resultCommentService.selectResultCommentById(req.getResultCommitId());
		//如果评论不为空
		String resultComment = StringUtil.decodeBase64(req.getResultComment());
		String content = StringUtil.removeHtml(resultComment);
		if (StringUtil.isNotBlank(content)) {      //如果不为空,则
			if (comment == null) {
				StaffEntity mySelf = staffService.selectStaffById(staffId);

				comment = new ResultComment();
				comment.setStaffId(staffId);
				comment.setRecordId(req.getRecordResultId());
				comment.setComment(resultComment);
				comment.setStaffName(mySelf.getName());
				comment.setFlowRecordId(lastUsedFlowRecord.getId());
				comment.setStatus(status);
				resultCommentService.insertResultComment(comment);
			} else {
				comment.setComment(resultComment);
				comment.setStatus(status);
				resultCommentService.updateResultCommentById(comment);
			}
		} else {
			if (comment != null) {          //如果 comment 不为空，删除评论
				resultCommentService.deleteResultCommentById(comment.getId());
			}
		}
	}

	@Override
	public int updateResultCommentCommentById(Long commandId, String commandValue){
		return resultCommentMapper.updateResultCommentCommentById(commandId, commandValue);
	}

}
