zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.changhong.epc.rely.api.service.impl;
 
import com.changhong.epc.bean.admin.InitCompany;
import com.changhong.epc.bean.form.ProcessState;
import com.changhong.epc.bean.tenant.system.permission.PowerButton;
import com.changhong.epc.bean.tenant.system.permission.PowerKey;
import com.changhong.epc.bean.tenant.system.permission.PowerValue;
import com.changhong.epc.constter.base.BaseConst;
import com.changhong.epc.constter.system.UserInfo;
import com.changhong.epc.constter.system.prop.ChangHongProperties;
import com.changhong.epc.rely.api.bean.Company;
import com.changhong.epc.rely.api.bean.Tenant;
import com.changhong.epc.rely.api.epc.admin.InitCompanyApi;
import com.changhong.epc.rely.api.service.PowerService;
import com.changhong.epc.rely.api.tool.PowerTool;
import com.changhong.epc.rely.api.tool.power.ShowButtonExpression;
import com.changhong.epc.rely.api.tool.power.ShowButtonResult;
import com.changhong.ssc.data.autho.api.service.api.UserApi;
import com.changhong.ssc.data.autho.base.struct.Condition;
import com.iemsoft.framework.cloud.core.thread.ThreadData;
import com.iemsoft.framework.cloud.core.tools.JSONTool;
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
 
@Service("powerService")
@Slf4j
public class PowerServiceImpl implements PowerService, BaseConst {
 
    static String powerJson = "{\"dataPermissionMap\":{\"button:*\":{\"createAllowed\":[{\"conditions\":[{\"column\":\"calculation_cost\",\"operator\":0,\"value\":\"y\"}],\"display\":\"计算费用\",\"entityId\":\"button:*\",\"type\":1031},{\"conditions\":[{\"column\":\"bill_upload\",\"operator\":0,\"value\":\"empty('orderCode')\"}],\"display\":\"票据上传\",\"entityId\":\"button:*\",\"type\":1031}],\"createForbidden\":[],\"deleteAllowed\":[],\"deleteForbidden\":[],\"retrieveAllowed\":[{\"conditions\":[{\"column\":\"implement\",\"operator\":0,\"value\":\"equals('processState', '30')\"}],\"display\":\"执行\",\"entityId\":\"button:*\",\"type\":1026},{\"conditions\":[{\"column\":\"calculation_cost\",\"operator\":0,\"value\":\"y\"}],\"display\":\"计算费用\",\"entityId\":\"button:*\",\"type\":1031},{\"conditions\":[{\"column\":\"q\",\"operator\":0,\"value\":\"equals('processState', '30')\"}],\"display\":\"扩展预算\",\"entityId\":\"button:*\",\"type\":1026},{\"conditions\":[{\"column\":\"showPic\",\"operator\":0,\"value\":\"!empty('orderCode')\"}],\"display\":\"查看票据\",\"entityId\":\"button:*\",\"type\":1030},{\"conditions\":[{\"column\":\"account\",\"operator\":0,\"value\":\"equals('orderStatus', '49')\"}],\"display\":\"重新记账\",\"entityId\":\"button:*\",\"type\":1026},{\"conditions\":[{\"column\":\"bill_upload\",\"operator\":0,\"value\":\"empty('orderCode')\"}],\"display\":\"票据上传\",\"entityId\":\"button:*\",\"type\":1031},{\"conditions\":[{\"column\":\"reUpload\",\"operator\":0,\"value\":\"equals('orderStatus', '15')&&equals('processState','10')\"}],\"display\":\"重新上传\",\"entityId\":\"button:*\",\"type\":1026},{\"conditions\":[{\"column\":\"againReview\",\"operator\":0,\"value\":\"equals('orderStatus', '15') && equals('processState', '10')\"}],\"display\":\"重新审单\",\"entityId\":\"button:*\",\"type\":1026}],\"retrieveForbidden\":[],\"updateAllowed\":[{\"conditions\":[{\"column\":\"calculation_cost\",\"operator\":0,\"value\":\"y\"}],\"display\":\"计算费用\",\"entityId\":\"button:*\",\"type\":1031},{\"conditions\":[{\"column\":\"showPic\",\"operator\":0,\"value\":\"!empty('orderCode')\"}],\"display\":\"查看票据\",\"entityId\":\"button:*\",\"type\":1030},{\"conditions\":[{\"column\":\"bill_upload\",\"operator\":0,\"value\":\"empty('orderCode')\"}],\"display\":\"票据上传\",\"entityId\":\"button:*\",\"type\":1031}],\"updateForbidden\":[]}},\"functionPermissionSet\":[{\"display\":\"我的待办(菜单)\",\"name\":\"process/processTask\",\"type\":0},{\"display\":\"合同管理(菜单)\",\"name\":\"Thecontract\",\"type\":0},{\"display\":\"配置表单\",\"name\":\"formSetting\",\"type\":0},{\"display\":\"消息中心(菜单)\",\"name\":\"message\",\"type\":0},{\"display\":\"预览表单\",\"name\":\"deleteForm\",\"type\":0},{\"display\":\"新建表单\",\"name\":\"newForm\",\"type\":0},{\"display\":\"费用类型配置\",\"name\":\"systemConfig/navtree\",\"type\":0},{\"display\":\"系统设置(菜单)\",\"name\":\"systemConfig\",\"type\":0},{\"display\":\"预算管理(菜单)\",\"name\":\"budget\",\"type\":0},{\"display\":\"主数据维护\",\"name\":\"maindatamaintenance\",\"type\":0},{\"display\":\"修改预算\",\"name\":\"editBudget\",\"type\":0},{\"display\":\"固定资产折旧\",\"name\":\"Fixedassets/depreciation\",\"type\":0},{\"display\":\"借款单(菜单)\",\"name\":\"person/personjie\",\"type\":0},{\"display\":\"管理中心(菜单)\",\"name\":\"cloudcenter\",\"type\":0},{\"display\":\"编辑表单\",\"name\":\"editForm\",\"type\":0},{\"display\":\"系统配置\",\"name\":\"systemConfig/systemConfig\",\"type\":0},{\"display\":\"预算使用配置\",\"name\":\"systemConfig/budgetList\",\"type\":0},{\"display\":\"还款单(菜单)\",\"name\":\"person/personhuan\",\"type\":0},{\"display\":\"选择表单\",\"name\":\"selectForm\",\"type\":0},{\"display\":\"查询表单\",\"name\":\"queryForm\",\"type\":0},{\"display\":\"(启用/禁用)表单\",\"name\":\"disableForm\",\"type\":0},{\"display\":\"费用报销(菜单)\",\"name\":\"form/submit\",\"type\":0},{\"display\":\"固定资产报销单\",\"name\":\"Fixedassets/FixedAssets\",\"type\":0},{\"display\":\"固定资产(菜单)\",\"name\":\"Fixedassets\",\"type\":0},{\"display\":\"个人借款(菜单)\",\"name\":\"person\",\"type\":0},{\"display\":\"合同报销单\",\"name\":\"Thecontract/contractExpense\",\"type\":0},{\"display\":\"预览表单\",\"name\":\"previewForm\",\"type\":0},{\"display\":\"预算单\",\"name\":\"budget/budget\",\"type\":0},{\"display\":\"删除系统变量\",\"name\":\"delFormula\",\"type\":0},{\"display\":\"删除预算\",\"name\":\"delBudget\",\"type\":0},{\"display\":\"补助标准\",\"name\":\"expend\",\"type\":0},{\"display\":\"预算回收\",\"name\":\"budget/budgetRecovery\",\"type\":0},{\"display\":\"合同信息\",\"name\":\"Thecontract/contractmassage\",\"type\":0},{\"display\":\"预算追加\",\"name\":\"budget/budgetAdditional\",\"type\":0},{\"display\":\"编辑系统变量\",\"name\":\"editFormula\",\"type\":0},{\"display\":\"新增系统变量\",\"name\":\"addFormula\",\"type\":0},{\"display\":\"费用申请(菜单)\",\"name\":\"form/applycation\",\"type\":0},{\"display\":\"主数据定义\",\"name\":\"masterdefinition\",\"type\":0},{\"display\":\"固定资产信息\",\"name\":\"Fixedassets/massage\",\"type\":0},{\"display\":\"新建预算\",\"name\":\"addBudget\",\"type\":0},{\"display\":\"报表中心(菜单)\",\"name\":\"reportCenter\",\"type\":0},{\"display\":\"系统变量配置\",\"name\":\"systemConfig/myFormula\",\"type\":0}]}";
 
    @Resource
    private InitCompanyApi initCompanyApi;
 
    @Resource
    @Setter
    private ShowButtonExpression showButtonExpression;
 
    @Resource
    private ChangHongProperties changHongProperties;
 
    @Override
    public PowerValue getUserPower() {
//        PowerValue powerValue = CacheUtils.get("USER_POWER_"+ThreadData.get(Keys.TOKEN), PowerValue.class);
//        if(ObjectUtil.notEmpty(powerValue)){
//            return powerValue;
//        }
 
 
        if(Objects.equals("admin", ThreadData.get(PERM_TYPE))){
            return getUserPower(changHongProperties.getPermissionAppKey(), changHongProperties.getPermissionSecretKey());
        }
 
        InitCompany initCompany = initCompanyApi.getInitCompany();
        if(ObjectUtil.empty(initCompany)){
            return new PowerValue();
        }
//        Assert.empty(initCompany, "C0010");
        String appKey = initCompany.getPowerAppKey();
        String secretKey = initCompany.getPowerSecretKey();
        return getUserPower(appKey, secretKey);
//        powerValue = getUserPower(appKey, secretKey);
//        CacheUtils.set("USER_POWER_"+ThreadData.get(Keys.TOKEN), powerValue, 1000*60*60*24);
//        return powerValue;
    }
 
    @Override
    public PowerValue getUserPower(String appKey, String secretKey){
        log.debug("获取权限-appKey:{}", appKey);
        log.debug("获取权限-secretKey:{}", secretKey);
        UserInfo user = ThreadData.get(USER_INFO);
        UserApi users = PowerTool.getUserApi(appKey, secretKey);
        if(ObjectUtil.empty(user) || ObjectUtil.empty(users)){
            return new PowerValue();
        }
        String openId = user.getLoginInfo().getOpenId();
        log.debug("获取权限-openId:{}", openId);
        try {
            return new PowerValue(users.getUserPermission(openId));
        }catch (RuntimeException e){
            log.error("{},{}", e.getMessage(), JSONTool.toJson(user));
//            throw new IEMRuntimeException(e.getMessage());
            return new PowerValue();
        }
    }
 
    @Override
    public PowerButton getShowButton(String formId, ProcessState state) {
        PowerValue powerValue = getUserPower();
//        PowerValue powerValue = JSONTool.toObj(powerJson, PowerValue.class);
        Set<String> hide = new LinkedHashSet<>();
        List<String> show = powerValue.getDataPowerConditionForFormId(formId, PowerValue.BUTTON_DATA_PREFIX)
                .stream()
                .filter(c->filterShow(c, state, hide))
                .map(Condition::getColumn)
                .collect(Collectors.toList());
        PowerButton p = new PowerButton(show, hide);
        log.debug("按钮权限结果:{}", p);
        return p;
    }
 
    public static void main(String... args){
        PowerServiceImpl p = new PowerServiceImpl();
        p.setShowButtonExpression(new ShowButtonExpression());
        System.out.println(
                p.getShowButton("cwvI3wti", JSONTool.toObj("{\"formId\":\"cwvI3wti\",\"tenantId\":\"100000001\"}", ProcessState.class))
        );
    }
 
    @Override
    public PowerKey createSubSystem(Tenant tenant, Company company) {
        try {
            return PowerTool.createChildSystem(company, tenant);
        }catch (RuntimeException e){
            log.error(e.getMessage(), e);
            return new PowerKey();
        }
    }
 
    public boolean filterShow(Condition c, ProcessState state, Set<String> hide){
        log.debug("按钮表达式:{}", c.toString());
        ShowButtonResult showButtonResult = showButtonExpression.execute(c.getValue(), state);
        if(showButtonResult.isShow()) {
            hide.addAll(showButtonResult.getHideButtons());
        }
        log.debug("返回结果:{}", JSONTool.toJson(showButtonResult));
        return showButtonResult.isShow();
    }
 
}