linwe
2023-12-23 55d677758efadb6d42e6d4e595cecc2c50c20d5a
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
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.property.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.repository.ProcessDefinitionQuery;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.utils.SpringUtils;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.flow.business.service.IFlowService;
import org.springblade.flow.core.constant.ProcessConstant;
import org.springblade.flow.core.entity.BladeFlow;
import org.springblade.flow.core.utils.FlowUtil;
import org.springblade.flow.core.utils.TaskUtil;
import org.springblade.modules.district.entity.DistrictEntity;
import org.springblade.modules.district.service.IDistrictService;
import org.springblade.modules.ownersCommittee.entity.OwnersCommitteeEntity;
import org.springblade.modules.ownersCommittee.service.IOwnersCommitteeService;
import org.springblade.modules.property.entity.PropertyCapitalApplyEntity;
import org.springblade.modules.property.mapper.PropertyCapitalApplyMapper;
import org.springblade.modules.property.service.IPropertyCapitalApplyService;
import org.springblade.modules.property.service.IPropertyDistrictUserService;
import org.springblade.modules.property.vo.PropertyCapitalApplyVO;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.Region;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IRegionService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
/**
 * 物业维修资金申请表 服务实现类
 *
 * @author BladeX
 * @since 2023-11-23
 */
@Service
public class PropertyCapitalApplyServiceImpl extends ServiceImpl<PropertyCapitalApplyMapper, PropertyCapitalApplyEntity> implements IPropertyCapitalApplyService {
 
    @Override
    public IPage<PropertyCapitalApplyVO> selectPropertyCapitalApplyPage(IPage<PropertyCapitalApplyVO> page, PropertyCapitalApplyVO propertyCapitalApply) {
        // 判断角色,物业角色只能查询当前小区的
 
        String userRole = AuthUtil.getUserRole();
        if (userRole.contains("wygly")) {
            IPropertyDistrictUserService bean = SpringUtils.getBean(IPropertyDistrictUserService.class);
            List<String> strings = bean.selectPropertyDistrictByUserId(AuthUtil.getUserId());
            propertyCapitalApply.setDistrictIdList(strings);
        }
 
 
        return page.setRecords(baseMapper.selectPropertyCapitalApplyPage(page, propertyCapitalApply));
    }
 
    @Autowired
    private IFlowService flowService;
    // private final IFlowService flowService;
    @Autowired
    private RepositoryService repositoryService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean startProcess(PropertyCapitalApplyVO applyVO) {
        String businessTable = FlowUtil.getBusinessTable(ProcessConstant.LEAVE_KEY);
        if (Func.isEmpty(applyVO.getId())) {
        // 保存leave
        applyVO.setApplyTime(DateUtil.now());
        save(applyVO);
        // 判断是否有业委会,查询业委会负责人
        IOwnersCommitteeService bean = SpringUtils.getBean(IOwnersCommitteeService.class);
        OwnersCommitteeEntity one = bean.getOne(Wrappers.<OwnersCommitteeEntity>lambdaQuery()
            .eq(OwnersCommitteeEntity::getAreaId, applyVO.getDistrictId())
            .eq(OwnersCommitteeEntity::getDeleteFlag,0)
            .eq(OwnersCommitteeEntity::getStatus,0)
            .last("limit 1"));
        Integer ownersCommitteeFlag = CommonConstant.NUMBER_TWO;
        if (one != null && one.getPrincipalId() != null) {
            ownersCommitteeFlag = CommonConstant.NUMBER_ONE;
        }
        // 查询街道负责人  通过小区id 查询街道编码
        // 通过街道编码查询街道名称,
        // 通过街道名称查询 人的部门是街道名称的
        IUserService user = SpringUtils.getBean(IUserService.class);
        IDistrictService district = SpringUtils.getBean(IDistrictService.class);
        IRegionService region = SpringUtils.getBean(IRegionService.class);
        IDeptService dept = SpringUtils.getBean(IDeptService.class);
        DistrictEntity one1 = district.getOne(Wrappers.<DistrictEntity>lambdaQuery()
            .eq(DistrictEntity::getId, applyVO.getDistrictId()));
            User one4 = null;
            // 查询街道责人
            try {
                Region one2 = region.getOne(Wrappers.<Region>lambdaQuery().eq(Region::getCode, one1.getCommunityCode().substring(0, 9)));
                Dept one3 = dept.getOne(Wrappers.<Dept>lambdaQuery().eq(Dept::getDeptName, one2.getName()));
                one4 = user.getOne(Wrappers.<User>lambdaQuery().eq(User::getDeptId, one3.getId())
                    .eq(User::getRoleId,"1729814500990304258")
                    .eq(User::getIsDeleted,0)
                    .last("limit 1"));
            } catch (Exception e) {
                throw new ServiceException("街道信息不存在");
            }
            // 查询住建局负责人
            User user4 = null;
            try {
                Region region2 = region.getOne(Wrappers.<Region>lambdaQuery().eq(Region::getCode, one1.getCommunityCode().substring(0, 6)));
                Dept dept3 = dept.getOne(Wrappers.<Dept>lambdaQuery().eq(Dept::getDeptName, region2.getName() + "住建局").last("limit 1"));
                user4 = user.getOne(Wrappers.<User>lambdaQuery().eq(User::getDeptId, dept3.getId())
                    .eq(User::getRoleId,"1738072768615333890")
                    .eq(User::getIsDeleted,0)
                    .last("limit 1"));
            } catch (Exception e) {
                throw new ServiceException("住建局信息不存在");
            }
            Kv variables = null;
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery().latestVersion().orderByProcessDefinitionKey().asc();
        // 启动流程
        if (CommonConstant.NUMBER_ONE.equals(ownersCommitteeFlag) && CommonConstant.NUMBER_ONE.equals(applyVO.getPropertyFlag())) {
            applyVO.setApplyStatus(CommonConstant.NUMBER_ONE);
            ProcessDefinition processDefinition = processDefinitionQuery.processDefinitionKey("haveMaintenanceFundApply").singleResult();
            applyVO.setProcessDefinitionId(processDefinition.getId());
            // 有物业和有业委会
            variables = Kv.create()
                .set(ProcessConstant.TASK_VARIABLE_CREATE_USER, AuthUtil.getUserName())
                .set("ownersCommitteeUser", TaskUtil.getTaskUser(one.getPrincipalId().toString()))
                .set("streetUser", TaskUtil.getTaskUser(one4.getId().toString()))
                .set("constructionUser", TaskUtil.getTaskUser(user4.getId().toString()))
                .set("applyUser", TaskUtil.getTaskUser(AuthUtil.getUserId().toString()))
                .set("ownersCommitteeFlag", ownersCommitteeFlag);
        } else {
            if(CommonConstant.NUMBER_TWO.equals(ownersCommitteeFlag) && CommonConstant.NUMBER_ONE.equals(applyVO.getPropertyFlag())){
                // 有物业和无业委会
                ProcessDefinition processDefinition = processDefinitionQuery.processDefinitionKey("haveMaintenanceFundApply").singleResult();
                applyVO.setProcessDefinitionId(processDefinition.getId());
            }else {
                // 无物业和无业委会
                ProcessDefinition processDefinition = processDefinitionQuery.processDefinitionKey("notMaintenanceFundApply").singleResult();
                applyVO.setProcessDefinitionId(processDefinition.getId());
            }
            applyVO.setApplyStatus(CommonConstant.NUMBER_TWO);
            variables = Kv.create()
                .set(ProcessConstant.TASK_VARIABLE_CREATE_USER, AuthUtil.getUserName())
                .set("streetUser", TaskUtil.getTaskUser(one4.getId().toString()))
                .set("constructionUser", TaskUtil.getTaskUser(user4.getId().toString()))
                .set("applyUser", TaskUtil.getTaskUser(AuthUtil.getUserId().toString()))
                .set("ownersCommitteeFlag", ownersCommitteeFlag);
        }
        BladeFlow flow = flowService.startProcessInstanceById(applyVO.getProcessDefinitionId(),
            FlowUtil.getBusinessKey(businessTable, String.valueOf(applyVO.getId())), variables);
        if (Func.isNotEmpty(flow)) {
            log.debug("流程已启动,流程ID:" + flow.getProcessInstanceId());
            // 返回流程id写入leave
            applyVO.setProcessInstanceId(flow.getProcessInstanceId());
            updateById(applyVO);
        } else {
            throw new ServiceException("开启流程失败");
        }
        } else {
            updateById(applyVO);
        }
        return true;
    }
 
 
}