package weaver.interfaces.mfkj.job;

import ln.TimeUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.time.*;

/**
 * @Title 周年纪念日  当天 自动增加 100 积分
 * @Author wangchaofa
 * @CreateDate 2024/2/4
 * @Version 1.0
 * @Description
 */
public class SynUserAnniversaryPointsJob extends BaseCronJob {

    BaseBean bb = new BaseBean();

    @Override
    public void execute() {
        bb.writeLog("========== start to syn user anniversary info ==========");
        try{
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            RecordSet rst = new RecordSet();

            int anniversary_points = Integer.parseInt(bb.getPropValue("syn_points_mfkj","anniversary_points"));

            String currdate = TimeUtil.getCurrentDateString();
            String currtime = currdate.substring(5,10);

            String sql ="  select k2.* from (select h.id,h.lastname,h.sex,h.departmentid,d.departmentname,s.subcompanyname,k.jobtitlename," +
                    "   h.companystartdate,convert(varchar(50), getdate(), 23) as enddate," +
                    "   (datediff(year,h.companystartdate,convert(varchar(50), getdate(), 23))) as zn" +
                    "   from hrmresource h" +
                    "   inner join HrmDepartment d on d.id = h.departmentid" +
                    "   inner join HrmSubCompany s on s.id = h.subcompanyid1" +
                    "   left join HrmJobTitles k on k.id = h.jobtitle\n" +
                    "   where right(h.companystartdate,5) = '"+currtime+"'" +
                    "   and h.status in(0,1,2,3)" +
                    "   and (h.belongto is null or  h.belongto = -1 ) " +
                    "   and h.companystartdate is not null "  +
                    " ) k2 " ;
            rs.executeQuery(sql);
            while (rs.next()){
                String companystartdate = Util.null2String(rs.getString("companystartdate"));
                String enddate = Util.null2String(rs.getString("enddate"));

                LocalDate date1 = LocalDate.parse(companystartdate);
                LocalDate date2 = LocalDate.parse(enddate);

                Period period = Period.between(date1, date2);
                int yearsDiff = period.getYears(); // 获取年数差距
                if(yearsDiff > 0){
                    String userid = Util.null2String(rs.getString("id"));
                    rs1.executeQuery("select * from uf_jftz where jfhdr=?",userid);
                    if(rs1.next()){
                        double zjf = weaver.general.Util.getDoubleValue(rs1.getString("zjf"),0);
                        double syjfs = weaver.general.Util.getDoubleValue(rs1.getString("syjfs"),0);
                        zjf += anniversary_points;
                        syjfs += anniversary_points;
                        boolean bool = rst.executeUpdate("update uf_jftz set zjf=?,syjfs=? where jfhdr=?",zjf,syjfs,userid);
                        rs.writeLog(userid+ " ====  周年纪念日当天  ============ 获得100积分  bool  =============== "+ bool);
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            bb.writeLog(e);
        }
        bb.writeLog("========== end to syn user anniversary info ==========");
    }
}
