package com.szwl.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.szwl.feign.bean.SyncOldFeign; import com.szwl.model.bo.R; import com.szwl.model.bo.ResponseModel; import com.szwl.model.entity.SysRole; import com.szwl.model.entity.SysUserRole; import com.szwl.model.entity.TAdmin; import com.szwl.service.SysRoleService; import com.szwl.service.SysUserRoleService; import com.szwl.service.TAdminService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; import java.util.List; import java.util.Objects; @Slf4j @Api(value = "/syncOldAdmin", tags = {"同步旧系统的 admin 用户数据"}) @RestController @RequestMapping("/syncOldAdmin") public class SyncOldAdminController { TAdminService adminService; SyncOldFeign syncOldFeign; SysRoleService sysRoleService; SysUserRoleService sysUserRoleService; public SyncOldAdminController(TAdminService adminService, SyncOldFeign syncOldFeign, SysRoleService sysRoleService, SysUserRoleService sysUserRoleService) { this.adminService = adminService; this.syncOldFeign = syncOldFeign; this.sysRoleService = sysRoleService; this.sysUserRoleService = sysUserRoleService; } @ApiOperation(value = "根据旧系统的层级关系来关联上级客户") @PostMapping("/relevanceSuperiorCustomer") public ResponseModel relevanceSuperiorCustomer(String startTime, String endTime) { int i = 0; // 根据时间获取旧系统用户信息 List oldAdminList = R.getDataIfSuccess(syncOldFeign.getAdminInTime(startTime, endTime)); // 先看在新系统中这些用户有没有关联上级 for (TAdmin admin : oldAdminList) { Long id = admin.getId(); LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.eq(TAdmin::getId, id); TAdmin adminNew = adminService.getOne(wrapper); if (Objects.nonNull(adminNew)) { String relationAdminId = adminNew.getRelationAdminId(); String adminId = String.valueOf(id); if (StringUtils.isEmpty(relationAdminId)) { // 如果在新系统中未关联上级 // 获取旧系统中对应的parentId Long parentId = R.getDataIfSuccess(syncOldFeign.getParentId(adminId)); if (parentId == null) { adminNew.setRelationAdminId(""); } adminNew.setRelationAdminId(String.valueOf(parentId)); adminService.updateById(adminNew); i++; } } } return R.ok("关联成功" + i + "个客户"); } @ApiOperation(value = "给指定时间范围内的所有旧系统用户设置新系统权限") @PostMapping("/addAllRoleInTime") public ResponseModel addAllRoleInTime(String startTime, String endTime) { if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) { return R.fail("参数不能为空"); } // 查看该时间范围内的用户 List oldAdminList = R.getDataIfSuccess(syncOldFeign.getAdminInTime(startTime, endTime)); for (TAdmin admin : oldAdminList) { Long adminId = admin.getId(); // 查询该 adminId 有没有权限 LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); lqw.eq(SysRole::getAdminId, adminId); List sysRoleList = sysRoleService.list(lqw); if (sysRoleList.isEmpty()) { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.eq(SysRole::getAdminId, "1"); wrapper.eq(SysRole::getRoleName, "商家"); SysRole template = sysRoleService.getOne(wrapper); SysRole role = new SysRole(); role.setAdminId(adminId); role.setMenuCodesJson(template.getMenuCodesJson()); role.setRoleName("商家"); sysRoleService.save(role); SysUserRole sysUserRole = new SysUserRole(); sysUserRole.setRoleId(role.getRoleId()); sysUserRole.setUserId(String.valueOf(adminId)); sysUserRoleService.save(sysUserRole); } } return R.ok("添加" + startTime + "至" + endTime + "的权限成功"); } @ApiOperation(value = "给用户 adminId 添加 sys_role 和 sys_user_role") @PostMapping("/addSysRole") public ResponseModel addSysRole(Long adminId) { if (adminId == null) { return R.fail("参数为空"); } // 查询该 adminId 有没有权限 LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); lqw.eq(SysRole::getAdminId, adminId); List sysRoleList = sysRoleService.list(lqw); if (sysRoleList.size() == 0) { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.eq(SysRole::getAdminId, "1"); wrapper.eq(SysRole::getRoleName, "商家"); SysRole template = sysRoleService.getOne(wrapper); SysRole role = new SysRole(); role.setAdminId(adminId); role.setMenuCodesJson(template.getMenuCodesJson()); role.setRoleName("商家"); sysRoleService.save(role); SysUserRole sysUserRole = new SysUserRole(); sysUserRole.setRoleId(role.getRoleId()); sysUserRole.setUserId(String.valueOf(adminId)); sysUserRoleService.save(sysUserRole); } return R.ok("添加" + adminId + "的权限成功"); } @ApiOperation(value = "修改 admin 的 type 类型") @PostMapping("/updateAdminInfo") public ResponseModel updateAdminInfo(String startTime, String endTime) { if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) { LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); lqw.between(TAdmin::getCreateDate, startTime, endTime); List adminList = adminService.list(lqw); for (TAdmin admin : adminList) { Integer type = admin.getType(); if (type == 0) { admin.setType(1); adminService.updateById(admin); } if (type == 1) { admin.setType(2); adminService.updateById(admin); } // 旧系统的是按照:管理,省级,市级,终端进行划分 // if (type == 3) { // // } } return R.ok("修改 admin 信息成功"); } else { return R.fail("时间参数不能为空"); } } @ApiOperation(value = "同步旧系统某一时间段内的 t_admin ") // admin 表已改回自增 @PostMapping("/syncAdminsInTime") public ResponseModel syncAdminsInTime(String startTime, String endTime) { if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) { List oldAdminList = R.getDataIfSuccess(syncOldFeign.getAdminInTime(startTime, endTime)); // System.out.println(oldAdminList); for (TAdmin oldAdmin : oldAdminList) { // adminService.saveOrUpdate(oldAdmin); // 防止新旧表结构不同,不做直接插入 Long id = oldAdmin.getId(); Date createDate = oldAdmin.getCreateDate(); Date modifyDate = oldAdmin.getModifyDate(); Long areaId = oldAdmin.getAreaId(); String qrCodeImgUrl = oldAdmin.getQrCodeImgUrl(); String department = oldAdmin.getDepartment(); String email = oldAdmin.getEmail(); Boolean isAdmined = oldAdmin.getIsAdmined(); Boolean isEnabled = oldAdmin.getIsEnabled(); Boolean isLocked = oldAdmin.getIsLocked(); Date lockedDate = oldAdmin.getLockedDate(); Date loginDate = oldAdmin.getLoginDate(); Integer loginFailureCount = oldAdmin.getLoginFailureCount(); String loginIp = oldAdmin.getLoginIp(); String tradeMerchantNo = oldAdmin.getTradeMerchantNo(); String name = oldAdmin.getName(); Long parentId = oldAdmin.getParentId(); String password = oldAdmin.getPassword(); Long noticeId = oldAdmin.getNoticeId(); Integer type = oldAdmin.getType(); String username = oldAdmin.getUsername(); String phone = oldAdmin.getPhone(); String isRefund = oldAdmin.getIsRefund(); String ifForeign = oldAdmin.getIfForeign(); String open = oldAdmin.getOpen(); String promoCodeOpen = oldAdmin.getPromoCodeOpen(); Date applyStartTime = oldAdmin.getApplyStartTime(); Date applyEndTime = oldAdmin.getApplyEndTime(); String code = oldAdmin.getCode(); String payPlatform = oldAdmin.getPayPlatform(); String logoRule = oldAdmin.getLogoRule(); String relationAdminId = oldAdmin.getRelationAdminId(); String managerId = oldAdmin.getManagerId(); Long agencyId = oldAdmin.getAgencyId(); Long merchantId = oldAdmin.getMerchantId(); Long personageId = oldAdmin.getPersonageId(); String companyType = oldAdmin.getCompanyType(); String currencySymbol = oldAdmin.getCurrencySymbol(); // TAdmin(id=1881, createDate=Mon Mar 20 18:22:07 CST 2023, modifyDate=Mon Mar 20 18:22:15 CST 2023, areaId=221, qrCodeImgUrl=null, department=null, email=1924983280@qq.com, isAdmined=false, isEnabled=true, isLocked=false, lockedDate=null, loginDate=Mon Mar 20 18:22:15 CST 2023, loginFailureCount=0, loginIp=127.0.0.1, tradeMerchantNo=null, name=张洁, parentId=1123, password=6b8b2ced48967281f4c13313ce0b39d7, noticeId=26, type=3, username=shx18334775173, phone=18334775173, isRefund=0, ifForeign=0, open=0, promoCodeOpen=null, applyStartTime=Mon Mar 20 18:22:07 CST 2023, applyEndTime=Mon Mar 27 18:22:07 CST 2023, code=null, payPlatform=null, logoRule=null, relationAdminId=null, managerId=null, agencyId=670, merchantId=1123, personageId=1881, companyType=null, currencySymbol=null) TAdmin admin = new TAdmin(); admin.setId(id); admin.setCreateDate(createDate); admin.setModifyDate(modifyDate); admin.setAreaId(areaId); admin.setQrCodeImgUrl(qrCodeImgUrl); admin.setDepartment(department); admin.setEmail(email); admin.setIsAdmined(isAdmined); admin.setIsEnabled(isEnabled); admin.setIsLocked(isLocked); admin.setLockedDate(lockedDate); admin.setLoginDate(loginDate); admin.setLoginFailureCount(loginFailureCount); admin.setLoginIp(loginIp); admin.setTradeMerchantNo(tradeMerchantNo); admin.setName(name); admin.setParentId(parentId); admin.setPassword(password); admin.setNoticeId(noticeId); admin.setType(type); admin.setUsername(username); admin.setPhone(phone); admin.setIsRefund(isRefund); admin.setIfForeign(ifForeign); admin.setOpen(open); admin.setPromoCodeOpen(promoCodeOpen); admin.setApplyStartTime(applyStartTime); admin.setApplyEndTime(applyEndTime); admin.setCode(code); admin.setPayPlatform(payPlatform); admin.setLogoRule(logoRule); admin.setRelationAdminId(relationAdminId); admin.setManagerId(managerId); admin.setAgencyId(agencyId); admin.setMerchantId(merchantId); admin.setPersonageId(personageId); admin.setCompanyType(companyType); admin.setCurrencySymbol(currencySymbol); adminService.save(admin); } return R.ok("同步" + startTime + "至" + endTime + "的 admin 信息成功"); }else { return R.fail("时间参数不能为空"); } } }