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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package cn.autoform.web.controller.formsetting;
 
import cn.autoform.bean.ProcessState;
import cn.autoform.bean.power.AutoPowerButton;
import cn.autoform.db.entity.*;
import cn.autoform.factory.FormFactory;
import cn.autoform.factory.product.ProductMethod.IOType;
import cn.autoform.fw.exception.FormClientException;
import cn.autoform.fw.exception.RestResult;
import cn.autoform.fw.utility.RestResultGenerator;
import cn.autoform.util.thread.Keys;
import cn.autoform.util.thread.ThreadData;
import cn.autoform.util.tool.JSONTool;
import cn.autoform.util.tool.RequestContext;
import cn.autoform.web.client.CscApiService;
import cn.autoform.web.client.WorkflowClient;
import cn.autoform.web.client.util.WorkflowTool;
import cn.autoform.web.controller.workflow.WorkflowController;
import cn.autoform.web.mapper.formset.FormSetMapper;
import cn.autoform.web.service.customButton.CustomButtonService;
import cn.autoform.web.service.formresource.FormResourceService;
import cn.autoform.web.service.formsetting.FormsettingService;
import cn.autoform.web.service.process.ProcessInstanceService;
import cn.autoform.web.service.process.ProcessStateService;
import com.google.common.base.Objects;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
 
@Slf4j
@RestController
public class FormsettingController<T> {
 
    @Autowired
    FormsettingService formsettingService = null;
    
    @Autowired
    CustomButtonService customButtonService = null;
    
    @Autowired
    FormResourceService formResoureService;
    
    @Autowired
    WorkflowController workflowController;
    
    @Autowired
    FormFactory formFactory;
    
    @Resource
    FormSetMapper FormSetMapper;
 
    @Autowired
    private WorkflowClient workflowClient;
 
    @Autowired
    private WorkflowTool workflowTool;
 
    @Autowired
    private CscApiService cscApi;
    
    @Resource(name = "processStateServiceImpl")
    private ProcessStateService process;
 
    @Autowired
    private ProcessInstanceService processInstance;
 
    /**
     * 获取当前表单工具栏设置信息
     * @param entity
     * @return
     * @throws IOException 
     * @throws TimeoutException 
     * @throws FormClientException 
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })//重写
    @RequestMapping(value = "${requset.formsetting01}", method = RequestMethod.POST)
    public RestResult<FormsettingEntity> getFormsettingInfoService(@RequestBody CustomButtonEntity entity) throws Exception,TimeoutException, IOException, FormClientException {
        FormsettingEntity re = formsettingService.getFormsettingInfo(entity);
        FormsettingEntity results = re!=null?re:new FormsettingEntity();
        //判断表单撤回是否启用
        String recallState = "0";
        if(results!=null){
            recallState = results.getRecallable()==null?"0":results.getRecallable();
        }
        
        FormsettingEntity result = results==null?new FormsettingEntity():results;
        
        
        List<FormresourceEntity> path = formResoureService.getFormresourcePath(entity.getTenantID(), entity.getFormID());
        ProcessState process =new ProcessState(entity.getFormID(), 
                entity.getDataRowNum(), entity.getTenantID());
        ProcessState state = this.process.selectState(process);
//        System.err.println(state);
//        String orderStatus = state.getOrderStatus();
        if(path!=null){
            for(FormresourceEntity f:path){
                result.setResourceFilePath(f.getResourceFilepath());
            }
        }
 
        AutoPowerButton autoPowerButton = customButtonService.selectName(entity, state == null ? process : state);
        if(entity.getType()!=null && !(entity.getType().equals(""))){
            CustomButtonExtend cust = new CustomButtonExtend();
            //把单据状态放进去
            cust.subCustomButton(autoPowerButton.getShow(),state,entity.getSetting()==null?"":entity.getSetting());
            result.setSubCustomButton(cust);
        }else{
            result.setCustomButtons(autoPowerButton.getShow());
        }
 
        //当是详细页的时候
        if(entity.getDataRowNum() != null && entity.getType() !=null && !(entity.getType().equals("")) && "XX".equals(entity.getType())){
            
            //获得当前用户名及创建数据用户名
 
            String flag = FormSetMapper.getFlagByFormID(entity.getFormID(), RequestContext.getTenantId());
            IOType where = "services".equals(flag) ? IOType.services : IOType.cloud;
            String userName = ThreadData.get(Keys.OPEN_ID);
            String parpantName = cscApi.getUserId(ThreadData.get(Keys.OPEN_ID));
            String createName = this.formFactory.createFormMethod(where).getUserName(  entity.getFormID()
                    , entity.getTenantID()
                    , entity.getDataRowNum());
            
            //用户名不一致的时候就~
            System.out.println("用户名:"+userName+"---"+createName);
            if(!userName.equals(createName)){
                this.setZore(result, results);
            }
            
            //PARAM
            ProcessInstance pros = new ProcessInstance(entity.getFormID()
                    ,entity.getTenantID(),entity.getDataRowNum());
            pros = processInstance.selectInst(pros);
            //查询是否创建流程  60 50 结束 61 结束  ,首先判断流程
            if(Objects.equal("20",state.getProcessState())){
                System.out.println("进入判断1");
                result.setEditablel("0",results);//不编辑
                result.setSubmitablel("0",results);//不提交
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("pageNum", 1);
                jsonObject.put("pageSize", 100);
                jsonObject.put("processInstId", pros.getProcessInstId());
                Map<String, Object> workflowResult = workflowClient.instSeacheHandle(workflowTool.getProcess(jsonObject));
//                Map<String, Object> workflowResult = workflowController.instLog(JSONTool.toJson(pros).toString(),null);
                if(Objects.equal("00000",workflowResult.get("resCode"))){
 
                    List<Map> data = Optional.ofNullable(workflowResult)
                            .map(d->d.get("data"))
                            .map(d->(Map)d)
                            .map(d->d.get("data"))
                            .map(d->(List)d)
                            .orElse(Collections.EMPTY_LIST);
                    //当前待办人
                    Set<String> currUsers = data
                            .stream()
//                            .findFirst()
                            .map(map-> map.get("openId"))
                            .map(Object::toString)
                            .collect(Collectors.toSet());
                    String processState = data
                            .stream()
                            .findFirst()
                            .map(map-> map.get("processState"))
                            .map(Object::toString)
                            .orElse("");
 
                    //如果提交了流程 但未结束
                    if(data != null &&  processState != null &&
                            !(processState.equals(60)
                                    || processState.equals(50)
                                    || processState.equals(0)
                                    || processState.equals(61))){
                        //提交,审批,退回,撤回,---逻辑操作
                        //单独判断审批按钮
                        log.debug("按钮权限-当前登录人:{}, 当前待办人:{}, 单据创建人:{}", userName, currUsers, createName);
                        if(currUsers.contains(userName)){
                            System.out.println("进入判断4");
                            results.setRejectable("1");
                            results.setAuditable("1");
                            result.setRejectablel("1",results);//审批
                            result.setAuditablel("1",results);//退回
                        }else{
                            result.setRejectablel("0",results);//审批
                            result.setAuditablel("0",results);//退回
                        }
 
 
 
                        if(Objects.equal(userName, state.getProcessUser()) && currUsers.contains(userName)
                                || getLog(pros.getProcessInstId()).equals(userName)
//                                (
//                                            ObjectUtil.empty(state.getProcessLog())
//                                        ||
//                                            Optional.ofNullable(data)
//                                                    .filter(d->d.size() > 1)
//                                                    .map(d->d.get(d.size()-1))
//                                                    .map(m->m.get("openId"))
//                                                    .map(Object::toString)
//                                                    .map(s-> Objects.equal(s, userName))
//                                                    .orElse(false)
//                                    )
                                ){
                            result.setRecallablel("1",results);//可撤回
                        }else{
                            result.setRecallablel("0",results);//不可撤回
                        }
                    
                    }
                }else{
                    this.setZore(result, results);
                }
                System.out.println("当前按钮:"+JSONTool.toJson(result));
            }else if(Objects.equal("30",state.getProcessState())){
                System.out.println("进入判断2");
                this.setZore(result, results);
            }else if(Objects.equal("10",state.getProcessState())){//没有提交流程
                //判断是否是自动审单中
                System.out.println("进入判断3");
                
                if("10".equals(state.getOrderStatus())){
                    //如果为自动审单中, 关闭编辑和提交按钮
                    result.setEditablel("0",results);//不编辑
                    result.setSubmitablel("0",results);//不提交
                }else{
                    result.setSubmitablel("1",results);//提交
                    result.setEditablel("1",results);//编辑
                }
                result.setRejectablel("0",results);//不审批
                result.setAuditablel("0",results);//不退回
                result.setRecallablel("0",results);//不可撤回
            }
            
        }
        autoPowerButton.hide(result);
        return RestResultGenerator.genSuccessResult(result);
    }
 
    public String getLog(String processInstId) throws TimeoutException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("processInstId", processInstId);
        Map<String, Object> workflowResult = workflowClient.instLog(workflowTool.getProcess(jsonObject));
        return Optional.ofNullable(workflowResult)
                .map(d->d.get("data"))
                .map(d->(Map)d)
                .map(d->d.get("data"))
                .map(d->(List)d)
                .filter(d->d.size()>1)
                .map(d->d.get(1))
                .map(d->((Map)d).get("openId"))
                .map(Object::toString)
                .orElse("");
    }
 
    public void setZore(FormsettingEntity result,FormsettingEntity results){
        result.setEditablel("0",results);//不编辑
        result.setSubmitablel("0",results);//不提交
        result.setRejectablel("0",results);//不审批
        result.setAuditablel("0",results);//不退回
        result.setRecallablel("0",results);//不可撤回
    }
    
    /**
     * 保存表单工具栏信息
     * @param entity
     * @return
     */
    @RequestMapping(value = "${requset.formsetting02}", method = RequestMethod.POST)
    public RestResult<Integer> saveFormsettingInfo(@RequestBody FormsettingEntity entity) {
        System.err.println(JSONTool.toJson(entity));
        int rs = 0;
        CustomButtonEntity cust = new CustomButtonEntity();
        cust.setTenantID(entity.getTenantID());
        cust.setFormID(entity.getFormID());
        FormsettingEntity result = formsettingService.getFormsettingInfo(cust);
        
        List<CustomButtonEntity> customButtons = entity.getCustomButtons();
        if(!customButtons.isEmpty()){
            this.customButtonService.addCustomButton(customButtons);
        }
        if(result == null) {
            rs = formsettingService.createFormsetting(entity);
        } else {
            rs = formsettingService.updateFormsetting(entity);
        }
 
        return RestResultGenerator.genSuccessResult(rs);
    }
    
    /**
     * 删除工具栏信息
     */
    @RequestMapping(value = "${requset.formsetting03}", method = RequestMethod.POST)
    public int deleteCustomButton(@RequestBody CustomButtonEntity customButton){
        return customButtonService.deleteCustomButton(customButton.getId());
    }
    
    /**
     * 修改工具栏信息
     */
    @RequestMapping(value = "${requset.formsetting04}", method = RequestMethod.POST)
    public int updateCustomButton(@RequestBody List<CustomButtonEntity> list){
        return customButtonService.updateCustomButton(list);
    }
    
}