package com.ruoyi.ccdi.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.ccdi.project.domain.dto.CcdiEvidenceQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiEvidenceSaveDTO;
import com.ruoyi.ccdi.project.domain.entity.CcdiEvidence;
import com.ruoyi.ccdi.project.domain.vo.CcdiEvidenceVO;
import com.ruoyi.ccdi.project.mapper.CcdiEvidenceMapper;
import com.ruoyi.ccdi.project.service.ICcdiEvidenceService;
import com.ruoyi.common.exception.ServiceException;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 项目证据Service实现类
 *
 * @author ruoyi
 */
@Service
public class CcdiEvidenceServiceImpl implements ICcdiEvidenceService {

    @Resource
    private CcdiEvidenceMapper evidenceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CcdiEvidenceVO saveEvidence(CcdiEvidenceSaveDTO dto, String operator) {
        CcdiEvidence evidence = new CcdiEvidence();
        BeanUtils.copyProperties(dto, evidence);
        evidence.setConfirmBy(operator);
        evidence.setConfirmTime(new Date());
        evidenceMapper.insert(evidence);
        return toVO(evidence);
    }

    @Override
    public List<CcdiEvidenceVO> listEvidence(CcdiEvidenceQueryDTO queryDTO) {
        if (queryDTO.getProjectId() == null) {
            throw new ServiceException("项目ID不能为空");
        }
        LambdaQueryWrapper<CcdiEvidence> wrapper = new LambdaQueryWrapper<CcdiEvidence>()
            .eq(CcdiEvidence::getProjectId, queryDTO.getProjectId())
            .orderByDesc(CcdiEvidence::getConfirmTime)
            .orderByDesc(CcdiEvidence::getEvidenceId);

        if (StringUtils.hasText(queryDTO.getEvidenceType())) {
            wrapper.eq(CcdiEvidence::getEvidenceType, queryDTO.getEvidenceType());
        }
        if (StringUtils.hasText(queryDTO.getKeyword())) {
            String keyword = queryDTO.getKeyword().trim();
            wrapper.and(item -> item
                .like(CcdiEvidence::getRelatedPersonName, keyword)
                .or()
                .like(CcdiEvidence::getRelatedPersonId, keyword)
                .or()
                .like(CcdiEvidence::getEvidenceTitle, keyword)
                .or()
                .like(CcdiEvidence::getEvidenceSummary, keyword)
            );
        }
        return evidenceMapper.selectList(wrapper).stream().map(this::toVO).toList();
    }

    @Override
    public CcdiEvidenceVO getEvidence(Long evidenceId) {
        CcdiEvidence evidence = evidenceMapper.selectById(evidenceId);
        if (evidence == null) {
            throw new ServiceException("证据不存在");
        }
        return toVO(evidence);
    }

    private CcdiEvidenceVO toVO(CcdiEvidence evidence) {
        CcdiEvidenceVO vo = new CcdiEvidenceVO();
        BeanUtils.copyProperties(evidence, vo);
        return vo;
    }
}
