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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.kingdee.eas.custom.beisen.utils.BeiSenUtils;
import com.kingdee.shr.base.syssetting.MSFServiceFacadeFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import javax.ejb.*;
import java.rmi.RemoteException;
import com.kingdee.bos.*;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.bos.metadata.rule.RuleExecutor;
import com.kingdee.bos.metadata.MetaDataPK;
//import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.framework.ejb.AbstractEntityControllerBean;
import com.kingdee.bos.framework.ejb.AbstractBizControllerBean;
//import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.service.ServiceContext;
import com.kingdee.bos.service.IServiceContext;

import java.lang.String;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;

public class SynchronousPosFacadeControllerBean extends AbstractSynchronousPosFacadeControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.custom.beisen.synchronouspos.SynchronousPosFacadeControllerBean");

    @Override
    protected String _syncPosToBeiSen(Context ctx, String posId, String syncStartTimeStr, String syncEndTimeStr) throws BOSException {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        super._syncPosToBeiSen(ctx, posId, syncStartTimeStr, syncEndTimeStr);
        Object obj=null;
        try {
            Map<String, Object> map = Maps.newHashMap();
            map.put("posId",posId);
            if(!StringUtils.isNotBlank(posId)) {
                if (StringUtils.isNotBlank(syncStartTimeStr)) {
                    map.put("syncStartTimeStr", syncStartTimeStr);
                } else if (StringUtils.isNotBlank(syncEndTimeStr)) {
                    map.put("syncEndTimeStr", syncEndTimeStr);
                } else {
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.DAY_OF_MONTH, 1);
                    syncEndTimeStr = sf.format(calendar.getTime());
                    calendar.add(Calendar.DAY_OF_MONTH, -2);
                    syncStartTimeStr = sf.format(calendar.getTime());
                    map.put("syncStartTimeStr", syncStartTimeStr);
                    map.put("syncEndTimeStr", syncEndTimeStr);
                }
            }
            obj = MSFServiceFacadeFactory.getLocalInstance(ctx).processService("synchronousPosOSF", map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return (null==obj?"":obj.toString());
    }



    @Override
    protected String _sendAddNewPosToBeisen(Context ctx, String jsonStr) throws BOSException {
        super._sendAddNewPosToBeisen(ctx, jsonStr);
        Object obj=null;
        try {

            JSONObject jsonObject = JSON.parseObject(jsonStr);
            Map<String, Object> map = Maps.newHashMap();
            map.putAll(jsonObject);
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            String syncStartTimeStr= (String) map.get("syncStartTimeStr");
            String syncEndTimeStr= (String) map.get("syncEndTimeStr");
            if(StringUtils.isNotBlank(syncStartTimeStr)) {
                map.put("syncStartTimeStr", syncStartTimeStr);
            }else if(StringUtils.isNotBlank(syncEndTimeStr)){
                map.put("syncEndTimeStr", syncEndTimeStr);
            }else {
                Calendar calendar = Calendar.getInstance();
                syncEndTimeStr=sf.format(calendar.getTime());
                calendar.add(Calendar.DAY_OF_MONTH,-1);
                syncStartTimeStr= sf.format(calendar.getTime());
                map.put("syncStartTimeStr", syncStartTimeStr);
                map.put("syncEndTimeStr", syncEndTimeStr);
            }
            obj = MSFServiceFacadeFactory.getLocalInstance(ctx).processService("synchronousPosOSF", map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return (null==obj?"":obj.toString());
    }

    @Override
    protected void _sendEditPosToBeisen(Context ctx, String jsonStr) throws BOSException {
        super._sendEditPosToBeisen(ctx, jsonStr);
        Object obj=null;
        try {
            JSONObject jsonObject = JSON.parseObject(jsonStr);
            Map<String, Object> map = Maps.newHashMap();
            map.putAll(jsonObject);
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            String syncStartTimeStr= (String) map.get("syncStartTimeStr");
            String syncEndTimeStr= (String) map.get("syncEndTimeStr");
            if(StringUtils.isNotBlank(syncStartTimeStr)) {
                map.put("syncStartTimeStr", syncStartTimeStr);
            }else if(StringUtils.isNotBlank(syncEndTimeStr)){
                map.put("syncEndTimeStr", syncEndTimeStr);
            }else {
                Calendar calendar = Calendar.getInstance();
                syncEndTimeStr=sf.format(calendar.getTime());
                calendar.add(Calendar.DAY_OF_MONTH,-1);
                syncStartTimeStr= sf.format(calendar.getTime());
                map.put("syncStartTimeStr", syncStartTimeStr);
                map.put("syncEndTimeStr", syncEndTimeStr);
            }
            obj = MSFServiceFacadeFactory.getLocalInstance(ctx).processService("synchronousPosOSF", map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return;
    }
}