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

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.loan.LoanSchedule;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.loan.ILoanScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LoanScheduleMapper;


/**
 * 业务进度Service业务层处理
 *
 * @author boman
 * @date 2024-04-24
 */
@Service
public class LoanScheduleServiceImpl implements ILoanScheduleService {
    @Autowired
    private LoanScheduleMapper loanScheduleMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 查询业务进度
     *
     * @param loanScheduleId 业务进度主键
     * @return 业务进度
     */
    @Override
    public LoanSchedule selectLoanScheduleByLoanScheduleId(Long loanScheduleId) {
        return loanScheduleMapper.selectLoanScheduleByLoanScheduleId(loanScheduleId);
    }

    /**
     * 查询业务进度列表
     *
     * @param loanSchedule 业务进度
     * @return 业务进度
     */
    @Override
    public AjaxResult selectLoanScheduleList(LoanSchedule loanSchedule) {
        //字典值中所有进度
        List<SysDictData> dictDataList = dictDataMapper.selectDictDataByType("loan_schedule");
        //查询该项目已有的进度
        List<LoanSchedule> loanSchedules = loanScheduleMapper.selectLoanScheduleList(loanSchedule);
        Map<String, List<LoanSchedule>> collect = null;
        if (loanSchedules != null && loanSchedules.size() > 0) {
            collect = loanSchedules.stream().collect(Collectors.groupingBy(LoanSchedule::getLoanScheduleValue));
        }
        List<LoanSchedule> loanScheduleList = new ArrayList<>();
        for (SysDictData sysDictData : dictDataList) {
            String dictValue = sysDictData.getDictValue();
            String dictLabel = sysDictData.getDictLabel();
            Long dictSort = sysDictData.getDictSort();
            LoanSchedule loanScheduleNew = new LoanSchedule();
            loanScheduleNew.setLoanScheduleName(dictLabel);
            loanScheduleNew.setLoanScheduleTime(null);
            loanScheduleNew.setLoanScheduleScore(dictSort);
            loanScheduleNew.setLoanScheduleValue(dictValue);
            if (collect != null && collect.size() > 0){
                List<LoanSchedule> loanSchedulesNew = collect.get(dictValue);
                if (loanSchedulesNew != null && loanSchedulesNew.size() > 0){
                    List<LoanSchedule> loanSchedules1 = collect.get(dictValue);
                    loanSchedules1.sort(Comparator.comparing(LoanSchedule::getLoanScheduleTime));
                    loanScheduleNew.setLoanScheduleTime(loanSchedules1.get(0).getLoanScheduleTime());
                }
            }
            loanScheduleList.add(loanScheduleNew);
        }
        loanScheduleList.sort(Comparator.comparing(LoanSchedule::getLoanScheduleScore));
        return AjaxResult.success(loanScheduleList);
    }

    /**
     * 新增业务进度
     *
     * @param loanSchedule 业务进度
     * @return 结果
     */
    @Override
    public int insertLoanSchedule(LoanSchedule loanSchedule) {
        loanSchedule.setCreateTime(DateUtils.getNowDate());
        return loanScheduleMapper.insertLoanSchedule(loanSchedule);
    }

    /**
     * 修改业务进度
     *
     * @param loanSchedule 业务进度
     * @return 结果
     */
    @Override
    public int updateLoanSchedule(LoanSchedule loanSchedule) {
        loanSchedule.setUpdateTime(DateUtils.getNowDate());
        return loanScheduleMapper.updateLoanSchedule(loanSchedule);
    }

    /**
     * 批量删除业务进度
     *
     * @param loanScheduleIds 需要删除的业务进度主键
     * @return 结果
     */
    @Override
    public int deleteLoanScheduleByLoanScheduleIds(Long[] loanScheduleIds) {
        return loanScheduleMapper.deleteLoanScheduleByLoanScheduleIds(loanScheduleIds);
    }

    /**
     * 删除业务进度信息
     *
     * @param loanScheduleId 业务进度主键
     * @return 结果
     */
    @Override
    public int deleteLoanScheduleByLoanScheduleId(Long loanScheduleId) {
        return loanScheduleMapper.deleteLoanScheduleByLoanScheduleId(loanScheduleId);
    }
}
