package org.jeecg.modules.finance.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.finance.entity.FdSupplementaryContractReceivables;
import org.jeecg.modules.finance.entity.FdSupplementaryContractReceivablesB;
import org.jeecg.modules.finance.service.IFdSupplementaryContractReceivablesBService;
import org.jeecg.modules.finance.service.IFdSupplementaryContractReceivablesService;
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/10/28 14:03
 */
public class FdSupplementaryContractReceivablesJob implements Job {

    @Resource
    IFdSupplementaryContractReceivablesService iFdSupplementaryContractReceivablesService;

    @Resource
    IFdSupplementaryContractReceivablesBService iFdSupplementaryContractReceivablesBService;

    @Resource
    IFdProjectReceivablesService iFdProjectReceivablesService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //主表数据
        List<FdSupplementaryContractReceivables> fdSupplementaryContractReceivablesList = iFdSupplementaryContractReceivablesService.getListAndCreditPeriod();

        //到款时间为空的子表数据
        LambdaQueryWrapper<FdSupplementaryContractReceivablesB> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.isNull(FdSupplementaryContractReceivablesB::getPaymentDate);
        lambdaQueryWrapper.eq(FdSupplementaryContractReceivablesB::getDelFlag, "0");
        lambdaQueryWrapper.orderByAsc(FdSupplementaryContractReceivablesB::getSerialNumber);
        List<FdSupplementaryContractReceivablesB> fdSupplementaryContractReceivablesBList = iFdSupplementaryContractReceivablesBService.list(lambdaQueryWrapper);
        //判断是否为空
        if (!fdSupplementaryContractReceivablesList.isEmpty() && fdSupplementaryContractReceivablesList.size() > 0&&!fdSupplementaryContractReceivablesBList.isEmpty()&&fdSupplementaryContractReceivablesBList.size()>0) {
            try {
                getListAndItems(fdSupplementaryContractReceivablesList,fdSupplementaryContractReceivablesBList);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    /***
     * @Title:获取邮件信息
            * @Description:TODO
            * @param fdSupplementaryContractReceivablesList
            * @return void
            * @exception
            * @author 陈创
            * @date 2019/10/28 15:22
            * @updatehistory:
     *   2019/10/28 15:22 新增
            */
    public void getListAndItems(List<FdSupplementaryContractReceivables> fdSupplementaryContractReceivablesList,List<FdSupplementaryContractReceivablesB> fdSupplementaryContractReceivablesBList) 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 (FdSupplementaryContractReceivables data : fdSupplementaryContractReceivablesList) {
            //主表id
            String dataId = data.getId();
            //付款节点名称
            String paymentNodeName = "";
            //客户信用期
            Integer creditPeriod=0;
            //判断是否是数字
            if(data.getCreditPeriod().matches("[0-9]+")){
                creditPeriod=Integer.parseInt(data.getCreditPeriod());
            }
            //子表循环
            for (FdSupplementaryContractReceivablesB dataItem : fdSupplementaryContractReceivablesBList) {
                //找到对应子表数据
                if (dataId.equals(dataItem.getPkSupplementaryContractReceivables()) && dataItem.getPaymentNoticeDate() != null) {
                    Date d1 = df.parse(formatter.format(nowDate));//当前日
                    Date d2 = df.parse(formatter.format(dataItem.getPaymentNoticeDate()));//付款通知书日期
                    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+dataItem.getMilestoneName()+"、";
                    }else if(differDay>30&&differDay%15==0){
                        //大于30天的则每15天
                        paymentNodeName=paymentNodeName+dataItem.getMilestoneName()+"、";
                    }
                }
            }
            Map<Object,Object> map=new HashMap<>();
            //邮件内容是否为空
            if(paymentNodeName!=null&&!paymentNodeName.equals("")){
                //财务负责人
                map.put("financePersonId",data.getFinancialManager());
                //项目经理PM
                map.put("bmPersonId",data.getPmPersonInCharge());
                //描述
                map.put("msg","项目应收账款单号:'"+data.getReceiptNumber()+"'-其中："+paymentNodeName+"超过30天暂未收到款！");
                mapList.add(map);
            }
        }
        //发送邮件
        if(mapList!=null&&mapList.size()>0){
            emailReminder(mapList);
        }
    }

    /***
     * @Title:发送邮件
            * @Description:TODO
            * @param mapList
            * @return void
            * @exception
            * @author 陈创
            * @date 2019/10/28 15:21
            * @updatehistory:
     *   2019/10/28 15:21 新增
            */
    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);
        }
    }
}
