package nccloud.web.hrwa.wagrade.action;

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

import com.alibaba.fastjson.JSONObject;

import nc.itf.hr.wa.IWaGradeQueryService;
import nc.itf.hr.wa.IWaGradeService;
import nc.vo.hrwa.gradeorg.WaGradeOrgVO;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.wa.grade.AggWaGradeVO;
import nc.vo.wa.grade.IWaGradeCommonDef;
import nc.vo.wa.grade.WaGradeVO;
import nc.vo.wa.grade.WaPrmlvVO;
import nc.vo.wa.grade.WaSeclvVO;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.web.hr.pub.HRCommonAction;
import nccloud.web.uapbd.commons.web.ParamUtils;

public class AssignAction extends HRCommonAction {

	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {
		ParamUtils param = new ParamUtils(request);
		String pkWaGrdWeb = param.getString("pk_wa_grd", null);
		String newOrg = param.getString("newOrg", null);
//		boolean val = "".equals(warinMsgBf.toString()) ? true : false;
		
		AggWaGradeVO aggGradeVo = (AggWaGradeVO) ServiceLocator.find(IWaGradeQueryService.class)
				.queryAggWagradeByGrdPK(pkWaGrdWeb);
		
		List<String> list = ServiceLocator.find(IWaGradeQueryService.class).queryWaGradeOrg(pkWaGrdWeb);
		String[] orgs = newOrg.split(",");
		List<WaGradeOrgVO> orgVOList = new ArrayList();
		List<AggWaGradeVO> aggWaGradeVOList = new ArrayList();
		if(orgs != null && orgs.length > 0) {
			for(String pk_org : orgs) {
				if(list.size() > 0) {
					if(!list.contains(pk_org)) {
						WaGradeOrgVO waGradeOrgVO = new WaGradeOrgVO();
						orgVOList.add(this.setWaGradeOrgVO(waGradeOrgVO,pkWaGrdWeb,pk_org));
						aggWaGradeVOList.add(this.copyAggWaGradeVO(aggGradeVo,pk_org));
					}
				} else {
					WaGradeOrgVO waGradeOrgVO = new WaGradeOrgVO();
					orgVOList.add(this.setWaGradeOrgVO(waGradeOrgVO,pkWaGrdWeb,pk_org));
					
					aggWaGradeVOList.add(this.copyAggWaGradeVO(aggGradeVo,pk_org));
				}
			}
		}
		
		List<AggWaGradeVO> returnList = new ArrayList();
		String pk="";
		if(aggWaGradeVOList.size() > 0) {
			for(AggWaGradeVO aggvo : aggWaGradeVOList) {
				AggWaGradeVO returnAggVO = ServiceLocator.find(IWaGradeService.class).insertWaGradeVO(aggvo);
				returnList.add(returnAggVO);
				pk = pk + returnAggVO.getParentVO().getPrimaryKey()+",";
			}
		}
		if(orgVOList.size() > 0) {
			ServiceLocator.find(IWaGradeService.class).insertWaGradeOrgVO(orgVOList.toArray(new WaGradeOrgVO[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 WaGradeOrgVO setWaGradeOrgVO(WaGradeOrgVO waGradeOrgVO,String pk_wa_grade,String pk_org) {
		waGradeOrgVO.setPk_wa_grade(pk_wa_grade);
		waGradeOrgVO.setPk_org(pk_org);
		return waGradeOrgVO;
	}

	public AggWaGradeVO copyAggWaGradeVO(AggWaGradeVO aggGradeVo,String pk_org){
		AggWaGradeVO newAggWaGradeVO = new AggWaGradeVO();
		WaGradeVO waGradeVO = (WaGradeVO) aggGradeVo.getParentVO().clone();
		waGradeVO.setPk_wa_grd(null);
		waGradeVO.setPk_org(pk_org);
		waGradeVO.setIsgroup(UFBoolean.TRUE);
		
		List<WaPrmlvVO> prmlList = new ArrayList();
		List<WaSeclvVO> seclList = new ArrayList();
		
		newAggWaGradeVO.setParentVO(waGradeVO);
		
		WaPrmlvVO[] waPrmlvVOs = (WaPrmlvVO[]) aggGradeVo.getTableVO(IWaGradeCommonDef.WA_PRMLV);
		if(waPrmlvVOs != null && waPrmlvVOs.length > 0) {
			for(WaPrmlvVO vo : waPrmlvVOs) {
				WaPrmlvVO waPrmlvVO = (WaPrmlvVO) vo.clone();
				waPrmlvVO.setPk_wa_grd(null);
				waPrmlvVO.setStatus(2);
				waPrmlvVO.setPk_wa_prmlv(null);
				waPrmlvVO.setIsgroup(UFBoolean.TRUE);
				prmlList.add(waPrmlvVO);
			}
			newAggWaGradeVO.setTableVO(WaPrmlvVO.TABLENAME, prmlList.toArray(new WaPrmlvVO[0]));
		}
		WaSeclvVO[] waSeclvVOs = (WaSeclvVO[]) aggGradeVo.getTableVO(IWaGradeCommonDef.WA_SECLV);
		if(waSeclvVOs != null && waSeclvVOs.length > 0) {
			for(WaSeclvVO vo : waSeclvVOs) {
				WaSeclvVO waSeclvVO = (WaSeclvVO) vo.clone();
				waSeclvVO.setPk_wa_grd(null);
				waSeclvVO.setStatus(2);
				waSeclvVO.setPk_wa_seclv(null);
				seclList.add(waSeclvVO);
			}
			newAggWaGradeVO.setTableVO(WaSeclvVO.TABLENAME, seclList.toArray(new WaSeclvVO[0]));
		}
		return newAggWaGradeVO;
	}
	
}
