package com.hdkj.lt.bf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdkj.lt.base.model.response.PageVO;
import com.hdkj.lt.bf.entity.FhzgProblemFeedback;
import com.hdkj.lt.bf.entity.FhzgProblemFeedbackAttachment;
import com.hdkj.lt.bf.entity.convert.DTOToEntityConvertor;
import com.hdkj.lt.bf.entity.convert.EntityToVOConvertor;
import com.hdkj.lt.bf.entity.dto.AttachmentDTO;
import com.hdkj.lt.bf.entity.dto.ProblemFeedbackDTO;
import com.hdkj.lt.bf.entity.dto.ProblemFeedbackListDTO;
import com.hdkj.lt.bf.entity.dto.ProblemFeedbackUpdateDTO;
import com.hdkj.lt.bf.entity.vo.ProblemFeedbackVO;
import com.hdkj.lt.bf.mapper.FhzgProblemFeedbackMapper;
import com.hdkj.lt.bf.service.FhzgProblemFeedbackAttachmentService;
import com.hdkj.lt.bf.service.FhzgProblemFeedbackService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Sunlit
 * @description 针对表【fhzg_problem_feedback(用户反馈表（新）)】的数据库操作Service实现
 * @createDate 2025-12-25 15:40:40
 */
@Service
@RequiredArgsConstructor
public class FhzgProblemFeedbackServiceImpl extends ServiceImpl<FhzgProblemFeedbackMapper, FhzgProblemFeedback>
        implements FhzgProblemFeedbackService {

    private final FhzgProblemFeedbackAttachmentService problemFeedbackAttachmentService;

    @Override
    public void save(ProblemFeedbackDTO problemFeedbackDTO) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        FhzgProblemFeedback fhzgProblemFeedback = new FhzgProblemFeedback();
        fhzgProblemFeedback.setUserId(loginUser.getUserid());
        fhzgProblemFeedback.setUserName(loginUser.getUsername());
        fhzgProblemFeedback.setProblemType(problemFeedbackDTO.getProblemType());
        fhzgProblemFeedback.setProblemDetail(problemFeedbackDTO.getProblemDetail());
        fhzgProblemFeedback.setDeleted(0);
        fhzgProblemFeedback.setStatus(0);
        fhzgProblemFeedback.setCreateTime(new Date());
        this.baseMapper.insert(fhzgProblemFeedback);
        List<FhzgProblemFeedbackAttachment> problemFeedbackAttachments = new ArrayList<>();
        problemFeedbackDTO.getAttachments().forEach(i -> {
            FhzgProblemFeedbackAttachment problemFeedbackAttachment = new FhzgProblemFeedbackAttachment();
            problemFeedbackAttachment.setProblemFeedbackId(fhzgProblemFeedback.getId());
            problemFeedbackAttachment.setAttachmentId(i.getId());
            problemFeedbackAttachment.setAttachmentName(i.getName());
            problemFeedbackAttachment.setFileSize(i.getSize());
            problemFeedbackAttachment.setCreateTime(new Date());
            problemFeedbackAttachments.add(problemFeedbackAttachment);
        });
        problemFeedbackAttachmentService.saveBatch(problemFeedbackAttachments);
    }

    @Override
    public PageVO<ProblemFeedbackVO> list(ProblemFeedbackListDTO problemFeedbackListDTO) {
        LambdaQueryWrapper<FhzgProblemFeedback> queryWrapper = new LambdaQueryWrapper<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //todo: 需要重新确定
        if (!loginUser.getRoles().contains("1058060809470746624")) {
            //非管理员权限只能查自己的问题
            queryWrapper.eq(FhzgProblemFeedback::getUserId, SecurityUtils.getUserId());
        }
        if (problemFeedbackListDTO.getCreateTime() != null) {
            LocalDateTime start = problemFeedbackListDTO.getCreateTime().atStartOfDay();
            LocalDateTime end = problemFeedbackListDTO.getCreateTime().atTime(23, 59, 59, 999999999);
            queryWrapper.between(FhzgProblemFeedback::getCreateTime, start, end);
        }
        queryWrapper.eq(!problemFeedbackListDTO.getUserName().isEmpty(), FhzgProblemFeedback::getUserName, problemFeedbackListDTO.getUserName());
        queryWrapper.eq(problemFeedbackListDTO.getProblemType() != null, FhzgProblemFeedback::getProblemType, problemFeedbackListDTO.getProblemType());

        queryWrapper.eq(problemFeedbackListDTO.getStatus() != null, FhzgProblemFeedback::getStatus, problemFeedbackListDTO.getStatus());
        queryWrapper.orderByDesc(FhzgProblemFeedback::getCreateTime);
        Page<FhzgProblemFeedback> page = new Page<>();
        page.setCurrent(problemFeedbackListDTO.getCurrent());
        page.setSize(problemFeedbackListDTO.getPageSize());
        Page<FhzgProblemFeedback> problemFeedbackPage = this.page(page, queryWrapper);
        List<ProblemFeedbackVO> problemFeedbackVOList = new ArrayList<>();
        problemFeedbackPage.getRecords().forEach(i -> {
            ProblemFeedbackVO problemFeedbackVO = EntityToVOConvertor.INSTANCE.toProblemFeedbackVO(i);
            List<FhzgProblemFeedbackAttachment> list = problemFeedbackAttachmentService.list(
                    new LambdaUpdateWrapper<FhzgProblemFeedbackAttachment>().eq(FhzgProblemFeedbackAttachment::getProblemFeedbackId, i.getId())
                            .orderByAsc(FhzgProblemFeedbackAttachment::getCreateTime));
            problemFeedbackVO.setAttachments(EntityToVOConvertor.INSTANCE.toFhzgProblemFeedbackAttachmentList(list));
            problemFeedbackVOList.add(problemFeedbackVO);
        });
        PageVO<ProblemFeedbackVO> result = new PageVO<>();
        result.setTotal(problemFeedbackPage.getTotal());
        result.setPageNum(problemFeedbackPage.getCurrent());
        result.setPageSize(problemFeedbackPage.getSize());
        result.setRecords(problemFeedbackVOList);
        return result;
    }

    @Override
    public void update(ProblemFeedbackUpdateDTO problemFeedbackUpdateDTO) {
        FhzgProblemFeedback fhzgProblemFeedback = DTOToEntityConvertor.INSTANCE.toFhzgProblemFeedback(problemFeedbackUpdateDTO);
        this.baseMapper.updateById(fhzgProblemFeedback);
        if (problemFeedbackUpdateDTO.getAttachments() != null) {
            List<FhzgProblemFeedbackAttachment> list = problemFeedbackAttachmentService.list(new LambdaUpdateWrapper<FhzgProblemFeedbackAttachment>().eq(FhzgProblemFeedbackAttachment::getProblemFeedbackId, problemFeedbackUpdateDTO.getId()));
            List<Long> attachmentIds = list.stream().map(FhzgProblemFeedbackAttachment::getAttachmentId).collect(Collectors.toList());
            List<Long> newAttachmentIds = problemFeedbackUpdateDTO.getAttachments().stream().map(AttachmentDTO::getId).collect(Collectors.toList());
            List<Long> needDeleted = attachmentIds.stream().filter(i -> !newAttachmentIds.contains(i)).collect(Collectors.toList());
            if (!needDeleted.isEmpty()) {
                problemFeedbackAttachmentService.remove(new LambdaUpdateWrapper<FhzgProblemFeedbackAttachment>().in(FhzgProblemFeedbackAttachment::getAttachmentId, needDeleted));
            }
            List<Long> needAdd = newAttachmentIds.stream().filter(i -> !attachmentIds.contains(i)).collect(Collectors.toList());
            List<FhzgProblemFeedbackAttachment> needAddList = new ArrayList<>();
            problemFeedbackUpdateDTO.getAttachments().forEach(i -> {
                if (needAdd.contains(i.getId())) {
                    FhzgProblemFeedbackAttachment attachment = new FhzgProblemFeedbackAttachment();
                    attachment.setAttachmentId(i.getId());
                    attachment.setAttachmentName(i.getName());
                    attachment.setCreateTime(new Date());
                    attachment.setFileSize(i.getSize());
                    attachment.setProblemFeedbackId(fhzgProblemFeedback.getId());
                    needAddList.add(attachment);
                }
            });
            problemFeedbackAttachmentService.saveBatch(needAddList);
        }
    }

    @Override
    public ProblemFeedbackVO getById(Long id) {
        FhzgProblemFeedback fhzgProblemFeedback = this.baseMapper.selectById(id);
        if (fhzgProblemFeedback == null) {
            return new ProblemFeedbackVO();
        }
        List<FhzgProblemFeedbackAttachment> list = problemFeedbackAttachmentService.list(
                new LambdaUpdateWrapper<FhzgProblemFeedbackAttachment>().eq(FhzgProblemFeedbackAttachment::getProblemFeedbackId, id)
                        .orderByAsc(FhzgProblemFeedbackAttachment::getCreateTime));
        ProblemFeedbackVO problemFeedbackVO = EntityToVOConvertor.INSTANCE.toProblemFeedbackVO(fhzgProblemFeedback);
        List<AttachmentDTO> attachmentDTOList = new ArrayList<>();
        list.forEach(i -> {
            AttachmentDTO attachmentDTO = new AttachmentDTO();
            attachmentDTO.setId(i.getAttachmentId());
            attachmentDTO.setName(i.getAttachmentName());
            attachmentDTO.setSize(i.getFileSize());
            attachmentDTO.setCreateTime(i.getCreateTime());
            attachmentDTOList.add(attachmentDTO);
        });
        problemFeedbackVO.setAttachments(attachmentDTOList);
        return problemFeedbackVO;
    }
}



