package nccloud.api.impl.mmbd.routing.rule;

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

import nc.bs.framework.common.NCLocator;
import nc.impl.pubapp.pattern.data.vo.VOQuery;
import nc.itf.bd.rt.rt0008.IRtQuery;
import nc.itf.bd.rt.rt0008.service.ProxyService;
import nc.itf.mmf.busi.measure.IMeasesureService;
import nc.mmbd.adapter.uap.UAPAdapter;
import nc.pubitf.bd.bdactivity.pub.IBDActivityPubQuery;
import nc.util.mmf.busi.measure.MeasureVO;
import nc.util.mmf.busi.service.MaterialPubService;
import nc.util.mmf.busi.service.OrgUnitPubService;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.util.mmf.framework.db.MMSqlBuilder;
import nc.vo.bd.bdactivity.entity.BDActivityVO;
import nc.vo.bd.bom.bom0202.enumeration.ItemSourceEnum;
import nc.vo.bd.material.MaterialVO;
import nc.vo.bd.rt.rt0004.entity.RcVO;
import nc.vo.bd.rt.rt0008.entity.AggRtVO;
import nc.vo.bd.rt.rt0008.entity.AssOrgVO;
import nc.vo.bd.rt.rt0008.entity.RtActivityVO;
import nc.vo.bd.rt.rt0008.entity.RtHeadVO;
import nc.vo.bd.rt.rt0008.entity.RtItemVO;
import nc.vo.bd.rt.rt0008.entity.RtOpacVO;
import nc.vo.bd.rt.rt0008.enumeration.FcontrolEnum;
import nc.vo.bd.rt.rt0008.enumeration.FprocessMoveTypeEnum;
import nc.vo.bd.rt.rt0008.enumeration.FsupplymodeEnum;
import nc.vo.ml.MultiLangUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.AppContext;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

public class ChangeAboutFieldValue {
	public void process(AggRtVO[] vos) {
		for (AggRtVO vo : vos) {
			this.fillHeadValue(vo);
		}
	}

	private void fillHeadValue(AggRtVO vo) {
		RtHeadVO hvo = (RtHeadVO) vo.getParentVO();
		AggRtVO[] aggvos = null;
		if(MMValueCheck.isNotEmpty(hvo.getPrimaryKey())) {
			IRtQuery rtquery = (IRtQuery) NCLocator.getInstance().lookup(IRtQuery.class);
			try {
				aggvos = rtquery.queryByIds(new String[] {hvo.getPrimaryKey()});
			} catch (Exception e) {
				ExceptionUtils.wrappException(e);
			}
			RtHeadVO oldHead = (RtHeadVO) aggvos[0].getParentVO();
			if (MMValueCheck.isEmpty(hvo.getCreator())) {
				hvo.setCreator(oldHead.getCreator());
			}
			if (MMValueCheck.isEmpty(hvo.getBillmaker())) {
				hvo.setBillmaker(oldHead.getBillmaker());
			}
			if (hvo.getCreationtime()==null) {
				hvo.setCreationtime(oldHead.getCreationtime());
			}
			
		}
		// 工厂关联工厂版本
		if (MMValueCheck.isNotEmpty(hvo.getPk_org()) && MMValueCheck.isEmpty(hvo.getPk_org_v())) {
			String vid = OrgUnitPubService.getNewVIDByOrgID(hvo.getPk_org());
			hvo.setPk_org_v(vid);
		}
		if (MMValueCheck.isNotEmpty(hvo.getPk_org_v()) && MMValueCheck.isEmpty(hvo.getPk_org())) {
			String oid = OrgUnitPubService.getOrgIDByVID(hvo.getPk_org());
			hvo.setPk_org(oid);
		}
		// 物料编码关联相关字段
		if (MMValueCheck.isNotEmpty(hvo.getCmaterialid()) && MMValueCheck.isEmpty(hvo.getCmaterialvid())) {
			String materialvid = hvo.getCmaterialid();
			materialvid = MaterialPubService.queryMaterialLatestVidByOid(new String[] { materialvid }).get(materialvid);
			hvo.setCmaterialvid(materialvid);
			this.setMaterialField(vo, hvo.getCmaterialid());
		}
		if (MMValueCheck.isEmpty(hvo.getCmaterialid()) && MMValueCheck.isNotEmpty(hvo.getCmaterialvid())) {
			String materialoid = hvo.getCmaterialid();
			materialoid = MaterialPubService.queryOidMaterialByLatestVid(new String[] { hvo.getCmaterialvid() })
					.get(hvo.getCmaterialvid());
			hvo.setCmaterialvid(materialoid);
			this.setMaterialField(vo, materialoid);
		}
		if (MMValueCheck.isEmpty(hvo.getHbcustomized())) {
			hvo.setHbcustomized(UFBoolean.FALSE);
		}
		try {
			this.fillBodyValue(vo);
		} catch (BusinessException e) {
			ExceptionUtils.wrappException(e);
		}
	}

	private void fillBodyValue(AggRtVO vo) throws BusinessException {
		RtHeadVO hvo = (RtHeadVO) vo.getParentVO();
		AssOrgVO[] avos = (AssOrgVO[]) vo.getChildren(AssOrgVO.class);
		AssOrgVO[] avoss = null;
		if (MMValueCheck.isNotEmpty(avos)) {
			if (MMValueCheck.isEmpty(avos[0].getPk_useorg()) && MMValueCheck.isEmpty(avos[0].getPk_org())
					&& MMValueCheck.isEmpty(avos[0].getPk_org_v()) && MMValueCheck.isEmpty(avos[0].getPk_useorg_v())) {
				avos[0].setPk_useorg(hvo.getPk_org());
				avos[0].setPk_org(hvo.getPk_org());
				avos[0].setPk_useorg_v(hvo.getPk_org_v());
				avos[0].setPk_org_v(hvo.getPk_org_v());
				vo.setChildren(AssOrgVO.class, avos);
			} else {
				List<String> oidList = new ArrayList<String>();
				List<String> vidList = new ArrayList<String>();
				for (AssOrgVO avo : avos) {
					if (MMValueCheck.isNotEmpty(avo.getPk_useorg())) {
						oidList.add(avo.getPk_useorg());
					}
					if (MMValueCheck.isNotEmpty(avo.getPk_useorg_v())) {
						vidList.add(avo.getPk_useorg_v());
					}
				}
				// 根据vid获取oid
				Map<String, String> oidmap = OrgUnitPubService.getOrgIDSByVIDS(vidList.toArray(new String[0]));
				// 根据oid获取vid
				Map<String, String> vidmap = OrgUnitPubService.getNewVIDSByOrgIDS(oidList.toArray(new String[0]));
				UFBoolean flag = UFBoolean.FALSE;
				for (AssOrgVO avo : avos) {
					if (MMValueCheck.isEmpty(avo.getPk_org()) && MMValueCheck.isNotEmpty(avo.getPk_useorg())) {
						avo.setPk_org(avo.getPk_useorg());
					}
					if (MMValueCheck.isEmpty(avo.getPk_org_v()) && MMValueCheck.isNotEmpty(avo.getPk_useorg_v())) {
						avo.setPk_org_v(avo.getPk_useorg_v());
					}
					if (MMValueCheck.isEmpty(avo.getPk_org_v()) && MMValueCheck.isNotEmpty(avo.getPk_org())) {
						avo.setPk_org_v(vidmap.get(avo.getPk_org()));
					}
					if (MMValueCheck.isEmpty(avo.getPk_org()) && MMValueCheck.isNotEmpty(avo.getPk_org_v())) {
						avo.setPk_org(oidmap.get(avo.getPk_org_v()));
					}
					if (MMValueCheck.isEmpty(avo.getPk_useorg()) && MMValueCheck.isNotEmpty(avo.getPk_useorg_v())) {
						avo.setPk_useorg(vidmap.get(avo.getPk_useorg_v()));
					}
					if (MMValueCheck.isEmpty(avo.getPk_useorg_v()) && MMValueCheck.isNotEmpty(avo.getPk_useorg())) {
						avo.setPk_useorg_v(vidmap.get(avo.getPk_useorg()));
					}
				}
				for (AssOrgVO avo : avos) {
					if (hvo.getPk_org().equals(avo.getPk_org())) {
						flag = UFBoolean.TRUE;
					}
					if (hvo.getPk_org_v().equals(avo.getPk_org_v())) {
						flag = UFBoolean.TRUE;
					}
					if (hvo.getPk_org().equals(avo.getPk_useorg())) {
						flag = UFBoolean.TRUE;
					}
					if (hvo.getPk_org_v().equals(avo.getPk_useorg_v())) {
						flag = UFBoolean.TRUE;
					}
				}
				if (flag.equals(UFBoolean.FALSE)) {
					avoss = new AssOrgVO[avos.length + 1];
					for (int i = 0; i < avos.length; i++) {
						avoss[i] = avos[i];
					}
					avoss[avos.length] = new AssOrgVO();
					avoss[avos.length].setPk_useorg(hvo.getPk_org());
					avoss[avos.length].setPk_org(hvo.getPk_org());
					avoss[avos.length].setPk_useorg_v(hvo.getPk_org_v());
					avoss[avos.length].setPk_org_v(hvo.getPk_org_v());
					vo.setChildren(AssOrgVO.class, avoss);
				}
			}
		} else {
			avos = new AssOrgVO[1];
			avos[0] = new AssOrgVO();
			// 设置使用组织的默认值
			avos[0].setPk_useorg(hvo.getPk_org());
			avos[0].setPk_org(hvo.getPk_org());
			avos[0].setPk_useorg_v(hvo.getPk_org_v());
			avos[0].setPk_org_v(hvo.getPk_org_v());
			vo.setChildren(AssOrgVO.class, avos);
		}

		RtItemVO[] itemVos = (RtItemVO[]) vo.getChildren(RtItemVO.class);
		for (RtItemVO itemvo : itemVos) {
			if (MMValueCheck.isEmpty(itemvo.getPk_org()) && MMValueCheck.isEmpty(itemvo.getPk_org_v())) {
				itemvo.setPk_org(hvo.getPk_org());
			}
			RtOpacVO[] rtVos = itemvo.getRtopacitems();
			RtActivityVO[] rtActVos = itemvo.getRtactitems();
			if (MMValueCheck.isEmpty(itemvo.getPk_group())) {
				String pk_group = AppContext.getInstance().getPkGroup();
				itemvo.setPk_group(pk_group);
			}
			if (MMValueCheck.isNotEmpty(itemvo.getPk_org()) && MMValueCheck.isEmpty(itemvo.getPk_org_v())) {
				String vid = OrgUnitPubService.getNewVIDByOrgID(itemvo.getPk_org());
				itemvo.setPk_org_v(vid);
			}
			if (MMValueCheck.isEmpty(itemvo.getPk_org()) && MMValueCheck.isNotEmpty(itemvo.getPk_org_v())) {
				String vid = OrgUnitPubService.getNewVIDByOrgID(itemvo.getPk_org());
				itemvo.setPk_org_v(vid);
			}
			if (MMValueCheck.isEmpty(itemvo.getCrcid())) {
				String pk_group = itemvo.getPk_group();
				MMSqlBuilder sql = new MMSqlBuilder();
				sql.and();
				sql.append(RcVO.PK_GROUP, pk_group);
				VOQuery<RcVO> query = new VOQuery<RcVO>(RcVO.class);
				// 查询标准工序
				RcVO[] rcVOs = query.query(sql.toString(), null);
				for (int i = 0; i < rcVOs.length; i++) {
					if (rcVOs[i].getPk_group().equals(rcVOs[i].getPk_org())) {
						itemvo.setCrcid(rcVOs[i].getCrcid());
						break;
					}
				}
			}
			// 查询工序VO
//			if (!MMValueCheck.isEmpty(itemVos[0].getCrcid())) {
//				AggRcVO[] crcvos = new BillQuery<AggRcVO>(AggRcVO.class).query(new String[] {itemVos[0].getCrcid()});
//				RcVO rcvo = (RcVO) crcvos[0].getParentVO();
//				String vrcname = rcvo.getVrcname();
//			}
			itemvo.setBcountpoint(UFBoolean.TRUE);
			itemvo.setBcheckpoint(UFBoolean.FALSE);
			itemvo.setProcessmovetype(FprocessMoveTypeEnum.NOMOVE.toIntValue());
			if (MMValueCheck.isEmpty(itemvo.getNworktime())) {
				itemvo.setNworktime(UFDouble.ONE_DBL);
			}
			// 孙表设值
			if (MMValueCheck.isNotEmpty(rtVos)) {
				for (RtOpacVO rtvo : rtVos) {
					if (MMValueCheck.isEmpty(rtvo.getVprocessno())) {
						rtvo.setVprocessno("10");
					}
					if (MMValueCheck.isEmpty(rtvo.getPk_group())) {
						String pk_group = AppContext.getInstance().getPkGroup();
						rtvo.setPk_group(pk_group);
					}
					if (MMValueCheck.isEmpty(rtvo.getPk_org()) && MMValueCheck.isEmpty(rtvo.getPk_org_v())) {
						if (MMValueCheck.isNotEmpty(itemvo.getPk_org())) {
							rtvo.setPk_org(itemvo.getPk_org());
						}
					}
					if (MMValueCheck.isNotEmpty(rtvo.getPk_org()) && MMValueCheck.isEmpty(rtvo.getPk_org_v())) {
						String vid = OrgUnitPubService.getNewVIDByOrgID(rtvo.getPk_org());
						rtvo.setPk_org_v(vid);
					}
					if (MMValueCheck.isEmpty(rtvo.getPk_org()) && MMValueCheck.isNotEmpty(rtvo.getPk_org_v())) {
						String vid = OrgUnitPubService.getNewVIDByOrgID(rtvo.getPk_org());
						rtvo.setPk_org_v(vid);
					}
					// 补全工序耗料物料
//					if (MMValueCheck.isEmpty(rtvo.getCmaterialid()) && MMValueCheck.isEmpty(rtvo.getCmaterialvid())) {
//						String pk_group = rtvo.getPk_group();
//						MMSqlBuilder sql = new MMSqlBuilder();
//						sql.and();
//						sql.append(MaterialVO.PK_GROUP, pk_group);
//						VOQuery<MaterialVO> query = new VOQuery<MaterialVO>(MaterialVO.class);
//						//查询标准工序
//						MaterialVO[] materialVos = query.query(sql.toString(), null);
//						for (MaterialVO materialvo : materialVos) {
//							if (MMValueCheck.isNotEmpty(hvo.getCmaterialid()) && !hvo.getCmaterialid().equals(materialvo.getPk_material())) {
//								rtvo.setCmaterialid(materialvo.getPk_material());
//							}
//						}
//					}
					if (MMValueCheck.isNotEmpty(rtvo.getCmaterialid()) && MMValueCheck.isEmpty(rtvo.getCmaterialvid())) {
						String materialvid = rtvo.getCmaterialid();
						materialvid = MaterialPubService.queryMaterialLatestVidByOid(new String[] { materialvid })
								.get(rtvo.getCmaterialid());
						rtvo.setCmaterialvid(materialvid);
					}
					if (MMValueCheck.isEmpty(rtvo.getCmaterialid()) && MMValueCheck.isNotEmpty(rtvo.getCmaterialvid())) {
						String materialoid = rtvo.getCmaterialid();
						materialoid = MaterialPubService.queryOidMaterialByLatestVid(new String[] { rtvo.getCmaterialvid() })
								.get(rtvo.getCmaterialvid());
						rtvo.setCmaterialvid(materialoid);
					}
					if (MMValueCheck.isNotEmpty(rtvo.getCmaterialid())) {
						IMeasesureService mservice = NCLocator.getInstance().lookup(IMeasesureService.class);
						Map<String, MeasureVO> measurevoMap;
						try {
							measurevoMap = mservice.getMeasureInfoMap(new String[] { rtvo.getCmaterialid() });
							MeasureVO mvo = measurevoMap.get(rtvo.getCmaterialid());
							String cassmeasureid = mvo.getCassMeasures();
							String cmeasureid = mvo.getCmeasureid();
							String vchangerate = mvo.getVchangeRate();
							rtvo.setCassmeasureid(cassmeasureid);
							rtvo.setCmeasureid(cmeasureid);
							rtvo.setVchangerate(vchangerate);
						} catch (BusinessException e) {
							ExceptionUtils.wrappException(e);
						}
					}
					if (MMValueCheck.isEmpty(rtvo.getNastnum()) && MMValueCheck.isEmpty(rtvo.getNnumber())) {
						rtvo.setNastnum(UFDouble.ONE_DBL);
						rtvo.setNnumber(UFDouble.ONE_DBL);
					}
					if (MMValueCheck.isEmpty(rtvo.getBacknumber())) {
						rtvo.setBacknumber(Integer.valueOf(1));
					}
					if (MMValueCheck.isEmpty(rtvo.getFsupplymode())) {
						rtvo.setFsupplymode(FsupplymodeEnum.NORMAL.toIntValue());
					}
					if (MMValueCheck.isEmpty(rtvo.getFpicksource())) {
						rtvo.setFpicksource(ItemSourceEnum.FROM_STORE.toIntValue());
					}
					if (MMValueCheck.isEmpty(rtvo.getFcontrol())) {
						rtvo.setFcontrol(FcontrolEnum.UNCONTROLABLE.toIntValue());
					}
				}
			}
			if (MMValueCheck.isNotEmpty(rtActVos)) {
				for (RtActivityVO ravo : rtActVos) {
					if (MMValueCheck.isEmpty(ravo.getPk_group())) {
						String pk_group = AppContext.getInstance().getPkGroup();
						ravo.setPk_group(pk_group);
					}
					if (MMValueCheck.isEmpty(ravo.getPk_org()) && MMValueCheck.isEmpty(ravo.getPk_org_v())) {
						if (MMValueCheck.isNotEmpty(itemvo.getPk_org())) {
							ravo.setPk_org(itemvo.getPk_org());
						}
					}
					if (MMValueCheck.isNotEmpty(ravo.getPk_org()) && MMValueCheck.isEmpty(ravo.getPk_org_v())) {
						String vid = OrgUnitPubService.getNewVIDByOrgID(ravo.getPk_org());
						ravo.setPk_org_v(vid);
					}
					if (MMValueCheck.isEmpty(ravo.getPk_org()) && MMValueCheck.isNotEmpty(ravo.getPk_org_v())) {
						String vid = OrgUnitPubService.getNewVIDByOrgID(ravo.getPk_org());
						ravo.setPk_org_v(vid);
					}
					if (MMValueCheck.isNotEmpty(ravo.getCactivityid())) {
						List<String> actids = new ArrayList<String>();
						actids.add(ravo.getCactivityid());
						IBDActivityPubQuery qry = NCLocator.getInstance().lookup(IBDActivityPubQuery.class);
						BDActivityVO[] acts = qry.queryBDActivityVOByPKs(actids);
						ravo.setCmeasureid(acts[0].getCmeasureid());
						ravo.setBilltime(acts[0].getBilltime());
						ravo.setBillbase(acts[0].getBillbase());
						ravo.setCaculateway(acts[0].getCaculateway());
					}
					if (MMValueCheck.isEmpty(ravo.getNstandardwknum())) {
						ravo.setNstandardwknum(UFDouble.ONE_DBL);
					}
					if (MMValueCheck.isNotEmpty(itemvo.getDeffectdate())) {
						ravo.setDeffectdate(itemvo.getDeffectdate());
					}
					if (MMValueCheck.isNotEmpty(itemvo.getDinvaliddate())) {
						ravo.setDinvaliddate(itemvo.getDinvaliddate());
					}
				}
			}
		}
	}

	private void setMaterialField(AggRtVO vo, String materialvid) {
		RtHeadVO hvo = (RtHeadVO) vo.getParentVO();
		RtItemVO[] itemVos = (RtItemVO[]) vo.getChildren(RtItemVO.class);
		if (MMValueCheck.isEmpty(hvo.getNastnum()) && MMValueCheck.isEmpty(hvo.getNnumber())) {
			hvo.setNastnum(UFDouble.ONE_DBL);
			hvo.setNnumber(UFDouble.ONE_DBL);
		}
		if (MMValueCheck.isEmpty(hvo.getNastnum()) && MMValueCheck.isNotEmpty(hvo.getNnumber())) {
			hvo.setNastnum(hvo.getNnumber());
		}
		if (MMValueCheck.isNotEmpty(hvo.getNastnum()) && MMValueCheck.isEmpty(hvo.getNnumber())) {
			hvo.setNnumber(hvo.getNastnum());
		}
		if (MMValueCheck.isEmpty(hvo.getBdefault())) {
			hvo.setBdefault(UFBoolean.TRUE);
		}
		if (MMValueCheck.isNotEmpty(materialvid)) {
			try {
				Map<String, MaterialVO> materialMap = UAPAdapter.queryMaterialBaseInfo(new String[] { materialvid });
				String materialname = MultiLangUtil.getSuperVONameOfCurrentLang(materialMap.get(materialvid), MaterialVO.NAME,
						null);
				IMeasesureService mservice = NCLocator.getInstance().lookup(IMeasesureService.class);
				Map<String, MeasureVO> measurevoMap;
				measurevoMap = mservice.getMeasureInfoMap(new String[] { materialvid });
				MeasureVO mvo = measurevoMap.get(materialvid);
				String cassmeasureid = mvo.getCassMeasures();
				String cmeasureid = mvo.getCmeasureid();
				String vchangerate = mvo.getVchangeRate();
				hvo.setCassmeasureid(cassmeasureid);
				hvo.setCmeasureid(cmeasureid);
				hvo.setVchangerate(vchangerate);
				for (RtItemVO itemVo : itemVos) {
					itemVo.setCbpriceassmeasureid(cassmeasureid);
					itemVo.setCbpricemeasureid(cmeasureid);
					itemVo.setVbpricechangerate(vchangerate);
				}
				hvo.setCmaterialname(materialname);
			} catch (BusinessException e) {
				ExceptionUtils.wrappException(e);
			}
		}
		String pk_group = AppContext.getInstance().getPkGroup();
		if (hvo.getPk_group() == null) {
			hvo.setPk_group(pk_group);
		}
		String newVersion = null;
		try {
			if (MMValueCheck.isNotEmpty(pk_group) && MMValueCheck.isNotEmpty(hvo.getPk_org())
					&& MMValueCheck.isNotEmpty(hvo.getCmaterialid()) && MMValueCheck.isNotEmpty(hvo.getCmaterialvid())) {
				newVersion = ProxyService.getIRtQueryService().getRtNewVersionByConWithCustParam(pk_group, hvo.getPk_org(),
						hvo.getCmaterialid(), hvo.getCmaterialvid(), false);
			}
		} catch (BusinessException e) {
			ExceptionUtils.wrappException(e);
		}
		if (newVersion != null) {
			hvo.setNewbomversion(newVersion);
		}
	}
}
