package com.ruoyi.system.service;


import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.jPush.JPushToolUtil;
import com.ruoyi.system.domain.loan.LoanApplication;
import com.ruoyi.system.domain.remind.WaitRemind;
import com.ruoyi.system.mapper.LoanApplicationMapper;
import com.ruoyi.system.mapper.WaitRemindMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class Task {
    @Resource
    private WaitRemindMapper waitRemindMapper;

    @Resource
    private LoanApplicationMapper loanApplicationMapper;

    @Autowired
    private ISysUserService sysUserService;

    /***
     *
     * @throws Exception
     */
    @Async
    //@Scheduled(cron = "0 0/2 * * * ? ")
    //@Scheduled(cron = "0 20 0 * * ? ")
    //@Scheduled(cron = "0/30 * * * * ? ")
    @Scheduled(cron = "0 0 2 * * ? ")
    public void popover() throws Exception {
        LoanApplication loanApplicationNew = new LoanApplication();
        loanApplicationNew.setActuallyTime(DateUtils.getNowDate());
        //所有需要保后的项目,给风险发送待办
        List<LoanApplication> loanApplications = loanApplicationMapper.selectLoanAfterList(loanApplicationNew);
        //找到所有需要保后的项目
        if (loanApplications != null && loanApplications.size() > 0) {
            for (LoanApplication loanApplication : loanApplications) {
                Long loanApplicationId = loanApplication.getLoanApplicationId();
                //先去查询是否已经有待办提醒
                List<WaitRemind> waitReminds = waitRemindMapper.selectWaitRemindByLoanApplicationId(loanApplicationId);
                if (waitReminds != null && waitReminds.size() > 0) {
                    //判断项目是否是大于500W 且放贷时间超过半年
                    Double actuallyAmount = loanApplication.getActuallyAmount();
                    Date actuallyTime = loanApplication.getActuallyTime();
                    if (actuallyAmount != null && ObjectUtils.isNotEmpty(actuallyTime)) {
                        if (actuallyAmount >= 500 && (DateUtils.toLocalDate(actuallyTime).plusDays(180).isAfter(LocalDate.now()) || DateUtils.toLocalDate(actuallyTime).plusDays(180).isEqual(LocalDate.now()))) {

                            if (waitReminds.size() == 1) {
                                //说明只有首保则新增待办提醒
                                WaitRemind waitRemind = new WaitRemind();
                                waitRemind.setLoanApplicationId(loanApplicationId);
                                waitRemind.setLoanApplicationNumber(loanApplication.getLoanApplicationNumber());
                                waitRemind.setRemindTitle("您有一条【" + loanApplication.getEnterpriseName() + "】的信息需要进行多次保后");
                                waitRemind.setRemindTime(DateUtils.getNowDate());
                                waitRemind.setRemindType("12");
                                waitRemind.setReadUserId(loanApplication.getfUserId().toString());
                                waitRemind.setRemindContent(loanApplication.getEnterpriseName() + "有一条贷款申请进入多次保后阶段需及时处理");
                                waitRemindMapper.insertWaitRemind(waitRemind);

                                //发极光推送
                                SysUser user = sysUserService.selectUserById(loanApplication.getfUserId());
                                if(StringUtils.isNotEmpty(user.getJgId())){
                                    String notificationTitle = "消息通知";
                                    String msgTitle = "保后处理";
                                    String msgContent = loanApplication.getEnterpriseName() + "有一条贷款申请进入多次保后阶段需及时处理";
                                    String jPushVO = "";
                                    JPushToolUtil.sendToRegistrationId(notificationTitle,msgTitle,msgContent,jPushVO,user.getJgId());
                                }
                            }
                        }
                    }
                } else {
                    //新增首保待办提醒
                    WaitRemind waitRemind = new WaitRemind();
                    waitRemind.setLoanApplicationId(loanApplicationId);
                    waitRemind.setLoanApplicationNumber(loanApplication.getLoanApplicationNumber());
                    waitRemind.setRemindTitle("您有一条【" + loanApplication.getEnterpriseName() + "】的信息需要进行保后");
                    waitRemind.setRemindTime(DateUtils.getNowDate());
                    waitRemind.setRemindType("12");
                    waitRemind.setReadUserId(loanApplication.getfUserId().toString());
                    waitRemind.setRemindContent(loanApplication.getEnterpriseName() + "有一条贷款申请进入保后阶段需及时处理");
                    waitRemindMapper.insertWaitRemind(waitRemind);

                    //发极光推送
                    SysUser user = sysUserService.selectUserById(loanApplication.getfUserId());
                    if(StringUtils.isNotEmpty(user.getJgId())){
                        String notificationTitle = "消息通知";
                        String msgTitle = "保后处理";
                        String msgContent = loanApplication.getEnterpriseName() + "有一条贷款申请进入保后阶段需及时处理";
                        String jPushVO = "";
                        JPushToolUtil.sendToRegistrationId(notificationTitle,msgTitle,msgContent,jPushVO,user.getJgId());
                    }
                }
            }
        }
    }
}
