
package nccloud.hrkq.hrkq.monthottimevo.action;

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

import nc.bs.logging.Logger;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.ui.querytemplate.querytree.IQueryScheme;
import nc.vo.hi.psndoc.PsnJobVO;
import nc.vo.hrkq.monthottime.AggMonthOtTimeVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.pub.Constructor;
import nccloud.base.exception.ExceptionUtils;
import nccloud.dto.baseapp.querytree.dataformat.QueryTreeFormatVO;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.container.SessionContext;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.querytemplet.QueryUtil4NCC;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.framework.web.ui.pattern.grid.GridOperator;
import nccloud.hrkq.hrkq.monthottimevo.hrkq.bean.PageQueryVO;
import nccloud.hrkq.hrkq.monthottimevo.hrkq.util.CommonUtil;
import nccloud.web.hrkq.common.utils.AttendDataUtil;
import nccloud.web.uapbd.commons.web.ParamUtils;

/**
 * 列表查询（查询方案）操作
 * @version  @since v3.5.6-1903
 */
public class MonthOtTimeVOAppQueryListAction implements ICommonAction {

  @Override
  public Object doAction(IRequest paramIRequest) {
    try {
    	ParamUtils param = new ParamUtils(paramIRequest);
		String appCode = param.getString("appCode",SessionContext.getInstance().getAppcode());
		String otapplytype = "1";
    	if("606570D0".equals(appCode)) {
			//按次
    		otapplytype = "1";
		}else if("606570C0".equals(appCode) || "606570E0".equals(appCode)) {
			//普通
			otapplytype = "2";
		}
      IMDPersistenceQueryService service = CommonUtil.getMDQueryService();
      PsnJobVO psnjobVO = AttendDataUtil.queryPsnJob(null);
      String userId = SessionContext.getInstance().getClientInfo().getUserid();
      String pk_psndocSql = " (pk_psndoc = '"+psnjobVO.getPk_psndoc()+"' or billmaker ='"+userId+"') and otapplytype = '"+otapplytype+"' and isnull(dr,0) = 0 order by creationtime desc";
      Collection<AggMonthOtTimeVO> bills2 =
          service.queryBillOfVOByCond(AggMonthOtTimeVO.class, pk_psndocSql, false,
              false);
      AggMonthOtTimeVO[] aggvos = bills2.toArray(new AggMonthOtTimeVO[0]);
      return bills2;
    } catch (BusinessException ex) {
      // 处理异常信息
      Logger.error(ex);
      ExceptionUtils.wrapException(ex);
    }
    // 转换grid信息，返回前端
    return null;
  }

  /**
   * 转换Grid信息
   *
   * @param pagevo
   * @param pageSize
   * @param pagecode
   * @return
   */
  private Grid covert(PageQueryVO pagevo, int pageSize, String pagecode) {
    AggMonthOtTimeVO[] bills =
        (AggMonthOtTimeVO[]) pagevo.getCurrentPageBills();

    Grid grid = null;
    if (bills != null) {
      int size = bills.length > pageSize ? pageSize : bills.length;
      Object[] heads = new Object[size];

      for (int i = 0; i < size; i++) {
        heads[i] = bills[i].getParent();
      }
      GridOperator operator = new GridOperator(pagecode);
      grid = operator.toGrid(heads);
      grid.getModel().setAllpks(pagevo.getPks());
    }
    return grid;
  }

  /**
   * 创建空页面信息
   *
   * @return
   */
  private PageQueryVO createNullPage() {
    String[] ids = new String[0];
    AggMonthOtTimeVO[] bills =
        Constructor.construct(AggMonthOtTimeVO.class, 0);
    PageQueryVO page = new PageQueryVO(ids, bills);
    return page;
  }

  /**
   * 设置默认页面
   *
   * @param info
   * @param aggvos
   * @return
   */
  private AggMonthOtTimeVO[] getDefaulePageBill(QueryTreeFormatVO info,
      AggMonthOtTimeVO... aggvos) {
    String pageSizeStr = info.getPageInfo().getPageSize();
    int pageSize = 10;
    if ((pageSizeStr != null) && (pageSizeStr.length() > 0)) {
      pageSize = Integer.parseInt(pageSizeStr);
    }
    info.getPageInfo().setPageIndex("0");
    List<AggMonthOtTimeVO> billLst = new ArrayList<AggMonthOtTimeVO>();
    for (int i = 0; (i < aggvos.length) && (i < pageSize); i++) {
      AggMonthOtTimeVO bill = new AggMonthOtTimeVO();

      bill.setParent(aggvos[i].getParent());
      billLst.add(bill);
    }
    return billLst.toArray(new AggMonthOtTimeVO[0]);
  }

  /**
   * 获取主键信息
   *
   * @param aggvos
   * @return
   */
  private String[] getPks(AggMonthOtTimeVO... aggvos) {
    List<String> pks = new ArrayList<String>();
    for (AggMonthOtTimeVO bill : aggvos) {
      pks.add(bill.getPrimaryKey());
    }
    return pks.toArray(new String[0]);
  }

  /**
   * 获取查询参数
   *
   * @param paramIRequest
   * @return
   */
  private QueryTreeFormatVO getQueryParam(IRequest paramIRequest) {
    String strRead = paramIRequest.read();
    QueryTreeFormatVO queryParam =
        JsonFactory.create().fromJson(strRead, QueryTreeFormatVO.class);
    return queryParam;
  }

  /**
   * 获取查询方案
   *
   * @param queryParam
   * @return
   */
  private IQueryScheme getScheme(QueryTreeFormatVO queryParam) {
    QueryUtil4NCC queryutil = new QueryUtil4NCC(queryParam);
    IQueryScheme scheme = queryutil.convertCondition();
    return scheme;
  }

  /**
   * 总页数
   *
   * @param pageInfo
   * @param length
   * @return
   */
  private int getTotalPage(nccloud.framework.web.ui.model.PageInfo pageInfo,
      int length) {
    int size = pageInfo.getPageSize();
    int total = 0;
    if ((length % size) == 0) {
      total = length / size;
    }
    else {
      total = (length / size) + 1;
    }
    return total;
  }

  /**
   * 页面设置
   *
   * @param retObj
   * @param pagevo
   * @param pageSize
   * @param pageIndex
   * @return
   */
  private Object pageSet(Object retObj, PageQueryVO pagevo, int pageSize,
      int pageIndex) {
    Grid grid = null;
    if ((retObj instanceof Grid)) {
      grid = (Grid) retObj;
    }

    nccloud.framework.web.ui.model.PageInfo pageInfo =
        new nccloud.framework.web.ui.model.PageInfo();
    pageInfo.setTotal(pagevo.getPks().length);
    pageInfo.setPageSize(pageSize);
    pageInfo.setPageIndex(pageIndex);
    pageInfo.setTotalPage(this.getTotalPage(pageInfo, pagevo.getPks().length));

    grid.getModel().setPageinfo(pageInfo);
    return grid;
  }

  /**
   * page信息转前端需要的grid模型
   *
   * @param pagevo
   * @param info
   * @param pagecode
   * @return
   */
  private Object transPageInfoToGrid(PageQueryVO pagevo,
      QueryTreeFormatVO info, String pagecode) {
    Object grid = null;
    int pageSize = Integer.parseInt(info.getPageInfo().getPageSize());
    int pageIndex = Integer.parseInt(info.getPageInfo().getPageIndex());
    if (pagevo != null) {
      if (pagevo.getCurrentPageBills().length == 0) {
        return null;
      }
      grid = this.covert(pagevo, pageSize, pagecode);

      grid = this.pageSet(grid, pagevo, pageSize, pageIndex);
    }
    return grid;
  }
}
