package com.izouma.jmrh.service;

import com.izouma.jmrh.domain.*;
import com.izouma.jmrh.enums.MessageType;
import com.izouma.jmrh.enums.Reading;
import com.izouma.jmrh.exception.BusinessException;
import com.izouma.jmrh.repo.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class AuditHistoryService {

    private ArticleRepo                 articleRepo;
    private OrgInfoRepo                 orgInfoRepo;
    private ResourceSupplyAndDemandRepo resourceSupplyAndDemandRepo;
    private PostRepo                    postRepo;


    public AuditHistory updateState(AuditHistory auditHistory) {
        User user = new User();
        if (auditHistory.getArticle() != null) {
            articleRepo.updateStateById(auditHistory.getArticle());
        } else if (auditHistory.getOrgInfo() != null) {
            OrgInfo orgInfo = orgInfoRepo.findById(auditHistory.getOrgInfo().getId())
                    .orElseThrow(new BusinessException("无记录"));
            auditHistory.setTitle(orgInfo.getOrgName());
            user.setId(orgInfo.getUserId());
            orgInfoRepo.updateStateById(auditHistory.getOrgInfo());
        } else if (auditHistory.getResourceSupplyAndDemand() != null) {
            ResourceSupplyAndDemand resourceSupplyAndDemand = resourceSupplyAndDemandRepo
                    .findById(auditHistory.getResourceSupplyAndDemand().getId())
                    .orElseThrow(new BusinessException("无记录"));
            user.setId(resourceSupplyAndDemand.getPublishersId());
            resourceSupplyAndDemandRepo.updateStatusById(auditHistory.getResourceSupplyAndDemand());
        } else if (auditHistory.getPost() != null) {
            Post post = postRepo.findById(auditHistory.getPost().getId()).orElseThrow(new BusinessException("无记录"));
            user.setId(post.getUserId());
            post.setStatus(auditHistory.getPost().getStatus());
            postRepo.save(post);
        }
        return auditHistory;
    }
}
