﻿/**
 * @desctiption   This file is generated by  code generation tool version 0.2 ^_^
 * @Created   2017-02-10
**/
package com.yh.hr.info.ver.unit.comm.web.action;

import com.yh.hr.info.ver.unit.person.dto.VerPbPersonInfoDTO;
import jade.workflow.utils.ObjectUtil;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.yh.component.dictionary.utils.DicHelper;
import com.yh.component.taglib.TableTagBean;
import com.yh.hr.info.ver.unit.comm.facade.PbYnGwEmployInfoFacade;
import com.yh.hr.info.ver.unit.comm.web.form.PbYnGwEmployInfoForm;
import com.yh.hr.info.ver.unit.person.facade.VerPbPersonInfoFacade;
import com.yh.hr.res.pb.dto.PbYnGwEmployInfoDTO;
import com.yh.platform.core.exception.ServiceException;
import com.yh.platform.core.util.BeanHelper;
import com.yh.platform.core.util.DateUtil;
import com.yh.platform.core.util.JSONHelper;
import com.yh.platform.core.util.NumberUtils;
import com.yh.platform.core.util.SpringBeanUtil;
import com.yh.platform.core.util.StringUtil;
import com.yh.platform.core.web.UserContext;
import com.yh.platform.core.web.action.BaseAction;

public class PbYnGwEmployInfoAction extends BaseAction {
	private PbYnGwEmployInfoFacade pbYnGwEmployInfoFacade = (PbYnGwEmployInfoFacade) SpringBeanUtil.getBean("pbYnGwEmployInfoFacade");
	private VerPbPersonInfoFacade	verPbPersonInfoFacade	= (VerPbPersonInfoFacade) SpringBeanUtil.getBean("verPbPersonInfoFacade");

	/**
	 * 列出所有 PbYnGwEmployInfo信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward listPbYnGwEmployInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		TableTagBean ttb = TableTagBean.getFromRequest(request);
		if (request.getMethod().equals("POST")) {
	        @SuppressWarnings("rawtypes")
			Map map = BeanUtils.describe(form);
	        ttb.getCondition().copyFrom(map, true);
	    } else {
	        BeanUtils.populate(form, ttb.getCondition());
	        request.setAttribute("pbYnGwEmployInfoForm", form);
	    }
        try {
        	List<PbYnGwEmployInfoDTO> list = pbYnGwEmployInfoFacade.find(ttb);
        	List<PbYnGwEmployInfoDTO> showList = new ArrayList<PbYnGwEmployInfoDTO>();
        	if(CollectionUtils.isNotEmpty(list))
			{
				for(PbYnGwEmployInfoDTO dto : list)
				{
					PbYnGwEmployInfoDTO showDto = new PbYnGwEmployInfoDTO();
					BeanHelper.copyProperties(dto, showDto);
					if(null != dto.getHisBeginDate())
					{
						showDto.setHisBeginDateStr(DateUtil.format(dto.getHisBeginDate(), "yyyy-MM-dd"));
					}
					showList.add(showDto);
				}
			}
	        request.setAttribute("list", showList);
        } catch (Exception se) {
			this.handleException(request, se, null);
			return mapping.findForward("fail");
		}
        return mapping.findForward("success");
	}
	
	/**
	 * 查找单个PbYnGwEmployInfo信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward show(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String ynGwEmployOid = request.getParameter("ynGwEmployOid");
        if (StringUtil.isBlank(ynGwEmployOid)) {
            ynGwEmployOid = ObjectUtil.getValue(request.getAttribute("ynGwEmployOid"), String.class);
        }
		try {
            if (StringUtils.isEmpty(ynGwEmployOid)) {
				throw new ServiceException("error.pk.invalid", "pbYnGwEmployInfo ynGwEmployOid is null");
			}
            PbYnGwEmployInfoDTO pbYnGwEmployInfoDto = pbYnGwEmployInfoFacade.get(ObjectUtil.getValue(ynGwEmployOid, java.lang.Long.class));
			if (null == pbYnGwEmployInfoDto) {
				throw new ServiceException(null, "查询不到相关信息");
			}
			PbYnGwEmployInfoForm pbYnGwEmployInfoForm = BeanHelper.copyProperties(pbYnGwEmployInfoDto, PbYnGwEmployInfoForm.class);
			if(null != pbYnGwEmployInfoForm.getHisBeginDate())
			{
				pbYnGwEmployInfoForm.setHisBeginDateStr(DateUtil.format(pbYnGwEmployInfoForm.getHisBeginDate(), "yyyy-MM-dd"));
			}if(null != pbYnGwEmployInfoForm.getEndDate())
			{
				pbYnGwEmployInfoForm.setEndDateStr(DateUtil.format(pbYnGwEmployInfoForm.getEndDate(), "yyyy-MM-dd"));
			}
			if(null != pbYnGwEmployInfoForm.getEndDateActual())
			{
				pbYnGwEmployInfoForm.setEndDateActualStr(DateUtil.format(pbYnGwEmployInfoForm.getEndDateActual(), "yyyy-MM-dd"));
			}
			
			request.setAttribute("pbYnGwEmployInfoForm", pbYnGwEmployInfoForm);
            request.setAttribute("ynGwEmployOid", ynGwEmployOid);
		} catch (Exception se) {
			this.handleException(request, se, "ynGwEmployOid=" + ynGwEmployOid);
			return mapping.getInputForward();
		}
		return mapping.findForward("success");
	}

	/**
	 * 跳转到 PbYnGwEmployInfo信息添加页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward goCreate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//参数校验，可根据实际情况修改
		PbYnGwEmployInfoForm pbYnGwEmployInfoForm = (PbYnGwEmployInfoForm)form;
		String urlId = request.getParameter("urlId");
		String personOid = request.getParameter("personOid");
		try
		{
			if(StringUtils.isEmpty(urlId)){
				throw new ServiceException("urlId is null");
			}
			if(StringUtils.isEmpty(personOid)){
				throw new ServiceException("personOid is null");
			}
			
			VerPbPersonInfoDTO verSyPersonDTO = verPbPersonInfoFacade.getVerPersonInfoById(NumberUtils.longValue(personOid));
			if(null != verSyPersonDTO){
				pbYnGwEmployInfoForm.setUnitOid(verSyPersonDTO.getUnitOid());
			}
			request.setAttribute("personOid", personOid);
			request.setAttribute("urlId", urlId);
			request.setAttribute("pbYnGwEmployInfoForm", pbYnGwEmployInfoForm);
		}
		catch(Exception se)
		{
			this.handleException(request, se, null);
			return mapping.getInputForward();
		}
		return mapping.findForward(FORWARD_SUCCESS);
		
	}

	/**
	 * 添加 PbYnGwEmployInfo信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (this.isCancelled(request)) {
			return mapping.findForward("cancel");
		}
		PbYnGwEmployInfoForm pbYnGwEmployInfoForm = (PbYnGwEmployInfoForm) form;
		try {
//			if(StringUtils.isEmpty(${fkOid}))
//			{
//				throw new ServiceException("${fkOid} is null");
//			}
			PbYnGwEmployInfoDTO pbYnGwEmployInfoDto = BeanHelper.copyProperties(pbYnGwEmployInfoForm, PbYnGwEmployInfoDTO.class);
//   gg 	pbYnGwEmployInfoDto.setPersonOid(NumberUtils.longValue(personOid));
			if(null==pbYnGwEmployInfoDto.getPersonOid()){
				response.getWriter().write(JSONHelper.fromObject(false, StringUtils.defaultIfEmpty( null,"获取的人员Id为null！请刷新重试或联系开发人员！")).toString());
				return null;
			}else if(null==pbYnGwEmployInfoDto.getHisPositionStatus()){
				response.getWriter().write(JSONHelper.fromObject(false, StringUtils.defaultIfEmpty( null,"获取的岗位聘任状态为空！请刷新重试或联系开发人员！")).toString());
				return null;
			}
			Boolean statusBoolean = pbYnGwEmployInfoFacade.checkStatus(pbYnGwEmployInfoDto);
			if(!statusBoolean){
				response.getWriter().write(JSONHelper.fromObject(false, StringUtils.defaultIfEmpty( null,"在任(聘)的院内岗位信息只能有一笔！")).toString());
				 return null;
			}else{
				pbYnGwEmployInfoDto.setCreateBy(UserContext.getLoginUserID());
				pbYnGwEmployInfoDto.setCreateName(UserContext.getLoginUserName());
				pbYnGwEmployInfoDto.setCreateDate(DateUtil.now());
				pbYnGwEmployInfoFacade.create(pbYnGwEmployInfoDto);
				response.getWriter().write(JSONHelper.fromObject(true, null).toString());
			}
		} catch (Exception se) {
			this.handleException(request, se, null);
			response.getWriter().write(JSONHelper.fromObject(false, StringUtils.defaultIfEmpty(se.getMessage(), "修改失败")).toString());
		}

		return null;
}


	/**
	 * 跳转到 PbYnGwEmployInfo信息修改（编辑）页面前的查询回显
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward goUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(this.isCancelled(request)) {
			return mapping.findForward("cancel");
		}

        String ynGwEmployOid = request.getParameter("ynGwEmployOid");
       
		try {
            if (StringUtils.isBlank(ynGwEmployOid)) {
				throw new ServiceException("error.pk.invalid", "pbYnGwEmployInfo ynGwEmployOid is null");
			}
            PbYnGwEmployInfoDTO pbYnGwEmployInfoDto = pbYnGwEmployInfoFacade.get(ObjectUtil.getValue(ynGwEmployOid, java.lang.Long.class));
			if (null == pbYnGwEmployInfoDto) {
				throw new ServiceException("error.object.notfound", "pbYnGwEmployInfo not found by ynGwEmployOid");
			}
			PbYnGwEmployInfoForm pbYnGwEmployInfoForm = BeanHelper.copyProperties(pbYnGwEmployInfoDto, PbYnGwEmployInfoForm.class);
			if(StringUtils.isNotEmpty(pbYnGwEmployInfoForm.getHisPositionType())){
				pbYnGwEmployInfoForm.setHisPositionTypeName(DicHelper.viewName("YHRS0113", pbYnGwEmployInfoForm.getHisPositionType()));
			}
			if(StringUtils.isNotEmpty(pbYnGwEmployInfoForm.getHisPositionLevel())){
				pbYnGwEmployInfoForm.setHisPositionLevelName(DicHelper.viewName("YHRS0124", pbYnGwEmployInfoForm.getHisPositionType()));
			}
			if(StringUtils.isNotEmpty(pbYnGwEmployInfoForm.getHisWorkType())){
				pbYnGwEmployInfoForm.setHisWorkTypeName(DicHelper.viewName("YHRS0112", pbYnGwEmployInfoForm.getHisWorkType()));
			}
			if(null != pbYnGwEmployInfoForm.getHisBeginDate())
			{
				pbYnGwEmployInfoForm.setHisBeginDateStr(DateUtil.format(pbYnGwEmployInfoForm.getHisBeginDate(), "yyyy-MM-dd"));
			}
			if(null != pbYnGwEmployInfoForm.getEndDate())
			{
				pbYnGwEmployInfoForm.setEndDateStr(DateUtil.format(pbYnGwEmployInfoForm.getEndDate(), "yyyy-MM-dd"));
			}
			if(null != pbYnGwEmployInfoForm.getEndDateActual())
			{
				pbYnGwEmployInfoForm.setEndDateActualStr(DateUtil.format(pbYnGwEmployInfoForm.getEndDateActual(), "yyyy-MM-dd"));
			}
			VerPbPersonInfoDTO verSyPersonDTO = verPbPersonInfoFacade.getVerPersonInfoById(pbYnGwEmployInfoDto.getPersonOid());
			if(null != verSyPersonDTO){
				pbYnGwEmployInfoForm.setUnitOid(verSyPersonDTO.getUnitOid());
			}
			request.setAttribute("pbYnGwEmployInfoForm", pbYnGwEmployInfoForm);
            request.setAttribute("ynGwEmployOid", ynGwEmployOid);
            request.setAttribute("urlId", request.getParameter("urlId"));
		} catch (Exception se) {
			this.handleException(request, se, "ynGwEmployOid=" + ynGwEmployOid);
			return mapping.getInputForward();
		}
		return goUpdateView(mapping, form, request, response);
	}

	/**
	 * 跳转到 PbYnGwEmployInfo信息修改（编辑）页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
    public ActionForward goUpdateView(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //add jsp init values
        @SuppressWarnings("unused")
		PbYnGwEmployInfoForm pbYnGwEmployInfoForm = (PbYnGwEmployInfoForm) form;
        return mapping.findForward("success");
    }

	/**
	 * 更新 PbYnGwEmployInfo信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(this.isCancelled(request)) {
			return mapping.findForward("cancel");
		}
        	//获得岗位聘任状态是否更改过的标识符
        String statusFlag = request.getParameter("statusFlag");
        Boolean statusBoolean = true;
		PbYnGwEmployInfoForm pbYnGwEmployInfoForm = (PbYnGwEmployInfoForm) form;
		try {
			PbYnGwEmployInfoDTO pbYnGwEmployInfoDto = new PbYnGwEmployInfoDTO();
			BeanHelper.copyProperties(pbYnGwEmployInfoForm, pbYnGwEmployInfoDto);
			if(null==pbYnGwEmployInfoDto.getPersonOid()){
				response.getWriter().write(JSONHelper.fromObject(false, StringUtils.defaultIfEmpty( null,"获取的人员Id为null！请刷新重试或联系开发人员！")).toString());
				return null;
			}else if(null==pbYnGwEmployInfoDto.getHisPositionStatus()){
				response.getWriter().write(JSONHelper.fromObject(false, StringUtils.defaultIfEmpty( null,"获取的岗位聘任状态为空！请刷新重试或联系开发人员！")).toString());
				return null;
			}
			//判断是否需要验证唯一性  
			//true为修改过，false为未修改
			
			if(StringUtils.isNotEmpty(statusFlag)&&statusFlag.equals("true")){
				statusBoolean = pbYnGwEmployInfoFacade.checkStatus(pbYnGwEmployInfoDto);
			}else if(StringUtils.isNotEmpty(statusFlag)&&statusFlag.equals("false")){
			}else if(StringUtils.isNotEmpty(statusFlag)&&statusFlag.equals("false")){
				
			}else{
				response.getWriter().write(JSONHelper.fromObject(false, StringUtils.defaultIfEmpty( null,"无法获得岗位聘任状态是否更改过，请刷新重试或联系开发人员！")).toString());
				 return null;
			}
			
			
			if(!statusBoolean){
				response.getWriter().write(JSONHelper.fromObject(false, StringUtils.defaultIfEmpty( null,"在任(聘)的院内岗位聘任信息只能有一笔！")).toString());
				 return null;
			}else{
				pbYnGwEmployInfoDto.setUpdateBy(UserContext.getLoginUserID());
				pbYnGwEmployInfoDto.setUpdateName(UserContext.getLoginUserName());
				pbYnGwEmployInfoDto.setUpdateDate(DateUtil.now());
				pbYnGwEmployInfoFacade.update(pbYnGwEmployInfoDto);
				response.getWriter().write(JSONHelper.fromObject(true, null).toString());
			}
		} catch (Exception se) {
			this.handleException(request, se, pbYnGwEmployInfoForm);
			response.getWriter().write(JSONHelper.fromObject(false, StringUtils.defaultIfEmpty(se.getMessage(), "修改失败")).toString());
		}

		return null;
	}



	/**
	 * 删除 PbYnGwEmployInfo信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
           String ynGwEmployOid = request.getParameter("ynGwEmployOid");
        try {
           if(StringUtils.isEmpty(ynGwEmployOid))
			{
				throw new ServiceException(null, "ynGwEmployOid is null");
			}
            pbYnGwEmployInfoFacade.delete(ObjectUtil.getValue(ynGwEmployOid, java.lang.Long.class));
            response.getWriter().write(JSONHelper.fromObject(true, null).toString());
        } catch (Exception se) {
            this.handleException(request, se, "ynGwEmployOid=" + ynGwEmployOid);
            return mapping.getInputForward();
        }
        return null;
    }
}
