package com.engine.salary.remote.mode.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.cube.biz.AppHelper;
import com.engine.cube.biz.DetachHelper;
import com.weaver.formmodel.util.StringHelper;
import weaver.conn.RecordSet;
import weaver.formmode.FormModeConfig;
import weaver.formmode.service.ModelInfoService;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.moduledetach.ManageDetachComInfo;
import weaver.workflow.workflow.WorkflowBillComInfo;

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

public class GetModeList extends AbstractCommonCommand<Map<String, Object>> {
	private FormModeConfig formModeConfig = new FormModeConfig();
    public GetModeList(Map<String, Object> params, User user){
        this.params = params;
        this.user = user;
    }
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> result = new HashMap<String,Object>();
        ManageDetachComInfo ManageDetachComInfo = new ManageDetachComInfo();
        boolean isUseFmManageDetach=ManageDetachComInfo.isUseFmManageDetach();
        int appid = Util.getIntValue(Util.null2String(params.get("appid")),1)  ;
        String aLLorCurrentKey = Util.null2String(params.get("aLLorCurrentKey"));
        int appidForAppname =Util.getIntValue(Util.null2String(this.params.get("appidForAppname")),1);
        ModelInfoService modelInfoService = new ModelInfoService();
        modelInfoService.setUser(user);
        List<Map<String, Object>> list;
        int currentSubCompanyId = Util.getIntValue(Util.null2String(params.get("currentSubCompanyId")), -1);
        if(user.getUID()==1&&isUseFmManageDetach&&currentSubCompanyId<1){
        	isUseFmManageDetach = false;
        }
        if(isUseFmManageDetach) {
            Map<String, Object> detach=DetachHelper.getUserDetachInfo(user, currentSubCompanyId, "FORMMODEAPP:ALL");
          	if(currentSubCompanyId<1){
              	currentSubCompanyId=Util.getIntValue(Util.null2String(detach.get("defaultSubCompanyId")));
          	}
            list = modelInfoService.getModelInfoByAppIdDetach(appid, currentSubCompanyId);
        } else {
            list = modelInfoService.getModelInfoByAppId(appid);
        }
        WorkflowBillComInfo workflowBillComInfo = new WorkflowBillComInfo();
        ArrayList<Map<String, String>> modeList = new ArrayList<Map<String, String>>();
        
        String vsql = "SELECT  m.formid,m.virtualformtype,m.vformtype FROM ModeFormExtend m  WHERE m.isvirtualform=1";
        RecordSet rs = new RecordSet();
        rs.execute(vsql);
        Map<String,String> map = new HashMap<String,String>();
        while(rs.next()){
        	String formid = rs.getString("formid");
        	String virtualformtype = rs.getString("virtualformtype");
        	String vformtype = rs.getString("vformtype");
        	map.put(formid, virtualformtype+"_"+vformtype);
        }
        FormModeConfig formModeConfig = new FormModeConfig();
        String VirtualFormPermissions = formModeConfig.getVirtualFormPermissions();
        String modeids = formModeConfig.getModeid();
        for(Map<String, Object> mode : list) {
            Map<String,String> newMode = new HashMap<String, String>();
            String key = Util.null2String(mode.get("id"));
            String formid = Util.null2String(mode.get("formid"));
            String isvirtualform = Util.null2String(mode.get("isvirtualform"));
            String virtualrightopen = Util.null2String(mode.get("virtualrightopen"));
            if(StringHelper.isEmpty(isvirtualform)){
            	isvirtualform = "0";
            }
            String tableName = Util.null2String(workflowBillComInfo.getTablename(formid));
            String virtualformtype = "";
            String vformtype = "";
            if("1".equals(isvirtualform)){
                continue;
            }
            newMode.put("key",key );
            newMode.put("domid",key );
            newMode.put("name", Util.null2String(mode.get("modename")));
            newMode.put("subname", tableName);
            newMode.put("isvirtualform", isvirtualform);
            newMode.put("virtualformtype", virtualformtype);
            newMode.put("vformtype", vformtype);
            modeList.add(newMode);
        }
        result.put("appid",appid+"");
        String appsubcompanyid = "";
        String appsubcompanyidname = "";
        if(appid>0){
        	String sql = "SELECT subcompanyid FROM modetreefield WHERE id="+appid;
        	rs.execute(sql);
        	if(rs.next()){
        		int subcompanyid = rs.getInt("subcompanyid");
        		if(subcompanyid>0){
        			appsubcompanyid = subcompanyid+"";
        			SubCompanyComInfo subCompanyComInfo = new SubCompanyComInfo();
        			appsubcompanyidname = subCompanyComInfo.getSubcompanyname(appsubcompanyid);
        		}
        	}
        }
        String appName = AppHelper.getAppName(appidForAppname+"");
        boolean issysadmin = user.getUID() == 1;
        result.put("appName",appName);
        result.put("issysadmin",issysadmin);
        result.put("aLLorCurrentKey",aLLorCurrentKey);
        result.put("appsubcompanyid",appsubcompanyid);
        result.put("appsubcompanyidname",appsubcompanyidname);
        result.put("modeList",modeList);
        result.put("canDeleteModeInfo",formModeConfig.isCanDeleteModeInfo());
        return result;
    }


    public boolean isHadRight(String modeid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select count(1) as rightcount from moderightinfo where modeid=? and ((righttype=0 and (sharetype<80 or sharetype>100)) or (righttype in (1,2,3) and (sharetype<80 or sharetype>100)))",modeid);
        int rightcount=0;
        if(rs.next()){
            rightcount = Util.getIntValue(rs.getString("rightcount"),0);
        }
        if(rightcount>0){
            return true;
        }else{
            return false;
        }
    }
}
