自动同步部门人员信息

This commit is contained in:
2026-04-29 21:48:06 +08:00
parent 6fc319ac64
commit a8338f3bcf
3 changed files with 143 additions and 0 deletions

View File

@@ -6,9 +6,11 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.Map;
@RestController
@@ -93,4 +95,17 @@ public class LbDepartmentUserController {
}
return ResponseEntity.badRequest().body(result);
}
@PostMapping("/syncFromDailyUserTrade")
@Operation(summary = "从lb_daily_user_trade增量同步到部门用户")
public ResponseEntity<Map<String, Object>> syncFromDailyUserTrade(
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
Map<String, Object> result = lbDepartmentUserService.syncFromDailyUserTrade(startDate, endDate);
Boolean success = (Boolean) result.get("success");
if (success != null && success) {
return ResponseEntity.ok(result);
}
return ResponseEntity.badRequest().body(result);
}
}

View File

@@ -3,6 +3,7 @@ package com.rj.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.rj.entity.LbDepartmentUser;
import java.time.LocalDate;
import java.util.Map;
public interface ILbDepartmentUserService extends IService<LbDepartmentUser> {
@@ -27,4 +28,6 @@ public interface ILbDepartmentUserService extends IService<LbDepartmentUser> {
String childUserId,
String childUserName,
String tenantId);
Map<String, Object> syncFromDailyUserTrade(LocalDate startDate, LocalDate endDate);
}

View File

@@ -3,20 +3,35 @@ package com.rj.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rj.entity.LbDailyUserTrade;
import com.rj.entity.LbDepartmentUser;
import com.rj.entity.LbPurchaseApply;
import com.rj.mapper.LbDailyUserTradeMapper;
import com.rj.mapper.LbDepartmentUserMapper;
import com.rj.mapper.LbPurchaseApplyMapper;
import com.rj.service.ILbDepartmentUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@Service
public class LbDepartmentUserServiceImpl extends ServiceImpl<LbDepartmentUserMapper, LbDepartmentUser>
implements ILbDepartmentUserService {
@Autowired
private LbDailyUserTradeMapper lbDailyUserTradeMapper;
@Autowired
private LbPurchaseApplyMapper lbPurchaseApplyMapper;
@Override
public Map<String, Object> add(LbDepartmentUser entity) {
Map<String, Object> result = new HashMap<>();
@@ -223,4 +238,114 @@ public class LbDepartmentUserServiceImpl extends ServiceImpl<LbDepartmentUserMap
return result;
}
}
@Override
public Map<String, Object> syncFromDailyUserTrade(LocalDate startDate, LocalDate endDate) {
Map<String, Object> result = new HashMap<>();
try {
if (startDate == null || endDate == null) {
result.put("success", false);
result.put("message", "开始日期和结束日期不能为空");
return result;
}
if (startDate.isAfter(endDate)) {
result.put("success", false);
result.put("message", "开始日期不能大于结束日期");
return result;
}
List<LbDailyUserTrade> tradeList = lbDailyUserTradeMapper.selectList(
new LambdaQueryWrapper<LbDailyUserTrade>()
.ge(LbDailyUserTrade::getReportDate, startDate)
.le(LbDailyUserTrade::getReportDate, endDate)
.isNotNull(LbDailyUserTrade::getNickname)
);
if (tradeList == null || tradeList.isEmpty()) {
result.put("success", true);
result.put("message", "无可同步数据");
result.put("insertedCount", 0);
result.put("skippedCount", 0);
result.put("sourceCount", 0);
result.put("startDate", startDate);
result.put("endDate", endDate);
return result;
}
List<LbDepartmentUser> existedUsers = this.list(
new LambdaQueryWrapper<LbDepartmentUser>()
.select(LbDepartmentUser::getName)
);
Set<String> existingNameSet = new HashSet<>();
for (LbDepartmentUser user : existedUsers) {
if (user.getName() != null && !user.getName().trim().isEmpty()) {
existingNameSet.add(user.getName().trim());
}
}
List<LbDepartmentUser> toInsert = new ArrayList<>();
int skippedCount = 0;
LocalDateTime now = LocalDateTime.now();
for (LbDailyUserTrade trade : tradeList) {
String nickname = trade.getNickname() == null ? null : trade.getNickname().trim();
if (nickname == null || nickname.isEmpty()) {
skippedCount++;
continue;
}
if (existingNameSet.contains(nickname)) {
skippedCount++;
continue;
}
LbDepartmentUser entity = new LbDepartmentUser();
entity.setId(UUID.randomUUID().toString());
entity.setTenantId(trade.getTenantId());
entity.setUserId(trade.getUserId());
entity.setName(nickname);
// 若手机号为空,则尝试从进货申请表补齐
entity.setPhone(findPhoneFromPurchaseApply(trade.getTenantId(), nickname));
entity.setCreateTime(now);
entity.setUpdateTime(now);
toInsert.add(entity);
existingNameSet.add(nickname);
}
if (!toInsert.isEmpty()) {
this.saveBatch(toInsert);
}
result.put("success", true);
result.put("message", "同步完成");
result.put("insertedCount", toInsert.size());
result.put("skippedCount", skippedCount);
result.put("sourceCount", tradeList.size());
result.put("startDate", startDate);
result.put("endDate", endDate);
return result;
} catch (Exception e) {
result.put("success", false);
result.put("message", "同步异常:" + e.getMessage());
return result;
}
}
private String findPhoneFromPurchaseApply(String tenantId, String applyUser) {
if (applyUser == null || applyUser.trim().isEmpty()) {
return null;
}
LambdaQueryWrapper<LbPurchaseApply> queryWrapper = new LambdaQueryWrapper<>();
if (tenantId != null && !tenantId.trim().isEmpty()) {
queryWrapper.eq(LbPurchaseApply::getTenantId, tenantId.trim());
}
queryWrapper.eq(LbPurchaseApply::getApplyUser, applyUser.trim())
.isNotNull(LbPurchaseApply::getApplyPhone)
.orderByDesc(LbPurchaseApply::getUpdateTime)
.orderByDesc(LbPurchaseApply::getCreateTime)
.last("limit 1");
LbPurchaseApply purchaseApply = lbPurchaseApplyMapper.selectOne(queryWrapper);
if (purchaseApply == null || purchaseApply.getApplyPhone() == null) {
return null;
}
String phone = purchaseApply.getApplyPhone().trim();
return phone.isEmpty() ? null : phone;
}
}