java计算公司加班时间

package com.supcon.oms.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author: zhaoxu
 * @date: 2020/9/12 12:55
 */
public class CalculateTime {
     
    public ArrayList<String> xiabanlist = new ArrayList<String>();
    public ArrayList<String> shangbanlist = new ArrayList<String>();
    public ArrayList<String> yueri = new ArrayList<String>();
    String bc_date = "";
    String bc_date_end = "";
    int sum = 0;
    public ArrayList<Integer> index = new ArrayList<Integer>();
    byte[] buffer;
    String all;
    int length;
    String lt = "";

    public void login(String username,String password) {
     
        final String loginURL = "登录验证的url地址";
        String form = "portal_username="+username+"&password="+password+"<=xxx&_eventId=submit&username="+username;
        final String headerAgent = "User-Agent";
        final String headerAgentArg = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36";
        try {
     
            CookieManager manager = new CookieManager();
            manager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
            CookieHandler.setDefault(manager);
            HttpURLConnection httpURLConnection = (HttpURLConnection) (new URL(loginURL).openConnection());
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setRequestProperty(headerAgent, headerAgentArg);
            httpURLConnection.connect();
            if (httpURLConnection.getResponseCode() == 200) {
     
                InputStream inputStream = httpURLConnection.getInputStream();
                buffer = new byte[1024];
                while ((length = inputStream.read(buffer)) != -1) {
     
                    all=all+new String(buffer,0,length,"UTF-8");
                }
                httpURLConnection.disconnect();
                // 通过第一次打开的页面获取lt
                lt = all.split(")[1].split("\" />")[0];
                all = null;
                // 第二次获取网页,会自动携带Cookie信息
                httpURLConnection = (HttpURLConnection) (new URL(loginURL).openConnection());
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setRequestProperty(headerAgent, headerAgentArg);
                httpURLConnection.setDoOutput(true);
                httpURLConnection.connect();
                httpURLConnection.getOutputStream().write(form.replace("xxx", lt).getBytes("UTF-8"));
                httpURLConnection.getInputStream();
                httpURLConnection.disconnect();
            }
        } catch (Exception e) {
     
            e.printStackTrace();
        }

    }

    public String calculate(String year1, String month1) throws IOException {
     
        xiabanlist.clear();
        shangbanlist.clear();
        yueri.clear();
        if (month1.equals("01") || month1.equals("03") || month1.equals("05") || month1.equals("07") || month1.equals("08") || month1.equals("10") || month1.equals("12")) {
     
            bc_date = year1 + "-" + month1 + "-01";
            bc_date_end = year1 + "-" + month1 + "-31";
        } else if (month1.equals("04") || month1.equals("06") || month1.equals("09") || month1.equals("11")) {
     
            bc_date = year1 + "-" + month1 + "-01";
            bc_date_end = year1 + "-" + month1 + "-30";
        } else if (Integer.valueOf(year1) % 4 == 0 && Integer.valueOf(year1) % 100 != 0 || Integer.valueOf(year1) % 400 == 0) {
     
            bc_date = year1 + "-" + month1 + "-01";
            bc_date_end = year1 + "-" + month1 + "-29";
        } else {
     
            bc_date = year1 + "-" + month1 + "-01";
            bc_date_end = year1 + "-" + month1 + "-28";
        }


        String url = "获取加班json数据的接口?staff_id=&bc_date=" + bc_date + "&bc_date_end=" + bc_date_end;
        String dateJson = getJson(url);
        JSONObject json = JSONObject.parseObject(dateJson);
        JSONArray list = JSONArray.parseArray(json.get("jsonList").toString());
        String sblist = "";
        String xblist = "";
        for (int i = 0; i < list.size(); i++) {
     
            String times = list.get(i).toString();
            JSONObject times1 = JSONObject.parseObject(times);
            String status = times1.get("status").toString();
            String riqi = "";
            String xbtime = "";
            String sbtime = "";
            if (status == "1" && i == 0) {
     
                continue;
            }
            //如果当前节点是上班节点
            else if (status.equals("1")) {
     
                //如果当前日期的上班节点还没有查找成功
                if (xiabanlist.size() == shangbanlist.size() + 1) {
     
                    sblist = list.get(i).toString();
                    JSONObject sb = JSONObject.parseObject(sblist);
                    sbtime = sb.get("kq_time").toString();
                    //如果今天上班未打卡,忽略这一天的打卡记录
                    if (!sbtime.split(" ")[0].equals(yueri.get(yueri.size() - 1))) {
     
                        yueri.remove(yueri.size() - 1);
                        xiabanlist.remove(xiabanlist.size() - 1);
                        continue;
                    }
                    //如果当前节点拥有下个节点
                    if (i + 1 < list.size()) {
     
                        String next = list.get(i + 1).toString();
                        JSONObject ne = JSONObject.parseObject(next);
                        //如果下个节点是下班节点
                        if (!ne.get("status").toString().equals("1")) {
     
                            //判断当前节点是否脱岗打卡
                            if (sb.get("addr_status") == null || !sb.get("addr_status").toString().equals("1")) {
     
                                System.out.println(sbtime + "上班");
                                shangbanlist.add(sbtime);
                            }
                        }
                        //如果下个节点是脱岗打卡
                        else if (ne.get("addr_status").toString().equals("1")) {
     
                            System.out.println(sbtime + "上班");
                            shangbanlist.add(sbtime);
                        }
                    }
                    //当前节点为最后一个节点
                    else {
     
                        System.out.println(sbtime + "上班");
                        shangbanlist.add(sbtime);
                    }
                }
            }
            //如果当前节点是下班节点
            else if (status.equals("5")) {
     
                if (xiabanlist.size() + 1 == shangbanlist.size() + 1) {
     
                    xblist = list.get(i).toString();
                    JSONObject xb = JSONObject.parseObject(xblist);
                    xbtime = xb.get("kq_time").toString();
                    riqi = xb.get("bc_date").toString();
                    if (xb.get("addr_status") == null || !xb.get("addr_status").toString().equals("1")) {
     
                        System.out.println(xbtime + "下班");
                        xiabanlist.add(xbtime);
                        yueri.add(riqi);
                    }
                }
            }
            //如果当前节点是周六/周日的打卡记录节点
            else if (status.equals("6")) {
     
                if (xiabanlist.size() == shangbanlist.size() + 1) {
     
                    //当前节点拥有下一个节点
                    sblist = list.get(i).toString();
                    JSONObject sb = JSONObject.parseObject(sblist);
                    sbtime = sb.get("kq_time").toString();
                    //如果今天上班未打卡,忽略这一天的打卡记录
                    if (!sbtime.split(" ")[0].equals(yueri.get(yueri.size() - 1))) {
     
                        yueri.remove(yueri.size() - 1);
                        xiabanlist.remove(xiabanlist.size() - 1);
                        continue;
                    }
                    if (i + 1 < list.size()) {
     
                        String next = list.get(i + 1).toString();
                        JSONObject ne = JSONObject.parseObject(next);
                        //如果下一个节点为脱岗打卡
                        if (ne.get("address_status") != null && ne.get("addr_status").toString().equals("1")) {
     
                            System.out.println(sbtime + "上班");
                            shangbanlist.add(sbtime);
                        } else {
     
                            String now = sb.get("kq_time").toString().split(" ")[0];
                            String n = ne.get("kq_time").toString().split(" ")[0];
                            //如果下个节点不是当前日期的节点
                            if (!n.equals(now)) {
     
                                sbtime = sb.get("kq_time").toString();
                                System.out.println(sbtime + "上班");
                                shangbanlist.add(sbtime);
                            }
                        }
                    } else {
     
                        System.out.println(sbtime + "上班");
                        shangbanlist.add(sbtime);
                    }
                }
                //如果当前节点为第一个下班节点
                else if (xiabanlist.size() + 1 == shangbanlist.size() + 1) {
     
                    xblist = list.get(i).toString();
                    JSONObject xb = JSONObject.parseObject(xblist);
                    xbtime = xb.get("kq_time").toString();
                    riqi = xb.get("bc_date").toString();
                    //如果当前节点为非脱岗节点
                    if (xb.get("addr_status") == null || !xb.get("addr_status").toString().equals("1")) {
     
                        System.out.println(xbtime + "下班");
                        xiabanlist.add(xbtime);
                        yueri.add(riqi);
                        index.add(yueri.size() - 1);

                    }
                }
            }
        }
        int index2 = 0;
        for (int i = 0; i < yueri.size(); i++) {
     
            Integer hour = Integer.valueOf(shangbanlist.get(i).split(" ")[1].split(":")[0]);
            Integer minute = Integer.valueOf(shangbanlist.get(i).split(" ")[1].split(":")[1]);
            Integer second = Integer.valueOf(shangbanlist.get(i).split(" ")[1].split(":")[2]);
            Integer hour1 = Integer.valueOf(xiabanlist.get(i).split(" ")[1].split(":")[0]);
            Integer minute1 = Integer.valueOf(xiabanlist.get(i).split(" ")[1].split(":")[1]);
            Integer second1 = Integer.valueOf(xiabanlist.get(i).split(" ")[1].split(":")[2]);
            //将所有的秒加起来
            Integer sum1 = hour1 * 60 * 60 + minute1 * 60 + second1 - hour * 60 * 60 - minute * 60 - second - 9 * 60 * 60 - 30 * 60;
            //如果当前节点为周六/周日节点
            if (index2 < index.size() && i == index.get(index2)) {
     
                sum = sum + hour1 * 60 * 60 + minute1 * 60 + second1 - hour * 60 * 60 - minute * 60 - second;
                index2++;
            } else if (sum1 >= 30 * 60) {
     
                sum = sum + sum1;
            }
        }
        //计算小时/分钟/秒
        int xiaoshi = sum / 3600;
        int fenzhong = sum % 3600 / 60;
        int miao = sum % 3600 % 60;

        System.out.println("您的加班时间为: " + xiaoshi + "小时" + fenzhong + "分钟" + miao + "秒");

        return "您在" + year1 + "年" + month1 + "月" + "的加班时间为: " + xiaoshi + "小时" + fenzhong + "分钟" + miao + "秒";
    }

    private String getJson(String url) throws IOException {
     
        Document doc = Jsoup.connect(url)
                .ignoreContentType(true)
                .get();
        return doc.select("body").text();
    }

    public static void main(String[] args) throws IOException {
     
        Scanner in = new Scanner(System.in);
        System.out.println("请输入您的用户名:");
        String username = in.nextLine();
        System.out.println("请输入您的密码:");
        String password = in.nextLine();
        System.out.println("请输入要查询的年份:");
        String year = in.nextLine();
        System.out.println("请输入要查询的月份:");
        String month = in.nextLine();
        new CalculateTime().login(username,password);
        new CalculateTime().calculate(year, month);
        System.out.println("按任意键退出。。。");
        in.nextLine();
        System.exit(0);
    }
}

你可能感兴趣的:(java计算公司加班时间)