package cn.huge.module.sync.service; import cn.huge.module.client.api.impl.CustClientImpl; import cn.huge.module.sync.dao.mapper.SyncMapper; import cn.huge.module.sync.domain.source.SourceCtAccount; import cn.huge.module.sync.domain.source.SourceCtUnit; import cn.huge.module.sync.domain.target.TargetCtAccount; import cn.huge.module.sync.domain.target.TargetCtUnit; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.converters.DateConverter; import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import lombok.extern.slf4j.Slf4j; import org.springframework.transaction.annotation.Transactional; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * @author liyj * @version 1.0.0 * @title: 客户用户账号表业务逻辑处理 * @Description 客户用户账号表业务逻辑处理 * @company hugeinfo * @Time 2024-08-17 15:30:56 */ @Slf4j @Service @Transactional(rollbackFor = Exception.class) public class SyncService { @Autowired private SyncMapper syncMapper; @Autowired private CustClientImpl custClient; private static final String MYSQL_TABLE = "dyh_ct_account"; public void syncData() { try { List sourceTableList = new ArrayList<>(); int page = 1; int size = 1000; //查询mysql总量 int total = syncMapper.countData(MYSQL_TABLE); //查出最大页数 int maxPage = 1; maxPage = (total / size); if (0 != total % size) { maxPage += 1; } for (int i = 1; i <= maxPage; i++) { // 查询MySQL表结构 Sort sort = Sort.by(Sort.Direction.ASC, "create_time"); PageRequest pageRequest = PageRequest.of(page-1, size, sort); sourceTableList = syncMapper.pageInfo(MYSQL_TABLE, pageRequest); page++; List targetTableList = new ArrayList<>(); for(SourceCtAccount sourceTable: sourceTableList){ TargetCtAccount tagetTable = new TargetCtAccount(); ConvertUtils.register(new DateConverter(null), java.util.Date.class); BeanUtils.copyProperties(tagetTable, sourceTable); // for(Field field: sourceCtUnit.getClass().getDeclaredFields()){ // convertType(field.get(sourceCtUnit), field.getName(), targetCtUnit); // } //字段名不一致,手动set tagetTable.setCipher(sourceTable.getCipher()); tagetTable.setAccType(1); targetTableList.add(tagetTable); } if(CollectionUtils.isNotEmpty(targetTableList)){ custClient.saveTableList1(targetTableList); } } } catch (Exception e) { e.printStackTrace(); } } /** * 时间转换的时候,调用初始化,时间类型转换 * 使用:直接在实体中加上一个注解形式。 */ public static void initDate(){ // 时间注册 DateConverter converter = new DateConverter(null); converter.setPattern(new String("yyyy-MM-dd HH:mm:ss")); ConvertUtils.register(converter, Date.class); } public void convertType(Object value, String columnName, TargetCtUnit targetCtUnit) { if (value == null) { targetCtUnit.setUnitType(Integer.valueOf("NULL")); }else if("unitType".equals(columnName) || "joinWay".equals(columnName) || "findStatus".equals(columnName) || "dispStatus".equals(columnName)|| "deleteStatus".equals(columnName)){ targetCtUnit.setUnitType(Integer.parseInt((String) value)); } } }