package nccloud.web.hrcm.contopinion.action;

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

import nc.hr.utils.ResHelper;
import nc.itf.hrcm.IContopinionManageService;
import nc.pub.tools.HRCMCommonValue;
import nc.vo.hrcm.contopinion.ContopinionVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.BusinessRuntimeException;
import nc.vo.uif2.LoginContext;
import nccloud.commons.lang.ArrayUtils;
import nccloud.commons.lang.StringUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.web.hr.pub.HRCommonAction;
import nccloud.web.hr.pub.HRNccTemplateUtils;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.uapbd.commons.web.ParamUtils;

/**
 * 合同续签意见征询【发送续签征询消息】操作
 * @author tianxfc
 *
 */
public class OpinionRemcSendMsgAction extends HRCommonAction{
	
	private Integer	icntsucc = 0;	// 成功发送的消息数
	
	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {
		ParamUtils param = new ParamUtils(request);
		LoginContext context = HRNccUtils.getLoginContext(param);
		
		String pk_org = param.getString("pk_org", null);
		if(StringUtils.isEmpty(pk_org)){
			throw new BusinessRuntimeException(ResHelper.getString("6001uif2", "06001uif20070")/* @res 请先选择人力资源组织！ */);
		}
		// 是否发送标识，第一次调用接口传N，第二次调用接口传Y
		String sendCheckStr = param.getString("sendCheck", "Y");
        boolean sendCheck = "Y".equals(sendCheckStr);
		ContopinionVO[] selDatas = HRNccTemplateUtils.getVOFromGrid(ContopinionVO.class, OpinionCommon.getModelStr(param, "contopiniongrid"));
		if(ArrayUtils.isEmpty(selDatas)){
			throw new BusinessRuntimeException(ResHelper.getString("6011opin", "06011opin0025")/* @res"请选择要操作的记录！" */);
		}
		return sendContOpinion(selDatas, context, sendCheck);
	}
	
	private String sendContOpinion(ContopinionVO[] contopinionVOs, LoginContext context, boolean sendCheck) throws Exception {
		/**未征询或者未反馈续签意见*/
		ArrayList<ContopinionVO> vtemp = new ArrayList<ContopinionVO>();
		/**反馈中的续签意见*/
		ArrayList<ContopinionVO> inFeedbackList = new ArrayList<ContopinionVO>();
		for (int i = 0; i < contopinionVOs.length; i++) {
			// conttype: 处理结果: 0=未征询，1=未反馈，2=反馈中，3=已反馈，4=已续签，5=已终止
			if (contopinionVOs[i].getConttype().equals(HRCMCommonValue.STATE_NOCONSULT) || contopinionVOs[i].getConttype().equals(HRCMCommonValue.STATE_NOFEEDBACK)) {
				vtemp.add(contopinionVOs[i]);
			}
			//modify by@chenzf 续签意见征询员工续签期限 2021-1-11 17:21:49 begin
//			if(contopinionVOs[i].getConttype().equals(HRCMCommonValue.STATE_INFEEDBACK)){
//				inFeedbackList.add(contopinionVOs[i]);
//			}
			if(contopinionVOs[i].getConttype().equals(HRCMCommonValue.STATE_INFEEDBACK) || contopinionVOs[i].getConttype().equals(HRCMCommonValue.STATE_NOCONSULT) || contopinionVOs[i].getConttype().equals(HRCMCommonValue.STATE_NOFEEDBACK)){
				inFeedbackList.add(contopinionVOs[i]);
			}
			//modify by@chenzf 续签意见征询员工续签期限 2021-1-11 17:21:49 end
		}
		if (vtemp.size() < 1 && inFeedbackList.size() < 1) {
			throw new BusinessException(ResHelper.getString("6011opin", "06011opin0008")/* @res"只有未反馈、未征询、反馈中的人员才能发送通知！" */);
		}
		// 校验是否存在符合征询条件的记录
		HashMap<String, Object> map = ServiceLocator.find(IContopinionManageService.class).getPsnjobDeptMap(vtemp.toArray(new ContopinionVO[0]));
		String[] deptPKs = (String[]) map.get("pks");

		String msg=null;
		if(inFeedbackList.size()<1){
			msg = (String) map.get("msg");
			//当反馈中的数据为0，包含未反馈的数据，并且未征询和未反馈的都没有部门负责人时，不能征询意见
			if(ArrayUtils.isEmpty(deptPKs)){
				throw new BusinessException(ResHelper.getString("6011opin", "06011opin0019"));
			}
		}else if(ArrayUtils.isEmpty(deptPKs) || deptPKs.length < 1){
			//只有反馈中
			/*"您选中的部分人员的处理状态为“反馈中”，系统将给符合条件员工发送征询意见催办通知，确认发送吗？"*/;
			msg = ResHelper.getString("6011opin", "06011opin0060");
		}else{
			//同时包含反馈中和未反馈
			/*"您选中的人员的处理状态包含“未反馈”和“反馈中”，系统将给符合条件人员的部门负责人或员工发送征询意见通知，确认发送吗？"*/
			msg = ResHelper.getString("6011opin", "06011opin0061");
		}
		
		if(msg != null) {
//			msg = "您好！您与公司的劳动合同即将到期，您满足签订无固定期限劳动合同的条件，请您反馈您的意向劳动合同期限类型。无固定期限/固定期限";
		}
		
		// 是否发送标识，第一次调用接口false，第二次调用接口true
		if(sendCheck){
			return ResHelper.getString("6011opin", "06011opin0057")/* @res "确认发送" */ + "\n" + msg;
//			return msg;
		}
		ContopinionVO[] vos = (ContopinionVO[]) map.get("vos");
		HashMap<String, List<ContopinionVO>> contopMap = new HashMap<String, List<ContopinionVO>>();
		HashMap<String, List<String>> psndeptMap = new HashMap<String, List<String>>();
		if(!ArrayUtils.isEmpty(deptPKs)){			
			for(int i = 0; i < deptPKs.length; i++){// 一个部门生成一个对应的通知
				List<ContopinionVO> depttemp = new ArrayList<ContopinionVO>();
				List<String> psndocList = new ArrayList<String>();
				for(ContopinionVO vo : vos){
					if(vo.getPk_dept().equals(deptPKs[i])){
						depttemp.add(vo);
						psndocList.add(vo.getPk_psnjob());
					}
				}
				contopMap.put(deptPKs[i], depttemp);
				psndeptMap.put(deptPKs[i], psndocList);
			}
		}

		//modify by@chenzf 续签意见征询员工续签期限 2021-1-11 17:21:49 begin
//		HashMap<String, Object> sendMsgMap = ServiceLocator.find(IContopinionManageService.class)
//				.sendNoticeToDeptManager(psndeptMap, contopMap, context.getPk_group(), context.getPk_org(), vos, inFeedbackList);
		HashMap<String, Object> sendMsgMap = ServiceLocator.find(IContopinionManageService.class)
				.sendNoticeToPsn(psndeptMap, contopMap, context.getPk_group(), context.getPk_org(), vos, inFeedbackList);
		//modify by@chenzf 续签意见征询员工续签期限 2021-1-11 17:21:49 end
		icntsucc = (Integer) sendMsgMap.get("cnt");
		if(icntsucc > 0){
			// 提示发送成功信息
			return ResHelper.getString("6011opin", "06011opin0010"/* @res "发送消息成功,共发送消息[{0}]条。" */, icntsucc.toString());
		}else{
			// 如果发送不成功，提示错误信息
			throw new BusinessException(ResHelper.getString("6011opin", "06011opin0011")/* @res "发送消息失败！" */);
		}
	}
	
}
