package com.kingdee.eas.custom.synctask.handler;

import com.kingdee.bos.Context;
import com.kingdee.eas.basedata.org.*;
import com.kingdee.eas.custom.synctask.ActionTypeEnum;
import com.kingdee.eas.custom.synctask.ISyncTranForIOTFacade;
import com.kingdee.eas.custom.synctask.SyncTranForIOTFacadeFactory;
import com.kingdee.eas.custom.synctask.SyncTranForOAFacadeFactory;
import com.kingdee.eas.hr.ats.AtsUtil;
import com.kingdee.eas.hr.org.web.handler.PositionListHandler;
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 org.apache.commons.lang3.StringUtils;
import org.springframework.ui.ModelMap;

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

import java.util.*;


/**
 * 职位日常维护list  OA同步
 *
 * @date 2024/10/23
 * @apiNote
 */
public class PositionListHandlerEx extends PositionListHandler {
    public void syncOAPositionAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
        Context ctx = SHRContext.getInstance().getContext();
        String billIds = request.getParameter("billIds");
        logger.error("syncOAPositionAction---billIds----" + billIds);
        try {
            Set<String> positionIds = null;
            if (StringUtils.isEmpty(billIds)) {
                IPosition iPosition = PositionFactory.getLocalInstance(ctx);
                PositionCollection positionCollection = iPosition.getPositionCollection("select id ,name,number");
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < positionCollection.size(); i++) {
                    String positionId = positionCollection.get(i).getId().toString();
                    sb.append(positionId).append(",");
                }
                sb.deleteCharAt(sb.lastIndexOf(","));
                billIds = sb.toString();
            }
            positionIds = AtsUtil.toSet(billIds);
            //部署测试环境需要注释此行
            ISyncTranForIOTFacade iSyncTranForIOTFacade = SyncTranForIOTFacadeFactory.getLocalInstance(ctx);
            Map<String, List<Map<String, String>>> result = iSyncTranForIOTFacade.syncOrgUnitToIot(billIds);
            logger.error("result---" + result);
            List<Map<String, String>> list = result.get("data");
            int success = positionIds.size();
            int failure = 0;
            BatchMessageTipsHeader batchMessageTipsHeader = new BatchMessageTipsHeader();
            if (list.size() > 0) {
                failure = list.size();
                for (int i = 0; i < list.size(); i++) {
                    BatchMessageTipsBody body = new BatchMessageTipsBody();
                    Map<String, String> map = list.get(i);
                    body.setId(map.get("id"));
                    body.setMuitTipsState(Boolean.FALSE);
                    body.setMuitTipsMessage(map.get("msg"));
                    batchMessageTipsHeader.addResult(body);
                }
                success -= list.size();
            }
            logger.error("handleEnableWithTips----- >> ");
            batchMessageTipsHeader.setBillId(billIds);
            batchMessageTipsHeader.setFailureCount(failure);
            batchMessageTipsHeader.setSuccessCount(success);
            logger.error("batchMessageTipsHeader  " + batchMessageTipsHeader.getSuccessCount());
            request.setAttribute("res_method_data", batchMessageTipsHeader);
            this.writeSuccessData(batchMessageTipsHeader);
            SyncTranForOAFacadeFactory.getLocalInstance(ctx).syncPositionToOA(billIds, ActionTypeEnum.ADD);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ShrWebBizException(e);
        }
    }
}
