cgf
2025-08-23 9ec0a61f90ac2464eebc643bfe2d93ac9ba6e569
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
package com.java110.user.cmd.owner;
 
import com.alibaba.fastjson.JSONObject;
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.AuthenticationFactory;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.core.factory.SendSmsFactory;
import com.java110.dto.app.AppDto;
import com.java110.dto.community.CommunityDto;
import com.java110.dto.msg.SmsDto;
import com.java110.dto.owner.OwnerAppUserDto;
import com.java110.dto.owner.OwnerDto;
import com.java110.dto.owner.OwnerRoomRelDto;
import com.java110.dto.room.RoomDto;
import com.java110.dto.user.UserAttrDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.common.ISmsInnerServiceSMO;
import com.java110.intf.community.ICommunityInnerServiceSMO;
import com.java110.intf.community.IRoomInnerServiceSMO;
import com.java110.intf.community.IRoomV1InnerServiceSMO;
import com.java110.intf.store.IStoreInnerServiceSMO;
import com.java110.intf.user.*;
import com.java110.po.owner.OwnerAppUserPo;
import com.java110.po.user.UserPo;
import com.java110.po.user.UserAttrPo;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.constant.UserLevelConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
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 org.springframework.util.CollectionUtils;
 
import java.util.List;
 
/**
 * 服务注册功能迁移
 */
@Java110Cmd(serviceCode = "owner.ownerRegister")
public class OwnerRegisterCmd extends Cmd {
    private final static Logger logger = LoggerFactory.getLogger(OwnerRegisterCmd.class);
    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;
 
    @Autowired
    private IUserAttrV1InnerServiceSMO userAttrV1InnerServiceSMOImpl;
 
    @Autowired
    private IStoreInnerServiceSMO storeInnerServiceSMOImpl;
 
    @Autowired
    private IUserInnerServiceSMO userInnerServiceSMOImpl;
 
    @Autowired
    private ISmsInnerServiceSMO smsInnerServiceSMOImpl;
 
    @Autowired
    private ICommunityInnerServiceSMO communityInnerServiceSMOImpl;
 
    @Autowired
    private IOwnerInnerServiceSMO ownerInnerServiceSMOImpl;
 
    @Autowired
    private IOwnerAppUserV1InnerServiceSMO ownerAppUserV1InnerServiceSMOImpl;
 
    @Autowired
    private IOwnerRoomRelV1InnerServiceSMO ownerRoomRelV1InnerServiceSMOImpl;
 
    @Autowired
    private IRoomInnerServiceSMO roomInnerServiceSMOImpl;
 
 
    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "link", "未包含联系电话");
        Assert.hasKeyAndValue(reqJson, "msgCode", "未包含联系电话验证码");
        Assert.hasKeyAndValue(reqJson, "password", "未包含密码");
 
        UserDto userDto = new UserDto();
        userDto.setTel(reqJson.getString("link"));
        userDto.setLevelCd(UserDto.LEVEL_CD_USER);
        List<UserDto> userDtos = userInnerServiceSMOImpl.getUsers(userDto);
 
        if (!ListUtil.isNull(userDtos)) {
            throw new CmdException("手机号已存在,请登陆");
        }
 
        SmsDto smsDto = new SmsDto();
        smsDto.setTel(reqJson.getString("link"));
        smsDto.setCode(reqJson.getString("msgCode"));
        smsDto = smsInnerServiceSMOImpl.validateCode(smsDto);
 
        if (!smsDto.isSuccess() && "ON".equals(MappingCache.getValue(MappingConstant.SMS_DOMAIN, SendSmsFactory.SMS_SEND_SWITCH))) {
            throw new IllegalArgumentException(smsDto.getMsg());
        }
    }
 
    @Override
    @Java110Transactional
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
 
 
        OwnerDto ownerDto = new OwnerDto();
        ownerDto.setLink(reqJson.getString("link"));
        List<OwnerDto> ownerDtos = ownerInnerServiceSMOImpl.queryOwnerMembers(ownerDto);
 
        //设置默认密码
        String userPassword = reqJson.getString("password");
        userPassword = AuthenticationFactory.passwdMd5(userPassword);
        String name = reqJson.getString("link");
        if (!ListUtil.isNull(ownerDtos)) {
            name = ownerDtos.get(0).getName();
        }
        //todo 注册用户
        UserPo userPo = new UserPo();
        userPo.setAddress("无");
        userPo.setUserId(GenerateCodeFactory.getUserId());
        userPo.setLevelCd(UserLevelConstant.USER_LEVEL_ORDINARY);
        userPo.setName(name);
        userPo.setTel(reqJson.getString("link"));
        userPo.setPassword(userPassword);
 
        int flag = userV1InnerServiceSMOImpl.saveUser(userPo);
        if (flag < 1) {
            throw new CmdException("注册失败");
        }
        //todo 保存openId
        String openId = reqJson.getString("openId");
        if (!StringUtil.isEmpty(openId)) {
            UserAttrPo userAttrPo = new UserAttrPo();
            userAttrPo.setAttrId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_attrId));
            userAttrPo.setSpecCd(UserAttrDto.SPEC_OPEN_ID);
            userAttrPo.setUserId(userPo.getUserId());
            userAttrPo.setValue(openId);
            flag = userAttrV1InnerServiceSMOImpl.saveUserAttr(userAttrPo);
            if (flag < 1) {
                throw new CmdException("注册失败");
            }
        }
 
        //todo 根据手机号未关联到业主直接返回成功,后续通过认证房屋的方式操作
        if (ListUtil.isNull(ownerDtos)) {
            return;
        }
        String appId = cmdDataFlowContext.getReqHeaders().get("app-id");
        String appType = "";
        if (AppDto.WECHAT_OWNER_APP_ID.equals(appId)) { //公众号
            appType = OwnerAppUserDto.APP_TYPE_WECHAT;
        } else if (AppDto.WECHAT_MINA_OWNER_APP_ID.equals(appId)) { //小程序
            appType = OwnerAppUserDto.APP_TYPE_WECHAT_MINA;
        } else {//app
            appType = OwnerAppUserDto.APP_TYPE_APP;
        }
 
        OwnerAppUserPo ownerAppUserPo = null;
 
        List<CommunityDto> communityDtos = null;
        for (OwnerDto tmpOwnerDto : ownerDtos) {
            //用userId和手机号查询绑定关系,没有则新增
            OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto();
            ownerAppUserDto.setLink(reqJson.getString("link"));
            ownerAppUserDto.setUserId(tmpOwnerDto.getUserId());
            List<OwnerAppUserDto> ownerAppUserDtos = ownerAppUserV1InnerServiceSMOImpl.queryOwnerAppUsers(ownerAppUserDto);
            if(!CollectionUtils.isEmpty(ownerAppUserDtos)){
                continue;
            }
            CommunityDto communityDto = new CommunityDto();
            communityDto.setState("1100");
            communityDto.setCommunityId(tmpOwnerDto.getCommunityId());
            communityDtos = communityInnerServiceSMOImpl.queryCommunitys(communityDto);
            if (ListUtil.isNull(communityDtos)) {
                continue;
            }
            communityDto = communityDtos.get(0);
            ownerAppUserPo = new OwnerAppUserPo();
            ownerAppUserPo.setAppUserId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_appUserId));
            ownerAppUserPo.setMemberId(tmpOwnerDto.getMemberId());
            ownerAppUserPo.setCommunityId(tmpOwnerDto.getCommunityId());
            ownerAppUserPo.setCommunityName(communityDto.getName());
            ownerAppUserPo.setAppUserName(tmpOwnerDto.getName());
            ownerAppUserPo.setIdCard(tmpOwnerDto.getIdCard());
            ownerAppUserPo.setLink(tmpOwnerDto.getLink());
            ownerAppUserPo.setOpenId("-1");
            ownerAppUserPo.setAppTypeCd("10010");
            ownerAppUserPo.setState(OwnerAppUserDto.STATE_NOT_AUDIT);
            ownerAppUserPo.setRemark("注册自动关联");
            ownerAppUserPo.setUserId(userPo.getUserId());
            ownerAppUserPo.setAppType(appType);
            ownerAppUserPo.setOwnerTypeCd(tmpOwnerDto.getOwnerTypeCd());
            queryOwnerRoom(tmpOwnerDto, ownerAppUserPo);
            flag = ownerAppUserV1InnerServiceSMOImpl.saveOwnerAppUser(ownerAppUserPo);
            if (flag < 1) {
                throw new CmdException("添加用户业主关系失败");
            }
        }
 
        cmdDataFlowContext.setResponseEntity(ResultVo.success());
    }
 
    private void queryOwnerRoom(OwnerDto ownerDto, OwnerAppUserPo ownerAppUserPo) {
 
 
        OwnerRoomRelDto ownerRoomRelDto = new OwnerRoomRelDto();
        ownerRoomRelDto.setOwnerId(ownerDto.getOwnerId());
 
        List<OwnerRoomRelDto> ownerRoomRelDtos = ownerRoomRelV1InnerServiceSMOImpl.queryOwnerRoomRels(ownerRoomRelDto);
 
        if (ListUtil.isNull(ownerRoomRelDtos)) {
            return;
        }
 
        RoomDto roomDto = new RoomDto();
        roomDto.setRoomId(ownerRoomRelDtos.get(0).getRoomId());
        List<RoomDto> roomDtos = roomInnerServiceSMOImpl.queryRooms(roomDto);
        if (ListUtil.isNull(roomDtos)) {
            return;
        }
 
        ownerAppUserPo.setRoomId(roomDtos.get(0).getRoomId());
        ownerAppUserPo.setRoomName(roomDtos.get(0).getFloorNum() + "-" + roomDtos.get(0).getUnitNum() + "-" + roomDtos.get(0).getRoomNum());
    }
 
 
}