package com.energy.manage.service.service.analysiscomment.impl;

import com.energy.manage.common.po.analysis.AnalysisCommentDescriptionPo;
import com.energy.manage.common.reponse.ConstVar;
import com.energy.manage.common.reponse.ResultResp;
import com.energy.manage.common.util.UUIDTools;
import com.energy.manage.service.domain.dto.analysis.AnalysisCommentDescriptionDto;
import com.energy.manage.service.domain.dto.analysis.AnalysisCommentTypeRelationDto;
import com.energy.manage.service.domain.dto.analysis.CommentDto;
import com.energy.manage.service.domain.vo.analysis.AnalysisCommentDescriptionVo;
import com.energy.manage.service.domain.vo.analysis.AnalysisCommentTypeRelationVo;
import com.energy.manage.service.mappers.analysis.AnalysisCommentDescriptionMapper;
import com.energy.manage.service.mappers.analysis.AnalysisCommentTypeRelationMapper;
import com.energy.manage.service.service.analysis.impl.AnalysisServiceImpl;
import com.energy.manage.service.service.analysiscomment.AnalysisCommentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author chy
 * @date 2024/5/24 17:12
 * @desc
 */

@Service
public class AnalysisCommentServiceImpl implements AnalysisCommentService {

    @Autowired
    private AnalysisCommentTypeRelationMapper analysisCommentTypeRelationtMapper;

    @Autowired
    private AnalysisCommentDescriptionMapper commentDescriptionMapper;

    /**
     * 操作内容
     */
    private static final String OPTION_CONTENT = "评论编辑";

    /**
     * 富文本类型查询
     * @param batchCode
     * @param analysisTypeCode
     * @return
     */
    @Override
    public List<AnalysisCommentTypeRelationVo> analysisCommentTypeList(String batchCode,String analysisTypeCode) {
        return analysisCommentTypeRelationtMapper.selectByAnalysisTypeCode(batchCode,analysisTypeCode);
    }

    /**
     * 富文本类型添加
     * @param typeRelationDto
     * @return
     */
    @Override
    public ResultResp addAnalysisCommentType(AnalysisCommentTypeRelationDto typeRelationDto){
        String saveTypeName = StringUtils.trim(typeRelationDto.getCommentTypeName());
        typeRelationDto.setCommentTypeName(saveTypeName);
        if(duplicateCheckByTypeName(typeRelationDto)){
            return ResultResp.FAIL(ConstVar.DUPLICATE_TYPE_NAME);
        }
        AnalysisCommentTypeRelationDto analysisCommentTypeRelationDto = new AnalysisCommentTypeRelationDto();
        String typeCode = UUIDTools.getUUIDLowCase();
        analysisCommentTypeRelationDto.setBatchCode(typeRelationDto.getBatchCode())
                                      .setAnalysisTypeCode(typeRelationDto.getAnalysisTypeCode())
                                      .setCommentTypeCode(typeCode)
                                      .setCommentTypeName(saveTypeName)
                                      .setCreateBy(typeRelationDto.getCreateBy());
        return analysisCommentTypeRelationtMapper.insertPoSelective(analysisCommentTypeRelationDto) > 0 ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }


    /**
     * 富文本类型删除
     * @param commentTypeCode
     * @return
     */
    @Override
    public ResultResp delAnalysisCommentType(String commentTypeCode){
        AnalysisCommentTypeRelationVo analysisCommentTypeRelationVo = analysisCommentTypeRelationtMapper.selectByTypeCode(commentTypeCode);
        if(analysisCommentTypeRelationVo == null){
            return ResultResp.SUCCESS();
        }
        if(analysisCommentTypeRelationVo.getIsDefined() == 0){
            return ResultResp.FAIL(ConstVar.NO_DIFINED_TYPE);
        }
        return analysisCommentTypeRelationtMapper.deleteByTypeCode(commentTypeCode) > 0 ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }

    /**
     * 富文本编辑
     * @param dto
     * @Param userId
     * @return
     */
    @Override
    public int analysisCommentEdit(AnalysisCommentDescriptionDto dto) {
        AtomicInteger count = new AtomicInteger(0);
        dto.getCommentList().parallelStream().forEach(item -> count.getAndAdd(saveCommentDescription(dto, item)));
        AnalysisServiceImpl.addOptionRecords(null, dto.getBatchCode(),OPTION_CONTENT);
        return count.get();
    }

    /**
     * 富文本查询
     * @param batchCode
     * @param analysisTypeCode
     * @return
     */
    @Override
    public List<AnalysisCommentDescriptionVo> analysisCommentDescList(String batchCode, String analysisTypeCode) {
        return commentDescriptionMapper.selecByBatchCodeAndAnalysisTypeCode(batchCode, analysisTypeCode);
    }


    /**
     * 评论类型名称查重
     * @param typeRelationDto
     * @return
     */
    private boolean duplicateCheckByTypeName(AnalysisCommentTypeRelationDto typeRelationDto){
        List<AnalysisCommentTypeRelationVo> voList = analysisCommentTypeRelationtMapper
                                                        .selectByAnalysisTypeCode(typeRelationDto.getBatchCode(), typeRelationDto.getAnalysisTypeCode());
        voList = voList.parallelStream().filter(vo -> vo.getCommentTypeName().equals(typeRelationDto.getCommentTypeName())).collect(Collectors.toList());
        return voList.size() > 0;
    }

    /**
     * 保存富文本
     * @param commentDescriptionDto
     */
    private int saveCommentDescription(AnalysisCommentDescriptionDto commentDescriptionDto, CommentDto commentDto){
        //批次编号
        String batchCode = commentDescriptionDto.getBatchCode();
        //分析类型编号
        String analysisTypeCode = commentDescriptionDto.getAnalysisTypeCode();
        //评论类型编号
        String commentTypeCode = commentDto.getCommentTypeCode();
        AnalysisCommentDescriptionPo commentDescriptionPo = new AnalysisCommentDescriptionPo();
        //拷贝批次编号 分析类型编号
        commentDescriptionPo.setBatchCode(batchCode)
                            .setAnalysisTypeCode(analysisTypeCode)
                            .setCommentTypeCode(commentTypeCode)
                            .setComment(commentDto.getComment())
                            .setCreateBy(commentDescriptionDto.getCreateBy())
                            .setUpdateBy(commentDescriptionDto.getUpdateBy());
//        int count =  commentDescriptionMapper.selectCountByAllCode(batchCode,analysisTypeCode,commentTypeCode);
        if(StringUtils.isBlank(commentDto.getComment())){
            return 0;
        }
        return commentDescriptionMapper.insertPoSelective(commentDescriptionPo);
        //空则添加 非空则编辑
//        if(count == 0){
//            //如果评论内容为空则不添加
//            if(StringUtils.isBlank(commentDto.getComment())){
//                return 0;
//            }
//           return commentDescriptionMapper.insertPoSelective(commentDescriptionPo);
//        }else{
//           return commentDescriptionMapper.updatePoSelective(commentDescriptionPo);
//        }
    }

}
