package nccloud.web.arap.paybill.action;

import nccloud.web.arap.arappub.action.CardBodyAfterEditPubAction;
import nccloud.web.arap.arappub.info.CardBodyEventInfo;
import nccloud.web.arap.paybill.handler.PayBillCardBodyEditHandlerList;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import nc.itf.gl.api.IResourceService;
import nc.md.model.MetaDataException;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.vo.arap.basebill.BaseAggVO;
import nc.vo.arap.pay.PayBillItemVO;
import nc.vo.arap.pay.PayBillVO;
import nc.vo.bd.supplier.SupplierVO;
import nc.vo.gl.openapi.GLQueryWebVO;
import nc.vo.org.AccountingBookVO;
import nc.vo.pub.lang.UFDate;
import nccloud.api.rest.utils.NCCRestUtils;
import nccloud.framework.service.ServiceLocator;

public class CardBodyAfterEditAction extends CardBodyAfterEditPubAction {
	/**
	 * 220201/224103/224105/224101科目的期末余额
	 */
	private String endlocamount220201 = "";
	private String endlocamount224103 = "";
	private String endlocamount224105 = "";
	private String endlocamount224101 = "";
	IMDPersistenceQueryService imDPersistenceService = ServiceLocator.find(IMDPersistenceQueryService.class);

	public void afterEvent(CardBodyEventInfo eventInfo) {
		PayBillCardBodyEditHandlerList handlerList = new PayBillCardBodyEditHandlerList();
		eventInfo = changeCard(eventInfo);
		handlerList.fireToHandler(eventInfo);
	}

	public CardBodyEventInfo changeCard(CardBodyEventInfo eventInfo) {
		BaseAggVO aggvo = eventInfo.getAggvo();
		String key = eventInfo.getEvent().getAttrcode();

		if (key.equals("supplier")) {
			PayBillItemVO[] payvoArry = (PayBillItemVO[]) aggvo.getChildrenVO();
			PayBillVO billvo = (PayBillVO) aggvo.getParent();
//			财务组织编码
			String pk_relorg = billvo.getPk_fiorg();
//			核算账薄编码
			String accbookCode = getAccbookCode(pk_relorg);
//			单据号
			String billno = billvo.getBillno();
//			单据日期
			UFDate billdate = billvo.getBilldate();
//			当前时间
			Long serverDate = System.currentTimeMillis();
//			如果有单据号则为修改,修改时单据日期选择现在
			if (billno != null) {
				billdate = new UFDate(serverDate);
			}
			for (int i = 0; i < payvoArry.length; i++) {
				PayBillItemVO payvo = payvoArry[i];
				String supplier = payvo.getSupplier();
//						如果供应商不是空或者null
				if (!"".equals(supplier) && null != supplier) {
					SupplierVO suppliervo = getSupplierVO(supplier);
					{
						if (suppliervo != null) {
//							供应商编码
							String code = suppliervo.getCode();
//							供应商名称
							String name = suppliervo.getName();
							getAccountingSubject(accbookCode, billdate, code, name);
							payvoArry[i].setDef30(endlocamount220201);
							payvoArry[i].setDef31(endlocamount224103);
							payvoArry[i].setDef32(endlocamount224105);
							payvoArry[i].setDef33(endlocamount224101);
						}
					}
				} else {
					payvoArry[i].setDef30(null);
					payvoArry[i].setDef31(null);
					payvoArry[i].setDef32(null);
					payvoArry[i].setDef33(null);
				}
			}
			aggvo.setChildrenVO(payvoArry);
			eventInfo.setAggvo(aggvo);
		}
		return eventInfo;

	}

	/**
	 * nccloud/api/gl/accountrep/assbalance 查询辅助余额接口.查220201/224103/224105的科目的期末余额
	 * 
	 * @param accbookCode    辅助核算编码
	 * @param billdate       单据日期
	 * @param checkvaluecode 供应商编码
	 * @param checkvaluename 供应商名称
	 */
	public void getAccountingSubject(String acccode, UFDate date, String code, String name) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String strdate = formatter.format(date.toDate());

		endlocamount220201 = getendlocamountBy(queryAssBalance(getRequest(acccode, strdate, code, name)));
		endlocamount224103 = getendlocamountBy(queryAssBalance(getRequest1(acccode, strdate, code, name)));
		endlocamount224105 = getendlocamountBy(queryAssBalance(getRequest2(acccode, strdate, code, name)));
		endlocamount224101 = getendlocamountBy(queryAssBalance(getRequest3(acccode, strdate, code, name)));
	}

	public List<Map<String, Object>> queryAssBalance(String json) {
		NCCRestUtils.initInvocationInfo();
		GLQueryWebVO qryWebVO = (GLQueryWebVO) new Gson().fromJson(json, GLQueryWebVO.class);
		List<Map<String, Object>> data = null;
		try {
			data = ServiceLocator.find(IResourceService.class).queryAssBalanceByVO(qryWebVO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (data == null) {
			return null;
		}
		return data;
	}

	/**
	 * 解析json获取期末余额
	 * 
	 * @param json
	 * @return 期末余额 endlocamount
	 */
	public String getendlocamountBy(List<Map<String, Object>> data) {
		if (data == null) {
			return null;
		}
		String str2 = (String) data.get(0).get("endlocamount");
		String str3 = (String) data.get(0).get("endorint");
		if (str2 == null || "".equals(str2)) {
			str2 = "0.0";
		} else {
			if ("借".equals(str3)) {
				str2 = "-" + str2;
			}
		}
		return str2;
	}

	/**
	 * 
	 * @return 获取核算账薄code
	 */
	public String getAccbookCode(String pk_relorg) {
		List<AccountingBookVO> bills = null;
		String sql = "pk_relorg = '" + pk_relorg + "' and dr = 0 ";
		String accbookCode = "";
		try {
			bills = (List<AccountingBookVO>) imDPersistenceService.queryBillOfVOByCond(AccountingBookVO.class, sql,
					false);
			if (bills.size() != 0) {
				accbookCode = bills.get(0).getCode();
			}
		} catch (MetaDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return accbookCode;

	}

	public SupplierVO getSupplierVO(String pk_supplier) {
		List<SupplierVO> supplier = null;
		SupplierVO suppliervo = null;
		String sql = "pk_supplier = '" + pk_supplier + "' and dr = 0 ";
		try {
			supplier = (List<SupplierVO>) imDPersistenceService.queryBillOfVOByCond(SupplierVO.class, sql, false);
			if (supplier.size() != 0) {
				suppliervo = supplier.get(0);
			}
		} catch (MetaDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return suppliervo;
	}

	private static String getRequest(String accbookCode, String strdate, String code, String name) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> accbookcodeList = new ArrayList<String>();
		accbookcodeList.add(accbookCode);
		map.put("accbookCode", accbookcodeList);
		map.put("queryByPeriod", "false");
		map.put("inclUntallyed", "true");
		map.put("inclTransfer", "true");
		map.put("inclError", "true");
		map.put("inclReclassify", "true");
		map.put("date", strdate);
		map.put("endDate", strdate);
		map.put("currtypeCode", "本币");

		List<Map<String, Object>> queryObjList = new ArrayList<Map<String, Object>>();
		Map<String, Object> quyMap = new HashMap<String, Object>();
		Map<String, Object> typeMap = new HashMap<String, Object>();

		typeMap.put("checktypecode", "会计科目");
		typeMap.put("checktypename", "会计科目");
		quyMap.put("checktype", typeMap);
		List<Map<String, String>> valueList = new ArrayList<Map<String, String>>();
		Map<String, String> valueMap = new HashMap<String, String>();

		valueMap.put("checkvaluecode", "220201");
		valueMap.put("checkvaluename", "应付款");
		valueList.add(valueMap);
		quyMap.put("checkvalue", valueList);
		queryObjList.add(quyMap);
		//
		Map<String, Object> quyMap1 = new HashMap<String, Object>();
		Map<String, Object> typeMap1 = new HashMap<String, Object>();

		typeMap1.put("checktypecode", "0019");
		typeMap1.put("checktypename", "供应商档案");
		quyMap1.put("checktype", typeMap1);
		List<Map<String, String>> valueList1 = new ArrayList<Map<String, String>>();
		Map<String, String> valueMap1 = new HashMap<String, String>();

		valueMap1.put("checkvaluecode", code);
		valueMap1.put("checkvaluename", name);
		valueList1.add(valueMap1);
		quyMap1.put("checkvalue", valueList1);
		queryObjList.add(quyMap1);
		map.put("queryObj", queryObjList);
		return new Gson().toJson(map);
	}

	private static String getRequest1(String accbookCode, String strdate, String code, String name) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> accbookcodeList = new ArrayList<String>();
		accbookcodeList.add(accbookCode);
		map.put("accbookCode", accbookcodeList);
		map.put("queryByPeriod", "false");
		map.put("inclUntallyed", "true");
		map.put("inclTransfer", "true");
		map.put("inclError", "true");
		map.put("inclReclassify", "true");
		map.put("date", strdate);
		map.put("endDate", strdate);
		map.put("currtypeCode", "本币");

		List<Map<String, Object>> queryObjList = new ArrayList<Map<String, Object>>();
		Map<String, Object> quyMap = new HashMap<String, Object>();
		Map<String, Object> typeMap = new HashMap<String, Object>();

		typeMap.put("checktypecode", "会计科目");
		typeMap.put("checktypename", "会计科目");
		quyMap.put("checktype", typeMap);
		List<Map<String, String>> valueList = new ArrayList<Map<String, String>>();
		Map<String, String> valueMap = new HashMap<String, String>();

		valueMap.put("checkvaluecode", "224103");
		valueMap.put("checkvaluename", "单位");
		valueList.add(valueMap);
		quyMap.put("checkvalue", valueList);
		queryObjList.add(quyMap);
		//
		Map<String, Object> quyMap1 = new HashMap<String, Object>();
		Map<String, Object> typeMap1 = new HashMap<String, Object>();

		typeMap1.put("checktypecode", "0019");
		typeMap1.put("checktypename", "供应商档案");
		quyMap1.put("checktype", typeMap1);
		List<Map<String, String>> valueList1 = new ArrayList<Map<String, String>>();
		Map<String, String> valueMap1 = new HashMap<String, String>();

		valueMap1.put("checkvaluecode", code);
		valueMap1.put("checkvaluename", name);
		valueList1.add(valueMap1);
		quyMap1.put("checkvalue", valueList1);
		queryObjList.add(quyMap1);
		map.put("queryObj", queryObjList);
		return new Gson().toJson(map);
	}

	private static String getRequest2(String accbookCode, String strdate, String code, String name) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> accbookcodeList = new ArrayList<String>();
		accbookcodeList.add(accbookCode);
		map.put("accbookCode", accbookcodeList);
		map.put("queryByPeriod", "false");
		map.put("inclUntallyed", "true");
		map.put("inclTransfer", "true");
		map.put("inclError", "true");
		map.put("inclReclassify", "true");
		map.put("date", strdate);
		map.put("endDate", strdate);
		map.put("currtypeCode", "本币");

		List<Map<String, Object>> queryObjList = new ArrayList<Map<String, Object>>();
		Map<String, Object> quyMap = new HashMap<String, Object>();
		Map<String, Object> typeMap = new HashMap<String, Object>();

		typeMap.put("checktypecode", "会计科目");
		typeMap.put("checktypename", "会计科目");
		quyMap.put("checktype", typeMap);
		List<Map<String, String>> valueList = new ArrayList<Map<String, String>>();
		Map<String, String> valueMap = new HashMap<String, String>();

		valueMap.put("checkvaluecode", "224105");
		valueMap.put("checkvaluename", "运费");
		valueList.add(valueMap);
		quyMap.put("checkvalue", valueList);
		queryObjList.add(quyMap);
		//
		Map<String, Object> quyMap1 = new HashMap<String, Object>();
		Map<String, Object> typeMap1 = new HashMap<String, Object>();

		typeMap1.put("checktypecode", "0019");
		typeMap1.put("checktypename", "供应商档案");
		quyMap1.put("checktype", typeMap1);
		List<Map<String, String>> valueList1 = new ArrayList<Map<String, String>>();
		Map<String, String> valueMap1 = new HashMap<String, String>();

		valueMap1.put("checkvaluecode", code);
		valueMap1.put("checkvaluename", name);
		valueList1.add(valueMap1);
		quyMap1.put("checkvalue", valueList1);
		queryObjList.add(quyMap1);
		map.put("queryObj", queryObjList);
		return new Gson().toJson(map);
	}

	private static String getRequest3(String accbookCode, String strdate, String code, String name) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> accbookcodeList = new ArrayList<String>();
		accbookcodeList.add(accbookCode);
		map.put("accbookCode", accbookcodeList);
		map.put("queryByPeriod", "false");
		map.put("inclUntallyed", "true");
		map.put("inclTransfer", "true");
		map.put("inclError", "true");
		map.put("inclReclassify", "true");
		map.put("date", strdate);
		map.put("endDate", strdate);
		map.put("currtypeCode", "本币");

		List<Map<String, Object>> queryObjList = new ArrayList<Map<String, Object>>();
		Map<String, Object> quyMap = new HashMap<String, Object>();
		Map<String, Object> typeMap = new HashMap<String, Object>();

		typeMap.put("checktypecode", "会计科目");
		typeMap.put("checktypename", "会计科目");
		quyMap.put("checktype", typeMap);
		List<Map<String, String>> valueList = new ArrayList<Map<String, String>>();
		Map<String, String> valueMap = new HashMap<String, String>();

		valueMap.put("checkvaluecode", "224101");
		valueMap.put("checkvaluename", "保证金");
		valueList.add(valueMap);
		quyMap.put("checkvalue", valueList);
		queryObjList.add(quyMap);
		//
		Map<String, Object> quyMap1 = new HashMap<String, Object>();
		Map<String, Object> typeMap1 = new HashMap<String, Object>();

		typeMap1.put("checktypecode", "0019");
		typeMap1.put("checktypename", "供应商档案");
		quyMap1.put("checktype", typeMap1);
		List<Map<String, String>> valueList1 = new ArrayList<Map<String, String>>();
		Map<String, String> valueMap1 = new HashMap<String, String>();

		valueMap1.put("checkvaluecode", code);
		valueMap1.put("checkvaluename", name);
		valueList1.add(valueMap1);
		quyMap1.put("checkvalue", valueList1);
		queryObjList.add(quyMap1);
		map.put("queryObj", queryObjList);
		return new Gson().toJson(map);
	}
}
