package nc.service.rest.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import nc.bs.dao.BaseDAO;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.vo.bd.psn.PsndocVO;
import nc.vo.gl.pubvoucher.DetailVO;
import nc.vo.gl.pubvoucher.VoucherVO;
import nc.vo.glcom.ass.AssVO;
import nc.vo.org.DeptVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;

/**
 * <b> NCC-NC推送凭证：数据转换工具类 </b>
 * 2020-11-18
 * @version NCC2005
 */
public class ConvertVoucherUtil {
	
	private static BaseDAO dao = new BaseDAO();
  
	/**
	 * JSON数组转成凭证VO List
	 * 
	 * @param data
	 * @return
	 * @throws BusinessException
	 */
	public static List<VoucherVO> jsonToVoucherVOList(JSONArray data) throws BusinessException {
		if (data == null || data.size() == 0) {
			throw new BusinessException("推送数据报文格式错误");
		}

		List<VoucherVO> voucherList = new ArrayList<VoucherVO>();
		for (int i = 0; i < data.size(); i++) {
			JSONObject voucherData = data.getJSONObject(i).getJSONObject("data"); // 报文数据
			VoucherVO voucherVO = ConvertVoucherUtil.jsonToVoucherVO(voucherData);
			voucherList.add(voucherVO);
		}
		return voucherList;
	}

	/**
	 * JSON对象转成凭证VO
	 * 
	 * @param data
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("deprecation")
	public static VoucherVO jsonToVoucherVO(JSONObject data) throws BusinessException {
		if (data == null) {
			throw new BusinessException("推送数据报文格式错误");
		}
		// 1.表头字段处理
		VoucherVO voucherVO = new VoucherVO();
		voucherVO.setPk_voucher(null);
		voucherVO.setStatus(VOStatus.NEW);
		JSONObject headData = data.getJSONObject("headitem"); // 表头数据
		for (String key : headData.keySet()) {
			String value = headData.get(key) == null ? "" : headData.get(key).toString().trim(); // 字段值
			if ("".equals(value)) {
				continue;
			}
			switch (key) {
				case "isdifflag":
					voucherVO.setIsdifflag("Y".equals(value) ? UFBoolean.TRUE : UFBoolean.FALSE);
					break;
				case "ismodelrecflag":
					voucherVO.setIsmodelrecflag("Y".equals(value) ? UFBoolean.TRUE : UFBoolean.FALSE);
					break;
				case "isOffer":
					voucherVO.setIsOffer("Y".equals(value) ? UFBoolean.TRUE : UFBoolean.FALSE);
					break;
				case "adjustperiod":
					voucherVO.setM_adjustperiod(value);
					break;
				case "detailmodflag":
					voucherVO.setDetailmodflag("Y".equals(value) ? UFBoolean.TRUE : UFBoolean.FALSE);
					break;
				case "explanation":
					voucherVO.setExplanation(value);
					break;
				case "modifyflag":
					voucherVO.setModifyflag(value);
					break;
				case "period":
					voucherVO.setPeriod(value);
					break;
				case "pk_accountingbook":
					voucherVO.setPk_accountingbook(value);
					break;
				case "pk_org":
					voucherVO.setPk_org(value);
					break;
				case "pk_system":
					voucherVO.setPk_system(value);
					break;
				case "pk_vouchertype":
					voucherVO.setPk_vouchertype(value);
					break;
				case "prepareddate":
					voucherVO.setPrepareddate(new UFDate(value));
					break;
				case "tempsaveflag":
					voucherVO.setTempsaveflag("Y".equals(value) ? UFBoolean.TRUE : UFBoolean.FALSE);
					break;
				case "totalcredit":
					voucherVO.setTotalcredit(new UFDouble(value));
					break;
				case "totalcreditglobal":
					voucherVO.setTotalcreditglobal(new UFDouble(value));
					break;
				case "totalcreditgroup":
					voucherVO.setTotalcreditgroup(new UFDouble(value));
					break;
				case "totaldebit":
					voucherVO.setTotaldebit(new UFDouble(value));
					break;
				case "totaldebitglobal":
					voucherVO.setTotaldebitglobal(new UFDouble(value));
					break;
				case "totaldebitgroup":
					voucherVO.setTotaldebitgroup(new UFDouble(value));
					break;
				case "voucherkind":
					voucherVO.setVoucherkind(Integer.valueOf(value));
					break;
				case "vouchertypename":
					voucherVO.setVouchertypename(value);
					break;
				case "pk_group":
					voucherVO.setPk_group(value);
					break;
				case "year":
					voucherVO.setYear(value);
					break;
				case "pk_org_v":
					voucherVO.setPk_org_v(value);
					break;
				case "free1": // 单据类型
					voucherVO.setFree2(value); // 前端页面free1对应VO的free2
					break;
				case "free2": // 单据号
					voucherVO.setFree3(value); // 前端页面free2对应VO的free3
					break;
				case "pk_prepared": // 制单人
					String sql="select factorvalue2 from fip_docview_b where pk_classview in (select pk_classview from fip_docview where viewcode='WY025')"
							+ " and nvl(dr,0)=0 and factorvalue1 in  (select pk_org from org_orgs where code='"+ voucherVO.getPk_accountingbook() +"')";
					
					HashMap<String, String> paramsMap = (HashMap<String, String>) dao.executeQuery(sql, new MapProcessor());
					String factorvalue2 = "";
					if(paramsMap!=null) {
						factorvalue2 = paramsMap.get("factorvalue2");
						
						String usql = "select user_code from sm_user where cuserid='"+ factorvalue2 +"'";
						HashMap<String, String> umap = (HashMap<String, String>) dao.executeQuery(usql, new MapProcessor());
						String userCode = "";
						if(umap!=null) {
							userCode = umap.get("user_code");
						}
						if(userCode != null && !"".equals(userCode)) {
							voucherVO.setPk_prepared(userCode);
						}else {
							voucherVO.setPk_prepared(value);
						}
					}else {
						voucherVO.setPk_prepared(value);	
					}
					break;
					
					//add by hanmh 2021-01-09 增加审核人、记账人传递	
				case "pk_checked": // 审核人
					voucherVO.setPk_checked(value);
					break;
				case "pk_manager": // 记账人
					voucherVO.setPk_manager(value);
					break;
					//add by hanmh 2021-01-09 增加审核人、记账人传递	
					
				default:
					throw new BusinessException("表头字段【" + key + "】赋值失败");
			}
		}
		// 2.表体字段处理
		JSONArray bodyDataArray = data.getJSONArray("bodyitems");
		DetailVO[] detailVOs = new DetailVO[bodyDataArray.size()]; // 表体VO
		for (int j = 0; j < bodyDataArray.size(); j++) {
			DetailVO detailVO = new DetailVO();
			detailVO.setStatus(VOStatus.NEW);
			detailVO.setPk_detail(null);
			JSONObject bodyData = bodyDataArray.getJSONObject(j);
			boolean flag = false;
			for (String key : bodyData.keySet()) {
				String value = bodyData.get(key) == null ? "" : bodyData.get(key).toString().trim(); // 字段值
				if ("".equals(value)) {
					continue;
				}
				switch (key) {
					case "accsubjcode":
						detailVO.setAccsubjcode(value);
						break;
					case "accsubjname":
						detailVO.setAccsubjname(value);
						break;
					case "excrate3":
						detailVO.setExcrate3(new UFDouble(value));
						break;
					case "excrate4":
						detailVO.setExcrate4(new UFDouble(value));
						break;
					case "ass": // 辅助属性
						try {
							JSONArray assArray = bodyData.getJSONArray("ass");
							AssVO[] assVOs = new AssVO[assArray.size()]; // 辅助属性VO
							for (int i = 0; i < assArray.size(); i++) {
								// NC传的数组有两个元素：1：辅助属性类型编码，如：客商(0004)；2：所选值的编码，如选了0001客商
								JSONArray valueArray = assArray.getJSONArray(i);
								String typeCode = valueArray.get(0).toString(); // 辅助属性类型编码，如：类型为：客商(0004)
								String valueCode = valueArray.getString(1).toString(); // 所选值的编码，如：选了0001客商
								AssVO assVO = new AssVO();
								assVO.setChecktypecode(typeCode);
								assVO.setCheckvaluecode(valueCode);
								assVOs[i] = assVO;
								if("F055".equals(typeCode)) {
									flag = true;
								}
							}
							detailVO.setAss(assVOs);
						} catch (Exception e) {
							throw new BusinessException("【ass】字段处理过程中出错，请检查传输值格式是否正确。错误详情：" + e.getMessage());
						}
						break;
					case "creditamount":
						detailVO.setCreditamount(new UFDouble(value));
						break;
					case "creditquantity":
						detailVO.setCreditquantity(new UFDouble(value));
						break;
					case "debitamount":
						detailVO.setDebitamount(new UFDouble(value));
						break;
					case "debitquantity":
						detailVO.setDebitquantity(new UFDouble(value));
						break;
					case "excrate2":
						detailVO.setExcrate2(new UFDouble(value));
						break;
					case "explanation":
						detailVO.setExplanation(value);
						break;
					case "fracdebitamount":
						detailVO.setFracdebitamount(new UFDouble(value));
						break;
					case "globalcreditamount":
						detailVO.setGlobalcreditamount(new UFDouble(value));
						break;
					case "globaldebitamount":
						detailVO.setGlobaldebitamount(new UFDouble(value));
						break;
					case "groupcreditamount":
						detailVO.setGroupcreditamount(new UFDouble(value));
						break;
					case "groupdebitamount":
						detailVO.setGroupdebitamount(new UFDouble(value));
						break;
					case "localcreditamount":
						detailVO.setLocalcreditamount(new UFDouble(value));
						break;
					case "localdebitamount":
						detailVO.setLocaldebitamount(new UFDouble(value));
						break;
					case "modifyflag":
						detailVO.setModifyflag(value);
						break;
					case "oppositesubj":
						detailVO.setOppositesubj(value);
						break;
					case "pk_accasoa":
						/***********************************************************************
						 * 科目传过来的是一个数组，长度为2：第一个元素为科目体系编码，按照项目要求存到自定义项4;
						 * 第二个元素是科目编码，存到当前字段pk_accasoa中，保存时再翻译成主键；
						 ***********************************************************************/
						try {
							JSONArray array = JSONArray.parseArray(value);
							if (array.size() != 2) {
								throw new BusinessException("字段【pk_accasoa】传的值格式错误");
							}
							voucherVO.setFree5(array.get(0) == null ? "" : array.get(0).toString()); // 前端free4对应VO中free5
							detailVO.setPk_accasoa(array.get(1) == null ? "" : array.get(1).toString());
						} catch (Exception e) {
							throw new BusinessException("【pk_accasoa】字段处理过程中出错，请检查传输值格式是否正确。错误详情：" + e.getMessage());
						}
						break;
					case "pk_accountingbook":
						detailVO.setPk_accountingbook(value);
						break;
					case "pk_currtype":
						detailVO.setPk_currtype(value);
						break;
					case "price":
						detailVO.setPrice(new UFDouble(value));
						break;
					case "no":
						detailVO.setNo(Integer.valueOf(value));
						break;
					case "pk_group":
						detailVO.setPk_group(value);
						break;
					case "pk_org":
						detailVO.setPk_org(value);
						break;
					case "pk_org_v":
						detailVO.setPk_org_v(value);
						break;
					case "pk_unit":
						detailVO.setPk_unit(value);
						break;
					case "pk_unit_v":
						detailVO.setPk_unit_v(value);
						break;
					case "prepareddate":
						detailVO.setPrepareddate(new UFDate(value));
						break;
					case "voucherkind":
						detailVO.setVoucherkind(Integer.valueOf(value));
						break;
					default:
						throw new BusinessException("表体字段【" + key + "】赋值失败");
				}
			}
			if(!flag) {
				//String sql="select factorvalue3 from fip_docview_b where pk_classview in (select pk_classview from fip_docview where viewcode='WYZZ005') "
					//	+ "and nvl(dr,0)=0 and desdocvalue "
				      //    + " in ('"+ detailVO.getPk_accasoa() +"') and factorvalue1= '"+ detailVO.getPk_org() +"' and factorvalue2 in (select code from bd_accchart where pk_accchart='"+ detailVO.getPk_accchart() +"')";
		          //select * from org_orgs where pk_org='1001H91000000000BMPZ';
		          //select * from bd_accasoa where pk_accasoa='1001H910000000013R5Q';
				//select * from push_voucher_log where json_data is not null;
				String sql="select factorvalue3 from fip_docview_b where pk_classview in (select pk_classview from fip_docview where viewcode='WYZZ005')"
						+ " and nvl(dr,0)=0 and factorvalue2  in ('"+ detailVO.getPk_accasoa() +"') and factorvalue1 in  (select pk_org from org_orgs where code='"+ detailVO.getPk_accountingbook() +"')";
				
				HashMap<String, String> paramsMap = (HashMap<String, String>) dao.executeQuery(sql, new MapProcessor());
				String factorvalue3 = "";
				if(paramsMap!=null) {
					factorvalue3 = paramsMap.get("factorvalue3");
	
					JSONArray assArray1 = new JSONArray();
					assArray1.add("F055");
					assArray1.add(factorvalue3);
					JSONArray assArray = new JSONArray();
					assArray.add(assArray1);
					for(int k=0;k<detailVO.getAss().length;k++) {
						JSONArray assArray2 = new JSONArray();
						assArray2.add(detailVO.getAss()[k].getChecktypecode());
						assArray2.add(detailVO.getAss()[k].getCheckvaluecode());

						assArray.add(assArray2);
					}
	
					AssVO[] assVOs = new AssVO[assArray.size()]; // 辅助属性VO
					for (int i = 0; i < assArray.size(); i++) {
						// NC传的数组有两个元素：1：辅助属性类型编码，如：客商(0004)；2：所选值的编码，如选了0001客商
						JSONArray valueArray = assArray.getJSONArray(i);
						String typeCode = valueArray.get(0).toString(); // 辅助属性类型编码，如：类型为：客商(0004)
						String valueCode = valueArray.getString(1).toString(); // 所选值的编码，如：选了0001客商
						AssVO assVO = new AssVO();
						assVO.setChecktypecode(typeCode);
						assVO.setCheckvaluecode(valueCode);
						assVOs[i] = assVO;
					}
					detailVO.setAss(assVOs);
				}
			}

			detailVOs[j] = detailVO;
		}
		voucherVO.setDetails(detailVOs);
		return voucherVO;
	}

	/**
	 * 凭证VO转换成JSON字符串
	 * 
	 * @param vo
	 * @return
	 * @throws BusinessException
	 */
	public static String voucherVOToJsonString(List<VoucherVO> voList) throws BusinessException {

		JSONArray array = new JSONArray();
		for (VoucherVO vo : voList) {
			// 1.表头字段处理
			Map<String, String> headData = ConvertVoucherUtil.convertVoucherHead(vo);
			// 2.表体字段处理
			String accasoaTableCode = vo.getFree5(); // 科目体系编码
			DetailVO[] detailVOs = vo.getDetails();
			List<Map<String, Object>> bodyData = ConvertVoucherUtil.convertVoucherBody(detailVOs, accasoaTableCode);
			// 3.按照NC63接口的要求，拼接JSON
			JSONObject voucherJson = new JSONObject();
			voucherJson.put("headitem", headData); // 表头
			voucherJson.put("bodyitems", bodyData); // 表体
			JSONObject data = new JSONObject();
			data.put("data", voucherJson);
			array.add(data);
		}
		JSONObject result = new JSONObject();
		result.put("data", array);
		return result.toJSONString();
	}

	/**
	 * 转换凭证表头，方便拼接JSON
	 * 
	 * @param vo
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("deprecation")
	public static Map<String, String> convertVoucherHead(VoucherVO vo) throws BusinessException {

		Map<String, String> headData = new HashMap<String, String>();
		headData.put("isdifflag", value(vo.getIsdifflag()));
		headData.put("ismodelrecflag", value(vo.getIsmodelrecflag()));
		headData.put("isOffer", value(vo.getIsOffer()));
		headData.put("adjustperiod", value(vo.getM_adjustperiod()));
		headData.put("detailmodflag", value(vo.getDetailmodflag()));
		headData.put("explanation", value(vo.getExplanation()));
		headData.put("free1", value(vo.getFree2())); // 单据类型
		headData.put("free2", value(vo.getFree3())); // 单据号
		headData.put("free3", value(vo.getNo())); // 凭证号
		headData.put("modifyflag", value(vo.getModifyflag()));
		headData.put("period", value(vo.getPeriod()));
		headData.put("pk_accountingbook", value(vo.getPk_accountingbook()));
		headData.put("pk_org", value(vo.getPk_org()));
		headData.put("pk_system", value(vo.getPk_system()));
		headData.put("pk_vouchertype", value(vo.getPk_vouchertype()));
		headData.put("prepareddate", value(vo.getPrepareddate().toString()));
		headData.put("tempsaveflag", value(vo.getTempsaveflag()));
		headData.put("totalcredit", value(vo.getTotalcredit()));
		headData.put("totalcreditglobal", value(vo.getTotalcreditglobal()));
		headData.put("totalcreditgroup", value(vo.getTotalcreditgroup()));
		headData.put("totaldebit", value(vo.getTotaldebit()));
		headData.put("totaldebitglobal", value(vo.getTotaldebitglobal()));
		headData.put("totaldebitgroup", value(vo.getTotaldebitgroup()));
		headData.put("voucherkind", value(vo.getVoucherkind()));
		headData.put("vouchertypename", value(vo.getVouchertypename()));
		headData.put("pk_group", value(vo.getPk_group()));
		headData.put("year", value(vo.getYear()));
		headData.put("pk_org_v", value(vo.getPk_org_v()));
		headData.put("pk_prepared", value(vo.getPk_prepared())); // 制单人
		headData.put("pk_checked", value(vo.getPk_checked())); // 审核人
		headData.put("pk_manager", value(vo.getPk_manager())); // 记账人
		headData.put("modifier", value(vo.getModifier())); // 修改人
		headData.put("pk_voucher", value(vo.getPk_voucher())); // 主键
		
		return headData;
	}

	/**
	 * 转换凭证表体，方便拼接JSON
	 * 
	 * @param vos
	 * @param accasoaTableCode
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("deprecation")
	public static List<Map<String, Object>> convertVoucherBody(DetailVO[] vos, String accasoaTableCode)
			throws BusinessException {
		List<Map<String, Object>> bodyDataList = new ArrayList<Map<String, Object>>();
		if (vos == null || vos.length == 0) {
			return bodyDataList;
		}
		for (DetailVO vo : vos) {
			Map<String, Object> bodyData = new HashMap<String, Object>();
			bodyData.put("accsubjcode", value(vo.getAccsubjcode()));
			bodyData.put("accsubjname", value(vo.getAccsubjname()).replaceAll("\\\\", "\\\\\\\\"));
			bodyData.put("excrate3", value(vo.getExcrate3()));
			bodyData.put("excrate4", value(vo.getExcrate4()));

			/*
			 * String sql =
			 * "select desdocvalue from fip_docview_b where factorvalue3 in ('F055') and " +
			 * "nvl(dr,0)=0 and factorvalue1 in (select c.code from bd_accasoa a,bd_account c "
			 * +
			 * "where a.pk_account = c.pk_account and nvl(a.dr,0)=0  and nvl(c.dr,0)=0 and a.pk_accasoa='"
			 * + vo.getPk_accasoa() +"') " +
			 * "and factorvalue2 in (select a.code from bd_accchart a,bd_accasoa b where b.pk_accasoa='"
			 * + vo.getPk_accasoa() +"' and " +
			 * "nvl(a.dr,0)=0 and nvl(b.dr,0)=0 and a.pk_accchart=b.pk_accchart)";
			 */
			String glsql = "select pk_accchart from gl_detail where pk_detail ='"+ vo.getPk_detail() +"'";
			HashMap<String, String> paramsMap = (HashMap<String, String>) dao.executeQuery(glsql, new MapProcessor());
			String pk_accchart = "";
			if(paramsMap!=null) {
				pk_accchart = paramsMap.get("pk_accchart");
			}
			
			String sql = "select factorvalue3 from fip_docview_b where pk_classview in (select pk_classview from fip_docview where viewcode='WYZZ004') and nvl(dr,0)=0 and factorvalue1"
					+ " in ('"+ vo.getPk_accasoa() +"') and factorvalue2 in (select code from bd_accchart where pk_accchart='"+ pk_accchart +"')";
			List<String> l=(List<String>) dao.executeQuery(sql, new ColumnListProcessor());
			
			// 辅助属性特殊处理
			JSONArray assArray = new JSONArray();
			
			AssVO[] assVOs = vo.getAss() == null ? new AssVO[0] : vo.getAss();
			for (AssVO assVO : assVOs) {
						
				JSONArray params = new JSONArray();
				params.add(assVO.getChecktypecode()); // 辅助核算项目编码（如：客商为0004）
				String checkValueCode = assVO.getCheckvaluecode(); // 辅助核算项目所选值编码（如：选了001客商）

				if ("b26fa3cb-4087-4027-a3b6-c83ab2a086a9".equals(assVO.getM_classid())) {
					// 部门：NC和NCC部门编码不一致，辅助为部门时需要翻译
					checkValueCode = transNccDeptCode2NcCode(assVO.getPk_Checkvalue());
				}
				
				//wby 2021-10-12 如果是员工档案，需要特殊处理（NCC人员def3为NC63人员编码）
				if ("40d39c26-a2b6-4f16-a018-45664cac1a1f".equals(assVO.getM_classid())) {
					// 部门：NC和NCC部门编码不一致，辅助为员工时需要翻译
					String psnCode = transNccPsndocCode2NcCode(assVO.getPk_Checkvalue());
					//def3为空，则用NCC人员编码
					if(!StringUtils.isEmpty(psnCode)) {
						checkValueCode = psnCode;
					}
				}
				params.add(checkValueCode);
				
				if(l!=null && l.size()>0 && l.contains(assVO.getChecktypecode())) {
				}else {
					assArray.add(params);
				}
			}
			bodyData.put("ass", assArray);

			bodyData.put("creditamount", value(vo.getCreditamount()));
			bodyData.put("creditquantity", value(vo.getCreditquantity()));
			bodyData.put("debitamount", value(vo.getDebitamount()));
			bodyData.put("debitquantity", value(vo.getDebitquantity()));
			bodyData.put("excrate2", value(vo.getExcrate2()));
			bodyData.put("explanation", value(vo.getExplanation()));
			bodyData.put("fracdebitamount", value(vo.getFracdebitamount()));
			bodyData.put("globalcreditamount", value(vo.getGlobalcreditamount()));
			bodyData.put("globaldebitamount", value(vo.getGlobaldebitamount()));
			bodyData.put("groupcreditamount", value(vo.getGroupcreditamount()));
			bodyData.put("groupdebitamount", value(vo.getGroupdebitamount()));
			bodyData.put("localcreditamount", value(vo.getLocalcreditamount()));
			bodyData.put("localdebitamount", value(vo.getLocaldebitamount()));
			bodyData.put("modifyflag", value(vo.getModifyflag()));
			bodyData.put("oppositesubj", value(vo.getOppositesubj()));

			// 科目需要特殊处理
			List<String> assaList = new ArrayList<String>();
			assaList.add(value(accasoaTableCode)); // NC推送凭证时传了科目表编码，NCC反推时需要带上此编码。
			assaList.add(value(vo.getPk_accasoa())); // 科目编码
			bodyData.put("pk_accasoa", assaList);

			bodyData.put("pk_accountingbook", value(vo.getPk_accountingbook()));
			bodyData.put("pk_currtype", value(vo.getPk_currtype()));
			bodyData.put("price", value(vo.getPrice()));
			bodyData.put("no", value(vo.getNo()));
			bodyData.put("pk_group", value(vo.getPk_group()));
			bodyData.put("pk_org", value(vo.getPk_org()));
			bodyData.put("pk_org_v", value(vo.getPk_org_v()));
			bodyData.put("pk_unit", value(vo.getPk_unit()));
			bodyData.put("pk_unit_v", value(vo.getPk_unit_v()));
			bodyData.put("prepareddate", value(vo.getPrepareddate()));
			bodyData.put("voucherkind", value(vo.getVoucherkind()));
			bodyDataList.add(bodyData);
		}
		return bodyDataList;
	}
	
	/**
	 * 根据NCC部门主键获取NC部门编码
	 * 
	 * @param pk_dept
	 * @return
	 * @throws BusinessException
	 */
	public static String transNccDeptCode2NcCode(String pk_dept) throws BusinessException {
		if (pk_dept == null || "".equals(pk_dept.trim()) || "~".equals(pk_dept.trim())) {
			return "~";
		}
		DeptVO deptVO = (DeptVO) ConvertVoucherUtil.dao.retrieveByPK(DeptVO.class, pk_dept);
		String ncDeptCode = deptVO.getDef1(); // NCC部门def1字段为NC63部门编码
		return (ncDeptCode == null || "".equals(ncDeptCode)) ? "" : ncDeptCode;
	}
	
	/**
	 * wby 20211013 根据NCC员工主键获取NC员工编码
	 * 
	 * @param pk_psndoc
	 * @return
	 * @throws BusinessException
	 */
	public static String transNccPsndocCode2NcCode(String pk_psndoc) throws BusinessException {
		if (pk_psndoc == null || "".equals(pk_psndoc.trim()) || "~".equals(pk_psndoc.trim())) {
			return "~";
		}
		PsndocVO vo = (PsndocVO) ConvertVoucherUtil.dao.retrieveByPK(PsndocVO.class, pk_psndoc);
		String ncPsndocCode = vo.getDef3(); // NCC部门def3字段为NC63部门编码
		return (ncPsndocCode == null || "".equals(ncPsndocCode)) ? "" : ncPsndocCode;
	}

	public static String value(Object value) {
		if (value == null) {
			return "";
		} else if (value instanceof UFBoolean) {
			return ((UFBoolean) value).booleanValue() ? "Y" : "N";
		} else {
			return value.toString();
		}
	}

	  public static void main(String[] args) {
			JSONObject res =  JSONObject.parseObject(" {\"code\":200,\"data\":[{\"period\":\"07\",\"free3\":1,\"year\":\"2021\",\"num\":\"1\"}],\"success\":true}");
			
			JSONArray array = res.getJSONArray("data"); // 返回凭证号：[{"period":"11","free3":13,"year":"2020","num":"13"}]
			for (int i = 0; i < array.size(); i++) {
				JSONObject obj = array.getJSONObject(i);
				String year = obj.getString("year"); // 年
				String period = obj.getString("period"); // 期间（月）
				String free1 = obj.getString("free1"); // 单据类型
				String free2 = obj.getString("free2"); // 单据号
				String free3 = obj.getString("free3"); // NC63回写的凭证号
				String nccVoucherNO = obj.getString("num"); // NCC凭证号
				String key = year + "_" + period + "_" + free1 + "_" + free2 + "_" + nccVoucherNO;
				System.out.println("NC63回写的凭证号:"+free3);
			}
	}
}
