package nccloud.web.hrwa.pubinfo.wadataexplain.action;

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

import nc.impl.pubapp.pattern.database.IDQueryBuilder;
import nccloud.itf.bd.wadataexplain.IWadataexplainQryService;
import nc.ui.querytemplate.querytree.IQueryScheme;
import nc.vo.hrwa.wadataexplain.Wadataexplain;
import nc.vo.pubapp.AppContext;
import nccloud.dto.baseapp.querytree.dataformat.QueryTreeFormatVO;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.convert.translate.Translator;
import nccloud.framework.web.ui.model.PageInfo;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.framework.web.ui.pattern.grid.GridOperator;
import nccloud.pubitf.platform.query.INCCloudQueryService;
import nccloud.web.action.NCCAction;

/***************************************************************
* @author tangcht
* @version nccloud1.0
* @since 2019-1-7
* @see
***************************************************************/
public class WadataexplainQueryAction extends NCCAction {

@Override
public <T> Object execute(IRequest request, T para) throws Exception {
    QueryTreeFormatVO info = (QueryTreeFormatVO) para;
    Grid grid = null;
    // 1、根据传入的参数构建scheme
    INCCloudQueryService qservice = ServiceLocator.find(INCCloudQueryService.class);
    IQueryScheme scheme = qservice.convertCondition(info);
    // 调用接口查询数据
    IWadataexplainQryService service = ServiceLocator.find(IWadataexplainQryService.class);
    String wherepart = scheme.getWhereSQLOnly();
    wherepart = wherepart == null ? "1=1" : wherepart;
    
	// add by longht 增加查询过滤，按权限项目 20211220 start
	String powersqlorg = " SELECT pk_org FROM sm_subject_org   where 1=1 "
			+ "   and ( subjectid = '"
			+ AppContext.getInstance().getPkUser() + "'"
			+ " or subjectid in ( SELECT sur.pk_role FROM sm_user_role sur where sur.cuserid = '"
			+ AppContext.getInstance().getPkUser() + "'))\n";
	wherepart = wherepart + " and pk_org in (" + powersqlorg + ")";
	
	// add by longht 增加查询过滤，按权限项目 20211220 start
	String powersqlclass = "SELECT  classid" + 
			"  FROM wa_clspower where 1=1"
			+ "   and ( pk_subject = '"
			+ AppContext.getInstance().getPkUser() + "'"
			+ " or pk_subject in ( SELECT sur.pk_role FROM sm_user_role sur where sur.cuserid = '"
			+ AppContext.getInstance().getPkUser() + "'))   "
			+ " and subject_type = '1' \n";

	wherepart = wherepart + " and pk_wa_class in (" + powersqlclass + ")";

	// add by longht 增加查询过滤，按权限项目 20211220 start
	String powersqlpro =
			"select  distinct  wa_classitem.pk_wa_classitem\n" +
					"          from wa_classitem\n" + 
					"         inner join wa_item\n" + 
					"            on wa_classitem.pk_wa_item = wa_item.pk_wa_item\n" + 
					"          inner  join wa_itempower\n" + 
					"            on wa_itempower.pk_wa_item = wa_classitem.pk_wa_item\n" + 
					"           and wa_itempower.pk_wa_class = wa_classitem.pk_wa_class\n" + 
					"           and wa_itempower.pk_org = wa_classitem.pk_org\n" + 
					"           and (wa_itempower.pk_subject = '"+ AppContext.getInstance().getPkUser() +"' or\n" + 
					"               wa_itempower.pk_subject in\n" + 
					"               (SELECT sur.pk_role\n" + 
					"                   FROM sm_user_role sur\n" + 
					"                  where sur.cuserid = '"+ AppContext.getInstance().getPkUser() +"'))\n" + 
					"         where 1 = 1\n" + 
					"           and wa_item.mid = 'N'\n" ;
					//"           and wa_itempower.editflag = 'Y'"; //20220105 add by   longht 去掉，保证能查到历史数据

	wherepart = wherepart +" and pk_wa_classitem in ("+powersqlpro+")";
	
	
	
    //add by longht 增加查询过滤，按权限项目 20211220  end 
    String[] ids = service.queryPKs(wherepart);
    if(ids == null || ids.length <= 0) {
    return grid;
    }
    int pageSize = Integer.parseInt(info.getPageInfo().getPageSize());
    int pageIndex = Integer.parseInt(info.getPageInfo().getPageIndex());
    PageInfo pageInfo = new PageInfo();
    pageInfo.setTotal(ids.length);
    pageInfo.setPageSize(pageSize);
    pageInfo.setTotalPage(this.getTotalPage(pageInfo, ids.length));
    setPageIndex(pageInfo,pageIndex);
    String[] PageIds = this.getCurrPageIds(ids, pageInfo);

    IDQueryBuilder sqlbuilder = new IDQueryBuilder();
    String wherePart = sqlbuilder.buildSQL("pk_wa_data_explain", PageIds);
    IWadataexplainQryService qryService = ServiceLocator.find(IWadataexplainQryService.class);
    Wadataexplain[] vos = qryService.query(wherePart);

    if(vos != null && vos.length > 0){

    GridOperator gridOperator = new GridOperator(WadataexplainConsts.pagecode);
    grid = gridOperator.toGrid(vos);

    // 翻译器，将主键翻译成名称
    Translator translator = new Translator();
    translator.translate(grid);

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

    @Override
    protected Class getParaClass() {
    return QueryTreeFormatVO.class;
    }

    private String[] getCurrPageIds(String[] ids,PageInfo page){
    List<String> list = new ArrayList<String>();
            int end = Math.min((page.getPageIndex()+1)*page.getPageSize(), page.getTotal());
            int start = ((page.getPageIndex()<0?0:page.getPageIndex()))*page.getPageSize()+1;
            int sum = end - start + 1;
            for(int i=0;i<sum;i++){
            list.add(ids[start-1]);
            start++;
            }

            return list.toArray(new String[0]);
            }

            private void setPageIndex(PageInfo page,int pageIndex){
            if(pageIndex<0)pageIndex =0;
            if((pageIndex+1)*page.getPageSize() < page.getTotal()){
            page.setPageIndex(pageIndex);
            }else{
            page.setPageIndex(page.getTotalPage()-1);
            }
            }

            private int getTotalPage(PageInfo pageInfo, int len) {

            int size = pageInfo.getPageSize();
            int total = 0;
            if (len % size == 0) {
            total = len / size;
            } else {
            total = len / size + 1;
            }
            return total;
            }

            }
