package nccloud.web.hrwa.waruleex.action;

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

import com.alibaba.fastjson.JSONObject;

import nc.itf.hr.wa.IWaGradeService;
import nc.itf.hr.wa.IWaRuleExQueryService;
import nc.itf.hr.wa.IWaRuleExService;
import nc.vo.hrwa.gradeorg.WaGradeOrgVO;
import nc.vo.hrwa.waruleex.AggWageformExVO;
import nc.vo.hrwa.waruleex.WageformExVO;
import nc.vo.hrwa.waruleex.WageformdetExVO;
import nc.vo.hrwa.waruleexorg.WageformExOrgVO;
import nc.vo.uif2.LoginContext;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.web.hr.pub.HRCommonAction;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.uapbd.commons.web.ParamUtils;

public class WaRuleExAssignAction extends HRCommonAction{
	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {
		ParamUtils param = new ParamUtils(request);
		LoginContext context = HRNccUtils.getLoginContext(param);
		String pkWaWageformex = param.getString("pk_wa_wageformex", null);
		String newOrg = param.getString("newOrg", null);
		WaRuleExCommon common = new WaRuleExCommon();
		AggWageformExVO aggWageformExVO = common.listHandleData(context, pkWaWageformex);
		
		//查询已经分配的组织信息
		List<String> list = ServiceLocator.find(IWaRuleExQueryService.class).queryOrgsByPkWageformEx(pkWaWageformex);
		
		String[] orgs = newOrg.split(",");
		//分配给组织aggvo数据
		List<AggWageformExVO> aggWageformExList = new ArrayList();
		//分配组织信息
		List<WageformExOrgVO> orgVOList = new ArrayList();
		if(orgs != null && orgs.length > 0) {
			for(String pk_org : orgs) {
				if(list != null && list.size() > 0) {
					if(!list.contains(pk_org)) {
						WageformExOrgVO wageformExOrgVO = new WageformExOrgVO();
						orgVOList.add(this.setWageformExOrgVO(wageformExOrgVO,pkWaWageformex,pk_org));
//						aggWageformExList.add(this.copyAggWageformExVO(aggWageformExVO,pk_org));
					}
				} else {
					WageformExOrgVO wageformExOrgVO = new WageformExOrgVO();
					orgVOList.add(this.setWageformExOrgVO(wageformExOrgVO,pkWaWageformex,pk_org));
//					aggWageformExList.add(this.copyAggWageformExVO(aggWageformExVO,pk_org));
				}
			}
		}
		
		List<AggWageformExVO> returnList = new ArrayList();
		String pk="";
//		if(aggWageformExList.size() > 0) {
//			for(AggWageformExVO aggvo : aggWageformExList) {
//				AggWageformExVO returnAggVO = ServiceLocator.find(IWaRuleExService.class).insertVO(aggvo);
//				returnList.add(returnAggVO);
//				pk = pk + returnAggVO.getParentVO().getPrimaryKey()+",";
//			}
//		}
		if(orgVOList.size() > 0) {
			ServiceLocator.find(IWaRuleExService.class).instertWageformExOrgVO(orgVOList.toArray(new WageformExOrgVO[0]));
		}
		
		
		boolean val = true;
		String msg = "";
		if(returnList.size() > 0) {
			pk = pk.substring(0,pk.length() - 1);
		} else {
			val = false;
			msg = "操作失败，请检查！";
		}
		JSONObject json = new JSONObject();
		json.put("message", msg);
		json.put("success", val);
		json.put("pk", pk);
		return json;
	}
	
	public WageformExOrgVO setWageformExOrgVO(WageformExOrgVO wageformExOrgVO,String pkWaWageformex,String pk_org) {
		wageformExOrgVO.setPk_wa_wageformex(pkWaWageformex);
		wageformExOrgVO.setPk_org(pk_org);
		return wageformExOrgVO;
	}
	
	
	public AggWageformExVO copyAggWageformExVO(AggWageformExVO aggWageformExVO,String pk_org){
		AggWageformExVO newAggVO = new AggWageformExVO();
		WageformExVO wageformExVO = (WageformExVO) aggWageformExVO.getParentVO().clone();
		WageformdetExVO[] vos = (WageformdetExVO[]) aggWageformExVO.getChildrenVO();
		List<WageformdetExVO> voList = new ArrayList();
		for(int i = 0; i < vos.length; i++) {
			WageformdetExVO wageformdetExVO = new WageformdetExVO();
			wageformdetExVO = (WageformdetExVO) vos[i].clone();
			wageformdetExVO.setPk_wa_wageformdetex(null);
			wageformdetExVO.setPk_wa_wageformex(null);
			wageformdetExVO.setStatus(2);
			voList.add(wageformdetExVO);
		}
		
		
		wageformExVO.setPk_wa_wageformex(null);
		wageformExVO.setPk_org(pk_org);
		newAggVO.setParentVO(wageformExVO);
		newAggVO.setChildrenVO(voList.toArray(new WageformdetExVO[0]));
		return newAggVO;
	}

}
