package com.ruoyi.framework.task;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.plant.domain.TBrithstaffmgr;
import com.ruoyi.project.plant.domain.TStaffmgr;
import com.ruoyi.project.plant.service.ITBrithstaffmgrService;
import com.ruoyi.project.plant.service.ITStaffmgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 定时任务生日提醒
 *
 * @author ruoyi
 */
@Component
public class BirthTask extends BaseController
{
    /**
     * 注入人员信息的接口
     */
    @Autowired
    private ITStaffmgrService staffmgrService;

    /**
     * 注入人员信息的接口
     */
    @Autowired
    private ITBrithstaffmgrService brithstaffmgrService;

    public void BirthTask() throws ParseException {
        brithstaffmgrService.deleteAllBirthstaffmgr();
        List<TStaffmgr> staffmgrs = staffmgrService.selectTaskList(new TStaffmgr());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Long week = Long.parseLong(String.valueOf(3 * 24 * 3600 * 1000));
        Long zero = Long.parseLong(String.valueOf(0));
        for (TStaffmgr s : staffmgrs) {
            if (s.getBirthday() != null) {
                String[] newDays = sdf.format(new Date()).split("-");
                String newYear = newDays[0];
                String[] births = sdf.format(s.getBirthday()).split("-");
                String birthMonth = births[1];
                String birthDay = births[2];
                String birthday = newYear + "-" + birthMonth + "-" + birthDay;
                long difference = sdf.parse(birthday).getTime() - sdf.parse(sdf.format(new Date())).getTime();
                if (difference >= zero && difference < week) {
                    TBrithstaffmgr birthstaffmgr = new TBrithstaffmgr();
                    birthstaffmgr.setStaffid(s.getStaffid());
                    birthstaffmgr.setDeptId(s.getDeptId());
                    birthstaffmgr.setBirthdate(sdf.parse(birthday));
                    brithstaffmgrService.insertTBrithstaffmgr(birthstaffmgr);
                }
            }
        }
    }
}
