package nc.impl.tbb;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;

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

import com.alibaba.fastjson.JSONArray;

import nc.bs.framework.adaptor.IHttpServletAdaptor;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.framework.server.ISecurityTokenCallback;
import nc.bs.servlet.service.BaseServlet;
import nc.itf.tbb.IBudgetService;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.BeanProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.log.NcLog;
import nc.ms.tb.control.CtlSchemeCTL;
import nc.ms.tb.rule.CtlSchemeServiceGetter;
import nc.vo.tb.task.MdTask;
import net.sf.json.JSONObject;

public class ResetBudgetImpl extends BaseServlet implements IHttpServletAdaptor{

	@Override
	public void doAction(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			IUAPQueryBS iuap = (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
			NCLocator.getInstance().lookup(ISecurityTokenCallback.class).token("NCSystem".getBytes(),"pfxx".getBytes());
			InvocationInfoProxy.getInstance().setGroupId("0001A110000000000LO7");
			String createStr = buildJson(req, resp, this.getClass().getName());
			NcLog.info("预算重启开始："+ createStr);
			JSONObject json = JSONObject.fromObject(createStr);
			
			checkJson(json);
			String code_entity = json.getString("code_entity");//责任主体
			String pk_year = json.getString("pk_year");//会计年度
			String entityPk = QueryOrgPk(code_entity);//责任主体pk
			
			//调用预算重启事务
			IBudgetService service = NCLocator.getInstance().lookup(IBudgetService.class);
			
			service.reset_RequiresNew(entityPk, pk_year);
			resp.getWriter().write(formatRSJsonData("成功", "", null).toString());
			NcLog.info("预算重启结束");
		} catch (Exception e) {
			resp.getWriter().write(formatRSJsonData("失败",e.getMessage() == null ? "" : e.getMessage(),null).toString());
			StringWriter stringWriter = new StringWriter();
			e.printStackTrace(new PrintWriter(stringWriter));
			//获取详细信息
			String msg = stringWriter.getBuffer().toString();
			NcLog.info("预算重启失败:"+ msg);
		}
	}
	
	
	private void checkJson(JSONObject json) throws Exception {
		
		StringBuffer mags = new StringBuffer();
		Boolean empty = true;
		
		String code_entity = "code_entity";
		String pk_year = "pk_year";
		
		String[] HeadKey = {code_entity,pk_year};
		
		for (String as : HeadKey) {

			if (json.getString(as).isEmpty()) {
				empty = false;
				mags.append("'" + as + "'");
			}
		}

		if (!empty) {
			throw new Exception("以下字段不可为空："+mags);
		}
	}
	
	/**
	 * 查询责任主体pk
	 * @param code_entity
	 * @return
	 * @throws Exception
	 */
	public String QueryOrgPk(String code_entity) throws Exception{
		IUAPQueryBS iuap = (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
		String qrysql = "select pk_org from org_orgs where code = '"+code_entity+"' and nvl(dr,0) = 0";
		String pk_org = (String) iuap.executeQuery(qrysql, new ColumnProcessor());
		if (pk_org == null) {
			throw new Exception("获取责任主体信息失败,未找到与参数" + code_entity + "有关的数据！");
		}
		return pk_org;
	}

}