xiaoyong931011
2021-08-02 8269efac48716a328c12a4e836a4efb4a5f277d9
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package com.xzx.gc.user.controller;
 
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzx.gc.common.Result;
import com.xzx.gc.common.annotations.valid.Update;
import com.xzx.gc.common.constant.CommonEnum;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.constant.UserEnum;
import com.xzx.gc.common.dto.CommonDto;
import com.xzx.gc.common.dto.log.OperationAppLog;
import com.xzx.gc.common.exception.RestException;
import com.xzx.gc.common.request.BaseController;
import com.xzx.gc.common.utils.BusinessUtil;
import com.xzx.gc.common.utils.IdUtils;
import com.xzx.gc.common.utils.MqUtil;
import com.xzx.gc.common.utils.SecurityUtil;
import com.xzx.gc.entity.*;
import com.xzx.gc.model.JsonResult;
import com.xzx.gc.model.admin.UserEditroleModel;
import com.xzx.gc.model.admin.UserModel;
import com.xzx.gc.model.user.SelectUserTypeDTO;
import com.xzx.gc.user.dto.RoleDto;
import com.xzx.gc.user.dto.SelectOtherUserDto;
import com.xzx.gc.user.mapper.OrderMapper;
import com.xzx.gc.user.mapper.OtherUserMapper;
import com.xzx.gc.user.mapper.SysStorageMapper;
import com.xzx.gc.user.mapper.UserRoleMapper;
import com.xzx.gc.user.service.*;
import com.xzx.gc.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;
 
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;
 
@Api(tags = "角色管理")
@RestController
@Validated
public class RoleController extends BaseController {
 
    @Autowired
    private OtherUserMapper otherUserMapper;
 
    @Autowired
    private FenceService fenceService;
 
    @Autowired
    private UserRoleMapper userRoleMapper;
 
 
    @Autowired
    private UserRoleService userRoleService;
 
    @Autowired
    private OtherUserService otherUserService;
 
 
    @Autowired
    private SysStorageMapper sysStorageMapper;
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private SessionUtil sessionUtil;
 
    @Autowired
    private OrderService orderService;
 
    @Autowired
    private CityPartnerService cityPartnerService;
 
    @ApiOperation(value = "分页查询所有角色")
    @PostMapping(value = "/role/select")
    public Result<PageInfo<OtherUserInfo>> selectReceiver(HttpServletRequest request,@RequestBody SelectOtherUserDto selectOtherUserDto){
        Example example=new Example(OtherUserInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delFlag",Constants.DEL_NOT_FLAG);
//        criteria.andEqualTo("isProhibit",Constants.DEL_NOT_FLAG);
        if(StrUtil.isNotBlank(selectOtherUserDto.getUserType())){
            criteria.andIn("userType",Arrays.asList(selectOtherUserDto.getUserType().split(",")));
        }
 
        String userId=getUserId(request);
        OtherUserInfo byId = otherUserService.findById(userId);
        if(byId!=null){
            criteria.andEqualTo("partnerId",byId.getPartnerId());
        }
 
        if(StrUtil.isNotBlank(selectOtherUserDto.getKeyword())){
            String keyword="%"+selectOtherUserDto.getKeyword()+"%";
            example.and().orLike("name",keyword).orLike("mobilePhone",keyword);
        }
 
        PageHelper.startPage(selectOtherUserDto.getPageNo(),selectOtherUserDto.getPageSize());
        List<OtherUserInfo> select = otherUserMapper.selectByExample(example);
        if(CollUtil.isNotEmpty(select)){
            for (OtherUserInfo userInfo : select) {
                List<UserRole> byRoleCode = userRoleService.findByRoleCode(userInfo.getUserType());
                if(CollUtil.isNotEmpty(byRoleCode)){
                    userInfo.setUserTypeName(byRoleCode.get(0).getRoleName());
                }
                String townId = userInfo.getTownId();
                List<Fence> byTownIds = fenceService.findByIds(townId);
                if(CollUtil.isNotEmpty(byTownIds)){
                    String collect = byTownIds.stream().map(Fence::getFenceName).collect(Collectors.joining(","));
                    userInfo.setTownNames(collect);
                }
 
                if(StrUtil.isNotBlank(userInfo.getPassword())){
                    String decrypt = SecurityUtil.decrypt(userInfo.getSalt(), userInfo.getPassword());
                    userInfo.setPassword(decrypt);
                }
                userInfo.setSalt(null);
 
                //转换areaId
                Integer cityId = userInfo.getCityId();
                Integer townshipId = userInfo.getTownshipId();
                if(townshipId!=null){
                    userInfo.setAreaId(townshipId.toString());
                    userInfo.setLevel(3);
                    userInfo.setAreaName(userInfo.getTownship());
                }else  if(cityId!=null){
                    userInfo.setAreaId(cityId.toString());
                    userInfo.setLevel(2);
                    userInfo.setAreaName(userInfo.getCity());
                }
 
                String storageId = userInfo.getStorageId();
                if(StrUtil.isNotBlank(storageId)){
                    String storageName = sysStorageMapper.selectByPrimaryKey(storageId).getStorageName();
                    userInfo.setStorageName(storageName);
                }
 
                //是否请假期间
                String leaveStartTime = userInfo.getLeaveStartTime();
                String leaveEndTime = userInfo.getLeaveEndTime();
                if(StrUtil.isNotBlank(leaveStartTime)){
                    if(DateUtil.compare(new Date(),DateUtil.parse(leaveStartTime))>=0&&DateUtil.compare(new Date(),DateUtil.parse(leaveEndTime))<=0){
                        userInfo.setLeaveFlag(true);
                    }
                }
 
            }
        }
        PageInfo<OtherUserInfo> pageInfo=new PageInfo<>(select);
        return Result.success(pageInfo);
    }
 
    @ApiOperation( value = "查询所有角色")
    @PostMapping("/role/selectUserType")
    public Result<List<UserRole>> add(HttpServletRequest request,@RequestBody SelectUserTypeDTO selectUserTypeDTO) {
        String userId = getUserId(request);
        List<UserRole> byRoleCode=null;
        //根据运营员查询其下所属的角色
        if(StrUtil.isBlank(selectUserTypeDTO.getExtra())) {
            String mobilePhone = otherUserService.findById(userId).getMobilePhone();
            //查出所有角色
            List<OtherUserInfo> byMobile = otherUserService.findByMobile(mobilePhone);
            List<String> collect = byMobile.stream().map(x -> x.getUserType()).collect(Collectors.toList());
            if(collect.contains(CommonEnum.打包站运营员.getValue())){
                byRoleCode = userRoleService.findByRoleCode(CommonEnum.打包员.getValue());
            }else if(collect.contains(CommonEnum.运营员.getValue())) {
                byRoleCode = userRoleService.findByRoleCode(null);
                //排除打包员相关和自己
                byRoleCode.removeIf(x->CommonEnum.打包站运营员.getValue().equals(x.getRoleCode())||CommonEnum.打包员.getValue().equals(x.getRoleCode())||CommonEnum.运营员.getValue().equals(x.getRoleCode()));
            }
        }else {
            byRoleCode = userRoleService.findByRoleCodes(selectUserTypeDTO.getExtra());
        }
        return Result.success(byRoleCode);
 
    }
 
 
    @ApiOperation( value = "新增角色")
    @PostMapping("/role/add")
    public Result add(HttpServletRequest request, @RequestBody RoleDto roleDto) {
        //设置合伙人ID
        OtherUserInfo byId = otherUserService.findById(getUserId(request));
        String partnerId = byId.getPartnerId();
 
        List<OtherUserInfo> byMobile = otherUserService.findByMobileForBidden(roleDto.getMobilePhone());
        if(CollUtil.isNotEmpty(byMobile)){
                throw new RestException("该手机号已存在别的身份");
        }
 
        Result<String> add = userRoleService.add(roleDto,partnerId);
        if(add.getCode()==0){
            String mobilePhone = userService.findOtherByUserId(getUserId(request),0);
            OperationAppLog build = OperationAppLog.builder().appPrograme(getFrontClient(request)).opreateName(mobilePhone)
                    .methodName(Constants.USER_MODUL_NAME).operateAction("新增角色-"+roleDto.getUserId()).build();
            mqUtil.sendApp(build);
        }
        return add;
    }
 
    @ApiOperation( value = "编辑角色")
    @PostMapping("/role/update")
    public Result update(HttpServletRequest request, @Validated(value = {Update.class}) @RequestBody RoleDto roleDto) {
        roleDto.setLoginUserId(getUserId(request));
 
        List<OtherUserInfo> byMobile = otherUserService.findByMobileForBidden(roleDto.getMobilePhone());
        if(CollUtil.isNotEmpty(byMobile)){
            //修改
            long count = byMobile.stream().filter(x -> !x.getUserId().equals(roleDto.getUserId())).count();
            if(count>0) {
                throw new RestException("该手机号已存在别的身份");
            }
        }
 
        Result edit = userRoleService.edit(roleDto);
        if(edit.getCode()==0){
            String mobilePhone = userService.findOtherByUserId(getUserId(request),0);
            OperationAppLog build = OperationAppLog.builder().appPrograme(getFrontClient(request)).opreateName(mobilePhone)
                    .methodName(Constants.USER_MODUL_NAME).operateAction("编辑角色-"+roleDto.getUserId()).build();
            mqUtil.sendApp(build);
        }
        return edit;
 
    }
 
    /**
     * 身份设定查询
     * @param userModel
     * @return
     */
    @PostMapping( "/admin/front/role/queryEditroleApi.do")
    @ApiOperation(value="用户管理-身份设定查询", notes="test: 仅0有正确返回")
    public JsonResult<Map<String, Object>> queryEditroleApi(@RequestBody UserEditroleModel userModel){
        Map<String, Object> map = userRoleService.queryEditroleApi(userModel);
        return JsonResult.success(map);
    }
 
    /**
     * 根据Id删除用户角色
     * @param userModel
     */
    @PostMapping( "/admin/front/role/delEditroleApi.json")
    @ApiOperation(value="用户管理-身份设定删除", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "userId", value = "用户Id", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "userType", value = "用户类型", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "mobilePhone", value = "手机号", required = true, dataType = "String")
    })
    public JsonResult<Map<String, Object>> delEditroleApi(@RequestBody UserModel userModel, HttpServletRequest request){
        CoreUser user = sessionUtil.getCurrentUser();
        String code=user.getCode();
        //合伙人的运营员那个账号不能删。其他合伙人的账号都可以通过运营员删
        // 运营管理员和超管最高权限。都可以删
 
        if(!SessionUtil.ACCESS_CS_ADMIN.equals(code)&&!SessionUtil.ACCESS_SUPPER_ADMIN.equals(code)){
            if(userModel.getUserType().equals(CommonEnum.打包站运营员.getValue())){
                return JsonResult.failMessage("不能删除主打包员的角色!");
            }
            if(userModel.getUserType().equals(CommonEnum.运营员.getValue())){
                return JsonResult.failMessage("不能删除主运营员的角色!");
            }
            if(userModel.getUserType().equals(CommonEnum.总部管理员.getValue())){
                return JsonResult.failMessage("不能删除总部管理员的角色!");
            }
        }
 
        if(userModel.getUserType().equals(CommonEnum.回收员.getValue())){
            List<OrderInfo> list1 =   orderService.findByManageReceiver(userModel.getUserId());
            if(CollUtil.isNotEmpty(list1)){
                return JsonResult.failMessage("当前回收员存在未完成的订单");
            }
        }
 
        otherUserService.delCuserById(userModel.getUserId(), userModel.getMobilePhone(), userModel.getUserType());
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("userId",userModel.getUserId());
 
        OperationAppLog build = OperationAppLog.builder().appPrograme(CommonEnum.后台.getValue()).opreateName(getAdminName(request))
                .methodName(Constants.ORDER_MODUL_NAME).operateAction("身份设定删除-"+userModel.getUserId()).build();
        mqUtil.sendApp(build);
 
        return JsonResult.success(map);
    }
 
    /**
     * @param userModel
     * @return
     */
    @PostMapping( "/admin/front/role/addEditroleApi.json")
    @ApiOperation(value="用户管理-身份设定(添加和修改接口合并)", notes="test: 仅0有正确返回")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "password", value = "初始密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "userType", value = "用户类型", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "mobilePhone", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "area", value = "地区Id", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "cityId", value = "市Id", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "roleId", value = "角色Id", required = true, dataType = "String")
    })
    public JsonResult<Map<String, Object>>  addEditroleApi(@RequestBody UserEditroleModel userModel,HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        CoreUser user = sessionUtil.getCurrentUser();
        String code=user.getCode();
        if(SessionUtil.ACCESS_SUPPER_ADMIN.equals(code)||SessionUtil.ACCESS_CS_ADMIN.equals(code)){
            if(StrUtil.isBlank(userModel.getPartnerId())){
                return JsonResult.failMessage("超管新增或修改用户身份必须选择合伙人!");
            }
        }else{
            CityPartner partner = cityPartnerService.findById(Convert.toInt(user.getId()));
            if(null!=partner){
                if(userModel.getUserType().equals(CommonEnum.总部管理员.getValue())){
                    return JsonResult.failMessage("合伙人不能新建总部管理员!");
 
                }
            }
        }
        if(userModel.getArea()==null&&"".equals(userModel.getArea())){
            return JsonResult.failMessage("地区不能为空!");
        }
        if(userModel.getUserType().equals(CommonEnum.打包员.getValue())){
            return JsonResult.failMessage("不能新增或修改打包员,请在手机端进行该操作!");
        }
        if(userModel.getUserType().equals(CommonEnum.打包站运营员.getValue())){
            return JsonResult.failMessage("不能新增主打包员,每个打包站只能有一个主打包员!");
        }
        if(userModel.getUserType().equals(CommonEnum.入库员.getValue())){
            if(StrUtil.isBlank(userModel.getStorageId())){
                return JsonResult.failMessage("仓管员必须关联仓库!");
            }
        }
        //新增 : 回收员与入库员不能共存
        int num = userRoleService.addEditroleApi(userModel);
        if(num>0){
            map.put("code",0);
            map.put("changeNum",num);
            map.put("msg","添加成功");
        }else if(num==-1){
            return JsonResult.failMessage("回收员不能与入库员共存!");
        }else if(num==-2){
            return JsonResult.failMessage("不能新增打包员!");
        }else if(num==-3){
            return JsonResult.failMessage("不能新增打包员!");
        }else{
            return JsonResult.failMessage("添加身份,区域不能为空!");
        }
 
        OperationAppLog build = OperationAppLog.builder().appPrograme(CommonEnum.后台.getValue()).opreateName(getAdminName(request))
                .methodName(Constants.ORDER_MODUL_NAME).operateAction("身份设定-"+userModel.getUserId()).build();
        mqUtil.sendApp(build);
 
        return JsonResult.success(map);
    }
    /**
     * 查询字典里面的角色
     * @return
     */
    @PostMapping( "/admin/front/role/queryAllRoleApi.json")
    @ApiOperation(value="用户管理-身份设定(查询字典里面的角色)", notes="test: 仅0有正确返回")
    public JsonResult<List<Map<String, Object>>> queryAllRoleApi(){
        List<String> partnerIds = cityPartnerService.queryPartnerByCurrent();
        String partnerId= "";
        if(null!=partnerIds&&partnerIds.size()>0){
            partnerId = partnerIds.get(0);
        }
        List<Map<String, Object>> list = userRoleMapper.queryAllAPPRoleExt(partnerId);
        return JsonResult.success(list);
    }
 
}