package com.kingdee.eas.custom.beisen.synchronouspos.handler;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.rabbitmq.guava.Lists;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.beisen.synchronouspos.osf.SyncOrgStaffManageRunnable;
import com.kingdee.eas.custom.beisen.synchronouspos.osf.SyncPosStaffManageRunnable;
import com.kingdee.shr.base.syssetting.IMSFServiceFacade;
import com.kingdee.shr.base.syssetting.MSFServiceFacadeFactory;
import com.kingdee.shr.base.syssetting.api.bean.BatchMessageTipsBody;
import com.kingdee.shr.base.syssetting.api.bean.BatchMessageTipsHeader;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.staffcontrol.web.handler.StaffManageProjectListHandler;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class StaffManageProjectListHandlerEx extends StaffManageProjectListHandler {
    private static Logger log = Logger.getLogger(StaffManageProjectListHandlerEx.class);

    public void batchDisableAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
        ExecutorCountUtil examples = ExecutorCountUtil.getExamples();
        throw new ShrWebBizException("执行中任务数："+Math.min(examples.get(),4)+"，等待中任务数："+Math.max(examples.get()-4,0)+"。");
    }

    public void batchEnableAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
        ExecutorCountUtil examples = ExecutorCountUtil.getExamples();
        Context ctx = SHRContext.getInstance().getContext(); // 重新获取上下文（如果需要）
        System.out.println("batchEnableAction");
        if (examples.get() <= 0) {
            try {
                IMSFServiceFacade msp = MSFServiceFacadeFactory.getLocalInstance(ctx);
                ExecutorService executor = Executors.newFixedThreadPool(4);

                Map<String, Object> map = Maps.newHashMap();
                map.put("mack", "org");
                Map<String, Object> result = (Map<String, Object>) msp.processService("syncStaffManageOSF", map);
                System.out.println("result:"+ JSON.toJSONString(result));

                List<Map<String, Object>> orgListMap = (List<Map<String, Object>>) result.get("orgListMap");
                List<Map<String, Object>> posListMap = (List<Map<String, Object>>) result.get("posListMap");
                int orgLen = orgListMap.size();
                int batchSize = 50;
                for (int i = 0; i < orgLen; i+=batchSize) {
                    examples.countPlusPlus();
                    int end = Math.min(i + batchSize, orgLen);
                    List<Map<String, Object>> orgSubListMap = orgListMap.subList(i,end);
                    SyncOrgStaffManageRunnable syncOrg = new SyncOrgStaffManageRunnable(ctx, orgSubListMap, examples);
                    executor.submit(syncOrg);
                }
                System.out.println("examples:"+ examples.get());

                int posLen = posListMap.size();
                for (int i = 0; i < posLen; i+=batchSize) {
                    examples.countPlusPlus();
                    int end = Math.min(i + batchSize, posLen);
                    List<Map<String, Object>> posSubListMap = posListMap.subList(i, end);
                    SyncPosStaffManageRunnable syncOrg = new SyncPosStaffManageRunnable(ctx, posSubListMap, examples);
                    executor.submit(syncOrg);
                }
                System.out.println("examples:"+ examples.get());
                executor.shutdown();

            } catch (BOSException | EASBizException e) {
                e.printStackTrace();
            }
            throw new ShrWebBizException("触发成功，已在后台执行中，请等待");
        } else {
            throw new ShrWebBizException("已触发，已在后台执行中，请等待");
        }
    }

    public static void main(String[] args) {
        ExecutorCountUtil examples = ExecutorCountUtil.getExamples();
        examples.countPlusPlus();
        System.out.println(examples.get());
        examples.countLessLess();
        System.out.println(examples.get());
    }

}
