package com.ruoyi.system.service.review.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.loan.LoanApplicationFj;
import com.ruoyi.system.domain.review.ReviewComments;
import com.ruoyi.system.mapper.LoanApplicationFjMapper;
import com.ruoyi.system.service.review.IReviewCommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ReviewCommentsMapper;

import static com.ruoyi.common.constant.CommonConstants.*;

/**
 * 业务审核意见Service业务层处理
 *
 * @author boman
 * @date 2024-04-24
 */
@Service
public class ReviewCommentsServiceImpl implements IReviewCommentsService {
    @Autowired
    private ReviewCommentsMapper reviewCommentsMapper;

    @Autowired
    private LoanApplicationFjMapper loanApplicationFjMapper;

    /**
     * 查询业务审核意见
     *
     * @param reviewCommentsId 业务审核意见主键
     * @return 业务审核意见
     */
    @Override
    public ReviewComments selectReviewCommentsByReviewCommentsId(Long reviewCommentsId) {
        ReviewComments reviewComments = reviewCommentsMapper.selectReviewCommentsByReviewCommentsId(reviewCommentsId);
        List<LoanApplicationFj> loanApplicationFjList = selectLoanApplicationFjList(reviewComments);
        reviewComments.setFjUrl(loanApplicationFjList);
        return reviewComments;
    }

    /**
     * 查询业务审核意见列表
     *
     * @param reviewComments 业务审核意见
     * @return 业务审核意见
     */
    @Override
    public List<ReviewComments> selectReviewCommentsList(ReviewComments reviewComments) {
        List<ReviewComments> reviewCommentsList = reviewCommentsMapper.selectReviewCommentsList(reviewComments);
        if (reviewCommentsList != null && reviewCommentsList.size() > 0) {
            for (ReviewComments comments : reviewCommentsList) {
                List<LoanApplicationFj> loanApplicationFjList = selectLoanApplicationFjList(comments);
                comments.setFjUrl(loanApplicationFjList);
            }
        }
        return reviewCommentsList;
    }

    //查询审核的对应类型附件
    public List<LoanApplicationFj> selectLoanApplicationFjList(ReviewComments reviewComments) {
        String auditSchedule = reviewComments.getAuditSchedule();
        Long loanApplicationId = reviewComments.getLoanApplicationId();
        String type = "-1";
        if (auditSchedule.equals(SIX)){
            type = "jzdcbg";
        }else if (auditSchedule.equals(FIV)){
            type = "dbxmhfhgb";
        }else if (auditSchedule.equals(SEV)){
            type = "psyjqpb,gdhyjy";
        }
        List<LoanApplicationFj> loanApplicationFjList = loanApplicationFjMapper.selectLoanApplicationFjByLoanApplicationIdAndType(loanApplicationId, type.split(","));
        return loanApplicationFjList;
    }

    /**
     * 新增业务审核意见
     *
     * @param reviewComments 业务审核意见
     * @return 结果
     */
    @Override
    public int insertReviewComments(ReviewComments reviewComments) {
        reviewComments.setCreateTime(DateUtils.getNowDate());
        return reviewCommentsMapper.insertReviewComments(reviewComments);
    }

    /**
     * 修改业务审核意见
     *
     * @param reviewComments 业务审核意见
     * @return 结果
     */
    @Override
    public int updateReviewComments(ReviewComments reviewComments) {
        reviewComments.setUpdateTime(DateUtils.getNowDate());
        return reviewCommentsMapper.updateReviewComments(reviewComments);
    }

    /**
     * 批量删除业务审核意见
     *
     * @param reviewCommentsIds 需要删除的业务审核意见主键
     * @return 结果
     */
    @Override
    public int deleteReviewCommentsByReviewCommentsIds(Long[] reviewCommentsIds) {
        return reviewCommentsMapper.deleteReviewCommentsByReviewCommentsIds(reviewCommentsIds);
    }

    /**
     * 删除业务审核意见信息
     *
     * @param reviewCommentsId 业务审核意见主键
     * @return 结果
     */
    @Override
    public int deleteReviewCommentsByReviewCommentsId(Long reviewCommentsId) {
        return reviewCommentsMapper.deleteReviewCommentsByReviewCommentsId(reviewCommentsId);
    }
}
