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> 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 select = otherUserMapper.selectByExample(example); if(CollUtil.isNotEmpty(select)){ for (OtherUserInfo userInfo : select) { List byRoleCode = userRoleService.findByRoleCode(userInfo.getUserType()); if(CollUtil.isNotEmpty(byRoleCode)){ userInfo.setUserTypeName(byRoleCode.get(0).getRoleName()); } String townId = userInfo.getTownId(); List 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 pageInfo=new PageInfo<>(select); return Result.success(pageInfo); } @ApiOperation( value = "查询所有角色") @PostMapping("/role/selectUserType") public Result> add(HttpServletRequest request,@RequestBody SelectUserTypeDTO selectUserTypeDTO) { String userId = getUserId(request); List byRoleCode=null; //根据运营员查询其下所属的角色 if(StrUtil.isBlank(selectUserTypeDTO.getExtra())) { String mobilePhone = otherUserService.findById(userId).getMobilePhone(); //查出所有角色 List byMobile = otherUserService.findByMobile(mobilePhone); List 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 byMobile = otherUserService.findByMobileForBidden(roleDto.getMobilePhone()); if(CollUtil.isNotEmpty(byMobile)){ throw new RestException("该手机号已存在别的身份"); } Result 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 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> queryEditroleApi(@RequestBody UserEditroleModel userModel){ Map 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> 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 list1 = orderService.findByManageReceiver(userModel.getUserId()); if(CollUtil.isNotEmpty(list1)){ return JsonResult.failMessage("当前回收员存在未完成的订单"); } } otherUserService.delCuserById(userModel.getUserId(), userModel.getMobilePhone(), userModel.getUserType()); Map 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> addEditroleApi(@RequestBody UserEditroleModel userModel,HttpServletRequest request){ Map 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>> queryAllRoleApi(){ List partnerIds = cityPartnerService.queryPartnerByCurrent(); String partnerId= ""; if(null!=partnerIds&&partnerIds.size()>0){ partnerId = partnerIds.get(0); } List> list = userRoleMapper.queryAllAPPRoleExt(partnerId); return JsonResult.success(list); } }