package com.ruoyi.invest.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.IdWorker;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.invest.domain.CreditReportQueryRecord;
import com.ruoyi.invest.domain.dto.CreditReportQueryRecordDto;
import com.ruoyi.invest.domain.vo.CreditReportAnalysisVO;
import com.ruoyi.invest.domain.vo.CreditReportQueryRecordVo;
import com.ruoyi.invest.mapper.CreditReportQueryRecordMapper;
import com.ruoyi.invest.service.ICreditReportQueryRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 征信报告查询记录Service业务层处理
 *
 * @author tianyongbao
 * @date 2024-03-20
 */
@Service
public class CreditReportQueryRecordServiceImpl implements ICreditReportQueryRecordService
{
    @Resource
    private CreditReportQueryRecordMapper creditReportQueryRecordMapper;

    /**
     * 查询征信报告查询记录
     *
     * @param id 征信报告查询记录主键
     * @return 征信报告查询记录
     */
    @Override
    public CreditReportQueryRecordVo selectCreditReportQueryRecordById(Long id)
    {
        return creditReportQueryRecordMapper.selectCreditReportQueryRecordById(id);
    }

    /**
     * 查询征信报告查询记录列表
     *
     * @param creditReportQueryRecordDto 征信报告查询记录
     * @return 征信报告查询记录
     */
    @Override
    public List<CreditReportQueryRecordVo> selectCreditReportQueryRecordList(CreditReportQueryRecordDto creditReportQueryRecordDto)
    {
        return creditReportQueryRecordMapper.selectCreditReportQueryRecordList(creditReportQueryRecordDto);
    }

    /**
     * 新增征信报告查询记录
     *
     * @param creditReportQueryRecord 征信报告查询记录
     * @return 结果
     */
    @Override
    public int insertCreditReportQueryRecord(CreditReportQueryRecord creditReportQueryRecord)
    {
        creditReportQueryRecord.setCreateBy(SecurityUtils.getUsername());
        creditReportQueryRecord.setCreateTime(DateUtils.getNowDate());
        creditReportQueryRecord.setId(IdWorker.getId());
        //查询原因为贷后管理或者本人查询，查询类型为软查询，否则为硬查询
        if(creditReportQueryRecord.getType().equals("4")||creditReportQueryRecord.getType().equals("5")){
            creditReportQueryRecord.setQueryType("2");
        }else {
            creditReportQueryRecord.setQueryType("1");
        }
        return creditReportQueryRecordMapper.insertCreditReportQueryRecord(creditReportQueryRecord);
    }

    /**
     * 修改征信报告查询记录
     *
     * @param creditReportQueryRecord 征信报告查询记录
     * @return 结果
     */
    @Override
    public int updateCreditReportQueryRecord(CreditReportQueryRecord creditReportQueryRecord)
    {
        creditReportQueryRecord.setUpdateBy(SecurityUtils.getUsername());
        creditReportQueryRecord.setUpdateTime(DateUtils.getNowDate());
        //查询原因为贷后管理或者本人查询，查询类型为软查询，否则为硬查询
        if(creditReportQueryRecord.getType().equals("4")||creditReportQueryRecord.getType().equals("5")){
            creditReportQueryRecord.setQueryType("2");
        }else {
            creditReportQueryRecord.setQueryType("1");
        }
        return creditReportQueryRecordMapper.updateCreditReportQueryRecord(creditReportQueryRecord);
    }

    /**
     * 批量删除征信报告查询记录
     *
     * @param ids 需要删除的征信报告查询记录主键
     * @return 结果
     */
    @Override
    public int deleteCreditReportQueryRecordByIds(Long[] ids)
    {
        return creditReportQueryRecordMapper.removeCreditReportQueryRecordByIds(ids);
    }

    /**
     * 删除征信报告查询记录信息
     *
     * @param id 征信报告查询记录主键
     * @return 结果
     */
    @Override
    public int deleteCreditReportQueryRecordById(Long id)
    {
        return creditReportQueryRecordMapper.removeCreditReportQueryRecordById(id);
    }


    /**
     * 查询信用卡账单
     *
     * @return 信用卡账单
     */
    @Override
    public CreditReportAnalysisVO selectCreditReportAnalysis(CreditReportQueryRecordDto dto)  {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date queryDate=DateUtils.getNowDate();
        if(dto.getQueryDateStart()!=null){
            try {
                queryDate=formatter.parse(dto.getQueryDateStart());
                dto.setQueryDateEnd(dto.getQueryDateStart());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        CreditReportAnalysisVO analysisVO=new CreditReportAnalysisVO();
        dto.setQueryType("1");

        Calendar calendarStart = Calendar.getInstance();
        calendarStart.setTime(queryDate);
        calendarStart.add(Calendar.MONTH, -1);
        dto.setQueryDateStart(formatter.format(calendarStart.getTime()));
        analysisVO.setLastOneMonths(creditReportQueryRecordMapper.selectCreditReportQueryRecordList(dto).size()+"");

        calendarStart.setTime(queryDate);
        calendarStart.add(Calendar.MONTH, -2);
        dto.setQueryDateStart(formatter.format(calendarStart.getTime()));
        analysisVO.setLastTwoMonths(creditReportQueryRecordMapper.selectCreditReportQueryRecordList(dto).size()+"");

        calendarStart.setTime(queryDate);
        calendarStart.add(Calendar.MONTH, -3);
        dto.setQueryDateStart(formatter.format(calendarStart.getTime()));
        analysisVO.setLastThreeMonths(creditReportQueryRecordMapper.selectCreditReportQueryRecordList(dto).size()+"");

        calendarStart.setTime(queryDate);
        calendarStart.add(Calendar.MONTH, -6);
        dto.setQueryDateStart(formatter.format(calendarStart.getTime()));
        analysisVO.setLastSixMonths(creditReportQueryRecordMapper.selectCreditReportQueryRecordList(dto).size()+"");


        calendarStart.setTime(queryDate);
        calendarStart.add(Calendar.MONTH, -12);
        dto.setQueryDateStart(formatter.format(calendarStart.getTime()));
        analysisVO.setLastOneYears(creditReportQueryRecordMapper.selectCreditReportQueryRecordList(dto).size()+"");


        calendarStart.setTime(queryDate);
        calendarStart.add(Calendar.MONTH, -24);
        dto.setQueryDateStart(formatter.format(calendarStart.getTime()));
        analysisVO.setLastTwoYears(creditReportQueryRecordMapper.selectCreditReportQueryRecordList(dto).size()+"");


        dto.setQueryDateStart("");
        analysisVO.setLastAllYears(creditReportQueryRecordMapper.selectCreditReportQueryRecordList(dto).size()+"");


        calendarStart.setTime(queryDate);
        calendarStart.add(Calendar.MONTH, -6);
        dto.setQueryDateStart(formatter.format(calendarStart.getTime()));
        dto.setType("5");
        dto.setQueryType("2");
        analysisVO.setLastSixMonthQueryCount(creditReportQueryRecordMapper.selectCreditReportQueryRecordList(dto).size()+"");

        calendarStart.setTime(queryDate);
        calendarStart.add(Calendar.MONTH, -6);
        dto.setQueryDateStart(formatter.format(calendarStart.getTime()));
        dto.setType("4");
        analysisVO.setLastSixMonthsAfterLoan(creditReportQueryRecordMapper.selectCreditReportQueryRecordList(dto).size()+"");

        calendarStart.setTime(queryDate);
        calendarStart.add(Calendar.MONTH, -24);
        dto.setQueryDateStart(formatter.format(calendarStart.getTime()));
        dto.setType("4");
        analysisVO.setLastTwoYearsAfterLoan(creditReportQueryRecordMapper.selectCreditReportQueryRecordList(dto).size()+"");

        dto.setQueryDateStart("");
        dto.setType("4");
        analysisVO.setTotalAfterLoan(creditReportQueryRecordMapper.selectCreditReportQueryRecordList(dto).size()+"");
        return analysisVO;
    }

    /**
     * 查询征信报告查询记录列表
     *
     * @param creditReportQueryRecordDto 征信报告查询记录
     * @return 征信报告查询记录
     */
    @Override
    public List<CreditReportQueryRecordVo> selectQueryInstitutionList(CreditReportQueryRecordDto creditReportQueryRecordDto)
    {
        return creditReportQueryRecordMapper.selectQueryInstitutionList(creditReportQueryRecordDto);
    }
}
