chengf
2025-12-03 f1635623ba8b1746ae2f2bb077b07805a76286d5
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
/*
 * Copyright 2017-2020 吴学文 and java110 team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.java110.community.cmd.community;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.community.bmo.community.ICommunityBMO;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.doc.annotation.*;
import com.java110.dto.community.CommunityDto;
import com.java110.dto.community.PropertyManagementRecordDto;
import com.java110.dto.privilege.RoleCommunityDto;
import com.java110.intf.community.ICommunityV1InnerServiceSMO;
import com.java110.intf.community.PropertyManagementRecordsServiceSMO;
import com.java110.intf.user.IRoleCommunityV1InnerServiceSMO;
import com.java110.po.community.PropertyManagementRecordPo;
import com.java110.po.privilege.RoleCommunityPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.slf4j.Logger;
import com.java110.core.log.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
 
import java.util.List;
import java.util.Map;
 
 
@Java110CmdDoc(title = "修改小区",
        description = "主要提供给外系统修改小区",
        httpMethod = "post",
        url = "http://{ip}:{port}/app/community.updateCommunity",
        resource = "communityDoc",
        author = "吴学文",
        serviceCode = "community.updateCommunity",
        seq = 2
)
 
@Java110ParamsDoc(params = {
        @Java110ParamDoc(name = "communityId", length = 30, remark = "小区编码"),
        @Java110ParamDoc(name = "address", length = 30, remark = "小区地址"),
        @Java110ParamDoc(name = "cityCode", length = 12, remark = "地区编码"),
        @Java110ParamDoc(name = "feePrice", type = "int", length = 11, remark = "小区收费价格"),
        @Java110ParamDoc(name = "mapX", length = 12, remark = "经度"),
        @Java110ParamDoc(name = "mapY", length = 12, remark = "纬度"),
        @Java110ParamDoc(name = "name", length = 64, remark = "名称"),
        @Java110ParamDoc(name = "nearbyLandmarks", length = 64, remark = "地标,如xx 公园旁"),
        @Java110ParamDoc(name = "payFeeMonth", type = "int", length = 11, remark = "小区收费周期"),
        @Java110ParamDoc(name = "tel", length = 11, remark = "小区客服电话"),
})
 
@Java110ResponseDoc(
        params = {
                @Java110ParamDoc(name = "code", type = "int", length = 11, defaultValue = "0", remark = "返回编号,0 成功 其他失败"),
                @Java110ParamDoc(name = "msg", type = "String", length = 250, defaultValue = "成功", remark = "描述"),
        }
)
 
@Java110ExampleDoc(
        reqBody = "{\"communityId\":\"2022092293190329\",\"name\":\"api接口小区1\",\"address\":\"天津省天津市和平区api接口小区\",\"nearbyLandmarks\":\"23\",\"cityCode\":\"120101\",\"mapX\":\"101.33\",\"mapY\":\"101.33\",\"payFeeMonth\":12,\"feePrice\":0,\"tel\":\"18909711443\",\"attrs\":[{\"domain\":\"COMMON\",\"listShow\":\"Y\",\"page\":-1,\"records\":0,\"required\":\"Y\",\"row\":0,\"specCd\":\"9329000004\",\"specHoldplace\":\"必填,请填写社区编码\",\"specId\":\"9329000004\",\"specName\":\"社区编码\",\"specShow\":\"Y\",\"specType\":\"2233\",\"specValueType\":\"1001\",\"statusCd\":\"0\",\"tableName\":\"building_community_attr\",\"total\":0,\"value\":\"123123\",\"values\":[],\"attrId\":\"112022092280950341\"}]}",
        resBody = "{'code':0,'msg':'成功'}"
)
/**
 * 类表述:更新
 * 服务编码:community.updateCommunity
 * 请求路劲:/app/community.UpdateCommunity
 * add by 吴学文 at 2021-09-18 12:54:57 mail: 928255095@qq.com
 * open source address: https://gitee.com/wuxw7/MicroCommunity
 * 官网:http://www.homecommunity.cn
 * 温馨提示:如果您对此文件进行修改 请不要删除原有作者及注释信息,请补充您的 修改的原因以及联系邮箱如下
 * // modify by 张三 at 2021-09-12 第10行在某种场景下存在某种bug 需要修复,注释10至20行 加入 20行至30行
 */
@Java110Cmd(serviceCode = "community.updateCommunity")
public class UpdateCommunityCmd extends Cmd {
 
    private static Logger logger = LoggerFactory.getLogger(UpdateCommunityCmd.class);
 
 
    @Autowired
    private ICommunityV1InnerServiceSMO communityV1InnerServiceSMOImpl;
 
    @Autowired
    private IRoleCommunityV1InnerServiceSMO roleCommunityV1InnerServiceSMOImpl;
 
 
    @Autowired
    private ICommunityBMO communityBMOImpl;
 
 
    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) {
 
        Assert.hasKeyAndValue(reqJson, "communityId", "小区ID不能为空");
        Assert.hasKeyAndValue(reqJson, "name", "必填,请填写小区名称");
        Assert.hasKeyAndValue(reqJson, "address", "必填,请填写小区地址");
        Assert.hasKeyAndValue(reqJson, "nearbyLandmarks", "必填,请填写小区附近地标");
 
        Assert.judgeAttrValue(reqJson);
 
    }
 
    @Autowired
    private PropertyManagementRecordsServiceSMO propertyManagementRecordsServiceSMO;
 
    @Override
    @Java110Transactional
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
 
        // 保存原始状态用于判断是否修改了状态
        CommunityDto communityDto = new CommunityDto();
        communityDto.setCommunityId(reqJson.getString("communityId"));
        String originalState = communityV1InnerServiceSMOImpl.queryCommunitys(communityDto).get(0).getState();
        String newState = reqJson.getString("state");
 
        communityBMOImpl.updateCommunityOne(reqJson, cmdDataFlowContext);
 
        // 新增:处理状态变更(在管/退管)
        // 只有状态发生变化时才处理
        if (!StringUtil.isEmpty(originalState) && !originalState.equals(newState)) {
            handleCommunityStateChange(reqJson.getString("communityId"), newState, communityV1InnerServiceSMOImpl.queryCommunitys(communityDto).get(0));
        }
 
        if (!reqJson.containsKey("attrs")) {
            return;
        }
 
        JSONArray attrs = reqJson.getJSONArray("attrs");
        if (attrs == null || attrs.size() < 1) {
            return;
        }
 
 
        JSONObject attr = null;
        for (int attrIndex = 0; attrIndex < attrs.size(); attrIndex++) {
            attr = attrs.getJSONObject(attrIndex);
            attr.put("communityId", reqJson.getString("communityId"));
            if (!attr.containsKey("attrId") || attr.getString("attrId").startsWith("-") || StringUtil.isEmpty(attr.getString("attrId"))) {
                communityBMOImpl.addAttr(attr, cmdDataFlowContext);
                continue;
            }
            communityBMOImpl.updateAttr(attr, cmdDataFlowContext);
        }
 
        //todo 如果没有修改名称直接返回
        if (StringUtil.isEmpty(reqJson.getString("name"))) {
            return;
        }
 
        //todo 修改角色小区
 
        RoleCommunityDto roleCommunityDto = new RoleCommunityDto();
        roleCommunityDto.setCommunityId(reqJson.getString("communityId"));
        List<RoleCommunityDto> roleCommunityDtos = roleCommunityV1InnerServiceSMOImpl.queryRoleCommunitys(roleCommunityDto);
 
        if (roleCommunityDtos == null || roleCommunityDtos.size() < 1) {
            return;
        }
        RoleCommunityPo roleCommunityPo = null;
        for (RoleCommunityDto tmpRoleCommunityDto : roleCommunityDtos) {
            roleCommunityPo = new RoleCommunityPo();
            roleCommunityPo.setRcId(tmpRoleCommunityDto.getRcId());
            roleCommunityPo.setCommunityName(reqJson.getString("name"));
            roleCommunityV1InnerServiceSMOImpl.updateRoleCommunity(roleCommunityPo);
        }
 
 
        cmdDataFlowContext.setResponseEntity(ResultVo.success());
    }
    /**
     * 处理小区状态变更(适配property_management_records表)
     * @param communityId 小区ID
     * @param newState 新状态(1400-退管,其他-在管)
     */
    private void handleCommunityStateChange(String communityId, String newState , CommunityDto communityDto) {
        // 1. 退管状态(state=1400):为该小区的有效在管记录添加退管时间
        if ("1400".equals(newState)) {
            // 查询小区当前有效的在管记录(status=0表示有效,management_end_date为null表示未退管)
            PropertyManagementRecordDto queryDto = new PropertyManagementRecordDto();
            queryDto.setCommunityId(communityId);
            queryDto.setStatus(0); // 有效记录
 
            List<Map> records = propertyManagementRecordsServiceSMO.queryPropertyManagementRecords(queryDto);
 
            if (records != null && !records.isEmpty()) {
                PropertyManagementRecordPo updatePo = new PropertyManagementRecordPo();
                updatePo.setManagementEndDate(communityDto.getTakeTime()); // 设置当前时间为退管时间
                // 退管后记录状态仍为有效(status=0),通过end_date区分是否在管
 
 
 
                updatePo.setId((String) records.get(records.size() - 1).get("id")); // 表主键ID
                int flag = propertyManagementRecordsServiceSMO.updatePropertyManagementRecord(updatePo);
                if (flag < 1) {
                    throw new CmdException("更新小区[" + communityId + "]退管记录失败");
                }
            }
        }
        // 2. 在管状态(state != 1400):为小区添加新的在管记录
        else {
            // 查询小区是否已有未退管的有效记录(避免重复添加)
            PropertyManagementRecordDto checkDto = new PropertyManagementRecordDto();
            checkDto.setCommunityId(communityId);
            checkDto.setStatus(0); // 有效记录
 
            // 创建新的在管记录
            PropertyManagementRecordPo addPo = new PropertyManagementRecordPo();
            addPo.setId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_id)); // 生成主键ID
            addPo.setCommunityId(communityId);
            addPo.setCommunityName(getCommunityName(communityId)); // 从小区信息中获取名称(需实现)
            addPo.setManagementStartDate(communityDto.getOutTime()); // 开始时间为当前时间
            addPo.setManagementEndDate(null); // 未退管,退管时间为null
            addPo.setStatus(0); // 状态:0-有效
            addPo.setRemark("小区状态变更为在管时自动添加");
 
            int flag = propertyManagementRecordsServiceSMO.savePropertyManagementRecord(addPo);
            if (flag < 1) {
                throw new CmdException("添加小区[" + communityId + "]在管记录失败");
            }
        }
    }
 
    // 辅助方法:获取小区名称(需根据实际业务实现,例如从小区表查询)
    private String getCommunityName(String communityId) {
        // 示例:从小区服务查询名称
        CommunityDto communityDto = new CommunityDto();
        communityDto.setCommunityId(communityId);
        List<CommunityDto> communities = communityV1InnerServiceSMOImpl.queryCommunitys(communityDto);
        return communities != null && !communities.isEmpty() ? communities.get(0).getName() : "";
    }
 
    // 辅助方法:获取当前管理公司ID(需根据实际业务实现,例如从登录信息或请求参数中获取)
    private String getCurrentManagementCompanyId() {
        // 示例:从请求上下文或当前登录信息中获取,此处仅为占位
        return "当前管理公司ID"; // 实际应替换为真实逻辑
    }
 
    // 辅助方法:获取当前管理公司名称(同上)
    private String getCurrentManagementCompanyName() {
        return "当前管理公司名称"; // 实际应替换为真实逻辑
    }
}