package nc.vo.ic.pub.rule;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.ic.person.WHManagerAndDeptSetter;
import nc.bs.ic.pub.base.ICRule;
import nc.itf.scmpub.reference.uap.bd.psn.PsndocPubService;
import nc.itf.scmpub.reference.uap.org.DeptPubService;
import nc.itf.scmpub.reference.uap.org.OrgUnitPubService;
import nc.itf.scmpub.reference.uap.org.StockOrgPubService;
import nc.itf.scmpub.reference.uap.rbac.UserManageQuery;
import nc.vo.bd.psn.PsndocVO;
import nc.vo.ic.pub.define.ICPubMetaNameConst;
import nc.vo.ic.pub.util.StringUtil;
import nc.vo.ic.pub.util.ValueCheckUtil;
import nc.vo.pubapp.AppContext;
import nc.vo.pubapp.pattern.model.entity.bill.AbstractBill;
import nc.vo.pubapp.pattern.pub.PubAppTool;

/**
 * @@description
 * @author lanli6
 * @date 2023年6月11日
 * @version ncc1.0
 * @param <E>
 */
public class ICOrgFillRule<E extends AbstractBill> extends ICRule<E> {

	/**
	 * 
	 */

	@Override
	public void process(E[] vos) {
		// 填充组织，人员，部门和日期
		setorgrelation(vos);

		setCbizDept(vos);

		// 设置库管员
		setCwhsmanagerid(vos);
	}

	/**
	 * 设置默认库管员
	 * 
	 * @param vos
	 */
	private void setCwhsmanagerid(E[] vos) {
		String key = InvocationInfoProxy.getInstance().getUserId();
		PsndocVO psnvo = WHManagerAndDeptSetter.queryPsndocByUserid(key);
		boolean powerEnable = WHManagerAndDeptSetter.isPowerEnable(key, psnvo);
		for (E vo : vos) {
			String cwhsmanagerid = (String) vo.getParent().getAttributeValue(ICPubMetaNameConst.CWHSMANAGERID);
			if (!PubAppTool.isNull(cwhsmanagerid)) {
				continue;
			}
			String pk_org = (String) vo.getParent().getAttributeValue(ICPubMetaNameConst.PK_ORG);
			if (null != psnvo && powerEnable && null != pk_org && StringUtil.isStringEqual(pk_org, psnvo.getPk_org())) {
				String pk_psndoc = psnvo.getPk_psndoc();
				vo.getParent().setAttributeValue(ICPubMetaNameConst.CWHSMANAGERID, pk_psndoc);
			}
		}
	}

	/**
	 * 设置默认人员部门
	 */
	private void setCbizDept(E[] vos) {
		Set<String> setcbiz = new HashSet<String>();
		for (E vo : vos) {
			String cbizid = (String) vo.getParentVO().getAttributeValue(ICPubMetaNameConst.CBIZID);
			if (PubAppTool.isNull(cbizid)) {
				cbizid = UserManageQuery.queryPsndocByUserid(AppContext.getInstance().getPkUser());
			}
			if (cbizid != null) {
				vo.getParentVO().setAttributeValue(ICPubMetaNameConst.CBIZID, cbizid);
				setcbiz.add(cbizid);
			}
		}
		if (null == setcbiz || setcbiz.size() <= 0) {
			return;
		}
		// 查询部门
		Map<String, List<String>> deptMap = PsndocPubService
				.queryDeptIDByPsndocIDs(setcbiz.toArray(new String[setcbiz.size()]));
		if (null != deptMap && deptMap.size() > 0) {
			Set<String> vids = new HashSet<String>();
			for (String cbi : deptMap.keySet()) {
				List<String> deptids = deptMap.get(cbi);
				if (!ValueCheckUtil.isNullORZeroLength(deptids)) {
					vids.addAll(deptids);
				}
			}
			Map<String, String> deptvidMap = new HashMap<>();
			if (!ValueCheckUtil.isNullORZeroLength(vids)) {
				deptvidMap = DeptPubService.getLastVIDSByDeptIDS(vids.toArray(new String[vids.size()]));
			}
			for (E vo : vos) {
				String cbizid = (String) vo.getParentVO().getAttributeValue(ICPubMetaNameConst.CBIZID);
				// zhangxinah 如果传了值了，就不覆盖了
				if (vo.getParentVO().getAttributeValue(ICPubMetaNameConst.CDPTVID) == null) {
					vo.getParentVO().setAttributeValue(ICPubMetaNameConst.CDPTID, deptMap.get(cbizid).get(0));
					vo.getParentVO().setAttributeValue(ICPubMetaNameConst.CDPTVID,
							deptvidMap.get(deptMap.get(cbizid).get(0)));
				}
			}
		}
	}

	/**
	 * 根据销售组织old设置vid,补充公司CORPOID
	 */
	private void setorgrelation(E[] vos) {
		Set<String> orgset = new HashSet<>();
		for (E vo : vos) {
			String pk_org = (String) vo.getParent().getAttributeValue(ICPubMetaNameConst.PK_ORG);
			if (null != pk_org) {
				orgset.add(pk_org);
			}
		}
		if (null == orgset || orgset.size() <= 0) {
			return;
		}
		Map<String, String> orgvidmap = OrgUnitPubService.getOrgVid(orgset.toArray(new String[orgset.size()]));
		Map<String, String> orgCorpmap = StockOrgPubService
				.queryCorpIDByStockOrgIDs(orgset.toArray(new String[orgset.size()]));
		Map<String, String> cropvidmap = new HashMap<String, String>();
		if (null != orgCorpmap && orgCorpmap.size() > 0) {
			cropvidmap = OrgUnitPubService.getOrgVid(orgCorpmap.keySet().toArray(new String[orgCorpmap.size()]));
		}
		for (E vo : vos) {
			String pk_org = (String) vo.getParent().getAttributeValue(ICPubMetaNameConst.PK_ORG);
			String pk_org_v = (String) vo.getParent().getAttributeValue(ICPubMetaNameConst.PK_ORG_V);
			// 设置pk_org_v
			if (null != pk_org && null == pk_org_v) {
				vo.getParentVO().setAttributeValue(ICPubMetaNameConst.PK_ORG, pk_org);
				vo.getParentVO().setAttributeValue(ICPubMetaNameConst.PK_ORG_V, orgvidmap.get(pk_org));
			}
			// 设置公司
			if (null != orgCorpmap && orgCorpmap.size() > 0) {
				vo.getParentVO().setAttributeValue(ICPubMetaNameConst.CORPOID, orgCorpmap.get(pk_org));
				vo.getParentVO().setAttributeValue(ICPubMetaNameConst.CORPVID, cropvidmap.get(orgCorpmap.get(pk_org)));
			}
		}

	}

}
