
package nccloud.hrkq.hrkq.monthottimevo.hrkq.util;


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

import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.pubitf.org.cache.IOrgUnitPubService_C;
import nc.vo.hrkq.monthottime.AggMonthOtTimeVO;
import nc.vo.hrkq.monthottime.MonthOtTimeBVO;
import nc.vo.org.OrgVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDateTime;
import nccloud.framework.core.model.entity.AbstractBill;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.SessionContext;
import nccloud.itf.hrkq.hrkq.IMonthottimevoMaintain;
import nccloud.pubitf.riart.pflow.ICloudScriptPFlowService;
import nccloud.vo.hrkq.hrkq.MonthOtTimeVOConst;

public class CommonUtil {

  private static IMonthottimevoMaintain maintain = null;

  private static IMDPersistenceQueryService persistenceQueryService = null;

  private static ICloudScriptPFlowService PFlowService = null;

  /**
   * 单据类型编码
   *
   * @return
   */
  public static String getBillTypeCode() {
    return MonthOtTimeVOConst.CONST_BILLTYPE_COST;
  }


  /**
   * 获取当前业务时间
   *
   * @return
   */
  public static UFDate getBusiDate() {
    return new UFDate(SessionContext.getInstance().getClientInfo()
        .getBizDateTime());
  }

  /**
   * 获取当前业务日期时间
   *
   * @return
   */
  public static UFDateTime getBusiDateTime() {
    return new UFDateTime(SessionContext.getInstance().getClientInfo()
        .getBizDateTime());
  }

  /**
   * 平台脚本服务类
   *
   * @return
   */
  public static synchronized ICloudScriptPFlowService getCPFlowService() {
    if (CommonUtil.PFlowService == null) {
      CommonUtil.PFlowService =
          ServiceLocator.find(ICloudScriptPFlowService.class);
    }
    return CommonUtil.PFlowService;
  }

  /**
   * 管理服务
   *
   * @return
   */
  public static IMonthottimevoMaintain getMaintainService() {
    if (CommonUtil.maintain == null) {
      CommonUtil.maintain = ServiceLocator.find(IMonthottimevoMaintain.class);
    }
    return CommonUtil.maintain;
  }

  /**
   * 元数据查询服务
   *
   * @return
   */
  public static IMDPersistenceQueryService getMDQueryService() {
    if (CommonUtil.persistenceQueryService == null) {
      CommonUtil.persistenceQueryService =
          ServiceLocator.find(IMDPersistenceQueryService.class);
    }
    return CommonUtil.persistenceQueryService;
  }


  /**
   * 查询财务组织所在集团
   *
   * @param pk_org
   * @return
   * @throws BusinessException
   */
  public static String getGroupByOrg(String pk_org) throws BusinessException {
    IOrgUnitPubService_C orgUnitQryService =
        ServiceLocator.find(IOrgUnitPubService_C.class);
    OrgVO[] orgVOs = orgUnitQryService.getOrgs(new String[] {
      pk_org
    }, new String[] {
      OrgVO.PK_GROUP
    });
    if ((orgVOs == null) || (orgVOs.length <= 0)) {
      return null;
    }
    return (String) orgVOs[0].getAttributeValue(OrgVO.PK_GROUP);
  }

  /**
   * 后台VO赋值前台的时间戳
   *
   * @param pk_org
   * @return
   */
  public static void setBillsTs(Map<String, String> pkMapTs,
      AggMonthOtTimeVO[] operaVOs) {
    if (pkMapTs == null) {
      return;
    }
    for (AggMonthOtTimeVO bill : operaVOs) {
      String ts = pkMapTs.get(bill.getParent().getPrimaryKey());
      if ((ts == null) || ts.equals("")) {
        break;
      }
      bill.getParent().setAttributeValue("ts", ts);
    }
  }

  /**
   * @Param:
   * @Description: list自身查询有无时间冲突,
   * 优化1: 如果自身的list过大, j遍历不能从0开始,只需要往后面数据比较大小
   * @Author: zyf    2019/3/29
   */
  public static String checkSelf(MonthOtTimeBVO[] list) {
      String res = null;
      if (list.length == 0) {
          return res;
      }
      for (int i = 0; i < list.length; i++) {
          UFDateTime I_S = (UFDateTime) list[i].getAttributeValue(MonthOtTimeBVO.OVERTIMEBEGINTIME);
          UFDateTime I_E = (UFDateTime) list[i].getAttributeValue(MonthOtTimeBVO.OVERTIMEENDTIME);
         // for (int j = 0; j < list.size(); j++) {
          for (int j = i+1; j < list.length; j++) {
             /* if (i == j) {
                  continue;  //自身不跟自身比较
              }*/
        	  UFDateTime J_S = (UFDateTime) list[j].getAttributeValue(MonthOtTimeBVO.OVERTIMEBEGINTIME);
        	  UFDateTime J_E = (UFDateTime) list[j].getAttributeValue(MonthOtTimeBVO.OVERTIMEENDTIME);
              //这里使用compareTo方法, 因为getTime()的时间不太准确
              if ((J_S.compareTo(I_S) == -1 && I_S.compareTo(J_E) == -1) || (J_S.compareTo(I_E) == -1 && I_E.compareTo(J_E) == -1) 
            		  || (I_S.compareTo(J_S) == -1 && J_S.compareTo(I_E) == -1)  || (I_S.compareTo(J_E) == -1 && J_E.compareTo(I_E) == -1)
              		  || J_E.compareTo(I_S) == 0 || J_S.compareTo(I_E) == 0 || J_E.compareTo(I_E) == 0 || J_S.compareTo(I_S) == 0) {
                  res =  dateToStr((UFDateTime)list[i].getAttributeValue(MonthOtTimeBVO.OVERTIMEBEGINTIME)) + " "
                          + dateToStr((UFDateTime)list[i].getAttributeValue(MonthOtTimeBVO.OVERTIMEBEGINTIME));
                  break;
              }
          }
      }
      return res;
  }
  
  /**
   * @Param: listNew 前端传的list
   * @Param: listOld 数据库list,
   * 优化2*****后端查询数据库可以根据前端的list里面最大时间和最小时间区间作为条件查询出来
   * @Description: 比较前端传的list跟数据库list有无时间冲突
   * @Author: zyf    2019/3/29
   */
  public static String checkTwoList(MonthOtTimeBVO[] listNew, MonthOtTimeBVO[] listOld) {
      String res = null;  //没有冲突返回null,有冲突返回冲突的时间段
      for (int i = 0; i < listNew.length; i++) {
    	  UFDateTime I_S = (UFDateTime)listNew[i].getAttributeValue(MonthOtTimeBVO.OVERTIMEBEGINTIME);
    	  UFDateTime I_E = (UFDateTime)listNew[i].getAttributeValue(MonthOtTimeBVO.OVERTIMEENDTIME);
    	  String jobIdNew = (String) listNew[i].getAttributeValue(MonthOtTimeBVO.PK_MONTHOTTIME_B);
          for (int j = 0; j < listOld.length; j++) {
        	  UFDateTime J_S = listOld[j].getOvertimebegintime();
        	  UFDateTime J_E = listOld[j].getOvertimeendtime();
        	  String jobIdOld = listOld[j].getPk_monthottime_b();

              if (jobIdNew != null && jobIdNew.equals(jobIdOld)) {
                  continue; // 前台如果是旧数据修改不能再跟自己比较
              }
              
              if(J_S == null || J_E == null || I_S == null || I_E == null) {
            	  continue;
              }
              
              //compareTo返回结果-1 0 1 表示前者比后者<,=,>关系 ,下面的if判断涉及具体的怎样比较可以自行优化
              if ((J_S.compareTo(I_S) == -1 && I_S.compareTo(J_E) == -1)
                      || (J_S.compareTo(I_E) == -1 && I_E.compareTo(J_E) == -1) || (I_S.compareTo(J_S) == -1 && J_S.compareTo(I_E) == -1)
                      || (I_S.compareTo(J_E) == -1 && J_E.compareTo(I_E) == -1) || J_E.compareTo(I_S) == 0 || J_S.compareTo(I_E) == 0
                      || J_E.compareTo(I_E) == 0 || J_S.compareTo(I_S) == 0) {
                  res = dateToStr((UFDateTime)listNew[i].getAttributeValue(MonthOtTimeBVO.OVERTIMEBEGINTIME)) + " "
                          + dateToStr((UFDateTime)listNew[i].getAttributeValue(MonthOtTimeBVO.OVERTIMEENDTIME));
                  break;
              }
          }
      }
      return res;
  }
  
  /**
   * 按次
   * @Param:
   * @Description: list自身查询有无时间冲突,
   * 优化1: 如果自身的list过大, j遍历不能从0开始,只需要往后面数据比较大小
   * @Author: zyf    2019/3/29
   */
  public static String checkSelfTimes(MonthOtTimeBVO[] list) {
      String res = null;
      if (list.length == 0) {
          return res;
      }
      List<String> keys = new ArrayList();
      for (int i = 0; i < list.length; i++) {
    	  Object otdate = list[i].getAttributeValue(MonthOtTimeBVO.OTDATE);
    	  String ottimestype = (String) list[i].getAttributeValue(MonthOtTimeBVO.OTTIMESTYPE);
    	  if(otdate != null) {
	    	  String key = dateToStr((UFDate)otdate) + ottimestype;
	    	  if(keys.contains(key)) {
	    		  res = (res == null ? "" : res) + dateToStr((UFDate)otdate) + " ";
	    		  continue;
	    	  }
	    	  keys.add(key);
    	  }
      }
      return res;
  }
  
  /**
   * 按次
   * @Param: listNew 前端传的list
   * @Param: listOld 数据库list,
   * 优化2*****后端查询数据库可以根据前端的list里面最大时间和最小时间区间作为条件查询出来
   * @Description: 比较前端传的list跟数据库list有无时间冲突
   * @Author: zyf    2019/3/29
   */
  public static String checkTwoListTimes(MonthOtTimeBVO[] listNew, MonthOtTimeBVO[] listOld) {
      String res = null;  //没有冲突返回null,有冲突返回冲突的时间段
      List<String> keys = new ArrayList();
      for (int i = 0; i < listOld.length; i++) {
    	  Object otdate = listOld[i].getAttributeValue(MonthOtTimeBVO.OTDATE);
    	  String ottimestype = (String) listOld[i].getAttributeValue(MonthOtTimeBVO.OTTIMESTYPE);
    	  if(otdate != null) {
	    	  String key = dateToStr((UFDate)otdate) + ottimestype;
	    	  keys.add(dateToStr((UFDate)otdate) + ottimestype);
    	  }
      }
      for (int i = 0; i < listNew.length; i++) {
    	  Object otdate = listNew[i].getAttributeValue(MonthOtTimeBVO.OTDATE);
    	  String ottimestype = (String) listNew[i].getAttributeValue(MonthOtTimeBVO.OTTIMESTYPE);
    	  if(otdate != null) {
    		  String key = dateToStr((UFDate)otdate) + ottimestype;
    		  if(keys.contains(key)) {
    			  res = (res == null ? "" : res) + dateToStr((UFDate)otdate) + " ";
    			  continue;
    		  }
    		  keys.add(key);
    	  }
      }
      return res;
  }
  
  public static final String DATE_FORMAT_Y_M_DHM = "yyyy-MM-dd HH:mm:ss";
  public static String dateToStr(UFDateTime date) {
      String strDate = "";
      SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_Y_M_DHM);
      strDate = sdf.format(date.getDate().toDate());
      return strDate;
  }
  
  public static final String DATE_FORMAT_Y_M_D = "yyyy-MM-dd";
  public static String dateToStr(UFDate date) {
      String strDate = "";
      SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_Y_M_D);
      strDate = sdf.format(date.toDate());
      return strDate;
  }
}
