package org.jeecg.modules.financeManage.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.financeManage.entity.FdProjectReceivables;
import org.jeecg.modules.financeManage.entity.FdProjectReceivablesB;
import org.jeecg.modules.financeManage.service.IFdProjectReceivablesBService;
import org.jeecg.modules.financeManage.service.IFdProjectReceivablesService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**项目应收款定时任务
 * 描述
 *
 * @author:陈创
 * @date:2019/8/22 11:49
 */
public class FdProjectReceivablesJob implements Job {
    @Resource
    IFdProjectReceivablesService iFdProjectReceivablesService;
    @Resource
    IFdProjectReceivablesBService iFdProjectReceivablesBService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("999999999999999999");
        //项目应收款主表数据
        List<FdProjectReceivables> fdProjectReceivablesList=iFdProjectReceivablesService.getListAndCreditPeriod();
        //项目应收款明细到款时间为空的数据
        LambdaQueryWrapper<FdProjectReceivablesB> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.isNull(FdProjectReceivablesB::getPaymentDate);
        lambdaQueryWrapper.orderByAsc(FdProjectReceivablesB::getNumber);
        List<FdProjectReceivablesB> fdProjectReceivablesBList=iFdProjectReceivablesBService.list(lambdaQueryWrapper);
        //子表数据不为空则
        if(fdProjectReceivablesBList!=null&&fdProjectReceivablesBList.size()>0){
            try {
                getRemindersData(fdProjectReceivablesList,fdProjectReceivablesBList);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

    }

    /***
     * @Title:获取需通知的数据
            * @Description:TODO
            * @param fdProjectReceivablesList, fdProjectReceivablesBList]
            * @return void
            * @exception
            * @author 陈创
            * @date 2019/8/22 14:41
            * @updatehistory:
     *   2019/8/22 14:41 新增
            */
    public void getRemindersData(List<FdProjectReceivables> fdProjectReceivablesList,List<FdProjectReceivablesB> fdProjectReceivablesBList) throws ParseException {
        //存取需要发邮件通知的人员以及内容描述
        List<Map<Object,Object>> mapList=new ArrayList<>();
        //当前时间
        Date nowDate=new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //循环主表
        for(FdProjectReceivables fdProjectReceivables:fdProjectReceivablesList){
            Map<Object,Object> map=new HashMap<>();
            //主表id
            String fId=fdProjectReceivables.getId();
            //客户信用期
            Integer creditPeriod=0;
            //判断是否是数字
            if(fdProjectReceivables.getCreditPeriod().matches("[0-9]+")){
                creditPeriod=Integer.parseInt(fdProjectReceivables.getCreditPeriod());
            }
            //付款节点名称
            String paymentNodeName="";
            //循环子表
            for(FdProjectReceivablesB fdProjectReceivablesB:fdProjectReceivablesBList){
                //找到主表对应的子表数据
                if(fId.equals(fdProjectReceivablesB.getPkHeadId())&&fdProjectReceivablesB.getNoticeDate()!=null){
                    Date d1 = df.parse(formatter.format(nowDate));//当前日
                    Date d2 = df.parse(formatter.format(fdProjectReceivablesB.getNoticeDate()));//付款通知书日期
                    Long diff = d1.getTime() - d2.getTime();
                    Long days = diff / (1000 * 60 * 60 * 24);//相差天
                    //与信用期相差天数
                    Integer differDay=days.intValue()-creditPeriod;
                    System.out.println("天数："+differDay);
                    //等于30天
                    if(differDay==30){
                        paymentNodeName=paymentNodeName+fdProjectReceivablesB.getMilepostName()+"、";
                    }else if(differDay>30&&differDay%15==0){
                        //大于30天的则每15天
                        paymentNodeName=paymentNodeName+fdProjectReceivablesB.getMilepostName()+"、";
                    }
                }
            }
            //是否有需要提醒的子表数据
            if(paymentNodeName!=null&&!paymentNodeName.equals("")){
                //财务负责人
                map.put("financePersonId",fdProjectReceivables.getFinancePersonId());
                //项目经理PM
                map.put("bmPersonId",fdProjectReceivables.getBmPersonId());
                //描述
                map.put("msg","项目应收账款单号:'"+fdProjectReceivables.getCode()+"'-其中："+paymentNodeName+"超过30天暂未收到款！");
                mapList.add(map);
            }
        }
        //发送邮件
        if(mapList!=null&&mapList.size()>0){
            emailReminder(mapList);
        }
    }

    /***
     * @Title:邮件发起
            * @Description:TODO
            * @param mapList
            * @return void
            * @exception
            * @author 陈创
            * @date 2019/8/22 14:42
            * @updatehistory:
     *   2019/8/22 14:42 新增
            */
    public void emailReminder(List<Map<Object,Object>> mapList){
        //循环通知数据
        for(Map<Object,Object> map:mapList){
            //财务人员
            String financePerson=map.get("financePersonId").toString();
            //项目经理PM
            String pm=map.get("bmPersonId").toString();
            //内容
            String msg=map.get("msg").toString();
            iFdProjectReceivablesService.noticeFinance(financePerson,"项目应收款超龄通知",msg);
            iFdProjectReceivablesService.noticeFinance(pm,"项目应收款超龄通知",msg);
        }
    }
}
