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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.basedata.org.AdminOrgUnitFactory;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.beisen.synchronousorg.SynchronousEmpBillFactory;
import com.kingdee.eas.custom.beisen.synchronousorg.synchronousOrgFactory;
import com.kingdee.eas.hr.org.web.handler.OrgUnitListHandler;
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.web.json.JSONUtils;
import org.springframework.ui.ModelMap;

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

public class OrgUnitListHandlerEx extends OrgUnitListHandler {
    public void synchronousOrgAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException, BOSException {
        Context ctx = SHRContext.getInstance().getContext();
        BatchMessageTipsHeader batchMessageTipsHeader = new BatchMessageTipsHeader();
        String ids = request.getParameter("id");
        int sucess = 0;
        int failure = 0;
        if (ids != null && ids.length() > 0) {
            String[] orgArray = ids.split(",");
            for (int i = 0; i < orgArray.length; i++) {
                BatchMessageTipsBody body = new BatchMessageTipsBody();
                try {
                    AdminOrgUnitInfo adminOrgUnitInfo = AdminOrgUnitFactory.getLocalInstance(ctx).getAdminOrgUnitInfo(new ObjectUuidPK(orgArray[i]));
                    try {
                        synchronousOrgFactory.getLocalInstance(ctx).createOrUpdateOrg(10, adminOrgUnitInfo.getId().toString(), false);
                        body.setId(orgArray[i]);
                        body.setMuitTipsState(true);
                        body.setMuitTipsMessage("同步成功【" + orgArray[i] + "】");
                        batchMessageTipsHeader.addResult(body);
                        sucess++;
                    } catch (Exception e) {
                        body.setId(orgArray[i]);
                        body.setMuitTipsState(false);
                        body.setMuitTipsMessage("后台事务执行失败【" + orgArray[i] + "】");
                        batchMessageTipsHeader.addResult(body);
                        e.printStackTrace();
                        failure++;
                    }

                } catch (BOSException e) {
                    body.setId(orgArray[i]);
                    body.setMuitTipsState(false);
                    body.setMuitTipsMessage("同步失败组织员工失败【" + orgArray[i] + "】");
                    batchMessageTipsHeader.addResult(body);
                    e.printStackTrace();
                } catch (EASBizException e) {
                    body.setId(orgArray[i]);
                    body.setMuitTipsState(false);
                    body.setMuitTipsMessage("同步失败组织员工失败【" + orgArray[i] + "】");
                    batchMessageTipsHeader.addResult(body);
                    e.printStackTrace();
                }
            }
        }
        batchMessageTipsHeader.setBillId(ids);
        batchMessageTipsHeader.setFailureCount(failure);
        batchMessageTipsHeader.setSuccessCount(sucess);
        JSONUtils.SUCCESS(batchMessageTipsHeader);

    }
}
