ServiceProviderController.java
8.61 KB
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
package com.glsx.usercenter.controller;
import com.alibaba.fastjson.JSONObject;
import com.glsx.common.annotation.Log;
import com.glsx.common.constants.Constants;
import com.glsx.common.constants.ResultObject;
import com.glsx.common.feign.client.CommonClient;
import com.glsx.common.feign.client.OrderClient;
import com.glsx.common.utils.DateUtil;
import com.glsx.common.utils.StringUtils;
import com.glsx.usercenter.constants.ResultCodeEnum;
import com.glsx.usercenter.mapper.SysUserImageMapper;
import com.glsx.usercenter.mapper.SysUserMapper;
import com.glsx.usercenter.pojo.sp.SysServiceArea;
import com.glsx.usercenter.pojo.user.*;
import com.glsx.usercenter.service.IAreaService;
import com.glsx.usercenter.service.ISysOrganizationService;
import com.glsx.usercenter.service.ISysProviderService;
import com.glsx.usercenter.service.SysUserService;
import com.glsx.usercenter.utils.AccountValidatorUtil;
import com.glsx.usercenter.utils.QRCodeUtil;
import com.glsx.usercenter.utils.ShiroUtil;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.groups.Default;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static com.glsx.common.constants.Constants.USER_TYPE_SP;
/**
* @author jin
* @version 1.4.0
* @Title ServiceProviderController
* @Description TODO 服务提供商
* @date 2019/8/8 16:16
*/
@RestController
@RequestMapping(value = "/provider")
public class ServiceProviderController extends BaseController {
@Autowired
private SysUserService sysUserService;
@Autowired
private CommonClient commonClient;
@Autowired
private SysUserImageMapper sysUserImageMapper;
@Autowired
private IAreaService iAreaService;
@Autowired
private ISysOrganizationService iSysOrganizationService;
@Autowired
private OrderClient orderClient;
@Autowired
private RedisSessionDAO redisSessionDAO;
@Autowired
private SysUserMapper sysUserMapper;
@Autowired
private ShiroUtil shiroUtil;
@Autowired
private ISysProviderService providerService;
/**
* 服务商管理-新增
*
* @param sysManager
* @param bindingResult
* @return
*/
@RequestMapping(value = "/save", method = {RequestMethod.POST})
@ResponseBody
@Log(menuName = "服务商管理-新增", operateType = Constants.SYS_LOG_OPERATE_TYPE_SAVE)
public ResultObject save(@Validated(value = {Default.class}) SysManager sysManager, String serviceKeys, String ranges, BindingResult bindingResult) {
logger.info("(新增, 编辑)服务商传入的参数为" + sysManager.toString());
if (bindingResult.hasErrors()) {
Map<String, String> errorMap = getErrors(bindingResult);
return new ResultObject(ResultCodeEnum.PARAM_ERROR.getCode(), ResultCodeEnum.PARAM_ERROR.getMsg(), errorMap);
}
if (StringUtils.isEmpty(sysManager.getPassword())) {
return new ResultObject(ResultCodeEnum.PARAM_ERROR.getCode(), ResultCodeEnum.PARAM_ERROR.getMsg());
}
if (!AccountValidatorUtil.isMobile(sysManager.getPhoneNumber())) {
return new ResultObject(ResultCodeEnum.PHONE_NUMBER_ERROR.getCode(), ResultCodeEnum.PHONE_NUMBER_ERROR.getMsg());
}
if (StringUtils.isEmpty(serviceKeys)) {
return new ResultObject(ResultCodeEnum.SERVICE_KEY_ERROR.getCode(), ResultCodeEnum.SERVICE_KEY_ERROR.getMsg());
}
if (StringUtils.isEmpty(ranges)) {
return new ResultObject(ResultCodeEnum.RANGES_ERROR.getCode(), ResultCodeEnum.RANGES_ERROR.getMsg());
}
if (sysManager.getOrgId() != null) {
//根据组织id查询出所属集团
Integer groupId = iSysOrganizationService.getGroupIdByOrgId(sysManager.getOrgId());
sysManager.setGroupId(groupId);
}
if (sysManager.getId() == null) {
if (!sysUserService.findByUserName(sysManager.getUsername()).isEmpty()) {
return new ResultObject(ResultCodeEnum.FAIL.getCode(), "该用户账号已存在");
}
sysManager.setPassword(sysUserService.encryptPassword(sysManager.getPassword()));
sysManager.setCreateTime(DateUtil.getCurrentTime());
sysManager.setCreateBy(0);//todo 测试直接写成admin
} else {
List<SysUser> sysUserList = new ArrayList<>();
SysUserExample sysUserExample = new SysUserExample();
sysUserExample.createCriteria().andUsernameEqualTo(sysManager.getUsername());
sysUserList = sysUserMapper.selectByExample(sysUserExample);
if (!sysUserList.isEmpty()) {
if (!sysUserList.get(0).getId().equals(sysManager.getId())) {
return new ResultObject(ResultCodeEnum.FAIL.getCode(), "该用户账号已存在");
}
}
//用户修改密码
sysUserExample.clear();
sysUserExample.createCriteria().andPasswordEqualTo(sysManager.getPassword());
sysUserList = sysUserMapper.selectByExample(sysUserExample);
if (sysUserList.isEmpty()) {
sysManager.setPassword(sysUserService.encryptPassword(sysManager.getPassword()));
}
//如果注销或停用的用户在线,需要强制下线
if (!"sys_user_status_normal".equals(sysManager.getUserStatus())) {
shiroUtil.singleTerminalTypeLogin(sysManager.getUsername());
}
sysManager.setUpdateBy(0);//todo 测试直接写成admin
sysManager.setUpdateTime(DateUtil.getCurrentTime());
}
//性别默认为未知
if (StringUtils.isEmpty(sysManager.getSex())) {
sysManager.setSex("sys_user_sex_unknown");
}
sysManager.setUserType(USER_TYPE_SP);
//所在省市
int id = sysUserService.saveUser(sysManager);
SysUserImageExample example = new SysUserImageExample();
SysUserImageExample.Criteria criteria = example.createCriteria();
criteria.andUserIdEqualTo(id).andImageKeyEqualTo(com.glsx.usercenter.constants.Constants.SYS_SERVICE_QRIMAGE);
List<SysUserImage> sysUserImages = sysUserImageMapper.selectByExample(example);
//服务商需要生成二维码
if (sysUserImages.isEmpty()) {
SysUserImage sysUserImage = new SysUserImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
QRCodeUtil.generateQRCode(String.valueOf(id), baos);
JSONObject obj = commonClient.uploadQRImage(baos.toByteArray());
String url = obj.getString("data");
sysUserImage.setUrl(url);
sysUserImage.setUserId(id);
sysUserImage.setImageKey(com.glsx.usercenter.constants.Constants.SYS_SERVICE_QRIMAGE);
sysUserImageMapper.insert(sysUserImage);
}
//技能
Integer deled = providerService.clearProviderService(id);
logger.info("清除服务商{}现有技能, 返回:{}", id, deled);
String[] strings = serviceKeys.split(",");
for (String serviceKey : strings) {
SysUserServices sysUserServices = new SysUserServices();
sysUserServices.setUserId(id);
sysUserServices.setServiceKey(serviceKey);
providerService.insertProviderService(sysUserServices);
}
//增加服务范围
Integer range = providerService.clearProviderRange(id);
logger.info("清除服务商{}现有服务范围, 返回:{}", id, range);
String[] rgs = ranges.split(",");
for (String area : rgs) {
SysServiceArea managerArea = new SysServiceArea();
int areaCode = Integer.parseInt(area);
managerArea.setAreaId(areaCode);
managerArea.setServiceProviderId(id);
managerArea.setAreaLevel(providerService.getAreaLeval(areaCode));
providerService.insertProviderArea(managerArea);
}
return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg(), id);
}
}