package com.kingdee.eas.custom.webbeisen.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.bsf.service.app.IHRMsfService;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.webbeisen.utils.BeiSenUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class GetBeiSenDataService implements IHRMsfService {
    @Override
    public Object process(Context context, Map map) throws EASBizException, BOSException {
        System.out.println("=========com.kingdee.eas.custom.webbeisen.service.GetBeiSenDataService=======");
        String param = map.get("param") == null ? "" : map.get("param").toString();
        JSONObject paramJSONObject = JSONObject.parseObject(param);
        JSONObject requestBody = new JSONObject();
        Integer staffStatus = paramJSONObject.getInteger("staffStatus");
        Integer infoCollectionStatus = paramJSONObject.getInteger("infoCollectionStatus");
        String isModify = paramJSONObject.getString("isModify");
        String endTime = paramJSONObject.getString("endTime");
        String startTime = paramJSONObject.getString("startTime");
        String batchId = paramJSONObject.getString("batchId");
        Integer day = paramJSONObject.getInteger("day");
        String staffId = paramJSONObject.getString("staffId");
        if (staffId!=null&&!staffId.equals("")){

        }else{
            if (staffStatus!=null){
                requestBody.put("staffStatus",staffStatus);
            }

            if (infoCollectionStatus!=null){
                requestBody.put("infoCollectionStatus",infoCollectionStatus);
            }

            if (isModify!=null){
                requestBody.put("isModify",isModify);
            }

            if (endTime!=null){
                requestBody.put("endTime",endTime);
            }
            if (startTime!=null){
                requestBody.put("startTime",startTime);
            }

            if (batchId!=null){
                requestBody.put("batchId",batchId);
            }

            if (startTime==null||startTime.equals("")){
                if(day==null||day.equals("")){
                    throw new RuntimeException("参数异常endTime为空必填 或者填入day");
                }else{
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                    Date date = new Date();
                    if (endTime==null||endTime.equals("")){
                        requestBody.put("endTime",format.format(date));
                        requestBody.put("startTime",getDayBefore(date, day));
                    }else {
                        requestBody.put("endTime",endTime);
                        try {
                            requestBody.put("startTime",getDayBefore(format.parse(endTime), day));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                    }

                }

            }
        }
        System.out.println("staffStatus："+staffStatus +
                "infoCollectionStatus："+infoCollectionStatus +
                "isModify："+isModify +
                "endTime："+endTime +
                "startTime："+startTime +
                "batchId："+batchId +
                "day："+day +
                "staffId："+staffId);

        BeiSenUtils beiSenUtils = new BeiSenUtils(context);
        System.out.println("=========requestBody："+requestBody);
        try {
            JSONArray staffIds = new JSONArray();
            if (staffId!=null&&!staffId.equals("")){
                String[] split = staffId.split(",");
                for (int i = 0; i < split.length; i++) {
                    staffIds.add(split[i]);
                }
            }else {
                staffIds  = beiSenUtils.getStaffIds(requestBody);
            }
            System.out.println("staffId："+staffIds);

            List<String> list = new ArrayList<String>();
            list.add("extendInfos");
            list.add("fileInfos");
            list.add("fields");
            JSONArray staffInfos = beiSenUtils.getStaffInfos(staffIds, list);
            System.out.println("staffInfos："+staffInfos);
            return staffInfos;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 获取指定日期前 x 天的日期字符串
     *
     * @param specifiedDay 指定的日期
     * @param x            天数
     * @return 指定日期前 x 天的日期字符串，格式为 yyyy-MM-dd
     */
    public String getDayBefore(Date specifiedDay, int x) {
        // 获取 Calendar 实例
        Calendar c = Calendar.getInstance();
        // 将 Calendar 的时间设置为指定日期
        c.setTime(specifiedDay);
        // 获取当前日期
        int day = c.get(Calendar.DATE);
        // 将日期向前移动 x 天
        c.set(Calendar.DATE, day - x);

        // 将移动后的日期格式化为 yyyy-MM-dd 格式的字符串
        String dayBefore = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(c.getTime());
        // 返回格式化后的日期字符串
        return dayBefore;
    }
}
