拆分模块
This commit is contained in:
175
server/src/main/java/com/ecep/contract/cloud/AbstractCtx.java
Normal file
175
server/src/main/java/com/ecep/contract/cloud/AbstractCtx.java
Normal file
@@ -0,0 +1,175 @@
|
||||
package com.ecep.contract.cloud;
|
||||
|
||||
import static com.ecep.contract.SpringApp.getBean;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.ds.other.service.SysConfService;
|
||||
import com.ecep.contract.util.MyStringUtils;
|
||||
import com.ecep.contract.util.NumberUtils;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public class AbstractCtx {
|
||||
@Setter
|
||||
SysConfService confService;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
Locale locale = Locale.getDefault();
|
||||
|
||||
public SysConfService getConfService() {
|
||||
if (confService == null) {
|
||||
confService = getBean(SysConfService.class);
|
||||
}
|
||||
return confService;
|
||||
}
|
||||
|
||||
|
||||
public boolean updateText(Supplier<String> getter, Consumer<String> setter, String text, MessageHolder holder, String topic) {
|
||||
if (!Objects.equals(getter.get(), text)) {
|
||||
setter.accept(text);
|
||||
holder.info(topic + "修改为: " + text);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean updateAppendText(Supplier<String> getter, Consumer<String> setter, String text, MessageHolder holder, String topic) {
|
||||
if (StringUtils.hasText(text)) {
|
||||
String str = MyStringUtils.appendIfAbsent(getter.get(), text);
|
||||
if (!Objects.equals(getter.get(), str)) {
|
||||
setter.accept(text);
|
||||
holder.info(topic + "修改为: " + text);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean updateLocalDate(Supplier<LocalDate> getter, Consumer<LocalDate> setter, java.sql.Date date, MessageHolder holder, String topic) {
|
||||
if (date != null) {
|
||||
return updateLocalDate(getter, setter, date.toLocalDate(), holder, topic);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean updateLocalDate(Supplier<LocalDate> getter, Consumer<LocalDate> setter, LocalDate date, MessageHolder holder, String topic) {
|
||||
return updateLocalDate(getter, setter, date, holder, topic, false);
|
||||
}
|
||||
|
||||
public boolean updateLocalDate(Supplier<LocalDate> getter, Consumer<LocalDate> setter, LocalDate date, MessageHolder holder, String topic, boolean allowNull) {
|
||||
if (date == null && !allowNull) {
|
||||
return false;
|
||||
}
|
||||
if (!Objects.equals(getter.get(), date)) {
|
||||
setter.accept(date);
|
||||
holder.info(topic + "更新为 " + date);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean updateLocalDate(Supplier<LocalDate> getter, Consumer<LocalDate> setter, String strDate, MessageHolder holder, String topic) {
|
||||
LocalDate date = null;
|
||||
if (StringUtils.hasText(strDate)) {
|
||||
try {
|
||||
date = LocalDate.parse(strDate);
|
||||
} catch (DateTimeParseException e) {
|
||||
holder.warn("无法解析的日期:" + strDate);
|
||||
}
|
||||
}
|
||||
return updateLocalDate(getter, setter, date, holder, topic);
|
||||
}
|
||||
|
||||
public boolean updateLocalDate(Supplier<LocalDate> getter, Consumer<LocalDate> setter, Timestamp timestamp, MessageHolder holder, String topic) {
|
||||
LocalDate date = null;
|
||||
|
||||
if (timestamp != null) {
|
||||
try {
|
||||
date = timestamp.toLocalDateTime().toLocalDate();
|
||||
} catch (DateTimeParseException e) {
|
||||
holder.warn("解析日期" + timestamp + " 异常:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
return updateLocalDate(getter, setter, date, holder, topic);
|
||||
}
|
||||
|
||||
public boolean updateLocalDateTime(Supplier<LocalDateTime> getter, Consumer<LocalDateTime> setter, Timestamp timestamp, MessageHolder holder, String topic) {
|
||||
LocalDateTime dateTime = null;
|
||||
|
||||
if (timestamp != null) {
|
||||
try {
|
||||
// fixed nanos
|
||||
timestamp.setNanos(0);
|
||||
dateTime = timestamp.toLocalDateTime();
|
||||
} catch (DateTimeParseException e) {
|
||||
holder.warn("解析日期" + timestamp + " 异常:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if (!Objects.equals(getter.get(), dateTime)) {
|
||||
setter.accept(dateTime);
|
||||
holder.info(topic + "修改为: " + dateTime);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean updateInstant(Supplier<Instant> getter, Consumer<Instant> setter, Instant instant, MessageHolder holder, String topic) {
|
||||
if (!Objects.equals(getter.get(), instant)) {
|
||||
setter.accept(instant);
|
||||
holder.info(topic + "修改为: " + instant);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean updateNumber(Supplier<Double> getter, Consumer<Double> setter, BigDecimal value, MessageHolder holder, String topic) {
|
||||
double val = value.doubleValue();
|
||||
return updateNumber(getter, setter, val, holder, topic);
|
||||
}
|
||||
|
||||
public boolean updateNumber(Supplier<Double> getter, Consumer<Double> setter, double value, MessageHolder holder, String topic) {
|
||||
if (getter.get() == null || !NumberUtils.equals(getter.get(), value)) {
|
||||
setter.accept(value);
|
||||
holder.info(topic + "修改为: " + value);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean updateNumber(Supplier<Float> getter, Consumer<Float> setter, float value, MessageHolder holder, String topic) {
|
||||
if (getter.get() == null || !NumberUtils.equals(getter.get(), value)) {
|
||||
setter.accept(value);
|
||||
holder.info(topic + "修改为: " + value);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean updateNumber(Supplier<Integer> getter, Consumer<Integer> setter, Integer value, MessageHolder holder, String topic) {
|
||||
if (getter.get() == null || !NumberUtils.equals(getter.get(), value)) {
|
||||
setter.accept(value);
|
||||
holder.info(topic + "修改为: " + value);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.ecep.contract.cloud;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.hibernate.annotations.ColumnDefault;
|
||||
import org.hibernate.proxy.HibernateProxy;
|
||||
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.model.IdentityEntity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import jakarta.persistence.Version;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* 记录同步来源
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
// @Entity
|
||||
// @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
|
||||
@ToString
|
||||
@MappedSuperclass
|
||||
public abstract class CloudBaseInfo implements IdentityEntity {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "ID", nullable = false)
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 平台编号
|
||||
*/
|
||||
@Column(name = "CLOUD_ID")
|
||||
private String cloudId;
|
||||
|
||||
/**
|
||||
* 本地更新时间戳,控制更新频率和重复更新
|
||||
*/
|
||||
@Column(name = "LATEST_UPDATE")
|
||||
private Instant latestUpdate;
|
||||
|
||||
/**
|
||||
* 关联的公司
|
||||
*/
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "COMPANY_ID")
|
||||
@ToString.Exclude
|
||||
private Company company;
|
||||
|
||||
@Version
|
||||
@ColumnDefault("0")
|
||||
@Column(name = "VERSION", nullable = false)
|
||||
private int version;
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null) return false;
|
||||
Class<?> oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass();
|
||||
Class<?> thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass();
|
||||
if (thisEffectiveClass != oEffectiveClass) return false;
|
||||
CloudBaseInfo cloudInfo = (CloudBaseInfo) o;
|
||||
return getId() != null && Objects.equals(getId(), cloudInfo.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
|
||||
}
|
||||
}
|
||||
90
server/src/main/java/com/ecep/contract/cloud/CloudInfo.java
Normal file
90
server/src/main/java/com/ecep/contract/cloud/CloudInfo.java
Normal file
@@ -0,0 +1,90 @@
|
||||
package com.ecep.contract.cloud;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.hibernate.annotations.ColumnDefault;
|
||||
import org.hibernate.proxy.HibernateProxy;
|
||||
|
||||
import com.ecep.contract.CloudType;
|
||||
import com.ecep.contract.model.Company;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Table;
|
||||
import jakarta.persistence.Version;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* 记录同步来源
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
//@org.springframework.data.relational.core.mapping.Table(name = "CLOUD_INFO")
|
||||
@Table(name = "CLOUD_INFO", schema = "supplier_ms")
|
||||
@ToString
|
||||
public class CloudInfo {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "ID", nullable = false)
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 记录源类型
|
||||
*/
|
||||
@Column(name = "CLOUD_TYPE", length = 50)
|
||||
private CloudType type;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Column(name = "CLOUD_ID")
|
||||
private String cloudId;
|
||||
|
||||
/**
|
||||
* 本地更新时间戳,控制更新频率和重复更新
|
||||
*/
|
||||
@Column(name = "LATEST_UPDATE")
|
||||
private Instant latestUpdate;
|
||||
|
||||
/**
|
||||
* 关联的公司
|
||||
*/
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "COMPANY_ID")
|
||||
@ToString.Exclude
|
||||
private Company company;
|
||||
|
||||
@Version
|
||||
@ColumnDefault("0")
|
||||
@Column(name = "VERSION", nullable = false)
|
||||
private int version;
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null) return false;
|
||||
Class<?> oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass();
|
||||
Class<?> thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass();
|
||||
if (thisEffectiveClass != oEffectiveClass) return false;
|
||||
CloudInfo cloudInfo = (CloudInfo) o;
|
||||
return getId() != null && Objects.equals(getId(), cloudInfo.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.ecep.contract.cloud;
|
||||
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Lazy
|
||||
@Repository
|
||||
public interface CloudInfoRepository
|
||||
// curd
|
||||
extends CrudRepository<CloudInfo, Integer>, PagingAndSortingRepository<CloudInfo, Integer> {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.ecep.contract.cloud;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.data.repository.config.BootstrapMode;
|
||||
|
||||
@Configuration
|
||||
@EnableJpaRepositories(basePackages = {"com.ecep.contract.cloud"}, bootstrapMode = BootstrapMode.LAZY)
|
||||
public class CloudRepositoriesConfig {
|
||||
// @Bean
|
||||
// @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
// public BeanDefinition cloudInfoRepositoryDefinition() {
|
||||
// BeanDefinition definition = new org.springframework.beans.factory.support.GenericBeanDefinition();
|
||||
// definition.setBeanClassName("com.ecep.contract.manager.cloud.CloudInfoRepository");
|
||||
// definition.setLazyInit(true);
|
||||
// return definition;
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
// public BeanDefinition cloudRkRepositoryDefinition() {
|
||||
// BeanDefinition definition = new org.springframework.beans.factory.support.GenericBeanDefinition();
|
||||
// definition.setBeanClassName("com.ecep.contract.manager.cloud.u8.CloudRkRepository");
|
||||
// definition.setLazyInit(true);
|
||||
// return definition;
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
// public BeanDefinition cloudTycRepositoryDefinition() {
|
||||
// BeanDefinition definition = new org.springframework.beans.factory.support.GenericBeanDefinition();
|
||||
// definition.setBeanClassName("com.ecep.contract.manager.cloud.u8.CloudTycRepository");
|
||||
// definition.setLazyInit(true);
|
||||
// return definition;
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
// public BeanDefinition cloudYuRepositoryDefinition() {
|
||||
// BeanDefinition definition = new org.springframework.beans.factory.support.GenericBeanDefinition();
|
||||
// definition.setBeanClassName("com.ecep.contract.manager.cloud.u8.CloudYuRepository");
|
||||
// definition.setLazyInit(true);
|
||||
// return definition;
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.ecep.contract.cloud.old;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.ecep.contract.model.CompanyContact;
|
||||
import com.ecep.contract.model.CompanyContract;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class CompanyContactUtils {
|
||||
/**
|
||||
* 使用 map 应用到 contact, 如有更新, 则返回 true
|
||||
*/
|
||||
public static boolean applyContactByMap(CompanyContact contact, Map<String, Object> map) {
|
||||
boolean modified = false;
|
||||
String name = (String) map.get("NAME");
|
||||
String phone = (String) map.get("PHONE");
|
||||
String email = (String) map.get("EMAIL");
|
||||
String address = (String) map.get("ADDRESS");
|
||||
if (name != null) {
|
||||
// 更新备注
|
||||
if (!Objects.equals(contact.getName(), name)) {
|
||||
contact.setName(name);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (phone != null) {
|
||||
// 更新备注
|
||||
if (!Objects.equals(contact.getPhone(), phone)) {
|
||||
contact.setPhone(phone);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (email != null) {
|
||||
// 更新备注
|
||||
if (!Objects.equals(contact.getEmail(), email)) {
|
||||
contact.setEmail(email);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (address != null) {
|
||||
// 更新备注
|
||||
if (!Objects.equals(contact.getAddress(), address)) {
|
||||
contact.setAddress(address);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
/**
|
||||
* 要和{@link CompanyContract#getContactKey(CompanyContact)}一致
|
||||
*/
|
||||
public static String getContactKey(Map<String, Object> map) {
|
||||
if (map == null) {
|
||||
return null;
|
||||
}
|
||||
String name = (String) map.get("NAME");
|
||||
String phone = (String) map.get("PHONE");
|
||||
String email = (String) map.get("EMAIL");
|
||||
return name + phone + email;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.ecep.contract.cloud.old;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.ecep.contract.model.CompanyCustomerFile;
|
||||
|
||||
public class CompanyCustomerFileUtils {
|
||||
public static boolean applyCustomerFileByMap(CompanyCustomerFile customerFile, Map<String, Object> m) {
|
||||
boolean modified = false;
|
||||
|
||||
String filePath = (String) m.get("FILE_PATH");
|
||||
String editFilePath = (String) m.get("EDIT_FILE_PATH");
|
||||
java.sql.Date signDate = (java.sql.Date) m.get("SIGN_DATE");
|
||||
Boolean valid = (Boolean) m.get("VALID");
|
||||
|
||||
if (filePath != null) {
|
||||
// file path
|
||||
if (!Objects.equals(customerFile.getFilePath(), filePath)) {
|
||||
customerFile.setFilePath(filePath);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (editFilePath != null) {
|
||||
// edit file path
|
||||
if (!Objects.equals(customerFile.getEditFilePath(), editFilePath)) {
|
||||
customerFile.setEditFilePath(editFilePath);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (signDate != null) {
|
||||
// date
|
||||
LocalDate localDate = signDate.toLocalDate();
|
||||
if (!Objects.equals(customerFile.getSignDate(), localDate)) {
|
||||
customerFile.setSignDate(localDate);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (valid != null) {
|
||||
// valid
|
||||
if (!Objects.equals(customerFile.isValid(), valid)) {
|
||||
customerFile.setValid(valid);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
public static String getCustomerFileKey(Map<String, Object> m) {
|
||||
return m.get("FILE_PATH").toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.ecep.contract.cloud.old;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.ecep.contract.model.CompanyVendorFile;
|
||||
|
||||
public class CompanyVendorFileUtils {
|
||||
public static boolean applyVendorFileByMap(CompanyVendorFile vendorFile, Map<String, Object> m) {
|
||||
boolean modified = false;
|
||||
|
||||
String filePath = (String) m.get("FILE_PATH");
|
||||
String editFilePath = (String) m.get("EDIT_FILE_PATH");
|
||||
java.sql.Date signDate = (java.sql.Date) m.get("SIGN_DATE");
|
||||
Boolean valid = (Boolean) m.get("VALID");
|
||||
|
||||
if (filePath != null) {
|
||||
// file path
|
||||
if (!Objects.equals(vendorFile.getFilePath(), filePath)) {
|
||||
vendorFile.setFilePath(filePath);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (editFilePath != null) {
|
||||
// edit file path
|
||||
if (!Objects.equals(vendorFile.getEditFilePath(), editFilePath)) {
|
||||
vendorFile.setEditFilePath(editFilePath);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (signDate != null) {
|
||||
// date
|
||||
LocalDate localDate = signDate.toLocalDate();
|
||||
if (!Objects.equals(vendorFile.getSignDate(), localDate)) {
|
||||
vendorFile.setSignDate(localDate);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (valid != null) {
|
||||
// valid
|
||||
if (!Objects.equals(vendorFile.isValid(), valid)) {
|
||||
vendorFile.setValid(valid);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
public static String getVendorFileKey(Map<String, Object> m) {
|
||||
return m.get("FILE_PATH").toString();
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,124 @@
|
||||
package com.ecep.contract.cloud.old;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.ds.company.service.CompanyService;
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.model.Contract;
|
||||
import com.ecep.contract.ui.Tasker;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class OldVersionSyncCustomerTask extends Tasker<Object> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(OldVersionSyncCustomerTask.class);
|
||||
|
||||
private final OldVersionService service = SpringApp.getBean(OldVersionService.class);
|
||||
private final CompanyService companyService = SpringApp.getBean(CompanyService.class);
|
||||
|
||||
private Map<Object, List<Map<String, Object>>> oldNameGroupedMap;
|
||||
private Map<Object, List<Map<String, Object>>> contactGroupedMap;
|
||||
private File basePath;
|
||||
private String titlePrefix = "";
|
||||
|
||||
public OldVersionSyncCustomerTask() {
|
||||
updateTitle("老版本-同步客户数据");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object execute(MessageHolder holder) throws Exception {
|
||||
updateTitle("老版本-同步客户数据");
|
||||
basePath = companyService.getCustomerBasePath();
|
||||
List<Runnable> runnable = Arrays.asList(this::loadOldNames, this::loadContacts, this::syncCustomers,
|
||||
this::syncContracts);
|
||||
|
||||
for (int i = 0; i < runnable.size(); i++) {
|
||||
titlePrefix = "老版本-同步客户数据-" + (i + 1) + "/" + runnable.size() + "-";
|
||||
try {
|
||||
runnable.get(i).run();
|
||||
} catch (Exception e) {
|
||||
logger.error("运行至 {} 时,发生错误中断", titlePrefix, e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void loadOldNames() {
|
||||
try (Stream<Map<String, Object>> stream = service.queryAllCustomerOldNameForStream()) {
|
||||
updateTitle("载入曾用名");
|
||||
oldNameGroupedMap = stream.takeWhile(v -> !isCancelled()).parallel()
|
||||
.collect(Collectors.groupingBy(m -> m.get("GID")));
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("import Customer oldNames = {} from old version app", oldNameGroupedMap.size());
|
||||
}
|
||||
updateProgress(0.1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void loadContacts() {
|
||||
try (Stream<Map<String, Object>> stream = service.queryAllCustomerContactForStream()) {
|
||||
updateTitle("载入联系人");
|
||||
contactGroupedMap = stream.takeWhile(v -> !isCancelled()).parallel()
|
||||
.collect(Collectors.groupingBy(m -> m.get("GID")));
|
||||
logger.debug("import Customer contacts = {} from old version app", contactGroupedMap.size());
|
||||
updateProgress(0.2, 1);
|
||||
}
|
||||
}
|
||||
|
||||
private void syncCustomers() {
|
||||
try (Stream<Map<String, Object>> stream = service.queryAllCustomerForStream()) {
|
||||
updateTitle("客户信息");
|
||||
long size = service.countOfCustomer();
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
stream.takeWhile(v -> !isCancelled()).forEach(map -> {
|
||||
if (isCancelled()) {
|
||||
updateMessage("Cancelled");
|
||||
return;
|
||||
}
|
||||
Company updated = service.syncCustomer(map, basePath, oldNameGroupedMap, contactGroupedMap);
|
||||
if (updated != null) {
|
||||
updateMessage(counter.get() + " / " + size + ">" + updated.getName());
|
||||
} else {
|
||||
updateMessage(counter.get() + " / " + size + ">" + map.get("NAME"));
|
||||
}
|
||||
updateProgress(counter.incrementAndGet(), size);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void syncContracts() {
|
||||
try (Stream<Map<String, Object>> stream = service.queryAllCustomerContractForStream()) {
|
||||
updateTitle("合同信息");
|
||||
long size = service.countOfCustomerContract();
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
stream.takeWhile(v -> !isCancelled()).forEach(map -> {
|
||||
if (isCancelled()) {
|
||||
updateMessage("Cancelled");
|
||||
return;
|
||||
}
|
||||
Contract updated = service.syncCustomerContract(map);
|
||||
if (updated != null) {
|
||||
updateMessage(counter.get() + " / " + size + ">" + updated.getName());
|
||||
} else {
|
||||
updateMessage(counter.get() + " / " + size + ">" + map.get("NO"));
|
||||
}
|
||||
updateProgress(counter.incrementAndGet(), size);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package com.ecep.contract.cloud.old;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.ds.company.service.CompanyService;
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.model.Contract;
|
||||
import com.ecep.contract.ui.Tasker;
|
||||
|
||||
public class OldVersionSyncVendorTask extends Tasker<Object> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(OldVersionSyncVendorTask.class);
|
||||
|
||||
private final OldVersionService service = SpringApp.getBean(OldVersionService.class);
|
||||
private final CompanyService companyService = SpringApp.getBean(CompanyService.class);
|
||||
private Map<Object, List<Map<String, Object>>> oldNameGroupedMap;
|
||||
private Map<Object, List<Map<String, Object>>> contactGroupedMap;
|
||||
private File basePath;
|
||||
private String titlePrefix = "";
|
||||
|
||||
public OldVersionSyncVendorTask() {
|
||||
updateTitle("老版本-同步供应商数据");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object execute(MessageHolder holder) throws Exception {
|
||||
basePath = companyService.getVendorBasePath();
|
||||
List<Runnable> runnable = Arrays.asList(this::loadOldNames, this::loadContacts, this::syncVendors,
|
||||
this::syncContracts);
|
||||
for (int i = 0; i < runnable.size(); i++) {
|
||||
titlePrefix = "老版本-同步供应商数据-" + (i + 1) + "/" + runnable.size() + "-";
|
||||
try {
|
||||
runnable.get(i).run();
|
||||
} catch (Exception e) {
|
||||
logger.error("运行至 {} 时,发生错误中断", titlePrefix, e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void loadOldNames() {
|
||||
try (Stream<Map<String, Object>> stream = service.queryAllVendorOldNameForStream()) {
|
||||
updateTitle("载入曾用名");
|
||||
oldNameGroupedMap = stream.takeWhile(v -> !isCancelled()).parallel()
|
||||
.collect(Collectors.groupingBy(m -> m.get("GID")));
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("import vendor oldNames = {} from old version app", oldNameGroupedMap.size());
|
||||
}
|
||||
updateProgress(0.1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadContacts() {
|
||||
try (Stream<Map<String, Object>> stream = service.queryAllVendorContactForStream()) {
|
||||
updateTitle("载入联系人");
|
||||
contactGroupedMap = stream.takeWhile(v -> !isCancelled()).parallel()
|
||||
.collect(Collectors.groupingBy(m -> m.get("GID")));
|
||||
logger.debug("import vendor contacts = {} from old version app", contactGroupedMap.size());
|
||||
updateProgress(0.2, 1);
|
||||
}
|
||||
}
|
||||
|
||||
private void syncVendors() {
|
||||
try (Stream<Map<String, Object>> stream = service.queryAllVendorForStream()) {
|
||||
updateTitle("客户信息");
|
||||
long size = service.countOfVendor();
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
stream.takeWhile(v -> !isCancelled()).forEach(map -> {
|
||||
if (isCancelled()) {
|
||||
updateMessage("Cancelled");
|
||||
return;
|
||||
}
|
||||
Company updated = service.syncVendor(map, basePath, oldNameGroupedMap, contactGroupedMap);
|
||||
if (updated != null) {
|
||||
updateMessage(counter.get() + " / " + size + ">" + updated.getName());
|
||||
} else {
|
||||
updateMessage(counter.get() + " / " + size + ">" + map.get("NAME"));
|
||||
}
|
||||
updateProgress(counter.incrementAndGet(), size);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void syncContracts() {
|
||||
try (Stream<Map<String, Object>> stream = service.queryAllVendorContractForStream()) {
|
||||
updateTitle("合同信息");
|
||||
long size = service.countOfVendorContract();
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
stream.takeWhile(v -> !isCancelled()).forEach(map -> {
|
||||
if (isCancelled()) {
|
||||
updateMessage("Cancelled");
|
||||
return;
|
||||
}
|
||||
Contract updated = service.syncVendorContract(map);
|
||||
if (updated != null) {
|
||||
updateMessage(counter.get() + " / " + size + ">" + updated.getName());
|
||||
} else {
|
||||
updateMessage(counter.get() + " / " + size + ">" + map.get("NO"));
|
||||
}
|
||||
updateProgress(counter.incrementAndGet(), size);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.ecep.contract.cloud.rk;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
import java.net.URI;
|
||||
|
||||
public class BlackListUpdateContext {
|
||||
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
private ObjectMapper objectMapper;
|
||||
private Proxy.Type proxyType;
|
||||
@Getter
|
||||
private Proxy socksProxy;
|
||||
@Setter
|
||||
@Getter
|
||||
private String url;
|
||||
@Getter
|
||||
private long elapse = 1440;
|
||||
|
||||
|
||||
public void setProxy(String proxy) {
|
||||
if (proxy != null) {
|
||||
URI proxyUri = URI.create(proxy);
|
||||
proxyType = Proxy.Type.valueOf(proxyUri.getScheme().toUpperCase());
|
||||
socksProxy = new Proxy(
|
||||
proxyType,
|
||||
new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public void setElapse(String value) {
|
||||
elapse = Long.parseLong(value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.ecep.contract.cloud.rk;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.ecep.contract.ds.MyRepository;
|
||||
import com.ecep.contract.model.CloudRk;
|
||||
import com.ecep.contract.model.Company;
|
||||
|
||||
@Repository
|
||||
public interface CloudRkRepository extends MyRepository<CloudRk, Integer> {
|
||||
|
||||
Stream<CloudRk> findByCloudLatestAfter(Instant timestamp);
|
||||
|
||||
Stream<CloudRk> findByCloudEntUpdateAfter(Instant timestamp);
|
||||
|
||||
/**
|
||||
* 按公司查找 Cloud RK
|
||||
*
|
||||
* @param company 公司对象
|
||||
* @return Cloud RK
|
||||
*/
|
||||
Optional<CloudRk> findByCompany(Company company);
|
||||
|
||||
/**
|
||||
* 按公司查找 Cloud RK
|
||||
*
|
||||
* @param companyId 公司对象编号
|
||||
* @return Cloud RK
|
||||
*/
|
||||
Optional<CloudRk> findByCompanyId(int companyId);
|
||||
|
||||
List<CloudRk> findAllByCompanyId(int companyId);
|
||||
|
||||
long countByLatestUpdateBefore(Instant instant);
|
||||
|
||||
long countByAutoUpdateIsTrueAndLatestUpdateBefore(Instant instant);
|
||||
|
||||
List<CloudRk> findTop100ByLatestUpdateBeforeOrderByLatestUpdateDesc(Instant instant);
|
||||
|
||||
List<CloudRk> findTop100ByAutoUpdateIsTrueAndLatestUpdateBeforeOrderByLatestUpdateDesc(Instant instant);
|
||||
|
||||
@Modifying
|
||||
@Transactional
|
||||
int deleteAllByCompany(Company company);
|
||||
}
|
||||
@@ -0,0 +1,379 @@
|
||||
package com.ecep.contract.cloud.rk;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.ecep.contract.util.FileUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.Caching;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.BlackReasonType;
|
||||
import com.ecep.contract.IEntityService;
|
||||
import com.ecep.contract.cloud.CloudInfo;
|
||||
import com.ecep.contract.ds.company.repository.CompanyBlackReasonRepository;
|
||||
import com.ecep.contract.ds.company.repository.CompanyOldNameRepository;
|
||||
import com.ecep.contract.ds.company.service.CompanyService;
|
||||
import com.ecep.contract.ds.other.service.SysConfService;
|
||||
import com.ecep.contract.model.CloudRk;
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.model.CompanyBlackReason;
|
||||
import com.ecep.contract.util.HttpJsonUtils;
|
||||
import com.ecep.contract.util.MyStringUtils;
|
||||
import com.fasterxml.jackson.annotation.JsonAlias;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import jakarta.persistence.criteria.Path;
|
||||
import lombok.Data;
|
||||
|
||||
@Lazy
|
||||
@Service
|
||||
@CacheConfig(cacheNames = "cloud-rk")
|
||||
public class CloudRkService implements IEntityService<CloudRk> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(CloudRkService.class);
|
||||
|
||||
public final static String ENTERPRISE_CREDIT_REPORT = "企业征信报告";
|
||||
|
||||
public static final String KEY_PROXY = "cloud.rk.proxy";
|
||||
public static final String KEY_SYNC_ELAPSE = "cloud.rk.sync.elapse";
|
||||
public static final long DEFAULT_SYNC_ELAPSE = 36000L;
|
||||
public static final String KEY_VENDOR_REPORT_URL = "cloud.rk.vendor.report.url";
|
||||
public static final String KEY_CUSTOMER_REPORT_URL = "cloud.rk.customer.report.url";
|
||||
public static final String KEY_ENT_SCORE_URL = "cloud.rk.ent_score.url";
|
||||
public static final String KEY_ENT_REPORT_URL = "cloud.rk.ent_report.url";
|
||||
public static final String KEY_ENT_FUZZY_URL = "cloud.rk.ent_fuzzy.url";
|
||||
public static final String KEY_BLACK_LIST_URL = "cloud.rk.black_list.url";
|
||||
public static final String KEY_BLACK_LIST_ELAPSE = "cloud.rk.black_list.elapse";
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
/**
|
||||
* 同步超时, 单位 毫秒
|
||||
*
|
||||
* @return 毫秒
|
||||
*/
|
||||
public long getSyncElapse() {
|
||||
String string = confService.getString(KEY_SYNC_ELAPSE);
|
||||
if (!StringUtils.hasText(string)) {
|
||||
return DEFAULT_SYNC_ELAPSE;
|
||||
}
|
||||
return MyStringUtils.toLong(string, DEFAULT_SYNC_ELAPSE);
|
||||
}
|
||||
|
||||
@Data
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class EntInfo {
|
||||
@JsonAlias("entid")
|
||||
private String id;
|
||||
@JsonAlias("entname")
|
||||
private String name;
|
||||
private boolean nowName;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private SysConfService confService;
|
||||
@Autowired
|
||||
private CloudRkRepository cloudRKRepository;
|
||||
@Lazy
|
||||
@Autowired
|
||||
private CompanyService companyService;
|
||||
@Autowired
|
||||
private CompanyOldNameRepository companyOldNameRepository;
|
||||
@Autowired
|
||||
private CompanyBlackReasonRepository companyBlackReasonRepository;
|
||||
|
||||
@Cacheable(key = "#p0")
|
||||
public CloudRk findById(Integer id) {
|
||||
return cloudRKRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
public Page<CloudRk> findAll(Specification<CloudRk> spec, Pageable pageable) {
|
||||
return cloudRKRepository.findAll(spec, pageable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Specification<CloudRk> getSpecification(String searchText) {
|
||||
if (!StringUtils.hasText(searchText)) {
|
||||
return null;
|
||||
}
|
||||
return (root, query, builder) -> {
|
||||
Path<Object> company = root.get("company");
|
||||
return builder.or(
|
||||
builder.like(company.get("name"), "%" + searchText + "%"),
|
||||
builder.like(company.get("shortName"), "%" + searchText + "%"),
|
||||
builder.like(root.get("cloudId"), "%" + searchText + "%"),
|
||||
builder.like(root.get("description"), "%" + searchText + "%"));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新黑名单列表
|
||||
*/
|
||||
public void updateBlackList(
|
||||
Company company, CloudRk cloudRk, BlackListUpdateContext context) throws IOException {
|
||||
List<String> companyNames = new ArrayList<>();
|
||||
companyNames.add(company.getName());
|
||||
// fixed 平台API使用企业名称,可能记录的是曾用名
|
||||
companyOldNameRepository.findAllByCompanyId(company.getId()).forEach(oldName -> {
|
||||
// 歧义的曾用名不采用
|
||||
if (oldName.getAmbiguity()) {
|
||||
return;
|
||||
}
|
||||
companyNames.add(oldName.getName());
|
||||
});
|
||||
|
||||
List<CompanyBlackReason> reasonList = new ArrayList<>();
|
||||
List<CompanyBlackReason> dbReasons = companyBlackReasonRepository.findAllByCompany(company);
|
||||
|
||||
companyNames.forEach(name -> {
|
||||
String url = context.getUrl() + URLEncoder.encode(name, StandardCharsets.UTF_8);
|
||||
try {
|
||||
HttpJsonUtils.get(url, json -> {
|
||||
if (!json.has("success") || !json.get("success").asBoolean()) {
|
||||
System.out.println("json = " + json.toPrettyString());
|
||||
return;
|
||||
}
|
||||
|
||||
if (json.has("data")) {
|
||||
JsonNode data = json.get("data");
|
||||
try {
|
||||
if (data.has("blackReason")) {
|
||||
for (JsonNode reason : data.get("blackReason")) {
|
||||
toCompanyBlackReasonList(company, BlackReasonType.BLACK, reason, dbReasons,
|
||||
reasonList, context.getObjectMapper());
|
||||
}
|
||||
}
|
||||
if (data.has("greyReason")) {
|
||||
for (JsonNode reason : data.get("greyReason")) {
|
||||
toCompanyBlackReasonList(company, BlackReasonType.GRAY, reason, dbReasons,
|
||||
reasonList, context.getObjectMapper());
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
logger.error("{} {},json = {}", company.getName(), ex.getMessage(), json, ex);
|
||||
throw new RuntimeException(json.toString(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
// 保存JSON数据到公司目录
|
||||
String companyPath = company.getPath();
|
||||
if (StringUtils.hasText(companyPath)) {
|
||||
File dir = new File(companyPath);
|
||||
if (dir.exists()) {
|
||||
File file = new File(dir, FileUtils.FILE_BLACK_LIST_JSON);
|
||||
try {
|
||||
objectMapper.writeValue(file, json);
|
||||
} catch (IOException e) {
|
||||
logger.warn("Unable Save BlackList to {}, company:{}", file, company.getName(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, context.getObjectMapper(), context.getSocksProxy());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
|
||||
if (!reasonList.isEmpty()) {
|
||||
companyBlackReasonRepository.saveAll(reasonList);
|
||||
}
|
||||
cloudRk.setCloudBlackListUpdated(Instant.now());
|
||||
}
|
||||
|
||||
private void toCompanyBlackReasonList(
|
||||
Company company, BlackReasonType type,
|
||||
JsonNode reason, List<CompanyBlackReason> dbReasons,
|
||||
List<CompanyBlackReason> reasonList, ObjectMapper objectMapper) throws JsonMappingException {
|
||||
ObjectNode object = (ObjectNode) reason;
|
||||
String key = "rk-" + object.remove("id").asText();
|
||||
CompanyBlackReason cbr = dbReasons.stream().filter(r -> r.getKey().equals(key)).findAny()
|
||||
.orElseGet(CompanyBlackReason::new);
|
||||
objectMapper.updateValue(cbr, reason);
|
||||
cbr.setCompany(company);
|
||||
cbr.setType(type);
|
||||
cbr.setKey(key);
|
||||
reasonList.add(cbr);
|
||||
}
|
||||
|
||||
public CompletableFuture<BlackListUpdateContext> createBlackListUpdateContext() {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
BlackListUpdateContext context = new BlackListUpdateContext();
|
||||
context.setObjectMapper(objectMapper);
|
||||
context.setProxy(confService.getString(KEY_PROXY));
|
||||
context.setUrl(confService.getString(KEY_BLACK_LIST_URL));
|
||||
// context.setElapse(confService.getLong(KEY_BLACK_LIST_ELAPSE));
|
||||
return context;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true
|
||||
*/
|
||||
public boolean checkBlackListUpdateElapse(
|
||||
Company company, CloudRk cloudRk, BlackListUpdateContext context) {
|
||||
Instant start = cloudRk.getCloudBlackListUpdated();
|
||||
if (start == null) {
|
||||
return true;
|
||||
}
|
||||
Instant elapse = start.plusSeconds(context.getElapse());
|
||||
return elapse.isBefore(Instant.now());
|
||||
}
|
||||
|
||||
public CloudRk getOrCreateCloudRk(CloudInfo info) {
|
||||
Optional<CloudRk> optional = cloudRKRepository.findById(info.getId());
|
||||
return optional.orElseGet(() -> getOrCreateCloudRk(info.getCompany()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回或者创建 Cloud RK
|
||||
*
|
||||
* @param company 公司对象
|
||||
* @return Cloud RK
|
||||
*/
|
||||
public CloudRk getOrCreateCloudRk(Company company) {
|
||||
Integer companyId = company.getId();
|
||||
List<CloudRk> list = cloudRKRepository.findAllByCompanyId(companyId);
|
||||
if (list.isEmpty()) {
|
||||
CloudRk rk = new CloudRk();
|
||||
rk.setCompany(company);
|
||||
rk.setCustomerGrade("");
|
||||
rk.setCustomerScore(-1);
|
||||
rk.setVendorGrade("");
|
||||
rk.setVendorScore(-1);
|
||||
rk.setRank("");
|
||||
return cloudRKRepository.save(rk);
|
||||
}
|
||||
if (list.size() == 1) {
|
||||
return list.getFirst();
|
||||
}
|
||||
|
||||
// 查询有 CloudId 的记录
|
||||
List<CloudRk> hasCouldIdList = list.stream().filter(v -> StringUtils.hasText(v.getCloudId()))
|
||||
.collect(Collectors.toList());
|
||||
// 没有匹配到一条时
|
||||
if (hasCouldIdList.isEmpty()) {
|
||||
// 保留第一条,其他删除
|
||||
CloudRk rk = list.removeFirst();
|
||||
cloudRKRepository.deleteAll(list);
|
||||
return rk;
|
||||
}
|
||||
|
||||
// 只有匹配到一条有 CloudId 的记录
|
||||
if (hasCouldIdList.size() == 1) {
|
||||
// 保留匹配的记录,其他删除
|
||||
CloudRk rk = hasCouldIdList.removeFirst();
|
||||
list.remove(rk);
|
||||
cloudRKRepository.deleteAll(list);
|
||||
return rk;
|
||||
}
|
||||
|
||||
// 查询有 CloudLatest 的记录
|
||||
List<CloudRk> hasLatestList = hasCouldIdList.stream().filter(v -> v.getCloudLatest() != null)
|
||||
.collect(Collectors.toList());
|
||||
// 没有匹配到一条时
|
||||
if (hasLatestList.isEmpty()) {
|
||||
// 保留第一条,其他删除
|
||||
CloudRk rk = hasCouldIdList.removeFirst();
|
||||
list.remove(rk);
|
||||
cloudRKRepository.deleteAll(list);
|
||||
return rk;
|
||||
}
|
||||
|
||||
// 只有匹配到一条有 CloudId 的记录
|
||||
if (hasLatestList.size() == 1) {
|
||||
// 保留匹配的记录,其他删除
|
||||
CloudRk rk = hasLatestList.removeFirst();
|
||||
list.remove(rk);
|
||||
cloudRKRepository.deleteAll(list);
|
||||
return rk;
|
||||
}
|
||||
|
||||
return hasLatestList.getFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存 Cloud Rk
|
||||
*
|
||||
* @param cloudRk Cloud Rk 对象
|
||||
* @return 更新的 Cloud Rk
|
||||
*/
|
||||
@CacheEvict(key = "#p0.id")
|
||||
public CloudRk save(CloudRk cloudRk) {
|
||||
return cloudRKRepository.save(cloudRk);
|
||||
}
|
||||
|
||||
@CacheEvict(key = "#p0.id")
|
||||
@Override
|
||||
public void delete(CloudRk entity) {
|
||||
cloudRKRepository.delete(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 在 {@link #getSyncElapse()} 毫秒前,更新的
|
||||
*
|
||||
* @return 记录条数
|
||||
*/
|
||||
public long countNeedUpdate() {
|
||||
Instant now = Instant.now();
|
||||
long elapse = getSyncElapse();
|
||||
Instant instant = now.minusSeconds(elapse);
|
||||
return cloudRKRepository.countByAutoUpdateIsTrueAndLatestUpdateBefore(instant);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回距离上次更新超过 SysConfig:cloud.rk.black_list.elapse 秒的公司
|
||||
*/
|
||||
// @Transactional
|
||||
public List<CloudRk> findNeedUpdate() {
|
||||
Instant now = Instant.now();
|
||||
long elapse = getSyncElapse();
|
||||
Instant instant = now.minusSeconds(elapse);
|
||||
return cloudRKRepository.findTop100ByAutoUpdateIsTrueAndLatestUpdateBeforeOrderByLatestUpdateDesc(instant);
|
||||
}
|
||||
|
||||
@CacheEvict
|
||||
public void deleteByCompany(Company company) {
|
||||
int deleted = cloudRKRepository.deleteAllByCompany(company);
|
||||
if (deleted > 0) {
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Delete {} records by company:#{}", deleted, company.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO 这个可以无法更新缓存
|
||||
@Caching(evict = {
|
||||
@CacheEvict(key = "#p0.id"),
|
||||
@CacheEvict(key = "#p1.id"),
|
||||
})
|
||||
public void resetTo(Company from, Company to) {
|
||||
List<CloudRk> list = cloudRKRepository.findAllByCompanyId(from.getId());
|
||||
for (CloudRk item : list) {
|
||||
item.setCompany(to);
|
||||
}
|
||||
cloudRKRepository.saveAll(list);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package com.ecep.contract.cloud.rk;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.cloud.rk.ctx.CloudRkCtx;
|
||||
import com.ecep.contract.constant.CloudServiceConstant;
|
||||
import com.ecep.contract.ds.contract.service.ContractService;
|
||||
import com.ecep.contract.model.CloudRk;
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.ui.Tasker;
|
||||
|
||||
/**
|
||||
* 集团相关方平台同步任务
|
||||
*/
|
||||
public class CloudRkSyncTask extends Tasker<Object> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(CloudRkSyncTask.class);
|
||||
|
||||
private ContractService contractService;
|
||||
|
||||
@Override
|
||||
protected Object execute(MessageHolder holder) throws Exception {
|
||||
updateTitle("集团相关方平台");
|
||||
|
||||
CloudRkCtx cloudRkCtx = null;
|
||||
CloudRkService service = null;
|
||||
try {
|
||||
cloudRkCtx = new CloudRkCtx();
|
||||
service = SpringApp.getBean(CloudRkService.class);
|
||||
cloudRkCtx.setCloudRkService(service);
|
||||
} catch (BeansException e) {
|
||||
holder.error("没有找到 " + CloudServiceConstant.RK_NAME + " 服务");
|
||||
return null;
|
||||
}
|
||||
|
||||
long total = service.countNeedUpdate();
|
||||
if (total == 0) {
|
||||
holder.info("没有需要更新");
|
||||
return null;
|
||||
}
|
||||
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
holder.info("统计需要更新的 " + total + " 条");
|
||||
|
||||
try {
|
||||
// 每次获取100条记录
|
||||
while (!isCancelled()) {
|
||||
List<CloudRk> needUpdate = service.findNeedUpdate();
|
||||
if (needUpdate.isEmpty()) {
|
||||
holder.info("处理完成");
|
||||
break;
|
||||
}
|
||||
for (CloudRk cloudRk : needUpdate) {
|
||||
if (isCancelled()) {
|
||||
break;
|
||||
}
|
||||
MessageHolder subHolder = holder.sub(counter.get() + " / " + total + ">");
|
||||
// fixed lazy
|
||||
Company company = cloudRk.getCompany();
|
||||
if (company == null) {
|
||||
subHolder.error("数据不完整,没有关联公司");
|
||||
break;
|
||||
}
|
||||
if (!Hibernate.isInitialized(company)) {
|
||||
company = getCompanyService().findById(company.getId());
|
||||
cloudRk.setCompany(company);
|
||||
}
|
||||
if (cloudRk.isAutoUpdate()) {
|
||||
LocalDate date = LocalDate.now().minusYears(3);
|
||||
long count = getContractService().findAllByCompany(company).stream()
|
||||
.filter(c -> c.getSetupDate() != null)
|
||||
.filter(c -> c.getSetupDate().isAfter(date))
|
||||
.count();
|
||||
if (count == 0) {
|
||||
holder.info("公司:" + company.getName() + " 没有3年以上的合同, 取消自动更新");
|
||||
cloudRk.setAutoUpdate(false);
|
||||
}
|
||||
}
|
||||
try {
|
||||
cloudRk.setDescription("");
|
||||
if (cloudRkCtx.syncCompany(company, cloudRk, subHolder)) {
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
cloudRk.setLatestUpdate(Instant.now());
|
||||
service.save(cloudRk);
|
||||
}
|
||||
// updateProgress(counter.incrementAndGet(), total);
|
||||
}
|
||||
}
|
||||
|
||||
if (isCancelled()) {
|
||||
updateMessage("Cancelled");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("运行至 {}/{} 时,发生错误中断", counter.get(), total, e);
|
||||
}
|
||||
// updateProgress(1, 1);
|
||||
return null;
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> delay(int second, Executor executor) {
|
||||
CompletableFuture<Void> future = new CompletableFuture<>();
|
||||
|
||||
long until = System.currentTimeMillis() + second * 1000L;
|
||||
String title = "";
|
||||
AtomicReference<Runnable> reference = new AtomicReference<>();
|
||||
Runnable runnable = () -> {
|
||||
long let = until - System.currentTimeMillis();
|
||||
if (let < 0) {
|
||||
System.out.println("complete @" + Thread.currentThread().getName());
|
||||
future.complete(null);
|
||||
} else {
|
||||
// 再次调度
|
||||
executor.execute(reference.get());
|
||||
updateTitle(title + " 延时 " + ((int) let / 1000) + "秒");
|
||||
}
|
||||
};
|
||||
reference.set(runnable);
|
||||
// 第一次调度
|
||||
executor.execute(runnable);
|
||||
return future;
|
||||
}
|
||||
|
||||
ContractService getContractService() {
|
||||
if (contractService == null) {
|
||||
contractService = getBean(ContractService.class);
|
||||
}
|
||||
return contractService;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
package com.ecep.contract.cloud.rk;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.ecep.contract.util.FileUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.ds.company.repository.CompanyContactRepository;
|
||||
import com.ecep.contract.ds.company.service.CompanyOldNameService;
|
||||
import com.ecep.contract.model.CloudRk;
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.model.CompanyContact;
|
||||
import com.ecep.contract.model.CompanyOldName;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class EntReportParser {
|
||||
private static final Logger logger = LoggerFactory.getLogger(EntReportParser.class);
|
||||
private ObjectMapper objectMapper;
|
||||
private Company company;
|
||||
private CloudRk cloudRk;
|
||||
private boolean modified = false;
|
||||
|
||||
|
||||
public void parse(JsonNode json) {
|
||||
if (!json.has("B1001")) {
|
||||
// 没有数据
|
||||
throw new RuntimeException("B1001 can't be null, json:" + json);
|
||||
}
|
||||
JsonNode b1001 = json.get("B1001");
|
||||
if (!b1001.has("count") || b1001.get("count").asInt() < 1 || !b1001.has("data")) {
|
||||
// 没有数据
|
||||
return;
|
||||
}
|
||||
JsonNode data = b1001.get("data");
|
||||
|
||||
updateCompanyProperty("entType", data.get("enttype"));
|
||||
updateCompanyProperty("entStatus", data.get("entstatus"));
|
||||
updateCompanyProperty("setupDate", data.get("esdate"));
|
||||
|
||||
updateCompanyUniscid(data);
|
||||
updateCompanyNameHistory(data);
|
||||
|
||||
updateCompanyProperty("regAddr", data.get("dom"));
|
||||
updateCompanyProperty("registeredCapital", data.get("regcap"));
|
||||
updateCompanyProperty("registeredCapitalCurrency", data.get("regcapcur"));
|
||||
updateCompanyProperty("legalRepresentative", data.get("frname"));
|
||||
updateCompanyProperty("district", data.get("regorgprovince"));
|
||||
updateCompanyProperty("telephone", data.get("tel"));
|
||||
updateCompanyProperty("address", data.get("oploc"));
|
||||
updateCompanyProperty("operationPeriodBegin", data.get("opfrom"));
|
||||
updateCompanyProperty("operationPeriodEnd", data.get("opto"), "-");
|
||||
updateCompanyProperty("industry", data.get("nicfulltitle"));
|
||||
|
||||
//
|
||||
updateCloudRkEntUpdateDate(data);
|
||||
|
||||
// 更新法人联系人
|
||||
updateLegalRepresentativeContact(data);
|
||||
|
||||
// 保存JSON数据到公司目录
|
||||
saveJsonToFile(json);
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void updateLegalRepresentativeContact(JsonNode data) {
|
||||
String legalRepresentative = company.getLegalRepresentative();
|
||||
if (!StringUtils.hasText(legalRepresentative)) {
|
||||
return;
|
||||
}
|
||||
CompanyContact contact = null;
|
||||
boolean modified = false;
|
||||
CompanyContactRepository contactRepository = SpringApp.getBean(CompanyContactRepository.class);
|
||||
List<CompanyContact> contactList = contactRepository.findAllByCompanyAndName(company, legalRepresentative);
|
||||
if (contactList == null) {
|
||||
// db error
|
||||
return;
|
||||
}
|
||||
if (contactList.isEmpty()) {
|
||||
//没有,创建法人联系人
|
||||
contact = new CompanyContact();
|
||||
contact.setCompany(company);
|
||||
contact.setName(legalRepresentative);
|
||||
contact.setPosition("法定代表人");
|
||||
contact.setCreated(LocalDate.now());
|
||||
modified = true;
|
||||
} else {
|
||||
Optional<CompanyContact> any = contactList.stream().filter(c -> "法定代表人".equals(c.getPosition())).findAny();
|
||||
if (any.isEmpty()) {
|
||||
any = contactList.stream().findAny();
|
||||
}
|
||||
contact = any.get();
|
||||
// if (contact.getPostion() == null || !contact.setPostion().contains("法定代表人")) {
|
||||
// contact.setMemo("法定代表人");
|
||||
// modified = true;
|
||||
// }
|
||||
}
|
||||
|
||||
if (!StringUtils.hasText(contact.getEmail())) {
|
||||
String text = data.get("email").asText();
|
||||
contact.setEmail(text);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!StringUtils.hasText(contact.getAddress())) {
|
||||
String text = company.getAddress();
|
||||
contact.setAddress(text);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!StringUtils.hasText(contact.getPhone())) {
|
||||
String text = company.getTelephone();
|
||||
contact.setPhone(text);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!StringUtils.hasText(contact.getPosition())) {
|
||||
contact.setPosition("法定代表人");
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
contactRepository.save(contact);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void updateCompanyNameHistory(JsonNode data) {
|
||||
JsonNode node = data.get("nameHistory");
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
// 历史曾用名
|
||||
String nameHistory = node.asText();
|
||||
if (!StringUtils.hasText(nameHistory)) {
|
||||
return;
|
||||
}
|
||||
List<String> historyNames = new ArrayList<>();
|
||||
for (String str : nameHistory.split(",")) {
|
||||
String trimmed = str.trim();
|
||||
if (StringUtils.hasText(trimmed)) {
|
||||
historyNames.add(trimmed);
|
||||
}
|
||||
}
|
||||
CompanyOldNameService service = SpringApp.getBean(CompanyOldNameService.class);
|
||||
List<CompanyOldName> oldNames = service.findAllByCompany(company);
|
||||
for (CompanyOldName oldName : oldNames) {
|
||||
historyNames.remove(oldName.getName());
|
||||
}
|
||||
for (String historyName : historyNames) {
|
||||
CompanyOldName oldName = new CompanyOldName();
|
||||
oldName.setName(historyName);
|
||||
oldName.setCompanyId(company.getId());
|
||||
oldName.setMemo("从相关方平台导入");
|
||||
oldName.setAmbiguity(false);
|
||||
service.save(oldName);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateCompanyUniscid(JsonNode data) {
|
||||
JsonNode node = data.get("uniscid");
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String uid = node.asText();
|
||||
if (StringUtils.hasText(uid)) {
|
||||
if (!uid.equals(company.getUniscid())) {
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("更新 {} 的 UNISCID {} -> {}", company.getName(), company.getUniscid(), uid);
|
||||
}
|
||||
company.setUniscid(uid);
|
||||
modified = true;
|
||||
}
|
||||
} else {
|
||||
//fixed 当平台返回的 社会统一信用代码为空时,如果原来已经有的,则不做更新
|
||||
if (StringUtils.hasText(company.getUniscid())) {
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("来自平台的 UNISCID 为空,但本地{}已经记录{},不做更改", company.getName(), company.getUniscid());
|
||||
}
|
||||
} else {
|
||||
company.setUniscid("");
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新 平台的数据更新时间戳
|
||||
*/
|
||||
private void updateCloudRkEntUpdateDate(JsonNode data) {
|
||||
JsonNode node = data.get("updated");
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
LocalDateTime updated = objectMapper.convertValue(node, LocalDateTime.class);
|
||||
cloudRk.setCloudEntUpdate(updated.toInstant(ZoneOffset.ofHours(8)));
|
||||
}
|
||||
|
||||
private void updateCompanyProperty(String field, JsonNode node, String... excludeValues) {
|
||||
if (node == null || node.isNull()) {
|
||||
return;
|
||||
}
|
||||
String text = node.asText();
|
||||
for (String excludeValue : excludeValues) {
|
||||
if (Objects.equals(text, excludeValue)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
updateCompanyProperty(field, node);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新属性通用方法, 数据的类型转换由 objectMapper 提供
|
||||
*
|
||||
* @param field 类属性名称
|
||||
* @param node 数据来源 json node
|
||||
*/
|
||||
private void updateCompanyProperty(String field, JsonNode node) {
|
||||
if (node == null || node.isNull()) {
|
||||
return;
|
||||
}
|
||||
PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(Company.class, field);
|
||||
if (descriptor == null) {
|
||||
logger.error("Company 的字段 {} 不存在,请确认.", field);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Object oldValue = descriptor.getReadMethod().invoke(company);
|
||||
Object newValue = objectMapper.convertValue(node, descriptor.getReadMethod().getReturnType());
|
||||
if (!Objects.equals(oldValue, newValue)) {
|
||||
descriptor.getWriteMethod().invoke(company, newValue);
|
||||
modified = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void saveJsonToFile(JsonNode json) {
|
||||
String companyPath = company.getPath();
|
||||
if (StringUtils.hasText(companyPath)) {
|
||||
File dir = new File(companyPath);
|
||||
if (dir.exists()) {
|
||||
File file = new File(dir, FileUtils.FILE_B1001_JSON);
|
||||
try {
|
||||
objectMapper.writeValue(file, json);
|
||||
} catch (IOException e) {
|
||||
logger.warn("Unable Save BlackList to {}, company:{}", file, company.getName(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,830 @@
|
||||
package com.ecep.contract.cloud.rk.ctx;
|
||||
|
||||
import static com.ecep.contract.SpringApp.getBean;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
import java.net.SocketException;
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.net.ssl.SSLException;
|
||||
|
||||
import com.ecep.contract.util.FileUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.BlackReasonType;
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.cloud.AbstractCtx;
|
||||
import com.ecep.contract.cloud.rk.CloudRkService;
|
||||
import com.ecep.contract.ds.company.service.CompanyBlackReasonService;
|
||||
import com.ecep.contract.ds.company.service.CompanyContactService;
|
||||
import com.ecep.contract.ds.company.service.CompanyOldNameService;
|
||||
import com.ecep.contract.ds.company.service.CompanyService;
|
||||
import com.ecep.contract.model.CloudRk;
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.model.CompanyBlackReason;
|
||||
import com.ecep.contract.model.CompanyContact;
|
||||
import com.ecep.contract.model.CompanyOldName;
|
||||
import com.ecep.contract.util.HttpJsonUtils;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
public class CloudRkCtx extends AbstractCtx {
|
||||
private static final Logger logger = LoggerFactory.getLogger(CloudRkCtx.class);
|
||||
@Setter
|
||||
private CloudRkService cloudRkService;
|
||||
@Setter
|
||||
private CompanyService companyService;
|
||||
@Setter
|
||||
private CompanyBlackReasonService companyBlackReasonService;
|
||||
@Setter
|
||||
private ObjectMapper objectMapper;
|
||||
private Proxy socksProxy;
|
||||
|
||||
public CloudRkService getCloudRkService() {
|
||||
if (cloudRkService == null) {
|
||||
cloudRkService = getBean(CloudRkService.class);
|
||||
}
|
||||
return cloudRkService;
|
||||
}
|
||||
|
||||
public CompanyService getCompanyService() {
|
||||
if (companyService == null) {
|
||||
companyService = getBean(CompanyService.class);
|
||||
}
|
||||
return companyService;
|
||||
}
|
||||
|
||||
CompanyBlackReasonService getCompanyBlackReasonService() {
|
||||
if (companyBlackReasonService == null) {
|
||||
companyBlackReasonService = getBean(CompanyBlackReasonService.class);
|
||||
}
|
||||
return companyBlackReasonService;
|
||||
}
|
||||
|
||||
ObjectMapper getObjectMapper() {
|
||||
if (objectMapper == null) {
|
||||
objectMapper = getBean(ObjectMapper.class);
|
||||
}
|
||||
return objectMapper;
|
||||
}
|
||||
|
||||
Proxy getSocksProxy() {
|
||||
if (socksProxy == null) {
|
||||
String proxy = getConfService().getString(CloudRkService.KEY_PROXY);
|
||||
URI proxyUri = URI.create(proxy);
|
||||
Proxy.Type proxyType = Proxy.Type.valueOf(proxyUri.getScheme().toUpperCase());
|
||||
socksProxy = new Proxy(
|
||||
proxyType,
|
||||
new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort())
|
||||
);
|
||||
}
|
||||
return socksProxy;
|
||||
}
|
||||
|
||||
public void post(String url, Consumer<Map<String, Object>> data, Consumer<JsonNode> consumer) throws IOException {
|
||||
HttpJsonUtils.post(url, data, consumer, getObjectMapper(), getSocksProxy());
|
||||
}
|
||||
|
||||
|
||||
public boolean syncCompany(Company company, CloudRk cloudRk, MessageHolder holder) {
|
||||
if (!StringUtils.hasText(cloudRk.getCloudId())) {
|
||||
holder.warn("未定义平台编号, 尝试从平台上自动获取");
|
||||
// 当未定义平台编号时,尝试自动获取
|
||||
if (!queryCloudIdAndSelectOne(company, cloudRk, holder)) {
|
||||
// 自动获取到平台编号失败,立即返回
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!StringUtils.hasText(cloudRk.getCloudId()) || cloudRk.getCloudId().equals("-")) {
|
||||
// 平台编号为 空 或 - 时,跳过同步
|
||||
holder.debug("平台编号为 空 或 - 时,跳过同步");
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean updated = false;
|
||||
try {
|
||||
if (updateEnterpriseInfo(company, cloudRk, holder)) {
|
||||
company = getCompanyService().save(company);
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (updateBlackList(company, cloudRk, holder)) {
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (updateEnterpriseCredit(company, cloudRk, holder)) {
|
||||
// cloudRk modified
|
||||
updated = true;
|
||||
}
|
||||
if (updateCustomerScore(company, cloudRk, holder)) {
|
||||
// cloudRk modified
|
||||
updated = true;
|
||||
}
|
||||
if (updateVendorScore(company, cloudRk, holder)) {
|
||||
// cloudRk modified
|
||||
updated = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 异常
|
||||
logger.error("使用评分接口更新企业资信评价等级时发生错误", e);
|
||||
cloudRk.setDescription("评分接口错误:" + e.getMessage());
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新黑名单列表
|
||||
*/
|
||||
public boolean updateBlackList(
|
||||
Company company, CloudRk cloudRk, MessageHolder holder
|
||||
) throws IOException {
|
||||
Instant start = cloudRk.getCloudBlackListUpdated();
|
||||
if (start != null) {
|
||||
long elapse = getConfService().getLong(CloudRkService.KEY_BLACK_LIST_ELAPSE);
|
||||
if (elapse > 0) {
|
||||
Instant next = start.plusSeconds(elapse);
|
||||
if (next.isAfter(Instant.now())) {
|
||||
holder.debug("更新时间未到, 上次更新时间 = " + start);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String api = getConfService().getString(CloudRkService.KEY_BLACK_LIST_URL);
|
||||
List<String> companyNames = getCompanyService().getAllNames(company);
|
||||
|
||||
|
||||
List<CompanyBlackReason> reasonList = new ArrayList<>();
|
||||
List<CompanyBlackReason> dbReasons = getCompanyBlackReasonService().findAllByCompany(company);
|
||||
for (String name : companyNames) {
|
||||
String url = api + URLEncoder.encode(name, StandardCharsets.UTF_8);
|
||||
try {
|
||||
HttpJsonUtils.get(url, json -> {
|
||||
applyBlackReason(json, company, cloudRk, reasonList, dbReasons, holder);
|
||||
saveJsonToFile(company, json, "black-" + name + ".json", holder);
|
||||
}, getObjectMapper(), getSocksProxy());
|
||||
} catch (IOException e) {
|
||||
catchException(e, holder);
|
||||
}
|
||||
}
|
||||
|
||||
if (reasonList.isEmpty()) {
|
||||
cloudRk.setCloudBlackListUpdated(Instant.now());
|
||||
return false;
|
||||
|
||||
}
|
||||
for (CompanyBlackReason companyBlackReason : reasonList) {
|
||||
getCompanyBlackReasonService().save(companyBlackReason);
|
||||
}
|
||||
cloudRk.setCloudBlackListUpdated(Instant.now());
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean applyBlackReason(
|
||||
JsonNode json, Company company, CloudRk cloudRk,
|
||||
List<CompanyBlackReason> reasonList, List<CompanyBlackReason> dbReasons,
|
||||
MessageHolder holder
|
||||
) {
|
||||
if (isUnSuccess(json, holder)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!json.has("data")) {
|
||||
holder.error("数据异常,返回的json中没有 data 字段");
|
||||
return false;
|
||||
}
|
||||
JsonNode data = json.get("data");
|
||||
|
||||
if (data.has("blackReason")) {
|
||||
for (JsonNode reason : data.get("blackReason")) {
|
||||
try {
|
||||
toCompanyBlackReasonList(company, BlackReasonType.BLACK, reason, dbReasons, reasonList);
|
||||
} catch (JsonMappingException e) {
|
||||
holder.error("blackReason " + e.getMessage() + ", " + reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (data.has("greyReason")) {
|
||||
for (JsonNode reason : data.get("greyReason")) {
|
||||
try {
|
||||
toCompanyBlackReasonList(company, BlackReasonType.GRAY, reason, dbReasons, reasonList);
|
||||
} catch (JsonMappingException e) {
|
||||
holder.error("greyReason " + e.getMessage() + ", " + reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void toCompanyBlackReasonList(
|
||||
Company company, BlackReasonType type,
|
||||
JsonNode reason, List<CompanyBlackReason> dbReasons,
|
||||
List<CompanyBlackReason> reasonList
|
||||
) throws JsonMappingException {
|
||||
ObjectNode object = (ObjectNode) reason;
|
||||
String key = "rk-" + object.remove("id").asText();
|
||||
CompanyBlackReason cbr = dbReasons.stream().filter(r -> r.getKey().equals(key)).findAny().orElseGet(CompanyBlackReason::new);
|
||||
getObjectMapper().updateValue(cbr, reason);
|
||||
cbr.setCompany(company);
|
||||
cbr.setType(type);
|
||||
cbr.setKey(key);
|
||||
reasonList.add(cbr);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新评分
|
||||
*/
|
||||
public boolean updateEnterpriseCredit(
|
||||
Company company, CloudRk cloudRk, MessageHolder holder
|
||||
) throws IOException {
|
||||
String api = getConfService().getString(CloudRkService.KEY_ENT_SCORE_URL);
|
||||
AtomicBoolean modified = new AtomicBoolean(false);
|
||||
try {
|
||||
post(api, data -> {
|
||||
data.put("entname", company.getName());
|
||||
// data.put("entid", cloudInfo.getCloudId());
|
||||
data.put("get", true);
|
||||
}, json -> {
|
||||
modified.set(applyEnterpriseCredit(json, cloudRk, holder));
|
||||
saveJsonToFile(company, json, "credit.json", holder);
|
||||
});
|
||||
} catch (IOException e) {
|
||||
catchException(e, holder);
|
||||
}
|
||||
return modified.get();
|
||||
}
|
||||
|
||||
private boolean applyEnterpriseCredit(JsonNode json, CloudRk cloudRk, MessageHolder holder) {
|
||||
if (isUnSuccess(json, holder)) {
|
||||
return false;
|
||||
}
|
||||
if (isUnHasField(json, "data", holder)) {
|
||||
return false;
|
||||
}
|
||||
JsonNode data = json.get("data");
|
||||
boolean modified = false;
|
||||
String level = "";
|
||||
String description = "";
|
||||
if (data.isNull()) {
|
||||
level = "-";
|
||||
} else {
|
||||
level = data.get("level").asText();
|
||||
description = data.get("levelDescription").asText();
|
||||
}
|
||||
|
||||
if (updateText(cloudRk::getRank, cloudRk::setRank, level, holder, "企业资信评价等级")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(cloudRk::getRankDescription, cloudRk::setRankDescription, description, holder, "企业资信评价等级说明")) {
|
||||
modified = true;
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
private boolean isUnHasField(JsonNode json, String field, MessageHolder holder) {
|
||||
if (!json.has("data")) {
|
||||
holder.error("数据异常,返回的json中没有 data 字段");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isUnSuccess(JsonNode json, MessageHolder holder) {
|
||||
if (isUnHasField(json, "success", holder)) {
|
||||
return true;
|
||||
}
|
||||
if (!json.get("success").asBoolean()) {
|
||||
holder.error("数据异常,返回 success = false, " + json);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 客户信用
|
||||
*/
|
||||
public boolean updateCustomerScore(
|
||||
Company company, CloudRk cloudRk, MessageHolder holder
|
||||
) throws IOException {
|
||||
String url = getConfService().getString(CloudRkService.KEY_CUSTOMER_REPORT_URL);
|
||||
AtomicBoolean modified = new AtomicBoolean(false);
|
||||
try {
|
||||
post(url, data -> {
|
||||
// data.put("entName", company.getName());
|
||||
data.put("entId", cloudRk.getCloudId());
|
||||
data.put("get", true);
|
||||
}, json -> {
|
||||
modified.set(applyCustomerScore(json, company, cloudRk, holder));
|
||||
saveJsonToFile(company, json, "customer-score.json", holder);
|
||||
});
|
||||
} catch (IOException e) {
|
||||
catchException(e, holder);
|
||||
}
|
||||
return modified.get();
|
||||
}
|
||||
|
||||
private boolean applyCustomerScore(JsonNode json, Company company, CloudRk cloudRk, MessageHolder holder) {
|
||||
if (isUnSuccess(json, holder)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean modified = false;
|
||||
String grade = "";
|
||||
int score = 0;
|
||||
String description = "";
|
||||
if (isUnHasField(json, "data", holder)) {
|
||||
grade = "-";
|
||||
score = -1;
|
||||
} else {
|
||||
JsonNode data = json.get("data");
|
||||
if (data.isNull()) {
|
||||
grade = "无";
|
||||
score = -1;
|
||||
} else {
|
||||
grade = data.get("grade").asText();
|
||||
score = data.get("totalScore").asInt();
|
||||
description = data.get("description").asText();
|
||||
}
|
||||
}
|
||||
|
||||
if (updateText(cloudRk::getCustomerGrade, cloudRk::setCustomerGrade, grade, holder, "客户信用评级")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateNumber(cloudRk::getCustomerScore, cloudRk::setCustomerScore, score, holder, "客户信用总分")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(cloudRk::getCustomerDescription, cloudRk::setCustomerDescription, description, holder, "客户信用评级说明")) {
|
||||
modified = true;
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
|
||||
public boolean updateVendorScore(
|
||||
Company company, CloudRk cloudRk, MessageHolder holder
|
||||
) throws IOException {
|
||||
String url = getConfService().getString(CloudRkService.KEY_VENDOR_REPORT_URL);
|
||||
AtomicBoolean modified = new AtomicBoolean(false);
|
||||
try {
|
||||
post(url, data -> {
|
||||
// data.put("entName", company.getName());
|
||||
data.put("entId", cloudRk.getCloudId());
|
||||
data.put("get", true);
|
||||
}, json -> {
|
||||
modified.set(applyVendorScore(json, cloudRk, holder));
|
||||
saveJsonToFile(company, json, "vendor-score.json", holder);
|
||||
});
|
||||
} catch (IOException e) {
|
||||
catchException(e, holder);
|
||||
}
|
||||
return modified.get();
|
||||
}
|
||||
|
||||
private boolean applyVendorScore(JsonNode json, CloudRk cloudRk, MessageHolder holder) {
|
||||
if (isUnSuccess(json, holder)) {
|
||||
return false;
|
||||
}
|
||||
boolean modified = false;
|
||||
String grade = "";
|
||||
int score = 0;
|
||||
String description = "";
|
||||
if (isUnHasField(json, "data", holder)) {
|
||||
grade = "-";
|
||||
score = -1;
|
||||
} else {
|
||||
JsonNode data = json.get("data");
|
||||
if (data.isNull()) {
|
||||
grade = "无";
|
||||
score = -1;
|
||||
} else {
|
||||
grade = data.get("scoreLevel").asText();
|
||||
score = data.get("score").asInt();
|
||||
description = data.get("scoreDes").asText();
|
||||
}
|
||||
}
|
||||
|
||||
if (updateText(cloudRk::getVendorGrade, cloudRk::setVendorGrade, grade, holder, "供应商信用得分")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateNumber(cloudRk::getVendorScore, cloudRk::setVendorScore, score, holder, "供应商信用总分")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(cloudRk::getVendorDescription, cloudRk::setVendorDescription, description, holder, "供应商信用评级说明")) {
|
||||
modified = true;
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动获取到平台编号
|
||||
*
|
||||
* @param company 公司对象
|
||||
* @param cloudRk Cloud Rk
|
||||
* @return true 更新了 cloudId,否则false
|
||||
*/
|
||||
private boolean queryCloudIdAndSelectOne(
|
||||
Company company, CloudRk cloudRk, MessageHolder holder
|
||||
) {
|
||||
try {
|
||||
List<CloudRkService.EntInfo> entInfos = queryEnterpriseWithFuzzy(company, cloudRk, holder);
|
||||
// 返回的查询结果为空时
|
||||
if (entInfos.isEmpty()) {
|
||||
// 设置为 -, 不在重复查找
|
||||
cloudRk.setCloudId("-");
|
||||
holder.warn("在平台中没有匹配到 " + company.getName());
|
||||
return false;
|
||||
}
|
||||
|
||||
// 在返回的结果中,找到与公司名字一致的一个
|
||||
Optional<CloudRkService.EntInfo> optional = entInfos.stream().filter(n -> n.getName().equals(company.getName())).findAny();
|
||||
if (optional.isPresent()) {
|
||||
cloudRk.setCloudId(optional.get().getId());
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
holder.error("在平台中查询到多个匹配 (" + entInfos.stream().map(CloudRkService.EntInfo::getName).collect(Collectors.joining(", ")) + "),请手工同步选择匹配");
|
||||
return false;
|
||||
|
||||
} catch (Exception e) {
|
||||
// 异常
|
||||
holder.error("查询接口获取企业平台编号发生错误 = " + e.getMessage());
|
||||
if (logger.isErrorEnabled()) {
|
||||
logger.error("使用模糊查询接口获取 {} 企业平台编号发生错误", company.getName(), e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 使用模糊查询接口查询相关企业信息
|
||||
*/
|
||||
public List<CloudRkService.EntInfo> queryEnterpriseWithFuzzy(
|
||||
Company company, CloudRk cloudRk, MessageHolder holder
|
||||
) throws IOException {
|
||||
String url = getConfService().getString(CloudRkService.KEY_ENT_FUZZY_URL);
|
||||
List<CloudRkService.EntInfo> results = new ArrayList<>();
|
||||
ObjectMapper objectMapper = getObjectMapper();
|
||||
try {
|
||||
HttpJsonUtils.post(url, data -> {
|
||||
data.put("theKey", company.getName());
|
||||
data.put("get", true);
|
||||
}, json -> {
|
||||
applyEnterpriseQuery(json, company, cloudRk, results, holder);
|
||||
saveJsonToFile(company, json, "fuzzy.json", holder);
|
||||
}, objectMapper, getSocksProxy());
|
||||
} catch (IOException ex) {
|
||||
catchException(ex, holder);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private boolean applyEnterpriseQuery(JsonNode json, Company company, CloudRk cloudRk, List<CloudRkService.EntInfo> results, MessageHolder holder) {
|
||||
if (!json.has("data")) {
|
||||
// 没有数据
|
||||
holder.error("数据异常,返回的json中没有 data 字段");
|
||||
return false;
|
||||
}
|
||||
JsonNode dataNode = json.get("data");
|
||||
if (!dataNode.isArray()) {
|
||||
holder.error("数据异常,返回的json中 data 字段不是数组");
|
||||
return false;
|
||||
}
|
||||
ObjectMapper objectMapper = getObjectMapper();
|
||||
|
||||
for (JsonNode node : dataNode) {
|
||||
try {
|
||||
CloudRkService.EntInfo entInfo = new CloudRkService.EntInfo();
|
||||
objectMapper.updateValue(entInfo, node);
|
||||
if (node.has("isNowName")) {
|
||||
String s = node.get("isNowName").asText();
|
||||
entInfo.setNowName(s.equals("1") || s.equals("true"));
|
||||
}
|
||||
results.add(entInfo);
|
||||
} catch (Exception e) {
|
||||
holder.error("更新企业信息失败:" + e.getMessage() + ", json=" + node.toString());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新企业工商注册信息
|
||||
*/
|
||||
public boolean updateEnterpriseInfo(
|
||||
Company company, CloudRk cloudRk, MessageHolder holder
|
||||
) throws IOException {
|
||||
String api = getConfService().getString(CloudRkService.KEY_ENT_REPORT_URL);
|
||||
Proxy socksProxy = getSocksProxy();
|
||||
holder.debug("更新企业工商注册信息: " + company.getName() + " @ " + api + ", proxy=" + socksProxy);
|
||||
AtomicBoolean modified = new AtomicBoolean(false);
|
||||
try {
|
||||
post(api, data -> {
|
||||
data.put("entName", company.getName());
|
||||
data.put("entid", cloudRk.getCloudId());
|
||||
data.put("get", true);
|
||||
data.put("method", "data");
|
||||
data.put("nodetype", "B1001");
|
||||
}, jsonNode -> {
|
||||
modified.set(applyEnterpriseInfo(jsonNode, company, cloudRk, holder));
|
||||
saveJsonToFile(company, jsonNode, FileUtils.FILE_B1001_JSON, holder);
|
||||
});
|
||||
} catch (IOException e) {
|
||||
catchException(e, holder);
|
||||
}
|
||||
return modified.get();
|
||||
}
|
||||
|
||||
private void catchException(IOException e, MessageHolder holder) throws IOException {
|
||||
if (e instanceof SSLException) {
|
||||
holder.error("网络错误:" + e.getMessage());
|
||||
// 网络错误时,抛出异常,中断后续网络请求
|
||||
throw e;
|
||||
}
|
||||
if (e instanceof SocketException) {
|
||||
holder.error("网络错误:" + e.getMessage());
|
||||
// 网络错误时,抛出异常,中断后续网络请求
|
||||
throw e;
|
||||
}
|
||||
holder.error(e.getMessage());
|
||||
}
|
||||
|
||||
private boolean applyEnterpriseInfo(JsonNode json, Company company, CloudRk cloudRk, MessageHolder holder) {
|
||||
if (!json.has("B1001")) {
|
||||
holder.error("数据异常,返回的json中没有 B1001 字段");
|
||||
return false;
|
||||
}
|
||||
JsonNode b1001 = json.get("B1001");
|
||||
if (!b1001.has("count") || b1001.get("count").asInt() < 1 || !b1001.has("data")) {
|
||||
holder.error("数据异常,B1001 字段没有数据");
|
||||
return false;
|
||||
}
|
||||
boolean modified = false;
|
||||
JsonNode data = b1001.get("data");
|
||||
if (updateText(company::getEntType, company::setEntType, data, "enttype", holder, "企业类型")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(company::getEntStatus, company::setEntStatus, data, "entstatus", holder, "企业状态")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDate(company::getSetupDate, company::setSetupDate, data, "esdate", holder, "成立日期")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(company::getUniscid, company::setUniscid, data, "uniscid", holder, "企业状态")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(company::getRegAddr, company::setRegAddr, data, "dom", holder, "注册地址")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(company::getRegisteredCapital, company::setRegisteredCapital, data, "regcap", holder, "注册资金")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(company::getRegisteredCapitalCurrency, company::setRegisteredCapitalCurrency, data, "regcapcur", holder, "资本金币种")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(company::getLegalRepresentative, company::setLegalRepresentative, data, "frname", holder, "法人")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(company::getDistrict, company::setDistrict, data, "regorgprovince", holder, "注册区域")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(company::getTelephone, company::setTelephone, data, "tel", holder, "注册电话")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(company::getAddress, company::setAddress, data, "oploc", holder, "通讯地址")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateOperationPeriodBegin(company, data, holder)) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateOperationPeriodEnd(company, data, holder)) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(company::getIndustry, company::setIndustry, data, "nicfulltitle", holder, "行业")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
updateCompanyNameHistory(company, data, holder.sub("曾用名"));
|
||||
updateLegalRepresentativeContact(company, data, holder.sub("法人联系方式"));
|
||||
updateInstant(cloudRk::getCloudEntUpdate, cloudRk::setCloudEntUpdate, data, "updated", holder, "更新时间", false);
|
||||
return modified;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新法人联系人联系方式
|
||||
*/
|
||||
private void updateLegalRepresentativeContact(Company company, JsonNode data, MessageHolder holder) {
|
||||
String legalRepresentative = company.getLegalRepresentative();
|
||||
if (!StringUtils.hasText(legalRepresentative)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
CompanyContactService contactService = SpringApp.getBean(CompanyContactService.class);
|
||||
List<CompanyContact> contactList = contactService.findAllByCompanyAndName(company, legalRepresentative);
|
||||
if (contactList == null) {
|
||||
// db error
|
||||
return;
|
||||
}
|
||||
CompanyContact contact = null;
|
||||
boolean modified = false;
|
||||
if (contactList.isEmpty()) {
|
||||
//没有,创建法人联系人
|
||||
contact = new CompanyContact();
|
||||
contact.setCompany(company);
|
||||
contact.setName(legalRepresentative);
|
||||
contact.setPosition("法定代表人");
|
||||
contact.setCreated(LocalDate.now());
|
||||
modified = true;
|
||||
} else {
|
||||
// 先尝试查找法人
|
||||
Optional<CompanyContact> any = contactList.stream().filter(c -> "法定代表人".equals(c.getPosition())).findAny();
|
||||
// 如果没有找到,列表中第一个联系人
|
||||
if (any.isEmpty()) {
|
||||
any = contactList.stream().findFirst();
|
||||
}
|
||||
contact = any.get();
|
||||
if (updateText(contact::getPosition, contact::setPosition, "法定代表人", holder, "职位")) {
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!StringUtils.hasText(contact.getEmail())) {
|
||||
if (updateText(contact::getEmail, contact::setEmail, data, "email", holder, "邮箱")) {
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!StringUtils.hasText(contact.getAddress())) {
|
||||
if (updateText(contact::getAddress, contact::setAddress, data, "oploc", holder, "地址")) {
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!StringUtils.hasText(contact.getPhone())) {
|
||||
if (updateText(contact::getPhone, contact::setPhone, data, "tel", holder, "电话")) {
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
contactService.save(contact);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private boolean updateOperationPeriodBegin(Company company, JsonNode data, MessageHolder holder) {
|
||||
return updateLocalDate(company::getOperationPeriodBegin, company::setOperationPeriodBegin, data, "opfrom", holder, "营业期限起始日期", true);
|
||||
}
|
||||
|
||||
private boolean updateOperationPeriodEnd(Company company, JsonNode data, MessageHolder holder) {
|
||||
JsonNode node = data.get("opto");
|
||||
if (node == null) {
|
||||
return false;
|
||||
}
|
||||
String text = node.asText();
|
||||
if (StringUtils.hasText(text)) {
|
||||
if (text.equals("-")) {
|
||||
return updateLocalDate(company::getOperationPeriodEnd, company::setOperationPeriodEnd, (LocalDate) null, holder, "营业期限截至日期", true);
|
||||
}
|
||||
return updateLocalDate(company::getOperationPeriodEnd, company::setOperationPeriodEnd, data, "opto", holder, "营业期限截至日期", true);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private void updateCompanyNameHistory(Company company, JsonNode data, MessageHolder holder) {
|
||||
JsonNode node = data.get("nameHistory");
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
// 历史曾用名
|
||||
String nameHistory = node.asText();
|
||||
if (!StringUtils.hasText(nameHistory)) {
|
||||
return;
|
||||
}
|
||||
List<String> historyNames = new ArrayList<>();
|
||||
for (String str : nameHistory.split(",")) {
|
||||
String trimmed = str.trim();
|
||||
if (StringUtils.hasText(trimmed)) {
|
||||
historyNames.add(trimmed);
|
||||
}
|
||||
}
|
||||
CompanyOldNameService service = SpringApp.getBean(CompanyOldNameService.class);
|
||||
List<CompanyOldName> oldNames = service.findAllByCompany(company);
|
||||
for (CompanyOldName oldName : oldNames) {
|
||||
// 已经存在的移除
|
||||
historyNames.remove(oldName.getName());
|
||||
}
|
||||
for (String historyName : historyNames) {
|
||||
CompanyOldName oldName = service.createNew(company, historyName, false);
|
||||
oldName.setMemo("从相关方平台导入");
|
||||
service.save(oldName);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean updateLocalDate(Supplier<LocalDate> getter, Consumer<LocalDate> setter, JsonNode data, String field, MessageHolder holder, String topic, boolean allowNull) {
|
||||
JsonNode node = data.get(field);
|
||||
if (node == null || node.isNull()) {
|
||||
return false;
|
||||
}
|
||||
LocalDate localDate = getObjectMapper().convertValue(node, LocalDate.class);
|
||||
if (localDate == null && !allowNull) {
|
||||
return false;
|
||||
}
|
||||
return updateLocalDate(getter, setter, localDate, holder, topic, allowNull);
|
||||
}
|
||||
|
||||
private boolean updateLocalDate(Supplier<LocalDate> getter, Consumer<LocalDate> setter, JsonNode data, String field, MessageHolder holder, String topic) {
|
||||
return updateLocalDate(getter, setter, data, field, holder, topic, false);
|
||||
}
|
||||
|
||||
private void updateInstant(Supplier<Instant> getter, Consumer<Instant> setter, JsonNode data, String field, MessageHolder holder, String topic, boolean allowNull) {
|
||||
JsonNode node = data.get("updated");
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
LocalDateTime updated = getObjectMapper().convertValue(node, LocalDateTime.class);
|
||||
if (updated == null) {
|
||||
if (!allowNull) {
|
||||
return;
|
||||
}
|
||||
updateInstant(getter, setter, null, holder, topic);
|
||||
return;
|
||||
}
|
||||
Instant instant = updated.toInstant(ZoneOffset.ofHours(8));
|
||||
updateInstant(getter, setter, instant, holder, topic);
|
||||
}
|
||||
|
||||
private boolean updateText(Supplier<String> getter, Consumer<String> setter, JsonNode data, String field, MessageHolder holder, String topic) {
|
||||
JsonNode node = data.get(field);
|
||||
if (node == null || node.isNull()) {
|
||||
return false;
|
||||
}
|
||||
String text = node.asText();
|
||||
if (!StringUtils.hasText(text)) {
|
||||
return false;
|
||||
}
|
||||
return updateText(getter, setter, text, holder, topic);
|
||||
}
|
||||
|
||||
private void saveJsonToFile(Company company, JsonNode json, String fileName, MessageHolder holder) {
|
||||
String companyPath = company.getPath();
|
||||
if (!StringUtils.hasText(companyPath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
File dir = new File(companyPath);
|
||||
if (!dir.exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
File file = new File(dir, fileName);
|
||||
try {
|
||||
getObjectMapper().writeValue(file, json);
|
||||
holder.debug("保存文件 " + file.getName());
|
||||
} catch (IOException e) {
|
||||
holder.error("保存文件 " + file.getName() + " 发生错误:" + e.getMessage());
|
||||
logger.error("Save {}", file.getAbsolutePath(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public CloudRk getOrCreateCloudRk(Company company) {
|
||||
return getCloudRkService().getOrCreateCloudRk(company);
|
||||
}
|
||||
|
||||
public CloudRk save(CloudRk cloudRk) {
|
||||
return getCloudRkService().save(cloudRk);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.ecep.contract.cloud.tyc;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.ecep.contract.ds.MyRepository;
|
||||
import com.ecep.contract.model.CloudTyc;
|
||||
import com.ecep.contract.model.Company;
|
||||
|
||||
@Repository
|
||||
public interface CloudTycRepository extends MyRepository<CloudTyc, Integer> {
|
||||
|
||||
List<CloudTyc> findAllByCompanyId(Integer companyId);
|
||||
|
||||
Optional<CloudTyc> findByCompanyId(Integer companyId);
|
||||
|
||||
@Modifying
|
||||
@Transactional
|
||||
int deleteAllByCompany(Company company);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package com.ecep.contract.cloud.tyc;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.IEntityService;
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.cloud.CloudInfo;
|
||||
import com.ecep.contract.constant.CloudServiceConstant;
|
||||
import com.ecep.contract.model.CloudTyc;
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.util.MyStringUtils;
|
||||
|
||||
@Lazy
|
||||
@Service
|
||||
public class CloudTycService implements IEntityService<CloudTyc> {
|
||||
public final static String TYC_ENTERPRISE_ANALYSIS_REPORT = "企业分析报告";
|
||||
public final static String TYC_ENTERPRISE_BASIC_REPORT = "基础版企业信用报告";
|
||||
public final static String TYC_ENTERPRISE_MAJOR_REPORT = "专业版企业信用报告";
|
||||
public final static String TYC_ENTERPRISE_CREDIT_REPORT = "企业信用信息公示报告";
|
||||
public static final String URL_COMPANY = "https://www.tianyancha.com/company/%s";
|
||||
public static final String URL_COMPANY_SEARCH = "https://www.tianyancha.com/search?key=%s";
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(CloudTycService.class);
|
||||
|
||||
/**
|
||||
* 天眼查报告,文件名中必须包含 天眼查 字样
|
||||
*
|
||||
* @param fileName 文件名
|
||||
* @return 是否是天眼查报告
|
||||
*/
|
||||
public static boolean isTycReport(String fileName) {
|
||||
// 文件名中包含 天眼查 字样
|
||||
return fileName.contains(CloudServiceConstant.TYC_NAME);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private CloudTycRepository cloudTycRepository;
|
||||
|
||||
public CloudTyc getOrCreateCloudTyc(CloudInfo info) {
|
||||
Optional<CloudTyc> optional = cloudTycRepository.findById(info.getId());
|
||||
return optional.orElseGet(() -> getOrCreateCloudTyc(info.getCompany()));
|
||||
}
|
||||
|
||||
public CloudTyc getOrCreateCloudTyc(Company company) {
|
||||
Integer companyId = company.getId();
|
||||
List<CloudTyc> list = cloudTycRepository.findAllByCompanyId(companyId);
|
||||
if (list.isEmpty()) {
|
||||
CloudTyc tyc = new CloudTyc();
|
||||
tyc.setCompany(company);
|
||||
tyc.setScore(-1);
|
||||
tyc.setCloudLatest(null);
|
||||
return cloudTycRepository.save(tyc);
|
||||
}
|
||||
if (list.size() == 1) {
|
||||
return list.getFirst();
|
||||
}
|
||||
|
||||
// 查询有 CloudId 的记录
|
||||
List<CloudTyc> hasCouldIdList = list.stream()
|
||||
.filter(v -> {
|
||||
return StringUtils.hasText(v.getCloudId())
|
||||
&& MyStringUtils.isAllDigit(v.getCloudId());
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 没有匹配到一条时
|
||||
if (hasCouldIdList.isEmpty()) {
|
||||
// 保留第一条,其他删除
|
||||
CloudTyc rk = list.removeFirst();
|
||||
cloudTycRepository.deleteAll(list);
|
||||
return rk;
|
||||
}
|
||||
|
||||
// 只有匹配到一条有 CloudId 的记录
|
||||
if (hasCouldIdList.size() == 1) {
|
||||
// 保留匹配的记录,其他删除
|
||||
CloudTyc rk = hasCouldIdList.removeFirst();
|
||||
list.remove(rk);
|
||||
cloudTycRepository.deleteAll(list);
|
||||
return rk;
|
||||
}
|
||||
|
||||
// 查询有 Score 的记录
|
||||
List<CloudTyc> hasLatestList = hasCouldIdList.stream().filter(v -> {
|
||||
return v.getScore() != null && v.getScore() > 0;
|
||||
}).collect(Collectors.toList());
|
||||
// 没有匹配到一条时
|
||||
if (hasLatestList.isEmpty()) {
|
||||
// 保留第一条,其他删除
|
||||
CloudTyc rk = hasCouldIdList.removeFirst();
|
||||
list.remove(rk);
|
||||
cloudTycRepository.deleteAll(list);
|
||||
return rk;
|
||||
}
|
||||
|
||||
// 只有匹配到一条有 CloudId 的记录
|
||||
if (hasLatestList.size() == 1) {
|
||||
// 保留匹配的记录,其他删除
|
||||
CloudTyc rk = hasLatestList.removeFirst();
|
||||
list.remove(rk);
|
||||
cloudTycRepository.deleteAll(list);
|
||||
return rk;
|
||||
}
|
||||
|
||||
return hasLatestList.getFirst();
|
||||
|
||||
}
|
||||
|
||||
public CloudTyc save(CloudTyc cloudTyc) {
|
||||
return cloudTycRepository.save(cloudTyc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(CloudTyc entity) {
|
||||
cloudTycRepository.delete(entity);
|
||||
}
|
||||
|
||||
|
||||
public void deleteByCompany(Company company) {
|
||||
int deleted = cloudTycRepository.deleteAllByCompany(company);
|
||||
if (deleted > 0) {
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Delete {} records by company:#{}", deleted, company.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void resetTo(Company from, Company to) {
|
||||
List<CloudTyc> list = cloudTycRepository.findAllByCompanyId(from.getId());
|
||||
for (CloudTyc item : list) {
|
||||
item.setCompany(to);
|
||||
}
|
||||
cloudTycRepository.saveAll(list);
|
||||
}
|
||||
|
||||
public CloudTyc findById(Integer id) {
|
||||
return cloudTycRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
public Page<CloudTyc> findAll(Specification<CloudTyc> spec, PageRequest pageable) {
|
||||
return cloudTycRepository.findAll(spec, pageable);
|
||||
}
|
||||
|
||||
public Page<CloudTyc> findAll(Specification<CloudTyc> spec, Pageable pageable) {
|
||||
return cloudTycRepository.findAll(spec, pageable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Specification<CloudTyc> getSpecification(String searchText) {
|
||||
if (!StringUtils.hasText(searchText)) {
|
||||
return null;
|
||||
}
|
||||
return (root, query, builder) -> {
|
||||
return builder.like(root.get("cloudId"), "%" + searchText + "%");
|
||||
};
|
||||
}
|
||||
|
||||
public void syncCompany(Company company, MessageHolder holder) {
|
||||
// TODO 从天眼查同步公司信息
|
||||
holder.warn("TODO 未实现");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.ecep.contract.ds.MyRepository;
|
||||
import com.ecep.contract.model.CloudYu;
|
||||
import com.ecep.contract.model.Company;
|
||||
|
||||
@Repository
|
||||
public interface CloudYuRepository extends MyRepository<CloudYu, Integer> {
|
||||
|
||||
List<CloudYu> findAllByCompanyId(Integer companyId);
|
||||
|
||||
Optional<CloudYu> findByCompanyId(Integer companyId);
|
||||
|
||||
@Modifying
|
||||
@Transactional
|
||||
int deleteAllByCompany(Company company);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.ds.contract.service.ContractGroupService;
|
||||
import com.ecep.contract.model.ContractGroup;
|
||||
import com.ecep.contract.ui.Tasker;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 同步合同分组
|
||||
*/
|
||||
public class ContractGroupSyncTask extends Tasker<Object> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ContractGroupSyncTask.class);
|
||||
@Setter
|
||||
private ContractGroupService contractGroupService;
|
||||
|
||||
public ContractGroupSyncTask() {
|
||||
updateTitle("用友U8系统-同步合同分组信息");
|
||||
}
|
||||
|
||||
ContractGroupService getContractGroupService() {
|
||||
if (contractGroupService == null) {
|
||||
contractGroupService = SpringApp.getBean(ContractGroupService.class);
|
||||
}
|
||||
return contractGroupService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object execute(MessageHolder holder) throws Exception {
|
||||
YongYouU8Service service = null;
|
||||
try {
|
||||
service = SpringApp.getBean(YongYouU8Service.class);
|
||||
} catch (BeansException e) {
|
||||
logger.error("can't get bean of YongYouU8Service", e);
|
||||
holder.error("can't get bean of YongYouU8Service");
|
||||
return null;
|
||||
}
|
||||
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
logger.info("读取 U8 系统 CM_Group 数据表...");
|
||||
List<Map<String, Object>> list = service.queryAllContractGroup();
|
||||
int size = list.size();
|
||||
holder.debug("总共读取 CM_Group 数据 " + size + " 条");
|
||||
|
||||
for (Map<String, Object> map : list) {
|
||||
if (isCancelled()) {
|
||||
holder.info("Cancelled");
|
||||
return null;
|
||||
}
|
||||
MessageHolder sub = holder.sub(counter.get() + "/" + size + ">");
|
||||
sync(map, sub);
|
||||
// 更新进度
|
||||
updateProgress(counter.incrementAndGet(), size);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void sync(Map<String, Object> map, MessageHolder holder) {
|
||||
boolean modified = false;
|
||||
|
||||
String groupCode = (String) map.get("cGroupID");
|
||||
String groupName = (String) map.get("cGroupName");
|
||||
String groupTitle = (String) map.get("cRemark");
|
||||
|
||||
ContractGroupService service = getContractGroupService();
|
||||
ContractGroup contractGroup = service.findByCode(groupCode);
|
||||
if (contractGroup == null) {
|
||||
contractGroup = service.newContractGroup();
|
||||
holder.info("新建合同分组:" + groupCode);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!Objects.equals(contractGroup.getCode(), groupCode)) {
|
||||
contractGroup.setCode(groupCode);
|
||||
holder.info("合同分组代码:" + contractGroup.getCode() + " -> " + groupCode);
|
||||
modified = true;
|
||||
}
|
||||
if (!Objects.equals(contractGroup.getName(), groupName)) {
|
||||
contractGroup.setName(groupName);
|
||||
holder.info("合同分组名称:" + contractGroup.getName() + " -> " + groupName);
|
||||
modified = true;
|
||||
}
|
||||
if (!Objects.equals(contractGroup.getTitle(), groupTitle)) {
|
||||
contractGroup.setTitle(groupTitle);
|
||||
holder.info("合同分组标题:" + contractGroup.getTitle() + " -> " + groupTitle);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
service.save(contractGroup);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.ds.contract.service.ContractKindService;
|
||||
import com.ecep.contract.model.ContractKind;
|
||||
import com.ecep.contract.ui.Tasker;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 同步合同分类
|
||||
*/
|
||||
public class ContractKindSyncTask extends Tasker<Object> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ContractKindSyncTask.class);
|
||||
@Setter
|
||||
private ContractKindService contractKindService;
|
||||
|
||||
public ContractKindSyncTask() {
|
||||
updateTitle("用友U8系统-同步合同分类信息");
|
||||
}
|
||||
|
||||
ContractKindService getContractKindService() {
|
||||
if (contractKindService == null) {
|
||||
contractKindService = SpringApp.getBean(ContractKindService.class);
|
||||
}
|
||||
return contractKindService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object execute(MessageHolder holder) throws Exception {
|
||||
YongYouU8Repository repository = null;
|
||||
try {
|
||||
repository = SpringApp.getBean(YongYouU8Repository.class);
|
||||
} catch (BeansException e) {
|
||||
logger.error("can't get bean of YongYouU8Repository", e);
|
||||
holder.error("can't get bean of YongYouU8Repository");
|
||||
return null;
|
||||
}
|
||||
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
logger.info("读取 U8 系统 CM_Kind 数据表...");
|
||||
List<Map<String, Object>> list = repository.queryAllContractKind();
|
||||
int size = list.size();
|
||||
holder.debug("总共读取 CM_Kind 数据 " + size + " 条");
|
||||
|
||||
for (Map<String, Object> map : list) {
|
||||
if (isCancelled()) {
|
||||
holder.info("Cancelled");
|
||||
return null;
|
||||
}
|
||||
MessageHolder sub = holder.sub(counter.get() + "/" + size + ">");
|
||||
sync(map, sub);
|
||||
// 更新进度
|
||||
updateProgress(counter.incrementAndGet(), size);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void sync(Map<String, Object> map, MessageHolder holder) {
|
||||
boolean modified = false;
|
||||
|
||||
String typeCode = (String) map.get("KindID");
|
||||
String typeName = (String) map.get("KindName");
|
||||
String typeTitle = (String) map.get("Description");
|
||||
|
||||
ContractKindService kindService = getContractKindService();
|
||||
ContractKind contractKind = kindService.findByCode(typeCode);
|
||||
if (contractKind == null) {
|
||||
contractKind = new ContractKind();
|
||||
holder.info("新建合同分类:" + typeCode);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!Objects.equals(contractKind.getCode(), typeCode)) {
|
||||
contractKind.setCode(typeCode);
|
||||
holder.info("合同分类代码:" + contractKind.getCode() + " -> " + typeCode);
|
||||
modified = true;
|
||||
}
|
||||
if (!Objects.equals(contractKind.getName(), typeName)) {
|
||||
contractKind.setName(typeName);
|
||||
holder.info("合同分类名称:" + contractKind.getName() + " -> " + typeName);
|
||||
modified = true;
|
||||
}
|
||||
if (!Objects.equals(contractKind.getTitle(), typeTitle)) {
|
||||
contractKind.setTitle(typeTitle);
|
||||
holder.info("合同分类描述:" + contractKind.getTitle() + " -> " + typeTitle);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
kindService.save(contractKind);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.ds.contract.tasker.AbstContractRepairTasker;
|
||||
|
||||
/**
|
||||
* 用友U8系统-同步全量合同
|
||||
*/
|
||||
public class ContractSyncAllTask extends AbstContractRepairTasker {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ContractSyncAllTask.class);
|
||||
|
||||
|
||||
@Override
|
||||
protected void repair(MessageHolder holder) {
|
||||
updateTitle("用友U8系统-同步全量合同");
|
||||
YongYouU8Repository repository = getBean(YongYouU8Repository.class);
|
||||
|
||||
long total = repository.countAllContracts();
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
|
||||
try (Stream<Map<String, Object>> stream = repository.queryAllContractForStream()) {
|
||||
stream.forEach(rs -> {
|
||||
if (isCancelled()) {
|
||||
holder.debug("Cancelled");
|
||||
return;
|
||||
}
|
||||
MessageHolder subHolder = holder.sub(counter.get() + " / " + total + "> ");
|
||||
try {
|
||||
repairFromCMList(rs, subHolder);
|
||||
} catch (Exception e) {
|
||||
updateMessage(e.getMessage());
|
||||
logger.error("data = {}", rs, e);
|
||||
|
||||
}
|
||||
updateProgress(counter.incrementAndGet(), total);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.ds.contract.service.ContractGroupService;
|
||||
import com.ecep.contract.ds.contract.service.ContractKindService;
|
||||
import com.ecep.contract.ds.contract.service.ContractTypeService;
|
||||
import com.ecep.contract.ds.other.service.EmployeeService;
|
||||
import com.ecep.contract.model.ContractGroup;
|
||||
import com.ecep.contract.model.ContractKind;
|
||||
import com.ecep.contract.model.ContractType;
|
||||
import com.ecep.contract.model.Employee;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public class ContractSyncContext {
|
||||
<T> T getBean(Class<T> requiredType) throws BeansException {
|
||||
return SpringApp.getBean(requiredType);
|
||||
}
|
||||
|
||||
private ContractTypeService contractTypeService;
|
||||
private ContractGroupService contractGroupService;
|
||||
private ContractKindService contractKindService;
|
||||
private EmployeeService employeeService;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
Consumer<String> consumer;
|
||||
|
||||
public ContractSyncContext() {
|
||||
}
|
||||
|
||||
private ContractTypeService getContractTypeService() {
|
||||
if (contractTypeService == null) {
|
||||
contractTypeService = getBean(ContractTypeService.class);
|
||||
}
|
||||
return contractTypeService;
|
||||
}
|
||||
|
||||
private ContractGroupService getContractGroupService() {
|
||||
if (contractGroupService == null) {
|
||||
contractGroupService = getBean(ContractGroupService.class);
|
||||
}
|
||||
return contractGroupService;
|
||||
}
|
||||
|
||||
private ContractKindService getContractKindService() {
|
||||
if (contractKindService == null) {
|
||||
contractKindService = getBean(ContractKindService.class);
|
||||
}
|
||||
return contractKindService;
|
||||
}
|
||||
|
||||
private EmployeeService getEmployeeService() {
|
||||
if (employeeService == null) {
|
||||
employeeService = getBean(EmployeeService.class);
|
||||
}
|
||||
return employeeService;
|
||||
}
|
||||
|
||||
public ContractType getTypeByCode(String typeCode) {
|
||||
return getContractTypeService().findByCode(typeCode);
|
||||
}
|
||||
|
||||
public ContractGroup getGroupByCode(String groupCode) {
|
||||
return getContractGroupService().findByCode(groupCode);
|
||||
}
|
||||
|
||||
public ContractKind getKindByName(String kindName) {
|
||||
return getContractKindService().findByName(kindName);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void updateMessage(String message) {
|
||||
Consumer<String> consumer = getConsumer();
|
||||
if (consumer != null) {
|
||||
consumer.accept(message);
|
||||
}
|
||||
}
|
||||
public Employee findEmployeeByCode(String personCode) {
|
||||
return getEmployeeService().findByCode(personCode);
|
||||
}
|
||||
public Employee findEmployeeByName(String personName) {
|
||||
if (personName == null) {
|
||||
return null;
|
||||
}
|
||||
return getEmployeeService().findByName(personName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.cloud.u8.ctx.ContractCtx;
|
||||
import com.ecep.contract.ds.contract.tasker.AbstContractRepairTasker;
|
||||
|
||||
/**
|
||||
* 合同同步任务
|
||||
*/
|
||||
public class ContractSyncTask extends AbstContractRepairTasker {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ContractSyncTask.class);
|
||||
private YongYouU8Repository repository;
|
||||
|
||||
public ContractSyncTask() {
|
||||
updateTitle("用友U8系统-同步合同");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void repair(MessageHolder holder) {
|
||||
updateTitle("用友U8系统-同步合同");
|
||||
try {
|
||||
repository = SpringApp.getBean(YongYouU8Repository.class);
|
||||
} catch (BeansException e) {
|
||||
holder.error("无法获取 YongYouU8Repository " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
if (getConfService().getBoolean(ContractCtx.KEY_SYNC_USE_LATEST_ID)) {
|
||||
syncByLatestId(holder);
|
||||
} else {
|
||||
syncByLatestDate(holder);
|
||||
}
|
||||
}
|
||||
|
||||
private void syncByLatestId(MessageHolder holder) {
|
||||
int latestId = getConfService().getInt(ContractCtx.KEY_SYNC_BY_LATEST_ID);
|
||||
updateTitle("用友U8系统-同步合同,从 " + latestId + " 开始");
|
||||
|
||||
Long total = repository.countAllContracts(latestId);
|
||||
updateTitle("用友U8系统-同步合同,从 " + latestId + " 开始,合计 " + total + " 条");
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("sync latest Id from {}, have {} record.", latestId, total);
|
||||
}
|
||||
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
AtomicReference<Integer> reference = new AtomicReference<>(latestId);
|
||||
try (Stream<Map<String, Object>> stream = repository.queryAllContractForStream(latestId)) {
|
||||
stream.forEach(rs -> {
|
||||
if (isCancelled()) {
|
||||
updateMessage("Cancelled");
|
||||
return;
|
||||
}
|
||||
MessageHolder subHolder = holder.sub(counter.get() + " / " + total + "> ");
|
||||
try {
|
||||
Integer ID = (Integer) rs.get("ID");
|
||||
repairFromCMList(rs, subHolder);
|
||||
if (ID != null) {
|
||||
Integer latest = reference.get();
|
||||
if (latest == null || ID.compareTo(latest) > 0) {
|
||||
reference.set(ID);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Contract sync failure {}, data = {}", e.getMessage(), rs, e);
|
||||
updateMessage(e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
updateProgress(counter.incrementAndGet(), total);
|
||||
});
|
||||
}
|
||||
getConfService().set(ContractCtx.KEY_SYNC_BY_LATEST_ID, String.valueOf(reference.get()));
|
||||
}
|
||||
|
||||
private void syncByLatestDate(MessageHolder holder) {
|
||||
String strDateTime = getConfService().getString(ContractCtx.KEY_SYNC_BY_LATEST_DATE);
|
||||
LocalDateTime latestDateTime = null;
|
||||
if (StringUtils.hasText(strDateTime)) {
|
||||
try {
|
||||
latestDateTime = LocalDateTime.parse(strDateTime);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
if (latestDateTime == null) {
|
||||
latestDateTime = LocalDateTime.of(2025, 1, 1, 0, 0, 0);
|
||||
}
|
||||
updateTitle("用友U8系统-同步合同,从 " + latestDateTime + " 开始");
|
||||
|
||||
Long total = repository.countAllContracts(latestDateTime);
|
||||
updateTitle("用友U8系统-同步合同,从 " + latestDateTime + " 开始,合计 " + total + " 条");
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("from latest date {}, have {} record.", latestDateTime, total);
|
||||
}
|
||||
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
AtomicReference<LocalDateTime> reference = new AtomicReference<>(latestDateTime);
|
||||
|
||||
try (Stream<Map<String, Object>> stream = repository.queryAllContractForStream(latestDateTime)) {
|
||||
stream.forEach(rs -> {
|
||||
if (isCancelled()) {
|
||||
updateMessage("Cancelled");
|
||||
return;
|
||||
}
|
||||
|
||||
MessageHolder subHolder = holder.sub(counter.get() + " / " + total + "> ");
|
||||
try {
|
||||
Timestamp dtDate = (Timestamp) rs.get("dtDate");
|
||||
|
||||
repairFromCMList(rs, subHolder);
|
||||
|
||||
if (dtDate != null) {
|
||||
LocalDateTime latest = reference.get();
|
||||
LocalDateTime localDate = dtDate.toLocalDateTime();
|
||||
if (latest == null || localDate.isAfter(latest)) {
|
||||
reference.set(localDate);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Contract sync failure {}, data = {}", e.getMessage(), rs, e);
|
||||
updateMessage(e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
updateProgress(counter.incrementAndGet(), total);
|
||||
});
|
||||
}
|
||||
getConfService().set(ContractCtx.KEY_SYNC_BY_LATEST_DATE, String.valueOf(reference.get()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.ds.contract.service.ContractTypeService;
|
||||
import com.ecep.contract.model.ContractType;
|
||||
import com.ecep.contract.ui.Tasker;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 同步合同类型
|
||||
*/
|
||||
public class ContractTypeSyncTask extends Tasker<Object> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ContractTypeSyncTask.class);
|
||||
@Setter
|
||||
private ContractTypeService contractTypeService;
|
||||
|
||||
public ContractTypeSyncTask() {
|
||||
updateTitle("用友U8系统-同步合同类型信息");
|
||||
}
|
||||
|
||||
ContractTypeService getContractTypeService() {
|
||||
if (contractTypeService == null) {
|
||||
contractTypeService = SpringApp.getBean(ContractTypeService.class);
|
||||
}
|
||||
return contractTypeService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object execute(MessageHolder holder) throws Exception {
|
||||
YongYouU8Repository repository = null;
|
||||
try {
|
||||
repository = SpringApp.getBean(YongYouU8Repository.class);
|
||||
} catch (BeansException e) {
|
||||
logger.error("can't get bean of YongYouU8Service", e);
|
||||
holder.error("can't get bean of YongYouU8Service");
|
||||
return null;
|
||||
}
|
||||
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
logger.info("读取 U8 系统 CM_Type,CM_TypeClass 数据表...");
|
||||
List<Map<String, Object>> list = repository.queryAllContractType();
|
||||
int size = list.size();
|
||||
holder.debug("总共读取 CM_Type,CM_TypeClass 数据 " + size + " 条");
|
||||
|
||||
for (Map<String, Object> map : list) {
|
||||
if (isCancelled()) {
|
||||
holder.info("Cancelled");
|
||||
return null;
|
||||
}
|
||||
MessageHolder sub = holder.sub(counter.get() + "/" + size + ">");
|
||||
sync(map, sub);
|
||||
// 更新进度
|
||||
updateProgress(counter.incrementAndGet(), size);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void sync(Map<String, Object> map, MessageHolder holder) {
|
||||
boolean modified = false;
|
||||
|
||||
String typeCode = (String) map.get("cTypeCode");
|
||||
String typeName = (String) map.get("cTypeName");
|
||||
String typeTitle = (String) map.get("cCharacter");
|
||||
String typeCatalog = (String) map.get("cClassName");
|
||||
String typeDirection = (String) map.get("cDirection");
|
||||
|
||||
ContractTypeService typeService = getContractTypeService();
|
||||
ContractType contractType = typeService.findByCode(typeCode);
|
||||
if (contractType == null) {
|
||||
contractType = new ContractType();
|
||||
holder.info("新建合同类型:" + typeCode);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!Objects.equals(contractType.getCode(), typeCode)) {
|
||||
contractType.setCode(typeCode);
|
||||
holder.info("合同类型代码:" + contractType.getCode() + " -> " + typeCode);
|
||||
modified = true;
|
||||
}
|
||||
if (!Objects.equals(contractType.getName(), typeName)) {
|
||||
contractType.setName(typeName);
|
||||
holder.info("合同类型名称:" + contractType.getName() + " -> " + typeName);
|
||||
modified = true;
|
||||
}
|
||||
if (!Objects.equals(contractType.getTitle(), typeTitle)) {
|
||||
contractType.setTitle(typeTitle);
|
||||
holder.info("合同类型标题:" + contractType.getTitle() + " -> " + typeTitle);
|
||||
modified = true;
|
||||
}
|
||||
if (!Objects.equals(contractType.getCatalog(), typeCatalog)) {
|
||||
contractType.setCatalog(typeCatalog);
|
||||
holder.info("合同类型分类:" + contractType.getCatalog() + " -> " + typeCatalog);
|
||||
modified = true;
|
||||
}
|
||||
if (!Objects.equals(contractType.getDirection(), typeDirection)) {
|
||||
contractType.setDirection(typeDirection);
|
||||
holder.info("合同类型方向:" + contractType.getDirection() + " -> " + typeDirection);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
typeService.save(contractType);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.ds.customer.service.CompanyCustomerService;
|
||||
import com.ecep.contract.model.CustomerCatalog;
|
||||
import com.ecep.contract.ui.Tasker;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 同步客户分类
|
||||
*/
|
||||
public class CustomerClassSyncTask extends Tasker<Object> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(CustomerClassSyncTask.class);
|
||||
@Setter
|
||||
private CompanyCustomerService companyCustomerService;
|
||||
|
||||
public CustomerClassSyncTask() {
|
||||
updateTitle("用友U8系统-同步客户分类信息");
|
||||
}
|
||||
|
||||
CompanyCustomerService getCompanyCustomerService() {
|
||||
if (companyCustomerService == null) {
|
||||
companyCustomerService = SpringApp.getBean(CompanyCustomerService.class);
|
||||
}
|
||||
return companyCustomerService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object execute(MessageHolder holder) throws Exception {
|
||||
YongYouU8Repository repository = null;
|
||||
try {
|
||||
repository = SpringApp.getBean(YongYouU8Repository.class);
|
||||
} catch (BeansException e) {
|
||||
logger.error("can't get bean of YongYouU8Repository", e);
|
||||
holder.error("can't get bean of YongYouU8Repository");
|
||||
return null;
|
||||
}
|
||||
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
logger.info("读取 U8 系统 CustomerClass 数据表...");
|
||||
List<Map<String, Object>> list = repository.queryAllCustomerClass();
|
||||
int size = list.size();
|
||||
holder.debug("总共读取 CustomerClass 数据 " + size + " 条");
|
||||
|
||||
for (Map<String, Object> map : list) {
|
||||
if (isCancelled()) {
|
||||
holder.info("Cancelled");
|
||||
return null;
|
||||
}
|
||||
MessageHolder sub = holder.sub(counter.get() + "/" + size + ">");
|
||||
sync(map, sub);
|
||||
// 更新进度
|
||||
updateProgress(counter.incrementAndGet(), size);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void sync(Map<String, Object> map, MessageHolder holder) {
|
||||
boolean modified = false;
|
||||
|
||||
String code = (String) map.get("cCCCode");
|
||||
String name = (String) map.get("cCCName");
|
||||
|
||||
CompanyCustomerService customerService = getCompanyCustomerService();
|
||||
CustomerCatalog customerCatalog = customerService.findCatalogByCode(code);
|
||||
if (customerCatalog == null) {
|
||||
customerCatalog = new CustomerCatalog();
|
||||
holder.info("新建客户分类:" + code);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!Objects.equals(customerCatalog.getCode(), code)) {
|
||||
customerCatalog.setCode(code);
|
||||
holder.info("客户分类代码:" + customerCatalog.getCode() + " -> " + code);
|
||||
modified = true;
|
||||
}
|
||||
if (!Objects.equals(customerCatalog.getName(), name)) {
|
||||
customerCatalog.setName(name);
|
||||
holder.info("客户分类名称:" + customerCatalog.getName() + " -> " + name);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
customerService.save(customerCatalog);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.cloud.u8.ctx.CompanyCtx;
|
||||
import com.ecep.contract.cloud.u8.ctx.CustomerCtx;
|
||||
import com.ecep.contract.constant.CloudServiceConstant;
|
||||
import com.ecep.contract.ds.company.service.CompanyService;
|
||||
import com.ecep.contract.ds.contract.tasker.AbstContractRepairTasker;
|
||||
import com.ecep.contract.ds.customer.service.CompanyCustomerService;
|
||||
import com.ecep.contract.model.CloudYu;
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.model.CompanyCustomer;
|
||||
import com.ecep.contract.model.CompanyCustomerEntity;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 同步客户任务
|
||||
*/
|
||||
public class CustomerSyncTask extends AbstContractRepairTasker {
|
||||
private static final Logger logger = LoggerFactory.getLogger(CustomerSyncTask.class);
|
||||
private YongYouU8Repository repository;
|
||||
private final CustomerCtx customerCtx = new CustomerCtx();
|
||||
@Setter
|
||||
private YongYouU8Service yongYouU8Service;
|
||||
|
||||
public CustomerSyncTask() {
|
||||
updateTitle("用友U8系统-同步客户");
|
||||
}
|
||||
|
||||
private CompanyCustomerService getCompanyCustomerService() {
|
||||
return customerCtx.getCompanyCustomerService();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void repair(MessageHolder holder) {
|
||||
try {
|
||||
yongYouU8Service = getBean(YongYouU8Service.class);
|
||||
} catch (BeansException e) {
|
||||
holder.warn("未启用 " + CloudServiceConstant.U8_NAME + " 服务");
|
||||
return;
|
||||
}
|
||||
repository = SpringApp.getBean(YongYouU8Repository.class);
|
||||
yongYouU8Service.initialize(customerCtx);
|
||||
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
Long total = repository.countAllCustomers();
|
||||
updateTitle("用友U8系统-同步客户,合计 " + total + " 条");
|
||||
try (Stream<Map<String, Object>> stream = repository.queryAllCustomerForStream()) {
|
||||
stream.forEach(rs -> {
|
||||
if (isCancelled()) {
|
||||
updateMessage("Cancelled");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
String cusCode = (String) rs.get("cCusCode");
|
||||
MessageHolder subHolder = holder.sub(counter.get() + " / " + total + "> " + cusCode + " ");
|
||||
boolean modified = false;
|
||||
CompanyCustomerEntity entity = customerCtx.findOrCreateByCode(rs, cusCode, subHolder);
|
||||
Map<String, Object> map = repository.findCustomerByCusCode(entity.getCode());
|
||||
if (map == null) {
|
||||
subHolder.error("客户项不存在:" + cusCode);
|
||||
return;
|
||||
}
|
||||
if (customerCtx.applyEntityDetail(entity, map, subHolder)) {
|
||||
modified = true;
|
||||
}
|
||||
if (modified) {
|
||||
entity = customerCtx.getCompanyCustomerEntityService().save(entity);
|
||||
}
|
||||
updateCompanyNameAndAbbName(entity, subHolder);
|
||||
updateCloudYu(entity);
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Customer sync failure {}, data = {}", e.getMessage(), rs, e);
|
||||
updateMessage(e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
updateProgress(counter.incrementAndGet(), total);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void updateCloudYu(CompanyCustomerEntity entity) {
|
||||
CompanyCustomer customer = entity.getCustomer();
|
||||
if (customer == null) {
|
||||
return;
|
||||
}
|
||||
if (!Hibernate.isInitialized(customer)) {
|
||||
customer = getCompanyCustomerService().findById(customer.getId());
|
||||
}
|
||||
Company company = customer.getCompany();
|
||||
if (company == null) {
|
||||
return;
|
||||
}
|
||||
CloudYu cloudYu = yongYouU8Service.getOrCreateCloudYu(company);
|
||||
if (cloudYu == null) {
|
||||
return;
|
||||
}
|
||||
cloudYu.setCustomerUpdateDate(LocalDate.now());
|
||||
cloudYu.setCloudLatest(Instant.now());
|
||||
cloudYu.setExceptionMessage("");
|
||||
yongYouU8Service.save(cloudYu);
|
||||
|
||||
}
|
||||
|
||||
private void updateCompanyNameAndAbbName(CompanyCustomerEntity entity, MessageHolder holder) {
|
||||
CompanyService companyService = customerCtx.getCompanyService();
|
||||
CompanyCustomer customer = entity.getCustomer();
|
||||
if (customer == null) {
|
||||
return;
|
||||
}
|
||||
if (!Hibernate.isInitialized(customer)) {
|
||||
customer = getCompanyCustomerService().findById(customer.getId());
|
||||
}
|
||||
Company company = customer.getCompany();
|
||||
if (company == null) {
|
||||
return;
|
||||
}
|
||||
if (!Hibernate.isInitialized(company)) {
|
||||
company = companyService.findById(company.getId());
|
||||
}
|
||||
boolean modified = false;
|
||||
CompanyCtx companyCtx = customerCtx.getCompanyCtx();
|
||||
if (companyCtx.updateCompanyNameIfAbsent(company, entity.getName(), holder)) {
|
||||
modified = true;
|
||||
}
|
||||
if (companyCtx.updateCompanyAbbNameIfAbsent(company, entity.getAbbName(), holder)) {
|
||||
modified = true;
|
||||
}
|
||||
if (modified) {
|
||||
companyService.save(company);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.cloud.CloudInfo;
|
||||
import com.ecep.contract.model.CloudYu;
|
||||
import com.ecep.contract.ui.Tasker;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 数据迁移任务,从CloudInfo迁移到CloudYu
|
||||
*/
|
||||
public class DateTransferTask extends Tasker<Object> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(DateTransferTask.class);
|
||||
@Setter
|
||||
private YongYouU8Service yongYouU8Service;
|
||||
|
||||
public DateTransferTask() {
|
||||
updateTitle("用友U8系统-数据迁移任务");
|
||||
}
|
||||
|
||||
YongYouU8Service getYongYouU8Service() {
|
||||
if (yongYouU8Service == null) {
|
||||
yongYouU8Service = SpringApp.getBean(YongYouU8Service.class);
|
||||
}
|
||||
return yongYouU8Service;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object execute(MessageHolder holder) throws Exception {
|
||||
YongYouU8Service u8Service = null;
|
||||
try {
|
||||
u8Service = getYongYouU8Service();
|
||||
} catch (BeansException e) {
|
||||
logger.error("获取用友U8服务失败", e);
|
||||
holder.error("获取用友U8服务失败");
|
||||
return null;
|
||||
}
|
||||
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
logger.info("开始数据迁移任务...");
|
||||
Iterable<CloudInfo> cloudInfos = u8Service.findAllCloudYu();
|
||||
|
||||
// 计算总数
|
||||
int total = 0;
|
||||
for (@SuppressWarnings("unused")
|
||||
CloudInfo info : cloudInfos) {
|
||||
total++;
|
||||
}
|
||||
holder.debug("总共需要迁移的数据量: " + total + " 条");
|
||||
|
||||
// 重新获取Iterable以进行遍历
|
||||
cloudInfos = u8Service.findAllCloudYu();
|
||||
|
||||
for (CloudInfo v : cloudInfos) {
|
||||
if (isCancelled()) {
|
||||
holder.info("迁移任务已取消");
|
||||
return null;
|
||||
}
|
||||
|
||||
MessageHolder sub = holder.sub(counter.get() + "/" + total + ">");
|
||||
try {
|
||||
sync(v, sub);
|
||||
} catch (Exception e) {
|
||||
sub.error("迁移失败: " + e.getMessage());
|
||||
logger.error("迁移数据失败", e);
|
||||
}
|
||||
|
||||
// 更新进度
|
||||
updateProgress(counter.incrementAndGet(), total);
|
||||
}
|
||||
|
||||
holder.info("数据迁移任务完成");
|
||||
return null;
|
||||
}
|
||||
|
||||
private void sync(CloudInfo v, MessageHolder holder) {
|
||||
YongYouU8Service u8Service = getYongYouU8Service();
|
||||
CloudYu cloudYu = u8Service.getOrCreateCloudYu(v);
|
||||
|
||||
if (copyTo(v, cloudYu)) {
|
||||
u8Service.save(cloudYu);
|
||||
holder.info("成功迁移数据: " + v.getCloudId());
|
||||
} else {
|
||||
holder.debug("数据未变更,无需迁移: " + v.getCloudId());
|
||||
}
|
||||
}
|
||||
|
||||
boolean copyTo(CloudInfo v, CloudYu cloudRk) {
|
||||
boolean modified = false;
|
||||
if (!Objects.equals(cloudRk.getLatestUpdate(), v.getLatestUpdate())) {
|
||||
cloudRk.setLatestUpdate(v.getLatestUpdate());
|
||||
modified = true;
|
||||
}
|
||||
if (!Objects.equals(cloudRk.getCompany(), v.getCompany())) {
|
||||
cloudRk.setCompany(v.getCompany());
|
||||
modified = true;
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.ds.other.service.DepartmentService;
|
||||
import com.ecep.contract.model.Department;
|
||||
import com.ecep.contract.model.Employee;
|
||||
import com.ecep.contract.ui.Tasker;
|
||||
|
||||
/**
|
||||
* 用友U8系统-同步员工信息
|
||||
*/
|
||||
public class EmployeesSyncTask extends Tasker<Object> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(EmployeesSyncTask.class);
|
||||
private final AtomicInteger counter = new AtomicInteger(0);
|
||||
|
||||
DepartmentService departmentService;
|
||||
|
||||
public EmployeesSyncTask() {
|
||||
updateTitle("用友U8系统-同步员工信息");
|
||||
}
|
||||
|
||||
DepartmentService getDepartmentService() {
|
||||
if (departmentService == null) {
|
||||
departmentService = SpringApp.getBean(DepartmentService.class);
|
||||
}
|
||||
return departmentService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object execute(MessageHolder holder) throws Exception {
|
||||
YongYouU8Service service = SpringApp.getBean(YongYouU8Service.class);
|
||||
|
||||
holder.debug("读取 U8 系统 Person 数据表...");
|
||||
List<Map<String, Object>> list = service.queryAllPerson();
|
||||
int size = list.size();
|
||||
holder.debug("总共读取 Person 数据 " + size + " 条");
|
||||
|
||||
for (Map<String, Object> rs : list) {
|
||||
if (isCancelled()) {
|
||||
holder.debug("Cancelled");
|
||||
return null;
|
||||
}
|
||||
|
||||
MessageHolder sub = holder.sub(counter.get() + "/" + size + ">");
|
||||
sync(rs, sub);
|
||||
|
||||
// 更新进度
|
||||
updateProgress(counter.incrementAndGet(), size);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void sync(Map<String, Object> rs, MessageHolder holder) {
|
||||
String personCode = (String) rs.get("cPersonCode");
|
||||
String personName = (String) rs.get("cPersonName");
|
||||
String departmentCode = (String) rs.get("cDepCode");
|
||||
String personEmail = (String) rs.get("cPersonEmail");
|
||||
String personPhone = (String) rs.get("cPersonPhone");
|
||||
java.sql.Timestamp personValidDate = (java.sql.Timestamp) rs.get("dPValidDate");
|
||||
java.sql.Timestamp personInValidDate = (java.sql.Timestamp) rs.get("dPInValidDate");
|
||||
boolean modified = false;
|
||||
|
||||
Employee employee = getEmployeeService().findByCode(personCode);
|
||||
// 按员工代码未匹配时,尝试使用名字去匹配
|
||||
if (employee == null) {
|
||||
employee = getEmployeeService().findByName(personName);
|
||||
if (employee == null) {
|
||||
employee = new Employee();
|
||||
employee.setCode(personCode);
|
||||
employee.setName(personName);
|
||||
employee.setActive(false);
|
||||
employee.setCreated(LocalDate.now());
|
||||
holder.info("创建员工:" + personCode + ", 姓名:" + personName + ".");
|
||||
|
||||
// consumer.accept("员工编号:" + personCode + ", 姓名:" + personName + ", 本地未创建");
|
||||
// return;
|
||||
}
|
||||
employee.setCode(personCode);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
MessageHolder subHolder = holder.sub(personName + ": ");
|
||||
|
||||
if (!StringUtils.hasText(employee.getName())) {
|
||||
employee.setName(personName);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(departmentCode)) {
|
||||
Department departmentByCode = getDepartmentService().findByCode(departmentCode);
|
||||
if (departmentByCode == null) {
|
||||
subHolder.warn("部门代码:" + departmentCode + "未匹配到部门");
|
||||
} else {
|
||||
if (!Objects.equals(employee.getDepartment(), departmentByCode)) {
|
||||
employee.setDepartment(departmentByCode);
|
||||
subHolder.info("更新部门:" + departmentByCode.getName());
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(personPhone)) {
|
||||
if (!Objects.equals(personPhone, employee.getPhone())) {
|
||||
employee.setPhone(personPhone);
|
||||
subHolder.info("更新电话:" + personPhone);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(personEmail)) {
|
||||
if (!Objects.equals(personEmail, employee.getEmail())) {
|
||||
employee.setEmail(personEmail);
|
||||
subHolder.info("更新Email:" + personEmail);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (personValidDate != null) {
|
||||
LocalDate localDate = personValidDate.toLocalDateTime().toLocalDate();
|
||||
if (!Objects.equals(localDate, employee.getEntryDate())) {
|
||||
employee.setEntryDate(localDate);
|
||||
subHolder.info("更新入职日期:" + localDate);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (personInValidDate != null) {
|
||||
LocalDate localDate = personInValidDate.toLocalDateTime().toLocalDate();
|
||||
if (!Objects.equals(localDate, employee.getLeaveDate())) {
|
||||
employee.setLeaveDate(localDate);
|
||||
subHolder.info("更新离职日期:" + localDate);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
getEmployeeService().save(employee);
|
||||
subHolder.info("更新保存");
|
||||
} else {
|
||||
subHolder.debug("无更新");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
|
||||
import com.ecep.contract.ds.other.service.SysConfService;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Lazy
|
||||
@Component
|
||||
@ConditionalOnBean(YongYouU8Service.class)
|
||||
public class U8DataSourceFactory {
|
||||
@Lazy
|
||||
@Autowired
|
||||
private SysConfService service;
|
||||
|
||||
public U8DataSourceFactory() {
|
||||
System.out.println("U8DataSourceFactory.U8DataSourceFactory");
|
||||
}
|
||||
|
||||
@Lazy
|
||||
@Bean
|
||||
// @ConfigurationProperties(prefix = "u8.datasource")
|
||||
public DataSource MSSQL_Server() {
|
||||
System.out.println("U8DataSourceFactory.MSSQL_Server");
|
||||
|
||||
String host = service.get("u8.db.server.ip", "192.168.1.1");
|
||||
String database = service.get("u8.db.database", "UF_DATA");
|
||||
String username = service.get("u8.db.server.name", "sa");
|
||||
String password = service.get("u8.db.server.password", "");
|
||||
boolean encrypt = service.get("u8.db.server.encrypt", false);
|
||||
boolean trustServerCertificate = service.get("u8.db.server.trustServerCertificate", true);
|
||||
|
||||
String url = "jdbc:sqlserver://" +
|
||||
host +
|
||||
";databaseName=" +
|
||||
database +
|
||||
";encrypt=" +
|
||||
encrypt +
|
||||
";trustServerCertificate=" +
|
||||
trustServerCertificate
|
||||
// + ";sslProtocol=TLSv1"
|
||||
;
|
||||
|
||||
HikariDataSource source = DataSourceBuilder.create()
|
||||
.type(HikariDataSource.class)
|
||||
.url(url)
|
||||
.username(username)
|
||||
.password(password)
|
||||
.driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
|
||||
.build();
|
||||
source.setMinimumIdle(0);
|
||||
return source;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.ds.vendor.repository.VendorClassRepository;
|
||||
import com.ecep.contract.model.VendorCatalog;
|
||||
import com.ecep.contract.ui.Tasker;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 同步供应商分类
|
||||
*/
|
||||
public class VendorClassSyncTask extends Tasker<Object> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(VendorClassSyncTask.class);
|
||||
@Setter
|
||||
private VendorClassRepository vendorClassRepository;
|
||||
|
||||
public VendorClassSyncTask() {
|
||||
updateTitle("用友U8系统-同步供应商分类信息");
|
||||
}
|
||||
|
||||
VendorClassRepository getVendorClassRepository() {
|
||||
if (vendorClassRepository == null) {
|
||||
vendorClassRepository = SpringApp.getBean(VendorClassRepository.class);
|
||||
}
|
||||
return vendorClassRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object execute(MessageHolder holder) throws Exception {
|
||||
YongYouU8Service service = null;
|
||||
try {
|
||||
service = SpringApp.getBean(YongYouU8Service.class);
|
||||
} catch (BeansException e) {
|
||||
logger.error("can't get bean of YongYouU8Service", e);
|
||||
holder.error("can't get bean of YongYouU8Service");
|
||||
return null;
|
||||
}
|
||||
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
logger.info("读取 U8 系统 VendorClass 数据表...");
|
||||
List<Map<String, Object>> list = service.queryAllVendorClass();
|
||||
int size = list.size();
|
||||
holder.debug("总共读取 VendorClass 数据 " + size + " 条");
|
||||
|
||||
for (Map<String, Object> map : list) {
|
||||
if (isCancelled()) {
|
||||
holder.info("Cancelled");
|
||||
return null;
|
||||
}
|
||||
MessageHolder sub = holder.sub(counter.get() + "/" + size + ">");
|
||||
sync(map, sub);
|
||||
// 更新进度
|
||||
updateProgress(counter.incrementAndGet(), size);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void sync(Map<String, Object> map, MessageHolder holder) {
|
||||
boolean modified = false;
|
||||
|
||||
String code = (String) map.get("cVCCode");
|
||||
String name = (String) map.get("cVCName");
|
||||
|
||||
VendorClassRepository repository = getVendorClassRepository();
|
||||
VendorCatalog vendorCatalog = repository.findByCode(code).orElse(null);
|
||||
if (vendorCatalog == null) {
|
||||
vendorCatalog = new VendorCatalog();
|
||||
holder.info("新建供应商分类:" + code);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!Objects.equals(vendorCatalog.getCode(), code)) {
|
||||
vendorCatalog.setCode(code);
|
||||
holder.info("供应商分类代码:" + vendorCatalog.getCode() + " -> " + code);
|
||||
modified = true;
|
||||
}
|
||||
if (!Objects.equals(vendorCatalog.getName(), name)) {
|
||||
vendorCatalog.setName(name);
|
||||
holder.info("供应商分类名称:" + vendorCatalog.getName() + " -> " + name);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
repository.save(vendorCatalog);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.cloud.u8.ctx.CompanyCtx;
|
||||
import com.ecep.contract.cloud.u8.ctx.VendorCtx;
|
||||
import com.ecep.contract.constant.CloudServiceConstant;
|
||||
import com.ecep.contract.ds.company.service.CompanyService;
|
||||
import com.ecep.contract.ds.contract.tasker.AbstContractRepairTasker;
|
||||
import com.ecep.contract.ds.vendor.service.CompanyVendorEntityService;
|
||||
import com.ecep.contract.ds.vendor.service.CompanyVendorService;
|
||||
import com.ecep.contract.model.CloudYu;
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.model.CompanyVendor;
|
||||
import com.ecep.contract.model.CompanyVendorEntity;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 供应商同步任务
|
||||
*/
|
||||
public class VendorSyncTask extends AbstContractRepairTasker {
|
||||
private static final Logger logger = LoggerFactory.getLogger(VendorSyncTask.class);
|
||||
private final VendorCtx vendorCtx = new VendorCtx();
|
||||
private CompanyVendorService companyVendorService;
|
||||
private CompanyVendorEntityService vendorEntityService;
|
||||
private YongYouU8Repository repository;
|
||||
@Setter
|
||||
private YongYouU8Service yongYouU8Service;
|
||||
|
||||
public VendorSyncTask() {
|
||||
updateTitle("用友U8系统-同步供应商");
|
||||
}
|
||||
|
||||
private CompanyVendorService getCompanyVendorService() {
|
||||
if (companyVendorService == null) {
|
||||
companyVendorService = SpringApp.getBean(CompanyVendorService.class);
|
||||
}
|
||||
return companyVendorService;
|
||||
}
|
||||
|
||||
private CompanyVendorEntityService getVendorEntityService() {
|
||||
if (vendorEntityService == null) {
|
||||
vendorEntityService = SpringApp.getBean(CompanyVendorEntityService.class);
|
||||
}
|
||||
return vendorEntityService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void repair(MessageHolder holder) {
|
||||
try {
|
||||
yongYouU8Service = getBean(YongYouU8Service.class);
|
||||
} catch (BeansException e) {
|
||||
holder.warn("未启用 " + CloudServiceConstant.U8_NAME + " 服务");
|
||||
return;
|
||||
}
|
||||
repository = SpringApp.getBean(YongYouU8Repository.class);
|
||||
yongYouU8Service.initialize(vendorCtx);
|
||||
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
Long total = repository.countAllVendors();
|
||||
updateTitle("用友U8系统-同步供应商,合计 " + total + " 条");
|
||||
|
||||
try (Stream<Map<String, Object>> stream = repository.queryAllVendorForStream()) {
|
||||
stream.forEach(rs -> {
|
||||
if (isCancelled()) {
|
||||
updateMessage("Cancelled");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
String venCode = (String) rs.get("cVenCode");
|
||||
MessageHolder subHolder = holder.sub(counter.get() + " / " + total + "> " + venCode + " ");
|
||||
boolean modified = false;
|
||||
CompanyVendorEntity entity = vendorCtx.findOrCreateByCode(venCode, subHolder);
|
||||
Map<String, Object> map = repository.findVendorByVendCode(entity.getCode());
|
||||
if (map == null) {
|
||||
subHolder.error("供应商项不存在:" + venCode);
|
||||
return;
|
||||
}
|
||||
if (vendorCtx.applyEntityDetail(entity, map, subHolder)) {
|
||||
modified = true;
|
||||
}
|
||||
if (modified) {
|
||||
entity = getVendorEntityService().save(entity);
|
||||
}
|
||||
updateCompanyNameAndAbbName(entity, subHolder);
|
||||
updateCloudYu(entity);
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("Vendor sync failure {}, data = {}", e.getMessage(), rs, e);
|
||||
updateMessage(e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
updateProgress(counter.incrementAndGet(), total);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void updateCloudYu(CompanyVendorEntity entity) {
|
||||
CompanyVendor vendor = entity.getVendor();
|
||||
if (vendor == null) {
|
||||
return;
|
||||
}
|
||||
if (!Hibernate.isInitialized(vendor)) {
|
||||
vendor = getCompanyVendorService().findById(vendor.getId());
|
||||
}
|
||||
Company company = vendor.getCompany();
|
||||
if (company == null) {
|
||||
return;
|
||||
}
|
||||
CloudYu cloudYu = yongYouU8Service.getOrCreateCloudYu(company);
|
||||
if (cloudYu == null) {
|
||||
return;
|
||||
}
|
||||
cloudYu.setVendorUpdateDate(LocalDate.now());
|
||||
cloudYu.setCloudLatest(Instant.now());
|
||||
cloudYu.setExceptionMessage("");
|
||||
yongYouU8Service.save(cloudYu);
|
||||
}
|
||||
|
||||
private void updateCompanyNameAndAbbName(CompanyVendorEntity entity, MessageHolder holder) {
|
||||
CompanyService companyService = vendorCtx.getCompanyService();
|
||||
CompanyVendor vendor = entity.getVendor();
|
||||
if (vendor == null) {
|
||||
return;
|
||||
}
|
||||
if (!Hibernate.isInitialized(vendor)) {
|
||||
vendor = getCompanyVendorService().findById(vendor.getId());
|
||||
}
|
||||
Company company = vendor.getCompany();
|
||||
if (company == null) {
|
||||
return;
|
||||
}
|
||||
if (!Hibernate.isInitialized(company)) {
|
||||
company = companyService.findById(company.getId());
|
||||
}
|
||||
boolean modified = false;
|
||||
CompanyCtx companyCtx = vendorCtx.getCompanyCtx();
|
||||
if (companyCtx.updateCompanyNameIfAbsent(company, entity.getName(), holder)) {
|
||||
modified = true;
|
||||
}
|
||||
if (companyCtx.updateCompanyAbbNameIfAbsent(company, entity.getAbbName(), holder)) {
|
||||
modified = true;
|
||||
}
|
||||
if (modified) {
|
||||
companyService.save(company);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,378 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.ColumnMapRowMapper;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.ecep.contract.ContractPayWay;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Lazy
|
||||
@Repository
|
||||
@ConditionalOnBean(YongYouU8Service.class)
|
||||
public class YongYouU8Repository {
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
@Lazy
|
||||
@Autowired
|
||||
@Qualifier("MSSQL_Server")
|
||||
private DataSource dataSource;
|
||||
|
||||
private JdbcTemplate getJdbcTemplate() {
|
||||
if (jdbcTemplate == null) {
|
||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
return jdbcTemplate;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回 U8 系统中 供应商总数
|
||||
*
|
||||
* @return 供应商总数
|
||||
*/
|
||||
public Long countAllVendors() {
|
||||
return getJdbcTemplate().queryForObject("select count(*) from Vendor", Long.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以流式返回所有供应商数据
|
||||
*
|
||||
* @return Stream 数据流
|
||||
*/
|
||||
public Stream<Map<String, Object>> queryAllVendorForStream() {
|
||||
return getJdbcTemplate().queryForStream(
|
||||
"select cVenCode,cVenName,cVenAbbName," +
|
||||
"cVCCode,cVenAddress,cast(dVenDevDate as DATE) as venDevDate," +
|
||||
"cVenBank, cVenAccount, cCreatePerson,cModifyPerson,dModifyDate " +
|
||||
"from Vendor",
|
||||
new ColumnMapRowMapper()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 以 U8 供应商 的代码查询 供应商
|
||||
*
|
||||
* @param vendCode U8 供应商 的代码,vendCode 唯一
|
||||
* @return 字段和值的Map集合, 没找到匹配的记录时,返回null
|
||||
*/
|
||||
public Map<String, Object> findVendorByVendCode(String vendCode) {
|
||||
try {
|
||||
return getJdbcTemplate().queryForObject("select cVenCode,cVenName,cVenAbbName," +
|
||||
"cVCCode,cVenAddress," +
|
||||
"cast(dVenDevDate as DATE) as devDate,dModifyDate," +
|
||||
"cVenBank,cVenAccount,cCreatePerson, cModifyPerson " +
|
||||
"from Vendor where cVenCode=?", new ColumnMapRowMapper(), vendCode);
|
||||
} catch (EmptyResultDataAccessException e) {
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(vendCode, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 U8 系统中 客户总数
|
||||
*
|
||||
* @return 客户总数
|
||||
*/
|
||||
public Long countAllCustomers() {
|
||||
return getJdbcTemplate().queryForObject("select count(*) from Customer", Long.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以流式返回所有客户数据
|
||||
*
|
||||
* @return Stream 数据流
|
||||
*/
|
||||
public Stream<Map<String, Object>> queryAllCustomerForStream() {
|
||||
return getJdbcTemplate().queryForStream(
|
||||
"select cCusCode,cCusName,cCusAbbName," +
|
||||
"cCCCode,cCusAddress,cast(dCusDevDate as DATE) as cusDevDate," +
|
||||
"cCusBank,cCusAccount, cCreatePerson,cModifyPerson,dModifyDate " +
|
||||
"from Customer",
|
||||
new ColumnMapRowMapper()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以 U8 客户 的代码查询 客户
|
||||
*
|
||||
* @param cusCode U8 客户 的代码,cusCode 唯一
|
||||
* @return 字段和值的Map集合
|
||||
*/
|
||||
public Map<String, Object> findCustomerByCusCode(String cusCode) {
|
||||
return getJdbcTemplate().queryForObject("select cCusCode,cCusName,cCusAbbName," +
|
||||
"cCCCode,cCusAddress," +
|
||||
"cast(dCusDevDate as DATE) as devDate,dModifyDate," +
|
||||
"cCusBank,cCusAccount,cCreatePerson,cModifyPerson " +
|
||||
"from Customer where cCusCode=?", new ColumnMapRowMapper(), cusCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 U8 系统中 合同总数
|
||||
* 在U8中合同在多个地方存在,CM_List, CM_Contract_A,CM_Contract_B,CM_Contract_C
|
||||
*
|
||||
* @return 合同总数
|
||||
*/
|
||||
public Long countAllContracts(int latestId) {
|
||||
return getJdbcTemplate().queryForObject("select count(*) from CM_List where ID>?", Long.class, latestId);
|
||||
}
|
||||
|
||||
public Long countAllContracts(LocalDateTime dateTime) {
|
||||
return getJdbcTemplate().queryForObject("select count(*) from CM_List where dtDate>?", Long.class, dateTime);
|
||||
}
|
||||
|
||||
public Long countAllContracts() {
|
||||
return getJdbcTemplate().queryForObject("select count(*) from CM_List", Long.class);
|
||||
}
|
||||
|
||||
public Long countAllContractB() {
|
||||
return getJdbcTemplate().queryForObject("select count(*) from CM_Contract_B", Long.class);
|
||||
}
|
||||
|
||||
public Long countAllContractB(LocalDate latestDate) {
|
||||
return getJdbcTemplate().queryForObject(
|
||||
"select count(*) " +
|
||||
"from CM_Contract_B " +
|
||||
"where strSetupDate>=? or (dtVaryDate is not null and dtVaryDate>=?)"
|
||||
, Long.class, latestDate, latestDate);
|
||||
}
|
||||
|
||||
public Stream<Map<String, Object>> queryAllContractForStream() {
|
||||
return getJdbcTemplate().queryForStream(
|
||||
"select * from CM_List", new ColumnMapRowMapper()
|
||||
);
|
||||
}
|
||||
|
||||
public Stream<Map<String, Object>> queryAllContractForStream(int latestId) {
|
||||
return getJdbcTemplate().queryForStream(
|
||||
"select * from CM_List where ID > ?",
|
||||
new ColumnMapRowMapper(),
|
||||
latestId
|
||||
);
|
||||
}
|
||||
|
||||
public Stream<Map<String, Object>> queryAllContractForStream(LocalDateTime dateTime) {
|
||||
return getJdbcTemplate().queryForStream(
|
||||
"select * from CM_List where dtDate > ?",
|
||||
new ColumnMapRowMapper(),
|
||||
dateTime
|
||||
);
|
||||
}
|
||||
|
||||
public Stream<Map<String, Object>> queryAllContractBForStream() {
|
||||
return getJdbcTemplate().queryForStream(
|
||||
"select GUID,strContractID,strContractName,strContractType,strParentID,strContractKind,strWay," +
|
||||
"strContractGrp,strContractDesc,strBisectionUnit," +
|
||||
// 时间
|
||||
"strContractOrderDate,strContractStartDate,strContractEndDate," +
|
||||
// 创建人和时间
|
||||
"strSetupPerson,strSetupDate," +
|
||||
// 生效人和时间
|
||||
"strInurePerson,strInureDate," +
|
||||
// 修改人和时间
|
||||
"strVaryPerson,dtVaryDate," +
|
||||
// 业务员
|
||||
"strPersonID, " +
|
||||
"dblTotalCurrency,dblExecCurrency,dblTotalQuantity,dblExecQuqantity " +
|
||||
"from CM_Contract_B ",
|
||||
new ColumnMapRowMapper()
|
||||
);
|
||||
}
|
||||
|
||||
public Stream<Map<String, Object>> queryAllContractBForStream(LocalDate latestDate) {
|
||||
return getJdbcTemplate().queryForStream(
|
||||
"select GUID,strContractID,strContractName,strContractType,strParentID,strContractKind,strWay," +
|
||||
"strContractGrp,strContractDesc,strBisectionUnit," +
|
||||
// 时间
|
||||
"strContractOrderDate,strContractStartDate,strContractEndDate," +
|
||||
// 创建人和时间
|
||||
"strSetupPerson,strSetupDate," +
|
||||
// 生效人和时间
|
||||
"strInurePerson,strInureDate," +
|
||||
// 修改人和时间
|
||||
"strVaryPerson,dtVaryDate," +
|
||||
// 业务员
|
||||
"strPersonID, " +
|
||||
"dblTotalCurrency,dblExecCurrency,dblTotalQuantity,dblExecQuqantity " +
|
||||
"from CM_Contract_B where strSetupDate>=? or (dtVaryDate is not null and dtVaryDate>=?)",
|
||||
new ColumnMapRowMapper(), latestDate, latestDate
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 CM_Contract_B 表中,检索 strBisectionUnit 是 unit 的记录
|
||||
*
|
||||
* @param unit 单位编码
|
||||
* @param payWay 付款方向,付 或 收
|
||||
*/
|
||||
public Stream<Map<String, Object>> queryAllContractByUnitForStream(String unit, ContractPayWay payWay) {
|
||||
return getJdbcTemplate().queryForStream(
|
||||
"select GUID,strContractID,strContractName,strContractType,strParentID,strContractKind,strWay," +
|
||||
"strContractGrp, strContractDesc,strBisectionUnit," +
|
||||
// 时间
|
||||
"strContractOrderDate,strContractStartDate,strContractEndDate," +
|
||||
// 创建人和时间
|
||||
"strSetupPerson,strSetupDate," +
|
||||
// 生效人和时间
|
||||
"strInurePerson,strInureDate," +
|
||||
// 修改人和时间
|
||||
"strVaryPerson,dtVaryDate," +
|
||||
// 业务员
|
||||
"strPersonID, " +
|
||||
"dblTotalCurrency,dblExecCurrency,dblTotalQuantity,dblExecQuqantity " +
|
||||
"from CM_Contract_B where strBisectionUnit=? and strWay=?",
|
||||
new ColumnMapRowMapper(), unit, payWay.getText()
|
||||
);
|
||||
}
|
||||
|
||||
public Map<String, Object> queryContractDetail(String guid) {
|
||||
return getJdbcTemplate().queryForObject("select GUID,strContractID,strContractName,strContractType,strParentID,strContractKind,strWay," +
|
||||
"strContractGrp, strContractDesc,strBisectionUnit," +
|
||||
// 时间
|
||||
"strContractOrderDate,strContractStartDate,strContractEndDate," +
|
||||
// 创建人和时间
|
||||
"strSetupPerson,strSetupDate," +
|
||||
// 生效人和时间
|
||||
"strInurePerson,strInureDate," +
|
||||
// 修改人和时间
|
||||
"strVaryPerson,dtVaryDate," +
|
||||
// 业务员
|
||||
"strPersonID, " +
|
||||
"dblTotalCurrency,dblExecCurrency,dblTotalQuantity,dblExecQuqantity " +
|
||||
" from CM_Contract_B where GUID = ?", new ColumnMapRowMapper(), guid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 合计合同的执行数量、金额、 不含税金额
|
||||
*
|
||||
* @param code 合同号
|
||||
* @return Map<String, Object>
|
||||
*/
|
||||
public Map<String, Object> sumContractExec(String code) {
|
||||
return getJdbcTemplate().queryForObject("select sum(decCount) as execQuantity, sum(decRateMoney) as execAmount, sum(decNoRateMoney) as execUnTaxAmount " +
|
||||
"from CM_ExecInterface " +
|
||||
"where cContractID = ?;", new ColumnMapRowMapper(), code);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> findAllContractExecByContractCode(String code) {
|
||||
return getJdbcTemplate().queryForList("select * from CM_ExecInterface where cContractID=?", code);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> findAllContractItemByContractCode(String code) {
|
||||
return getJdbcTemplate().queryForList("select * from CM_Contract_Item_B where strContractID=?", code);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> findAllContractPayPlanByContractCode(String code) {
|
||||
return getJdbcTemplate().queryForList("select * from CM_Contract_Pay where strContractID=?", code);
|
||||
}
|
||||
|
||||
public Map<String, Object> querySalesOrderDetail(String code) {
|
||||
return getJdbcTemplate().queryForObject("select * " +
|
||||
" from SO_SOMain where cSOCode = ?", new ColumnMapRowMapper(), code);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> findAllSalesOrderItemByContractCode(String code) {
|
||||
return getJdbcTemplate().queryForList("select * from SO_SODetails where cContractID=?", code);
|
||||
}
|
||||
|
||||
public Map<String, Object> queryPurchaseOrderDetail(Integer code) {
|
||||
return getJdbcTemplate().queryForObject("select * " +
|
||||
" from PO_Pomain where POID = ?", new ColumnMapRowMapper(), code);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 采购合同项
|
||||
*
|
||||
* @param code 采购合同号
|
||||
* @return List<Map < String, Object>>
|
||||
*/
|
||||
public List<Map<String, Object>> findAllPurchaseOrderItemByContractCode(String code) {
|
||||
return getJdbcTemplate().queryForList("select * from PO_Podetails where ContractCode=?", code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询存货档案数据
|
||||
*
|
||||
* @param code 存货编码
|
||||
* @return Map<String, Object>
|
||||
*/
|
||||
public Map<String, Object> queryInventoryDetail(String code) {
|
||||
return getJdbcTemplate().queryForObject("select I.*, U.cComUnitName from Inventory as I left join ComputationUnit as U on I.cComUnitCode=U.cComunitCode where cInvCode = ?", new ColumnMapRowMapper(), code);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryAllPerson() {
|
||||
return getJdbcTemplate().queryForList("select * from Person;");
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryAllContractGroup() {
|
||||
return getJdbcTemplate().queryForList("select cGroupID, cGroupName,cRemark from CM_Group;");
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryAllContractType() {
|
||||
return getJdbcTemplate().queryForList(
|
||||
"select cTypeCode, cTypeName, CM_TypeClass.cClassName, cCharacter, cDirection " +
|
||||
"from CM_Type left join CM_TypeClass on CM_Type.cClassCode = CM_TypeClass.cClassCode;");
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryAllContractKind() {
|
||||
return getJdbcTemplate().queryForList("select KindID,KindName,Description from CM_Kind;");
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryAllVendorClass() {
|
||||
return getJdbcTemplate().queryForList("select cVCCode, cVCName, iVCGrade from VendorClass;");
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryAllCustomerClass() {
|
||||
return getJdbcTemplate().queryForList("select cCCCode, cCCName, iCCGrade from CustomerClass;");
|
||||
}
|
||||
|
||||
|
||||
public List<Map<String, Object>> findAllPurchaseBillVoucherByVendorCode(String code) {
|
||||
return getJdbcTemplate().queryForList("select * from PurBillVouch where cVenCode=?", code);
|
||||
}
|
||||
|
||||
public Map<String, Object> findPurchaseBillVoucherById(Integer pbvId) {
|
||||
return getJdbcTemplate().queryForObject("select * from PurBillVouch where PBVID = ?", new ColumnMapRowMapper(), pbvId);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> findAllPurchaseBillVoucherItemByPbvId(int pbvId) {
|
||||
return getJdbcTemplate().queryForList("select * from PurBillVouchs where PBVID=?", pbvId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过合同编号查询采购
|
||||
*
|
||||
* @param contractCode 合同编号
|
||||
*/
|
||||
public List<Map<String, Object>> findAllPurchaseBillVoucherItemByContractCode(String contractCode) {
|
||||
return getJdbcTemplate().queryForList("select * from PurBillVouchs where ContractCode=?", contractCode);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> findAllSalesBillVoucherByCustomerCode(String code) {
|
||||
return getJdbcTemplate().queryForList("select * from SaleBillVouch where cCusCode=?", code);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> findAllSalesBillVoucherBySalesOrderCode(String code) {
|
||||
return getJdbcTemplate().queryForList("select * from SaleBillVouch where cSOCode=?", code);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> findAllSalesBillVoucherItemBySBVID(int sbvid) {
|
||||
return getJdbcTemplate().queryForList("select * from SaleBillVouchs where SBVID=?", sbvid);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
package com.ecep.contract.cloud.u8;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.IEntityService;
|
||||
import com.ecep.contract.cloud.CloudInfo;
|
||||
import com.ecep.contract.cloud.CloudInfoRepository;
|
||||
import com.ecep.contract.cloud.u8.ctx.AbstractYongYouU8Ctx;
|
||||
import com.ecep.contract.ds.company.service.CompanyService;
|
||||
import com.ecep.contract.ds.customer.service.CompanyCustomerService;
|
||||
import com.ecep.contract.ds.other.service.EmployeeService;
|
||||
import com.ecep.contract.ds.vendor.service.CompanyVendorService;
|
||||
import com.ecep.contract.model.CloudYu;
|
||||
import com.ecep.contract.model.Company;
|
||||
|
||||
@Lazy
|
||||
@Service
|
||||
@ConditionalOnProperty(name = "cloud.u8.enabled", havingValue = "true")
|
||||
public class YongYouU8Service implements IEntityService<CloudYu> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(YongYouU8Service.class);
|
||||
|
||||
public static final String KEY_HOST_IP = "u8.db.server.ip";
|
||||
public static final String KEY_DATABASE = "u8.db.database";
|
||||
public static final String KEY_USER_NAME = "u8.db.server.name";
|
||||
public static final String KEY_PASSWORD = "u8.db.server.password";
|
||||
|
||||
@Lazy
|
||||
@Autowired
|
||||
private YongYouU8Repository repository;
|
||||
@Lazy
|
||||
@Autowired
|
||||
private CloudInfoRepository cloudInfoRepository;
|
||||
@Lazy
|
||||
@Autowired
|
||||
private CompanyService companyService;
|
||||
@Lazy
|
||||
@Autowired
|
||||
private CompanyVendorService companyVendorService;
|
||||
@Lazy
|
||||
@Autowired
|
||||
private CloudYuRepository cloudYuRepository;
|
||||
@Lazy
|
||||
@Autowired
|
||||
private EmployeeService employeeService;
|
||||
@Lazy
|
||||
@Autowired
|
||||
private CompanyCustomerService companyCustomerService;
|
||||
|
||||
public YongYouU8Service() {
|
||||
|
||||
}
|
||||
|
||||
public CloudYu findById(Integer id) {
|
||||
return cloudYuRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建
|
||||
*
|
||||
* @param info 供应商信息
|
||||
* @return 供应商对象
|
||||
*/
|
||||
public CloudYu getOrCreateCloudYu(CloudInfo info) {
|
||||
Optional<CloudYu> optional = cloudYuRepository.findById(info.getId());
|
||||
return optional.orElseGet(() -> getOrCreateCloudYu(info.getCompany()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建或获取供应商云信息
|
||||
*
|
||||
* @param company 公司
|
||||
* @return 供应商云信息
|
||||
*/
|
||||
public CloudYu getOrCreateCloudYu(Company company) {
|
||||
Integer companyId = company.getId();
|
||||
List<CloudYu> list = cloudYuRepository.findAllByCompanyId(companyId);
|
||||
if (list.isEmpty()) {
|
||||
CloudYu cloudYu = new CloudYu();
|
||||
cloudYu.setCompany(company);
|
||||
cloudYu.setExceptionMessage("");
|
||||
cloudYu.setVendorUpdateDate(null);
|
||||
cloudYu.setCustomerUpdateDate(null);
|
||||
cloudYu.setCloudLatest(null);
|
||||
return cloudYuRepository.save(cloudYu);
|
||||
}
|
||||
|
||||
// 只有匹配到一条有 CloudId 的记录
|
||||
if (list.size() == 1) {
|
||||
// 保留匹配的记录,其他删除
|
||||
CloudYu rk = list.removeFirst();
|
||||
list.remove(rk);
|
||||
cloudYuRepository.deleteAll(list);
|
||||
return rk;
|
||||
}
|
||||
|
||||
return list.getFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存 Cloud Yu
|
||||
*
|
||||
* @param cloudYu Cloud Yu 对象
|
||||
* @return 更新的 Cloud Yu
|
||||
*/
|
||||
public CloudYu save(CloudYu cloudYu) {
|
||||
return cloudYuRepository.save(cloudYu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(CloudYu entity) {
|
||||
cloudYuRepository.delete(entity);
|
||||
}
|
||||
|
||||
public void deleteByCompany(Company company) {
|
||||
int deleted = cloudYuRepository.deleteAllByCompany(company);
|
||||
if (deleted > 0) {
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Delete {} records by company:#{}", deleted, company.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void resetTo(Company from, Company to) {
|
||||
List<CloudYu> list = cloudYuRepository.findAllByCompanyId(from.getId());
|
||||
for (CloudYu item : list) {
|
||||
item.setCompany(to);
|
||||
}
|
||||
cloudYuRepository.saveAll(list);
|
||||
}
|
||||
|
||||
public Page<CloudYu> findAll(Specification<CloudYu> spec, Pageable pageable) {
|
||||
return cloudYuRepository.findAll(spec, pageable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Specification<CloudYu> getSpecification(String searchText) {
|
||||
if (!StringUtils.hasText(searchText)) {
|
||||
return null;
|
||||
}
|
||||
return (root, query, builder) -> {
|
||||
return builder.like(root.get("cloudId"), "%" + searchText + "%");
|
||||
};
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryAllContractKind() {
|
||||
return repository.queryAllContractKind();
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryAllVendorClass() {
|
||||
return repository.queryAllVendorClass();
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryAllCustomerClass() {
|
||||
return repository.queryAllCustomerClass();
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryAllContractType() {
|
||||
return repository.queryAllContractType();
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryAllContractGroup() {
|
||||
return repository.queryAllContractGroup();
|
||||
}
|
||||
|
||||
public Long countAllCustomers() {
|
||||
return repository.countAllCustomers();
|
||||
}
|
||||
|
||||
public Stream<Map<String, Object>> queryAllCustomerForStream() {
|
||||
return repository.queryAllCustomerForStream();
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryAllPerson() {
|
||||
return repository.queryAllPerson();
|
||||
}
|
||||
|
||||
public Map<String, Object> findVendorByVendCode(String unit) {
|
||||
return repository.findVendorByVendCode(unit);
|
||||
}
|
||||
|
||||
public Map<String, Object> findCustomerByCusCode(String unit) {
|
||||
return repository.findCustomerByCusCode(unit);
|
||||
}
|
||||
|
||||
public void initialize(AbstractYongYouU8Ctx ctx) {
|
||||
ctx.setRepository(repository);
|
||||
ctx.setCompanyService(companyService);
|
||||
ctx.setEmployeeService(employeeService);
|
||||
ctx.setCompanyVendorService(companyVendorService);
|
||||
ctx.setCompanyCustomerService(companyCustomerService);
|
||||
}
|
||||
|
||||
public Iterable<CloudInfo> findAllCloudYu() {
|
||||
return cloudInfoRepository.findAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
package com.ecep.contract.cloud.u8.ctx;
|
||||
|
||||
import static com.ecep.contract.SpringApp.getBean;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.cloud.AbstractCtx;
|
||||
import com.ecep.contract.cloud.u8.YongYouU8Repository;
|
||||
import com.ecep.contract.constant.CloudServiceConstant;
|
||||
import com.ecep.contract.ds.company.service.CompanyService;
|
||||
import com.ecep.contract.ds.customer.service.CompanyCustomerEntityService;
|
||||
import com.ecep.contract.ds.customer.service.CompanyCustomerService;
|
||||
import com.ecep.contract.ds.other.service.EmployeeService;
|
||||
import com.ecep.contract.ds.vendor.service.CompanyVendorEntityService;
|
||||
import com.ecep.contract.ds.vendor.service.CompanyVendorService;
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.model.CompanyCustomer;
|
||||
import com.ecep.contract.model.CompanyCustomerEntity;
|
||||
import com.ecep.contract.model.CompanyVendor;
|
||||
import com.ecep.contract.model.CompanyVendorEntity;
|
||||
import com.ecep.contract.model.Employee;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public class AbstractYongYouU8Ctx extends AbstractCtx {
|
||||
@Getter
|
||||
@Setter
|
||||
YongYouU8Repository repository;
|
||||
@Setter
|
||||
CompanyService companyService;
|
||||
@Setter
|
||||
EmployeeService employeeService;
|
||||
@Setter
|
||||
CompanyCustomerService companyCustomerService;
|
||||
@Setter
|
||||
CompanyCustomerEntityService companyCustomerEntityService;
|
||||
@Setter
|
||||
CompanyVendorService companyVendorService;
|
||||
@Setter
|
||||
CompanyVendorEntityService companyVendorEntityService;
|
||||
|
||||
|
||||
public void from(AbstractYongYouU8Ctx parent) {
|
||||
repository = parent.repository;
|
||||
companyService = parent.companyService;
|
||||
employeeService = parent.employeeService;
|
||||
companyCustomerService = parent.companyCustomerService;
|
||||
companyVendorService = parent.companyVendorService;
|
||||
}
|
||||
|
||||
public void initializeRepository(MessageHolder holder) {
|
||||
if (repository != null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
repository = getBean(YongYouU8Repository.class);
|
||||
} catch (BeansException e) {
|
||||
holder.warn("未启用 " + CloudServiceConstant.U8_NAME + " 服务");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public CompanyService getCompanyService() {
|
||||
if (companyService == null) {
|
||||
companyService = getBean(CompanyService.class);
|
||||
}
|
||||
return companyService;
|
||||
}
|
||||
|
||||
EmployeeService getEmployeeService() {
|
||||
if (employeeService == null) {
|
||||
employeeService = getBean(EmployeeService.class);
|
||||
}
|
||||
return employeeService;
|
||||
}
|
||||
|
||||
public CompanyCustomerService getCompanyCustomerService() {
|
||||
if (companyCustomerService == null) {
|
||||
companyCustomerService = getBean(CompanyCustomerService.class);
|
||||
}
|
||||
return companyCustomerService;
|
||||
}
|
||||
|
||||
public CompanyCustomerEntityService getCompanyCustomerEntityService() {
|
||||
if (companyCustomerEntityService == null) {
|
||||
companyCustomerEntityService = getBean(CompanyCustomerEntityService.class);
|
||||
}
|
||||
return companyCustomerEntityService;
|
||||
}
|
||||
|
||||
public CompanyVendorService getCompanyVendorService() {
|
||||
if (companyVendorService == null) {
|
||||
companyVendorService = getBean(CompanyVendorService.class);
|
||||
}
|
||||
return companyVendorService;
|
||||
}
|
||||
|
||||
public CompanyVendorEntityService getCompanyVendorEntityService() {
|
||||
if (companyVendorEntityService == null) {
|
||||
companyVendorEntityService = getBean(CompanyVendorEntityService.class);
|
||||
}
|
||||
return companyVendorEntityService;
|
||||
}
|
||||
|
||||
boolean updateEmployeeByCode(Supplier<Employee> getter, Consumer<Employee> setter, String code, MessageHolder holder, String topic) {
|
||||
if (StringUtils.hasText(code)) {
|
||||
Employee employee = getEmployeeService().findByCode(code);
|
||||
if (employee != null) {
|
||||
if (!Objects.equals(getter.get(), employee)) {
|
||||
setter.accept(employee);
|
||||
holder.info(topic + "更新为 " + employee.getName());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
boolean updateEmployeeByName(Supplier<Employee> getter, Consumer<Employee> setter, String name, MessageHolder holder, String topic) {
|
||||
if (StringUtils.hasText(name)) {
|
||||
Employee employee = getEmployeeService().findByName(name);
|
||||
if (employee != null) {
|
||||
if (!Objects.equals(getter.get(), employee)) {
|
||||
setter.accept(employee);
|
||||
holder.info(topic + "更新为 " + employee.getName());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean updateCompanyByCustomerCode(Supplier<Company> getter, Consumer<Company> setter, String customerCode, MessageHolder holder, String topic) {
|
||||
Company company = null;
|
||||
if (StringUtils.hasText(customerCode)) {
|
||||
CompanyCustomerEntityService customerEntityService = getCompanyCustomerEntityService();
|
||||
CompanyCustomerService customerService = getCompanyCustomerService();
|
||||
CompanyCustomerEntity entity = customerEntityService.findByCustomerCode(customerCode);
|
||||
if (entity == null) {
|
||||
holder.warn("无效" + topic + ":" + customerCode);
|
||||
} else {
|
||||
CompanyCustomer customer = entity.getCustomer();
|
||||
if (customer == null) {
|
||||
holder.warn("无效" + topic + ":" + customerCode);
|
||||
} else {
|
||||
if (!Hibernate.isInitialized(customer)) {
|
||||
customer = customerService.findById(customer.getId());
|
||||
}
|
||||
company = customer.getCompany();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (company == null) {
|
||||
setter.accept(null);
|
||||
holder.warn("无效" + topic + ":" + customerCode);
|
||||
return true;
|
||||
} else {
|
||||
if (!Objects.equals(getter.get(), company)) {
|
||||
setter.accept(company);
|
||||
if (!Hibernate.isInitialized(company)) {
|
||||
company = getCompanyService().findById(company.getId());
|
||||
}
|
||||
holder.info(topic + "修改为: " + company.getName());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
boolean updateCompanyByVendorCode(Supplier<Company> getter, Consumer<Company> setter, String vendorCode, MessageHolder holder, String topic) {
|
||||
Company company = null;
|
||||
if (StringUtils.hasText(vendorCode)) {
|
||||
CompanyVendorEntityService vendorEntityService = getCompanyVendorEntityService();
|
||||
CompanyVendorService customerService = getCompanyVendorService();
|
||||
CompanyVendorEntity entity = vendorEntityService.findByCode(vendorCode);
|
||||
if (entity == null) {
|
||||
holder.warn("无效" + topic + ":" + vendorCode);
|
||||
} else {
|
||||
CompanyVendor customer = entity.getVendor();
|
||||
if (customer == null) {
|
||||
holder.warn("无效" + topic + ":" + vendorCode);
|
||||
} else {
|
||||
if (!Hibernate.isInitialized(customer)) {
|
||||
customer = customerService.findById(customer.getId());
|
||||
}
|
||||
company = customer.getCompany();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (company == null) {
|
||||
setter.accept(null);
|
||||
holder.warn("无效" + topic + ":" + vendorCode);
|
||||
return true;
|
||||
} else {
|
||||
if (!Objects.equals(getter.get(), company)) {
|
||||
setter.accept(company);
|
||||
if (!Hibernate.isInitialized(company)) {
|
||||
company = getCompanyService().findById(company.getId());
|
||||
}
|
||||
holder.info(topic + "修改为: " + company.getName());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.ecep.contract.cloud.u8.ctx;
|
||||
|
||||
import static com.ecep.contract.SpringApp.getBean;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.ds.company.service.CompanyBankAccountService;
|
||||
import com.ecep.contract.model.Company;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
public class CompanyBankAccountCtx extends AbstractYongYouU8Ctx {
|
||||
@Setter
|
||||
private CompanyBankAccountService companyBankAccountService;
|
||||
|
||||
CompanyBankAccountService getCompanyBankAccountService() {
|
||||
if (companyBankAccountService == null) {
|
||||
companyBankAccountService = getBean(CompanyBankAccountService.class);
|
||||
}
|
||||
return companyBankAccountService;
|
||||
}
|
||||
|
||||
public void updateBankAccount(Company company, String bank, String bankAccount, MessageHolder holder) {
|
||||
getCompanyBankAccountService().updateBankAccount(company, bank, bankAccount, holder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package com.ecep.contract.cloud.u8.ctx;
|
||||
|
||||
import static com.ecep.contract.SpringApp.getBean;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.constant.CloudServiceConstant;
|
||||
import com.ecep.contract.ds.company.service.CompanyOldNameService;
|
||||
import com.ecep.contract.ds.company.service.CompanyService;
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.model.CompanyOldName;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
public class CompanyCtx extends AbstractYongYouU8Ctx {
|
||||
/**
|
||||
* 自动创建公司的时间
|
||||
*/
|
||||
public static final String AUTO_CREATE_COMPANY_AFTER = "cloud.u8.auto-create-company-after";
|
||||
|
||||
@Setter
|
||||
private CompanyOldNameService companyOldNameService;
|
||||
|
||||
CompanyOldNameService getCompanyOldNameService() {
|
||||
if (companyOldNameService == null) {
|
||||
companyOldNameService = getBean(CompanyOldNameService.class);
|
||||
}
|
||||
return companyOldNameService;
|
||||
}
|
||||
|
||||
public boolean updateCompanyNameIfAbsent(Company company, String name, MessageHolder holder) {
|
||||
if (!StringUtils.hasText(name)) {
|
||||
return false;
|
||||
}
|
||||
if (StringUtils.hasText(company.getName())) {
|
||||
if (Objects.equals(company.getName(), name)) {
|
||||
return false;
|
||||
}
|
||||
appendOldNameIfAbsent(company, name, false, holder);
|
||||
return false;
|
||||
} else {
|
||||
return updateText(company::getName, company::setName, name, holder, "公司名称");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean updateCompanyAbbNameIfAbsent(Company company, String abbName, MessageHolder holder) {
|
||||
if (!StringUtils.hasText(abbName)) {
|
||||
return false;
|
||||
}
|
||||
// 简称与公司名相同,跳过
|
||||
if (Objects.equals(company.getName(), abbName)) {
|
||||
return false;
|
||||
}
|
||||
if (StringUtils.hasText(company.getShortName())) {
|
||||
if (Objects.equals(company.getShortName(), abbName)) {
|
||||
return false;
|
||||
}
|
||||
appendOldNameIfAbsent(company, abbName, true, holder);
|
||||
return false;
|
||||
} else {
|
||||
return updateText(company::getShortName, company::setShortName, abbName, holder, "公司简称");
|
||||
}
|
||||
}
|
||||
|
||||
private void appendOldNameIfAbsent(Company company, String name, boolean ambiguity, MessageHolder holder) {
|
||||
List<CompanyOldName> oldNames = getCompanyOldNameService().findAllByCompanyAndName(company, name);
|
||||
if (oldNames.isEmpty()) {
|
||||
CompanyOldName companyOldName = getCompanyOldNameService().createNew(company, name, ambiguity);
|
||||
companyOldName.setMemo("来自" + CloudServiceConstant.U8_NAME);
|
||||
getCompanyOldNameService().save(companyOldName);
|
||||
holder.info("新增曾用名:" + name);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean syncCompany(Company company, MessageHolder holder) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Company findOrCreateByNameOrAbbName(String name, String abbName, LocalDate developDate, MessageHolder holder) {
|
||||
Company company = getCompanyService().findAndRemoveDuplicateCompanyByNameOrAbbName(name, abbName);
|
||||
if (company != null) {
|
||||
return company;
|
||||
}
|
||||
|
||||
// 判断是否是个人
|
||||
holder.warn("企业库中找不到" + name + " 或 " + abbName + " 的企业");
|
||||
// 推测个人用户,归入散户
|
||||
if (name.length() < 4 && !name.contains("公司")) {
|
||||
company = getCompanyService().findByName("散户");
|
||||
if (company == null) {
|
||||
return null;
|
||||
}
|
||||
holder.info(name + " 个人用户归集到散户");
|
||||
return company;
|
||||
}
|
||||
|
||||
// 尝试创建公司
|
||||
company = autoCreateNewCompanyAfter(developDate, name, abbName, holder);
|
||||
if (company != null) {
|
||||
holder.info("自动创建公司:" + company.getName());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Company autoCreateNewCompanyAfter(LocalDate devDate, String name, String abbName, MessageHolder holder) {
|
||||
if (devDate == null) {
|
||||
holder.warn("开发时间未知,不创建公司");
|
||||
return null;
|
||||
}
|
||||
CompanyService companyService = getCompanyService();
|
||||
String autoCreateAfter = getConfService().getString(AUTO_CREATE_COMPANY_AFTER);
|
||||
// 当配置存在,且开发时间小于指定时间,不创建
|
||||
if (StringUtils.hasText(autoCreateAfter)) {
|
||||
LocalDate miniDate = LocalDate.parse(autoCreateAfter);
|
||||
if (devDate.isBefore(miniDate)) {
|
||||
// 创建时间小于指定时间,不创建
|
||||
holder.warn(name + " 的开发时间 " + devDate + " 是 " + miniDate + " 之前的, 按规定不创建, 如有需要请手动创建");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Company com = companyService.createNewCompany(name);
|
||||
com.setShortName(abbName);
|
||||
return companyService.save(com);
|
||||
}
|
||||
}
|
||||
1375
server/src/main/java/com/ecep/contract/cloud/u8/ctx/ContractCtx.java
Normal file
1375
server/src/main/java/com/ecep/contract/cloud/u8/ctx/ContractCtx.java
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,303 @@
|
||||
package com.ecep.contract.cloud.u8.ctx;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.cloud.old.OldVersionService;
|
||||
import com.ecep.contract.ds.customer.service.CompanyCustomerEntityService;
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.model.CompanyCustomer;
|
||||
import com.ecep.contract.model.CompanyCustomerEntity;
|
||||
import com.ecep.contract.model.CustomerCatalog;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
public class CustomerCtx extends AbstractYongYouU8Ctx {
|
||||
private static final String AUTO_CREATE_CUSTOMER_AFTER = "cloud.u8.auto-create-customer-after";
|
||||
@Setter
|
||||
private CompanyCtx companyCtx;
|
||||
@Setter
|
||||
private ContractCtx contractCtx;
|
||||
@Setter
|
||||
private CompanyBankAccountCtx companyBankAccountCtx;
|
||||
@Setter
|
||||
private CompanyCustomerEntityService customerEntityService;
|
||||
|
||||
public CompanyCtx getCompanyCtx() {
|
||||
if (companyCtx == null) {
|
||||
companyCtx = new CompanyCtx();
|
||||
companyCtx.from(this);
|
||||
}
|
||||
return companyCtx;
|
||||
}
|
||||
|
||||
ContractCtx getContractCtx() {
|
||||
if (contractCtx == null) {
|
||||
contractCtx = new ContractCtx();
|
||||
contractCtx.from(this);
|
||||
}
|
||||
return contractCtx;
|
||||
}
|
||||
|
||||
CompanyBankAccountCtx getCompanyBankAccountCtx() {
|
||||
if (companyBankAccountCtx == null) {
|
||||
companyBankAccountCtx = new CompanyBankAccountCtx();
|
||||
companyBankAccountCtx.from(this);
|
||||
}
|
||||
return companyBankAccountCtx;
|
||||
}
|
||||
|
||||
CompanyCustomerEntityService getCustomerEntityService() {
|
||||
if (customerEntityService == null) {
|
||||
customerEntityService = SpringApp.getBean(CompanyCustomerEntityService.class);
|
||||
}
|
||||
return customerEntityService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新客户相关项
|
||||
*
|
||||
* @param item
|
||||
* @param unitCode
|
||||
* @param holder
|
||||
*/
|
||||
public CompanyCustomerEntity updateCustomerEntityDetailByCode(CompanyCustomerEntity item, String unitCode, MessageHolder holder) {
|
||||
if (applyEntityDetail(item, repository.findCustomerByCusCode(unitCode), holder)) {
|
||||
item = save(item);
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
public boolean applyEntityDetail(CompanyCustomerEntity item, Map<String, Object> map, MessageHolder holder) {
|
||||
|
||||
String name = (String) map.get("cCusName");
|
||||
String abbName = (String) map.get("cCusAbbName");
|
||||
String cusCode = (String) map.get("cCusCode");
|
||||
String classCode = (String) map.get("cCCCode");
|
||||
|
||||
String createPerson = (String) map.get("cCreatePerson");
|
||||
String modifyPerson = (String) map.get("cModifyPerson");
|
||||
java.sql.Date devDate = (java.sql.Date) map.get("devDate");
|
||||
java.sql.Timestamp modifyDate = (java.sql.Timestamp) map.get("dModifyDate");
|
||||
|
||||
String bank = (String) map.get("cCusBank");
|
||||
String bankAccount = (String) map.get("cCusAccount");
|
||||
|
||||
|
||||
boolean modified = false;
|
||||
if (updateText(item::getName, item::setName, name, holder, "名称")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(item::getAbbName, item::setAbbName, abbName, holder, "简称")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(item::getCode, item::setCode, cusCode, holder, "客户编号")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (updateCustomerCatalog(item::getCatalog, item::setCatalog, classCode, holder, "分类")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(item::getCreator, item::setCreator, createPerson, holder, "创建人")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(item::getModifier, item::setModifier, modifyPerson, holder, "修改人")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDate(item::getDevelopDate, item::setDevelopDate, devDate, holder, "发展日期")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDate(item::getModifyDate, item::setModifyDate, modifyDate, holder, "修改日期")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
LocalDate today = LocalDate.now();
|
||||
if (item.getUpdatedDate() == null || item.getUpdatedDate().isBefore(today)) {
|
||||
item.setUpdatedDate(today);
|
||||
holder.info("更新日期更新为 " + today);
|
||||
}
|
||||
}
|
||||
|
||||
CompanyCustomer customer = item.getCustomer();
|
||||
if (customer == null) {
|
||||
// 如果没有关联客户,则根据供应商名称或别名查找公司
|
||||
Company company = findOrCreateCompanyByCustomerEntity(item, holder);
|
||||
if (company != null) {
|
||||
// 找到客户
|
||||
|
||||
customer = getCompanyCustomerService().findByCompany(company);
|
||||
if (customer == null) {
|
||||
customer = createCustomerByCustomerEntity(item, holder);
|
||||
if (customer != null) {
|
||||
customer.setCompany(company);
|
||||
customer = getCompanyCustomerService().save(customer);
|
||||
}
|
||||
}
|
||||
|
||||
if (customer != null) {
|
||||
item.setCustomer(customer);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (customer != null) {
|
||||
if (!Hibernate.isInitialized(customer)) {
|
||||
customer = getCompanyCustomerService().findById(customer.getId());
|
||||
}
|
||||
Company company = customer.getCompany();
|
||||
if (company != null) {
|
||||
getCompanyBankAccountCtx().updateBankAccount(company, bank, bankAccount, holder);
|
||||
}
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
private boolean updateCustomerCatalog(Supplier<CustomerCatalog> getter, Consumer<CustomerCatalog> setter, String catalogCode, MessageHolder holder, String topic) {
|
||||
CustomerCatalog catalog = null;
|
||||
if (StringUtils.hasText(catalogCode)) {
|
||||
catalog = getCompanyCustomerService().findCatalogByCode(catalogCode);
|
||||
}
|
||||
if (catalog == null) {
|
||||
setter.accept(null);
|
||||
holder.warn("无效" + topic + ":" + catalogCode);
|
||||
return true;
|
||||
} else {
|
||||
if (!Objects.equals(getter.get(), catalog)) {
|
||||
if (!Hibernate.isInitialized(catalog)) {
|
||||
catalog = getCompanyCustomerService().findCatalogByCode(catalogCode);
|
||||
}
|
||||
setter.accept(catalog);
|
||||
holder.info(topic + "修改为: " + catalog.getName());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean syncCustomer(Company company, MessageHolder holder) {
|
||||
CompanyCustomer companyCustomer = getCompanyCustomerService().findByCompany(company);
|
||||
if (companyCustomer == null) {
|
||||
holder.error("客户未创建, 如需要请手动创建.");
|
||||
return false;
|
||||
}
|
||||
// 检索相关项
|
||||
List<CompanyCustomerEntity> entities = getCustomerEntityService().findAllByCustomer(companyCustomer);
|
||||
if (entities.isEmpty()) {
|
||||
holder.error("客户未关联任何相关项");
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean updated = false;
|
||||
boolean companyModified = false;
|
||||
|
||||
// 更新相关项
|
||||
for (CompanyCustomerEntity entity : entities) {
|
||||
if (!StringUtils.hasText(entity.getCode())) {
|
||||
holder.warn("相关项:" + entity.getCode() + " 无效,跳过");
|
||||
continue;
|
||||
}
|
||||
if (applyEntityDetail(entity, repository.findCustomerByCusCode(entity.getCode()), holder)) {
|
||||
entity = getCustomerEntityService().save(entity);
|
||||
}
|
||||
|
||||
if (getCompanyCtx().updateCompanyNameIfAbsent(company, entity.getName(), holder)) {
|
||||
companyModified = true;
|
||||
}
|
||||
if (getCompanyCtx().updateCompanyAbbNameIfAbsent(company, entity.getAbbName(), holder)) {
|
||||
companyModified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (companyModified) {
|
||||
company = getCompanyService().save(company);
|
||||
updated = true;
|
||||
}
|
||||
|
||||
// 更新客户的开发日期
|
||||
if (updateCustomerDevelopDate(companyCustomer, entities, holder)) {
|
||||
companyCustomer = getCompanyCustomerService().save(companyCustomer);
|
||||
updated = true;
|
||||
}
|
||||
|
||||
for (CompanyCustomerEntity entity : entities) {
|
||||
if (getContractCtx().syncByCustomerEntity(companyCustomer, entity, holder)) {
|
||||
updated = true;
|
||||
}
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
private boolean updateCustomerDevelopDate(CompanyCustomer companyCustomer, List<CompanyCustomerEntity> entities, MessageHolder holder) {
|
||||
LocalDate developDate = null;
|
||||
for (CompanyCustomerEntity entity : entities) {
|
||||
// 取最早的开发日期
|
||||
if (developDate == null || entity.getDevelopDate().isBefore(developDate)) {
|
||||
developDate = entity.getDevelopDate();
|
||||
}
|
||||
}
|
||||
return updateLocalDate(companyCustomer::getDevelopDate, companyCustomer::setDevelopDate, developDate, holder, "开发日期");
|
||||
}
|
||||
|
||||
public CompanyCustomerEntity findOrCreateByCode(Map<String, Object> rs, String cusCode, MessageHolder subHolder) {
|
||||
CompanyCustomerEntityService customerEntityService = getCompanyCustomerEntityService();
|
||||
CompanyCustomerEntity entity = customerEntityService.findByCustomerCode(cusCode);
|
||||
if (entity == null) {
|
||||
entity = new CompanyCustomerEntity();
|
||||
entity.setCode(cusCode);
|
||||
entity = customerEntityService.save(entity);
|
||||
subHolder.info("创建客户相关项:" + cusCode);
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
private CompanyCustomer createCustomerByCustomerEntity(CompanyCustomerEntity entity, MessageHolder holder) {
|
||||
LocalDate developDate = entity.getDevelopDate();
|
||||
if (developDate == null) {
|
||||
holder.warn(entity.getName() + " 没有设置开发日期,跳过");
|
||||
return null;
|
||||
}
|
||||
// 创建发展日期从2023年1月1日后的
|
||||
LocalDate start = LocalDate.of(2023, 1, 1);
|
||||
String autoCreateAfter = getConfService().getString(AUTO_CREATE_CUSTOMER_AFTER);
|
||||
if (StringUtils.hasText(autoCreateAfter)) {
|
||||
start = LocalDate.parse(autoCreateAfter);
|
||||
}
|
||||
if (developDate.isBefore(start)) {
|
||||
// start 之前的不自动创建
|
||||
holder.warn(entity.getName() + " 的发展日期 " + developDate + " 是 " + start + " 之前的, 按规定不自动创建客户, 如有需要请手动创建");
|
||||
return null;
|
||||
}
|
||||
|
||||
CompanyCustomer companyCustomer = new CompanyCustomer();
|
||||
int nextId = SpringApp.getBean(OldVersionService.class).newCompanyCustomer(entity.getName());
|
||||
companyCustomer.setId(nextId);
|
||||
companyCustomer.setDevelopDate(developDate);
|
||||
holder.info("新客户:" + entity.getName() + "分配编号:" + nextId);
|
||||
companyCustomer.setCreated(Instant.now());
|
||||
return companyCustomer;
|
||||
}
|
||||
|
||||
private Company findOrCreateCompanyByCustomerEntity(CompanyCustomerEntity entity, MessageHolder holder) {
|
||||
String name = entity.getName();
|
||||
String abbName = entity.getAbbName();
|
||||
return getCompanyCtx().findOrCreateByNameOrAbbName(name, abbName, entity.getDevelopDate(), holder);
|
||||
}
|
||||
|
||||
public CompanyCustomerEntity save(CompanyCustomerEntity entity) {
|
||||
return getCustomerEntityService().save(entity);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
package com.ecep.contract.cloud.u8.ctx;
|
||||
|
||||
import static com.ecep.contract.SpringApp.getBean;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.constant.CloudServiceConstant;
|
||||
import com.ecep.contract.ds.other.service.InventoryCatalogService;
|
||||
import com.ecep.contract.ds.other.service.InventoryService;
|
||||
import com.ecep.contract.model.Inventory;
|
||||
import com.ecep.contract.model.InventoryCatalog;
|
||||
import com.ecep.contract.model.Price;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
public class InventoryCtx extends AbstractYongYouU8Ctx {
|
||||
@Setter
|
||||
private InventoryService inventoryService;
|
||||
@Setter
|
||||
private InventoryCatalogService inventoryCatalogService;
|
||||
|
||||
public InventoryService getInventoryService() {
|
||||
if (inventoryService == null) {
|
||||
inventoryService = getBean(InventoryService.class);
|
||||
}
|
||||
return inventoryService;
|
||||
}
|
||||
|
||||
public InventoryCatalogService getInventoryCatalogService() {
|
||||
if (inventoryCatalogService == null) {
|
||||
inventoryCatalogService = getBean(InventoryCatalogService.class);
|
||||
}
|
||||
return inventoryCatalogService;
|
||||
}
|
||||
|
||||
private boolean applyInventoryDetail(Inventory item, Map<String, Object> map, MessageHolder holder) {
|
||||
String catalogCode = (String) map.get("cInvCCode");
|
||||
String name = (String) map.get("cInvName");
|
||||
String spec = (String) map.get("cInvStd");
|
||||
String unitCode = (String) map.get("cComUnitCode");
|
||||
String unitName = (String) map.get("cComUnitName");
|
||||
String createPerson = (String) map.get("cCreatePerson");
|
||||
String modifyPerson = (String) map.get("cModifyPerson");
|
||||
Timestamp createDate = (Timestamp) map.get("dSDate");
|
||||
Timestamp modifyDate = (Timestamp) map.get("dModifyDate");
|
||||
|
||||
boolean modified = false;
|
||||
|
||||
Double outTaxRate = (Double) map.get("iTaxRate");
|
||||
Double inTaxRate = (Double) map.get("iImpTaxRate");
|
||||
// 参考成本
|
||||
Double baseCostPrice = (Double) map.get("iInvSPrice");
|
||||
// 最新成本
|
||||
Double latestCostPrice = (Double) map.get("iInvNCost");
|
||||
// 计划价/售价
|
||||
Double plannedPrice = (Double) map.get("iInvRCost");
|
||||
// 最低售价
|
||||
Double lowestSalePrice = (Double) map.get("iInvLSCost");
|
||||
// 参考售价
|
||||
Double suggestedSalePrice = (Double) map.get("iInvSCost");
|
||||
|
||||
|
||||
if (!item.isNameLock()) {
|
||||
if (updateText(item::getName, item::setName, name, holder, "名称")) {
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!item.isSpecificationLock()) {
|
||||
if (updateText(item::getSpecification, item::setSpecification, spec, holder, "规格")) {
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (updateText(item::getUnit, item::setUnit, unitName, holder, "单位")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (updatePrice(item.getPurchasePrice(), inTaxRate, latestCostPrice, holder, "采购")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updatePrice(item.getSalePrice(), outTaxRate, suggestedSalePrice, holder, "销售")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (updateInventoryCatalog(item::getCatalog, item::setCatalog, catalogCode, holder, "类别")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(item::getCreator, item::setCreator, createPerson, holder, "创建人")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDate(item::getCreateTime, item::setCreateTime, createDate, holder, "创建时间")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(item::getUpdater, item::setUpdater, modifyPerson, holder, "修改人")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDateTime(item::getUpdateDate, item::setUpdateDate, modifyDate, holder, "修改时间")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
return modified;
|
||||
}
|
||||
|
||||
private boolean updatePrice(Price price, Double taxRate, Double preTaxPrice, MessageHolder holder, String title) {
|
||||
boolean modified = false;
|
||||
if (taxRate != null) {
|
||||
if (updateNumber(price::getTaxRate, price::setTaxRate, taxRate.floatValue(), holder, title + "税率")) {
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (preTaxPrice != null) {
|
||||
if (updateNumber(price::getPreTaxPrice, price::setPreTaxPrice, preTaxPrice, holder, title + "未税单价")) {
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
|
||||
public boolean syncInventoryDetailByCode(Inventory inventory, String inventoryCode, MessageHolder holder) {
|
||||
if (repository == null) {
|
||||
return false;
|
||||
}
|
||||
Map<String, Object> map = null;
|
||||
try {
|
||||
map = repository.queryInventoryDetail(inventoryCode);
|
||||
} catch (NoSuchBeanDefinitionException e) {
|
||||
holder.warn("未启用 " + CloudServiceConstant.U8_NAME + " 服务");
|
||||
return false;
|
||||
} catch (EmptyResultDataAccessException e) {
|
||||
holder.error("无此存货:" + inventoryCode);
|
||||
return false;
|
||||
}
|
||||
if (applyInventoryDetail(inventory, map, holder)) {
|
||||
inventory.setUpdateDate(LocalDateTime.now());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 依据存货分类编号更新存货分类
|
||||
*
|
||||
* @param getter 获取的函数
|
||||
* @param setter 设置的函数
|
||||
* @param catalogCode 存货分类编号
|
||||
* @param holder 消息处理对象
|
||||
* @param topic 主题
|
||||
* @return 是否更新
|
||||
*/
|
||||
private boolean updateInventoryCatalog(Supplier<InventoryCatalog> getter, Consumer<InventoryCatalog> setter, String catalogCode, MessageHolder holder, String topic) {
|
||||
InventoryCatalog catalog = null;
|
||||
if (StringUtils.hasText(catalogCode)) {
|
||||
catalog = getInventoryCatalogService().findByCode(catalogCode);
|
||||
}
|
||||
if (catalog == null) {
|
||||
setter.accept(null);
|
||||
holder.warn("无效" + topic + ":" + catalogCode);
|
||||
return true;
|
||||
} else {
|
||||
if (!Objects.equals(getter.get(), catalog)) {
|
||||
if (!Hibernate.isInitialized(catalog)) {
|
||||
catalog = getInventoryCatalogService().findByCode(catalogCode);
|
||||
}
|
||||
setter.accept(catalog);
|
||||
holder.info(topic + "修改为: " + catalog.getName());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 依据存货编号更新存货
|
||||
*
|
||||
* @param getter 获取的函数
|
||||
* @param setter 设置的函数
|
||||
* @param inventoryCode 存货编号
|
||||
* @param holder 消息持有者
|
||||
* @param topic 主题
|
||||
* @return 是否更新
|
||||
*/
|
||||
public boolean syncInventoryDetailByCode(Supplier<Inventory> getter, Consumer<Inventory> setter, String inventoryCode, MessageHolder holder, String topic) {
|
||||
if (!StringUtils.hasText(inventoryCode)) {
|
||||
return false;
|
||||
}
|
||||
Inventory inventory = null;
|
||||
InventoryService service = getInventoryService();
|
||||
if (StringUtils.hasText(inventoryCode)) {
|
||||
inventory = service.findByCode(inventoryCode);
|
||||
if (inventory == null) {
|
||||
inventory = service.createNewInstance();
|
||||
inventory.setCode(inventoryCode);
|
||||
if (syncInventoryDetailByCode(inventory, inventoryCode, holder)) {
|
||||
inventory = getInventoryService().save(inventory);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (inventory == null) {
|
||||
setter.accept(null);
|
||||
holder.warn("无效" + topic + ":" + inventoryCode);
|
||||
return true;
|
||||
} else {
|
||||
if (!Objects.equals(getter.get(), inventory)) {
|
||||
if (!Hibernate.isInitialized(inventory)) {
|
||||
inventory = service.findByCode(inventoryCode);
|
||||
}
|
||||
setter.accept(inventory);
|
||||
holder.info(topic + "修改为: " + inventory.getName());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.ecep.contract.cloud.u8.ctx;
|
||||
|
||||
import static com.ecep.contract.SpringApp.getBean;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.ds.company.service.InvoiceService;
|
||||
import com.ecep.contract.model.Invoice;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
public class InvoiceCtx extends AbstractYongYouU8Ctx {
|
||||
@Setter
|
||||
private InvoiceService invoiceService;
|
||||
|
||||
InvoiceService getInvoiceService() {
|
||||
if (invoiceService == null) {
|
||||
invoiceService = getBean(InvoiceService.class);
|
||||
}
|
||||
return invoiceService;
|
||||
}
|
||||
|
||||
public boolean updateInvoiceByNumber(Supplier<Invoice> getter, Consumer<Invoice> setter, String invoiceNumber,
|
||||
MessageHolder holder, String topic) {
|
||||
// TODO 从U8系统中获取数据
|
||||
Invoice invoice = null;
|
||||
if (StringUtils.hasText(invoiceNumber)) {
|
||||
invoice = getInvoiceService().findByCode(invoiceNumber);
|
||||
if (invoice == null) {
|
||||
invoice = new Invoice();
|
||||
invoice.setCode(invoiceNumber);
|
||||
invoice = getInvoiceService().save(invoice);
|
||||
}
|
||||
}
|
||||
if (invoice == null) {
|
||||
setter.accept(null);
|
||||
holder.warn("无效" + topic + ":" + invoiceNumber);
|
||||
return true;
|
||||
} else {
|
||||
if (!Objects.equals(getter.get(), invoice)) {
|
||||
setter.accept(invoice);
|
||||
holder.info(topic + "修改为: " + invoice.getCode());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,430 @@
|
||||
package com.ecep.contract.cloud.u8.ctx;
|
||||
|
||||
import static com.ecep.contract.SpringApp.getBean;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.constant.CloudServiceConstant;
|
||||
import com.ecep.contract.ds.contract.service.PurchaseBillVoucherItemService;
|
||||
import com.ecep.contract.ds.contract.service.PurchaseBillVoucherService;
|
||||
import com.ecep.contract.ds.contract.service.PurchaseOrderItemService;
|
||||
import com.ecep.contract.ds.contract.service.PurchaseOrdersService;
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.model.CompanyVendor;
|
||||
import com.ecep.contract.model.CompanyVendorEntity;
|
||||
import com.ecep.contract.model.Contract;
|
||||
import com.ecep.contract.model.Inventory;
|
||||
import com.ecep.contract.model.Invoice;
|
||||
import com.ecep.contract.model.PurchaseBillVoucher;
|
||||
import com.ecep.contract.model.PurchaseBillVoucherItem;
|
||||
import com.ecep.contract.model.PurchaseOrder;
|
||||
import com.ecep.contract.model.PurchaseOrderItem;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
public class PurchaseBillVoucherCtx extends AbstractYongYouU8Ctx {
|
||||
@Setter
|
||||
private PurchaseOrdersService purchaseOrdersService;
|
||||
@Setter
|
||||
private PurchaseOrderItemService purchaseOrderItemService;
|
||||
@Setter
|
||||
private PurchaseBillVoucherService purchaseBillVoucherService;
|
||||
@Setter
|
||||
private PurchaseBillVoucherItemService purchaseBillVoucherItemService;
|
||||
|
||||
InventoryCtx inventoryCtx;
|
||||
InvoiceCtx invoiceCtx;
|
||||
ContractCtx contractCtx;
|
||||
|
||||
PurchaseOrdersService getPurchaseOrdersService() {
|
||||
if (purchaseOrdersService == null) {
|
||||
purchaseOrdersService = getBean(PurchaseOrdersService.class);
|
||||
}
|
||||
return purchaseOrdersService;
|
||||
}
|
||||
|
||||
PurchaseBillVoucherService getPurchaseBillVoucherService() {
|
||||
if (purchaseBillVoucherService == null) {
|
||||
purchaseBillVoucherService = getBean(PurchaseBillVoucherService.class);
|
||||
}
|
||||
return purchaseBillVoucherService;
|
||||
}
|
||||
|
||||
private PurchaseOrderItemService getPurchaseOrderItemService() {
|
||||
if (purchaseOrderItemService == null) {
|
||||
purchaseOrderItemService = getBean(PurchaseOrderItemService.class);
|
||||
}
|
||||
return purchaseOrderItemService;
|
||||
}
|
||||
|
||||
PurchaseBillVoucherItemService getPurchaseBillVoucherItemService() {
|
||||
if (purchaseBillVoucherItemService == null) {
|
||||
purchaseBillVoucherItemService = getBean(PurchaseBillVoucherItemService.class);
|
||||
}
|
||||
return purchaseBillVoucherItemService;
|
||||
}
|
||||
|
||||
InventoryCtx getInventoryCtx() {
|
||||
if (inventoryCtx == null) {
|
||||
inventoryCtx = new InventoryCtx();
|
||||
inventoryCtx.from(this);
|
||||
}
|
||||
return inventoryCtx;
|
||||
}
|
||||
|
||||
InvoiceCtx getInvoiceCtx() {
|
||||
if (invoiceCtx == null) {
|
||||
invoiceCtx = new InvoiceCtx();
|
||||
invoiceCtx.from(this);
|
||||
}
|
||||
return invoiceCtx;
|
||||
}
|
||||
|
||||
ContractCtx getContractCtx() {
|
||||
if (contractCtx == null) {
|
||||
contractCtx = new ContractCtx();
|
||||
contractCtx.from(this);
|
||||
}
|
||||
return contractCtx;
|
||||
}
|
||||
|
||||
public void syncByCompany(Company company, MessageHolder holder) {
|
||||
PurchaseBillVoucherService voucherService = getPurchaseBillVoucherService();
|
||||
PurchaseBillVoucherItemService voucherItemService = getPurchaseBillVoucherItemService();
|
||||
List<PurchaseBillVoucher> vouchers = voucherService.findAll((root, q, cb) -> {
|
||||
return cb.equal(root.get("company"), company);
|
||||
}, Sort.unsorted());
|
||||
holder.debug("查找到 " + vouchers.size() + " 条专用发票记录在数据库中");
|
||||
Map<Integer, PurchaseBillVoucher> voucherMap = vouchers.stream()
|
||||
.collect(Collectors.toMap(PurchaseBillVoucher::getRefId, item -> item));
|
||||
|
||||
CompanyVendor vendor = getCompanyVendorService().findByCompany(company);
|
||||
if (vendor != null) {
|
||||
List<CompanyVendorEntity> entities = getCompanyVendorEntityService().findAllByVendor(vendor);
|
||||
holder.debug(company.getName() + " 有 "
|
||||
+ entities.stream().map(CompanyVendorEntity::getCode).collect(Collectors.joining(", ")) + " 关联项");
|
||||
for (CompanyVendorEntity entity : entities) {
|
||||
// 查询 U8 数据库
|
||||
List<Map<String, Object>> ds = repository.findAllPurchaseBillVoucherByVendorCode(entity.getCode());
|
||||
holder.debug(
|
||||
"供应商关联项: " + entity.getCode() + " 查找到 " + ds.size() + " 条专用发票记录在 " + CloudServiceConstant.U8_NAME);
|
||||
|
||||
for (Map<String, Object> map : ds) {
|
||||
Integer pbvid = (Integer) map.get("PBVID");
|
||||
if (pbvid == 0) {
|
||||
holder.warn("跳过无效专用发票记录:缺少 PBVID");
|
||||
continue;
|
||||
}
|
||||
holder.debug("发票 #" + pbvid);
|
||||
PurchaseBillVoucher voucher = voucherMap.get(pbvid);
|
||||
boolean voucherModified = false;
|
||||
if (voucher == null) {
|
||||
voucher = new PurchaseBillVoucher();
|
||||
voucher.setCompany(company);
|
||||
voucher.setRefId(pbvid);
|
||||
voucherMap.put(pbvid, voucher);
|
||||
voucherModified = true;
|
||||
|
||||
holder.info("新增发票记录");
|
||||
}
|
||||
|
||||
if (applyPurchaseBillVoucherDetail(voucher, map, holder.sub("-- | "))) {
|
||||
voucherModified = true;
|
||||
}
|
||||
|
||||
if (voucherModified) {
|
||||
voucher = voucherService.save(voucher);
|
||||
voucherMap.put(pbvid, voucher);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<PurchaseBillVoucherItem> items = voucherItemService.findAll((root, q, cb) -> {
|
||||
return cb.equal(root.get("voucher").get("company"), company);
|
||||
}, Sort.unsorted());
|
||||
|
||||
// 按 order 分组
|
||||
Map<PurchaseBillVoucher, Map<Integer, PurchaseBillVoucherItem>> itemMap = items.stream()
|
||||
.collect(Collectors.groupingBy(PurchaseBillVoucherItem::getVoucher,
|
||||
Collectors.toMap(PurchaseBillVoucherItem::getRefId, item -> item)));
|
||||
for (PurchaseBillVoucher voucher : voucherMap.values()) {
|
||||
|
||||
// 查询 U8 数据库
|
||||
List<Map<String, Object>> ds = repository.findAllPurchaseBillVoucherItemByPbvId(voucher.getRefId());
|
||||
holder.debug("专用发票#" + voucher.getRefId() + "查找到 " + ds.size() + "条条目记录在 " + CloudServiceConstant.U8_NAME);
|
||||
Map<Integer, PurchaseBillVoucherItem> subItemMap = itemMap.computeIfAbsent(voucher, k -> new HashMap<>());
|
||||
for (Map<String, Object> map : ds) {
|
||||
Integer refId = (Integer) map.get("ID");
|
||||
if (refId == 0) {
|
||||
holder.warn("跳过无效专用发票记录:缺少 ID");
|
||||
continue;
|
||||
}
|
||||
PurchaseBillVoucherItem item = subItemMap.remove(refId);
|
||||
boolean itemModified = false;
|
||||
if (item == null) {
|
||||
item = new PurchaseBillVoucherItem();
|
||||
item.setVoucher(voucher);
|
||||
item.setRefId(refId);
|
||||
itemModified = true;
|
||||
|
||||
holder.info("新增专用发票条目 #" + refId);
|
||||
}
|
||||
MessageHolder subHolder = holder.sub("-- | ");
|
||||
if (applyPurchaseBillVoucherItemDetail(item, map, subHolder)) {
|
||||
itemModified = true;
|
||||
}
|
||||
if (itemModified) {
|
||||
item = voucherItemService.save(item);
|
||||
}
|
||||
}
|
||||
for (PurchaseBillVoucherItem item : subItemMap.values()) {
|
||||
holder.info("删除专用发票条目");
|
||||
voucherItemService.delete(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void syncByContract(Contract contract, MessageHolder holder) {
|
||||
PurchaseBillVoucherService voucherService = getPurchaseBillVoucherService();
|
||||
PurchaseBillVoucherItemService voucherItemService = getPurchaseBillVoucherItemService();
|
||||
List<PurchaseBillVoucherItem> items = voucherItemService.findAll((root, q, cb) -> {
|
||||
return cb.equal(root.get("contract"), contract);
|
||||
}, Sort.unsorted());
|
||||
|
||||
Map<Integer, PurchaseBillVoucherItem> itemMap = items.stream()
|
||||
.collect(Collectors.toMap(PurchaseBillVoucherItem::getRefId, item -> item));
|
||||
// 查询 U8 数据库
|
||||
List<Map<String, Object>> ds = repository.findAllPurchaseBillVoucherItemByContractCode(contract.getCode());
|
||||
holder.debug("合同编号#" + contract.getCode() + "查找到 " + ds.size() + "条发票条目记录在 " + CloudServiceConstant.U8_NAME);
|
||||
|
||||
for (Map<String, Object> map : ds) {
|
||||
Integer refId = (Integer) map.get("ID");
|
||||
if (refId == 0) {
|
||||
holder.warn("跳过无效专用发票记录:缺少 ID");
|
||||
continue;
|
||||
}
|
||||
PurchaseBillVoucherItem item = itemMap.remove(refId);
|
||||
boolean itemModified = false;
|
||||
if (item == null) {
|
||||
item = new PurchaseBillVoucherItem();
|
||||
PurchaseBillVoucher voucher = this.updateVoucherByPBVID((Integer) map.get("PBVID"), holder);
|
||||
item.setVoucher(voucher);
|
||||
item.setRefId(refId);
|
||||
itemModified = true;
|
||||
|
||||
holder.info("新增专用发票条目 #" + refId);
|
||||
}
|
||||
MessageHolder subHolder = holder.sub("-- | ");
|
||||
if (applyPurchaseBillVoucherItemDetail(item, map, subHolder)) {
|
||||
itemModified = true;
|
||||
}
|
||||
if (itemModified) {
|
||||
item = voucherItemService.save(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private PurchaseBillVoucher updateVoucherByPBVID(Integer pbvId, MessageHolder holder) {
|
||||
PurchaseBillVoucherService voucherService = getPurchaseBillVoucherService();
|
||||
PurchaseBillVoucher voucher = voucherService.findByRefId(pbvId);
|
||||
if (voucher != null) {
|
||||
return voucher;
|
||||
}
|
||||
voucher = new PurchaseBillVoucher();
|
||||
// 查询 U8 数据库
|
||||
Map<String, Object> map = repository.findPurchaseBillVoucherById(pbvId);
|
||||
applyPurchaseBillVoucherDetail(voucher, map, holder.sub("-- | "));
|
||||
voucher = voucherService.save(voucher);
|
||||
return voucher;
|
||||
}
|
||||
|
||||
private boolean applyPurchaseBillVoucherDetail(PurchaseBillVoucher voucher, Map<String, Object> map,
|
||||
MessageHolder holder) {
|
||||
String code = String.valueOf(map.get("PBVID"));
|
||||
String vendorCode = (String) map.get("cVenCode");
|
||||
String invoiceNumber = (String) map.get("cPBVCode");
|
||||
String personCode = (String) map.get("cPersonCode");
|
||||
String inCode = (String) map.get("cInCode");
|
||||
|
||||
Timestamp invoiceDate = (Timestamp) map.get("dPBVDate");
|
||||
Timestamp voucherDate = (Timestamp) map.get("dVouDate");
|
||||
|
||||
BigDecimal amount = (BigDecimal) map.get("iSum");
|
||||
|
||||
String maker = (String) map.get("cPBVMaker");
|
||||
String verifier = (String) map.get("cPBVVerifier");
|
||||
Timestamp makeTime = (Timestamp) map.get("cmaketime");
|
||||
Timestamp auditTime = (Timestamp) map.get("dverifysystime");
|
||||
|
||||
String description = (String) map.get("cPBVMemo");
|
||||
|
||||
boolean modified = false;
|
||||
|
||||
holder.debug("发票号码:" + invoiceNumber);
|
||||
|
||||
if (updateCompanyByVendorCode(voucher::getCompany, voucher::setCompany, vendorCode, holder, "供应商")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateInvoice(voucher::getInvoice, voucher::setInvoice, invoiceNumber, holder, "发票")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (voucher.getInvoice() != null) {
|
||||
Invoice invoice = voucher.getInvoice();
|
||||
if (updateLocalDate(invoice::getInvoiceDate, invoice::setInvoiceDate, voucherDate, holder, "开票日期")) {
|
||||
invoice = getInvoiceCtx().getInvoiceService().save(invoice);
|
||||
voucher.setInvoice(invoice);
|
||||
}
|
||||
}
|
||||
|
||||
if (updateEmployeeByCode(voucher::getEmployee, voucher::setEmployee, personCode, holder, "业务员")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(voucher::getMaker, voucher::setMaker, maker, holder, "制单人")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(voucher::getVerifier, voucher::setVerifier, verifier, holder, "审核人")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDateTime(voucher::getMakerDate, voucher::setMakerDate, makeTime, holder, "制单时间")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDateTime(voucher::getVerifierDate, voucher::setVerifierDate, auditTime, holder, "审核时间")) {
|
||||
modified = true;
|
||||
}
|
||||
if (!Objects.equals(voucher.getDescription(), description)) {
|
||||
voucher.setDescription(description);
|
||||
holder.info("描述修改为: " + description);
|
||||
modified = true;
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
private boolean applyPurchaseBillVoucherItemDetail(PurchaseBillVoucherItem item, Map<String, Object> map,
|
||||
MessageHolder holder) {
|
||||
String code = String.valueOf(map.get("ID"));
|
||||
String inventoryCode = (String) map.get("cInvCode");
|
||||
String contractCode = (String) map.get("ContractCode");
|
||||
|
||||
String title = (String) map.get("cInvCode");
|
||||
double quantity = (double) map.get("iPBVQuantity");
|
||||
BigDecimal taxRate = (BigDecimal) map.get("iOriTaxPrice");
|
||||
BigDecimal taxPrice = (BigDecimal) map.get("iTaxPrice");
|
||||
BigDecimal amount = (BigDecimal) map.get("iSum");
|
||||
|
||||
// RdsId
|
||||
|
||||
// 采购订单条目refId
|
||||
Integer purchaseOrderItemId = (Integer) map.get("iPOsID");
|
||||
|
||||
String description = (String) map.get("cdescription");
|
||||
Timestamp signDate = (Timestamp) map.get("dSignDate");
|
||||
|
||||
boolean modified = false;
|
||||
|
||||
holder.debug("条目:" + title + " x " + amount);
|
||||
if (updateInventory(item::getInventory, item::setInventory, inventoryCode, holder, "商品")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updatePurchaseOrderItem(item::getOrderItem, item::setOrderItem, purchaseOrderItemId, holder, "采购订单条目")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateContractByContractCode(item::getContract, item::setContract, contractCode, holder, "合同")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (updateNumber(item::getPrice, item::setPrice, taxPrice, holder, "含税单价")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateNumber(item::getQuantity, item::setQuantity, quantity, holder, "数量")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateAppendText(item::getDescription, item::setDescription, description, holder, "描述")) {
|
||||
modified = true;
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
boolean updatePurchaseOrderItem(Supplier<PurchaseOrderItem> getter, Consumer<PurchaseOrderItem> setter,
|
||||
Integer orderItemRefId, MessageHolder holder, String topic) {
|
||||
PurchaseOrderItem item = null;
|
||||
if (orderItemRefId != null) {
|
||||
item = getPurchaseOrderItemService().findByRefId(orderItemRefId);
|
||||
}
|
||||
if (item == null) {
|
||||
setter.accept(null);
|
||||
holder.warn("无效" + topic + ":" + orderItemRefId);
|
||||
return true;
|
||||
} else {
|
||||
if (!Objects.equals(getter.get(), item)) {
|
||||
setter.accept(item);
|
||||
holder.info(topic + "修改为: " + item.getRefId());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean updatePurchaseOrder(Supplier<PurchaseOrder> getter, Consumer<PurchaseOrder> setter, Integer orderRefId,
|
||||
MessageHolder holder, String topic) {
|
||||
PurchaseOrder order = null;
|
||||
if (orderRefId != null) {
|
||||
order = getPurchaseOrdersService().findByRefId(orderRefId);
|
||||
}
|
||||
if (order == null) {
|
||||
setter.accept(null);
|
||||
holder.warn("无效" + topic + ":" + orderRefId);
|
||||
return true;
|
||||
} else {
|
||||
if (!Objects.equals(getter.get(), order)) {
|
||||
setter.accept(order);
|
||||
holder.info(topic + "修改为: " + order.getCode());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean updateContractByContractCode(Supplier<Contract> getter, Consumer<Contract> setter, String contractCode,
|
||||
MessageHolder holder, String topic) {
|
||||
Contract contract = null;
|
||||
if (contractCode != null) {
|
||||
contract = getContractCtx().findContractByCode(contractCode);
|
||||
}
|
||||
if (contract == null) {
|
||||
setter.accept(null);
|
||||
holder.warn("无效" + topic + ":" + contractCode);
|
||||
return true;
|
||||
} else {
|
||||
if (!Objects.equals(getter.get(), contract)) {
|
||||
setter.accept(contract);
|
||||
holder.info(topic + "修改为: " + contract.getCode());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean updateInventory(Supplier<Inventory> getter, Consumer<Inventory> setter, String inventoryCode,
|
||||
MessageHolder holder, String topic) {
|
||||
return getInventoryCtx().syncInventoryDetailByCode(getter, setter, inventoryCode, holder, topic);
|
||||
}
|
||||
|
||||
private boolean updateInvoice(Supplier<Invoice> getter, Consumer<Invoice> setter, String invoiceNumber,
|
||||
MessageHolder holder, String topic) {
|
||||
return getInvoiceCtx().updateInvoiceByNumber(getter, setter, invoiceNumber, holder, topic);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,332 @@
|
||||
package com.ecep.contract.cloud.u8.ctx;
|
||||
|
||||
import static com.ecep.contract.SpringApp.getBean;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.constant.CloudServiceConstant;
|
||||
import com.ecep.contract.ds.contract.service.ContractService;
|
||||
import com.ecep.contract.ds.contract.service.PurchaseOrderItemService;
|
||||
import com.ecep.contract.ds.contract.service.PurchaseOrdersService;
|
||||
import com.ecep.contract.model.Contract;
|
||||
import com.ecep.contract.model.Inventory;
|
||||
import com.ecep.contract.model.PurchaseOrder;
|
||||
import com.ecep.contract.model.PurchaseOrderItem;
|
||||
import com.ecep.contract.util.NumberUtils;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
public class PurchaseOrderCtx extends AbstractYongYouU8Ctx {
|
||||
@Setter
|
||||
private ContractService contractService;
|
||||
@Setter
|
||||
private PurchaseOrdersService purchaseOrdersService;
|
||||
@Setter
|
||||
private PurchaseOrderItemService purchaseOrderItemService;
|
||||
|
||||
InventoryCtx inventoryCtx;
|
||||
CompanyBankAccountCtx companyBankAccountCtx;
|
||||
|
||||
InventoryCtx getInventoryCtx() {
|
||||
if (inventoryCtx == null) {
|
||||
inventoryCtx = new InventoryCtx();
|
||||
inventoryCtx.from(this);
|
||||
}
|
||||
return inventoryCtx;
|
||||
}
|
||||
|
||||
CompanyBankAccountCtx getCompanyBankAccountCtx() {
|
||||
if (companyBankAccountCtx == null) {
|
||||
companyBankAccountCtx = new CompanyBankAccountCtx();
|
||||
companyBankAccountCtx.from(this);
|
||||
}
|
||||
return companyBankAccountCtx;
|
||||
}
|
||||
|
||||
ContractService getContractService() {
|
||||
if (contractService == null) {
|
||||
contractService = getBean(ContractService.class);
|
||||
}
|
||||
return contractService;
|
||||
}
|
||||
|
||||
PurchaseOrdersService getPurchaseOrdersService() {
|
||||
if (purchaseOrdersService == null) {
|
||||
purchaseOrdersService = getBean(PurchaseOrdersService.class);
|
||||
}
|
||||
return purchaseOrdersService;
|
||||
}
|
||||
|
||||
PurchaseOrderItemService getPurchaseOrderItemService() {
|
||||
if (purchaseOrderItemService == null) {
|
||||
purchaseOrderItemService = getBean(PurchaseOrderItemService.class);
|
||||
}
|
||||
return purchaseOrderItemService;
|
||||
}
|
||||
|
||||
public List<PurchaseOrder> syncByContract(Contract contract, MessageHolder holder) {
|
||||
PurchaseOrdersService ordersService = getPurchaseOrdersService();
|
||||
PurchaseOrderItemService itemService = getPurchaseOrderItemService();
|
||||
|
||||
List<PurchaseOrder> orders = ordersService.findAll((root, q, cb) -> {
|
||||
return cb.equal(root.get("contract"), contract);
|
||||
}, Sort.unsorted());
|
||||
holder.debug("查找到 " + orders.size() + " 条采购订单记录在数据库中");
|
||||
Map<Integer, PurchaseOrder> ordersMap = orders.stream()
|
||||
.collect(Collectors.toMap(PurchaseOrder::getRefId, item -> item));
|
||||
|
||||
List<PurchaseOrderItem> items = itemService.findAll((root, q, cb) -> {
|
||||
return cb.equal(root.get("order").get("contract"), contract);
|
||||
}, Sort.unsorted());
|
||||
|
||||
// 按 order 分组
|
||||
Map<PurchaseOrder, Map<Integer, PurchaseOrderItem>> itemMap = items.stream()
|
||||
.collect(Collectors.groupingBy(PurchaseOrderItem::getOrder,
|
||||
Collectors.toMap(PurchaseOrderItem::getRefId, item -> item)));
|
||||
|
||||
// 查询 U8 数据库
|
||||
List<Map<String, Object>> ds = repository.findAllPurchaseOrderItemByContractCode(contract.getCode());
|
||||
holder.debug("查找到 " + ds.size() + " 条采购订单条目记录在 " + CloudServiceConstant.U8_NAME);
|
||||
|
||||
Map<PurchaseOrder, List<PurchaseOrderItem>> updateMap = new HashMap<>();
|
||||
for (Map<String, Object> map : ds) {
|
||||
Integer poId = (Integer) map.get("POID");
|
||||
if (poId == 0) {
|
||||
holder.warn("跳过无效采购订单记录:缺少 POID");
|
||||
continue;
|
||||
}
|
||||
PurchaseOrder order = ordersMap.get(poId);
|
||||
if (order == null) {
|
||||
order = new PurchaseOrder();
|
||||
order.setContract(contract);
|
||||
order.setRefId(poId);
|
||||
|
||||
order = ordersService.save(order);
|
||||
ordersMap.put(poId, order);
|
||||
holder.info("新增采购订单 #" + poId);
|
||||
}
|
||||
}
|
||||
for (Map<String, Object> map : ds) {
|
||||
// holder.debug("条目:" + map.toString());
|
||||
Integer poId = (Integer) map.get("POID");
|
||||
if (poId == 0) {
|
||||
holder.warn("跳过无效采购订单记录:缺少 POID");
|
||||
continue;
|
||||
}
|
||||
PurchaseOrder order = ordersMap.get(poId);
|
||||
|
||||
boolean itemModified = false;
|
||||
List<PurchaseOrderItem> updates = updateMap.computeIfAbsent(order, k -> new ArrayList<>());
|
||||
|
||||
// 获取条目标识并处理 null
|
||||
Integer refId = (Integer) map.get("ID");
|
||||
if (poId == null) {
|
||||
holder.warn("跳过条目:订单 " + poId + " 缺少 ID");
|
||||
continue;
|
||||
}
|
||||
Map<Integer, PurchaseOrderItem> subItemMap = itemMap.get(order);
|
||||
PurchaseOrderItem item = null;
|
||||
if (subItemMap != null) {
|
||||
item = subItemMap.remove(refId);
|
||||
}
|
||||
|
||||
if (item == null) {
|
||||
item = new PurchaseOrderItem();
|
||||
item.setOrder(order);
|
||||
item.setRefId(refId);
|
||||
itemModified = true;
|
||||
|
||||
holder.info("新增采购订单条目 #" + refId);
|
||||
}
|
||||
MessageHolder subHolder = holder.sub("---| ");
|
||||
if (applyPurchaseOrderItemDetail(item, map, subHolder)) {
|
||||
itemModified = true;
|
||||
}
|
||||
if (itemModified) {
|
||||
item = itemService.save(item);
|
||||
}
|
||||
updates.add(item);
|
||||
}
|
||||
|
||||
for (PurchaseOrder order : updateMap.keySet()) {
|
||||
if (applyPurchaseOrderDetail(order, repository.queryPurchaseOrderDetail(order.getRefId()), holder)) {
|
||||
ordersService.save(order);
|
||||
}
|
||||
}
|
||||
|
||||
if (!itemMap.isEmpty()) {
|
||||
for (Map<Integer, PurchaseOrderItem> subMap : itemMap.values()) {
|
||||
if (!subMap.isEmpty()) {
|
||||
for (PurchaseOrderItem item : subMap.values()) {
|
||||
holder.info("删除采购订单条目");
|
||||
itemService.delete(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (PurchaseOrder order : updateMap.keySet()) {
|
||||
itemMap.remove(order);
|
||||
}
|
||||
if (!itemMap.isEmpty()) {
|
||||
holder.info("剩余 " + itemMap.size() + " 个采购订单条目");
|
||||
}
|
||||
}
|
||||
|
||||
return new ArrayList<>(updateMap.keySet());
|
||||
}
|
||||
|
||||
private boolean applyPurchaseOrderDetail(PurchaseOrder order, Map<String, Object> map, MessageHolder holder) {
|
||||
String code = (String) map.get("cPOID");
|
||||
|
||||
String venBank = (String) map.get("cVenBank");
|
||||
String venBankAccount = (String) map.get("cVenAccount");
|
||||
|
||||
String venCode = (String) map.get("cVenCode");
|
||||
String memo = (String) map.get("cMemo");
|
||||
|
||||
boolean modified = false;
|
||||
|
||||
if (!Objects.equals(order.getCode(), code)) {
|
||||
order.setCode(code);
|
||||
holder.info("订单编号更新为 " + code);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (updateEmployeeByCode(order::getEmployee, order::setEmployee, (String) map.get("cPersonCode"), holder,
|
||||
"业务员")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(order::getMaker, order::setMaker, (String) map.get("cMaker"), holder, "制单人")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(order::getVerifier, order::setVerifier, (String) map.get("cVerifier"), holder,
|
||||
"审核人")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(order::getCloser, order::setCloser, (String) map.get("cCloser"), holder, "订单关闭人")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDateTime(order::getMakerDate, order::setMakerDate, (Timestamp) map.get("cmaketime"), holder,
|
||||
"制单日期")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDateTime(order::getModifyDate, order::setModifyDate, (Timestamp) map.get("cModifyTime"), holder,
|
||||
"修改日期")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDateTime(order::getVerifierDate, order::setVerifierDate, (Timestamp) map.get("cAuditTime"),
|
||||
holder, "审核日期")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDateTime(order::getCloserDate, order::setCloserDate, (Timestamp) map.get("dCloseTime"), holder,
|
||||
"关闭日期")) {
|
||||
modified = true;
|
||||
}
|
||||
if (!Objects.equals(order.getVendorCode(), venCode)) {
|
||||
order.setVendorCode(venCode);
|
||||
holder.info("供方代码修改为: " + venCode);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!Objects.equals(order.getDescription(), memo)) {
|
||||
order.setDescription(memo);
|
||||
holder.info("描述修改为: " + memo);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
updateCompanyBankAccount(order.getContract(), venBank, venBankAccount, holder);
|
||||
|
||||
return modified;
|
||||
}
|
||||
|
||||
private void updateCompanyBankAccount(Contract contract, String bank, String bankAccount, MessageHolder holder) {
|
||||
if (contract == null) {
|
||||
return;
|
||||
}
|
||||
if (!Hibernate.isInitialized(contract)) {
|
||||
contract = getContractService().findById(contract.getId());
|
||||
}
|
||||
getCompanyBankAccountCtx().updateBankAccount(contract.getCompany(), bank, bankAccount, holder);
|
||||
}
|
||||
|
||||
private boolean applyPurchaseOrderItemDetail(PurchaseOrderItem item, Map<String, Object> map,
|
||||
MessageHolder holder) {
|
||||
Integer refId = (Integer) map.get("ID");
|
||||
String inventoryCode = (String) map.get("cInvCode");
|
||||
String contractCode = (String) map.get("ContractCode");
|
||||
|
||||
String title = (String) map.get("cInvCode");
|
||||
double quantity = (double) map.get("iQuantity");
|
||||
BigDecimal taxRate = (BigDecimal) map.get("iPerTaxRate");
|
||||
BigDecimal taxPrice = (BigDecimal) map.get("iTaxPrice");
|
||||
double exclusiveTaxPrice = (double) map.get("iUnitPrice");
|
||||
BigDecimal amount = (BigDecimal) map.get("iSum");
|
||||
|
||||
Timestamp arriveDate = (Timestamp) map.get("dArriveDate");
|
||||
|
||||
boolean modified = false;
|
||||
|
||||
holder.debug("条目:" + title + " x " + amount);
|
||||
if (updateInventory(item::getInventory, item::setInventory, inventoryCode, holder, "商品")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!Objects.equals(item.getRefId(), refId)) {
|
||||
item.setRefId(refId);
|
||||
holder.info("RefId修改为: " + refId);
|
||||
modified = true;
|
||||
}
|
||||
if (!NumberUtils.equals(item.getExclusiveTaxPrice(), exclusiveTaxPrice)) {
|
||||
item.setExclusiveTaxPrice(exclusiveTaxPrice);
|
||||
holder.info("不含税单价修改为: " + exclusiveTaxPrice);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!NumberUtils.equals(item.getPrice(), taxPrice.doubleValue())) {
|
||||
item.setPrice(taxPrice.doubleValue());
|
||||
holder.info("含税单价修改为: " + taxPrice.doubleValue());
|
||||
modified = true;
|
||||
}
|
||||
if (!NumberUtils.equals(item.getTaxRate(), taxRate.doubleValue())) {
|
||||
item.setTaxRate(taxRate.doubleValue());
|
||||
holder.info("税率修改为: " + taxRate);
|
||||
modified = true;
|
||||
}
|
||||
if (!NumberUtils.equals(item.getQuantity(), quantity)) {
|
||||
item.setQuantity(quantity);
|
||||
holder.info("数量修改为: " + quantity);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (updateLocalDate(item::getArriveDate, item::setArriveDate, (Timestamp) map.get("dArriveDate"), holder,
|
||||
"开始日期")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!Objects.equals(item.getDescription(), contractCode)) {
|
||||
item.setDescription(contractCode);
|
||||
holder.info("描述修改为: " + contractCode);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
return modified;
|
||||
}
|
||||
|
||||
boolean updateInventory(Supplier<Inventory> getter, Consumer<Inventory> setter, String inventoryCode,
|
||||
MessageHolder holder, String topic) {
|
||||
return getInventoryCtx().syncInventoryDetailByCode(getter, setter, inventoryCode, holder, topic);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.ecep.contract.cloud.u8.ctx;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.model.Inventory;
|
||||
import com.ecep.contract.model.PurchaseSettlementVoucher;
|
||||
import com.ecep.contract.model.PurchaseSettlementVoucherItem;
|
||||
|
||||
public class PurchaseSettlementVoucherCtx extends AbstractYongYouU8Ctx {
|
||||
InventoryCtx inventoryCtx;
|
||||
|
||||
InventoryCtx getInventoryCtx() {
|
||||
if (inventoryCtx == null) {
|
||||
inventoryCtx = new InventoryCtx();
|
||||
inventoryCtx.from(this);
|
||||
}
|
||||
return inventoryCtx;
|
||||
}
|
||||
|
||||
private boolean applyPurchaseSettlementVoucherDetail(PurchaseSettlementVoucher voucher, Map<String, Object> map,
|
||||
MessageHolder holder) {
|
||||
String code = (String) map.get("cSVCode");
|
||||
String vendorCode = (String) map.get("cVenCode");
|
||||
String personCode = (String) map.get("cPersonCode");
|
||||
double taxRate = (double) map.get("iTaxRate");
|
||||
String maker = (String) map.get("cMaker");
|
||||
Timestamp date = (Timestamp) map.get("dSVDate");
|
||||
|
||||
String description = (String) map.get("cSVMemo");
|
||||
boolean modified = false;
|
||||
if (updateText(voucher::getCode, voucher::setCode, code, holder, "编号:")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateCompanyByVendorCode(voucher::getCompany, voucher::setCompany, vendorCode, holder, "供应商")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByCode(voucher::getEmployee, voucher::setEmployee, personCode, holder, "业务员")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(voucher::getMaker, voucher::setMaker, maker, holder, "制单人")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDate(voucher::getDate, voucher::setDate, date, holder, "日期:")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateAppendText(voucher::getDescription, voucher::setDescription, description, holder, "描述")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
return modified;
|
||||
}
|
||||
|
||||
private boolean applyPurchaseSettlementVoucherItemDetail(PurchaseSettlementVoucherItem item,
|
||||
Map<String, Object> map, MessageHolder holder) {
|
||||
String inventoryCode = (String) map.get("cInvCode");
|
||||
String pivCode = (String) map.get("cPIVCode");
|
||||
String accountant = (String) map.get("cbAccounter");
|
||||
Integer rdsID = (Integer) map.get("iRdsID");
|
||||
double quantity = (double) map.get("iPBVQuantity");
|
||||
String description = (String) map.get("cbMemo");
|
||||
boolean modified = false;
|
||||
if (updateInventory(item::getInventory, item::setInventory, inventoryCode, holder, "商品")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(item::getAccountant, item::setAccountant, accountant, holder, "会计师")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateNumber(item::getQuantity, item::setQuantity, quantity, holder, "数量")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateAppendText(item::getDescription, item::setDescription, description, holder, "描述")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
return modified;
|
||||
}
|
||||
|
||||
boolean updateInventory(Supplier<Inventory> getter, Consumer<Inventory> setter, String inventoryCode,
|
||||
MessageHolder holder, String topic) {
|
||||
return getInventoryCtx().syncInventoryDetailByCode(getter, setter, inventoryCode, holder, topic);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,365 @@
|
||||
package com.ecep.contract.cloud.u8.ctx;
|
||||
|
||||
import static com.ecep.contract.SpringApp.getBean;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.constant.CloudServiceConstant;
|
||||
import com.ecep.contract.ds.contract.service.ContractService;
|
||||
import com.ecep.contract.ds.contract.service.SaleOrdersService;
|
||||
import com.ecep.contract.ds.contract.service.SalesBillVoucherService;
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.model.CompanyCustomer;
|
||||
import com.ecep.contract.model.CompanyCustomerEntity;
|
||||
import com.ecep.contract.model.Contract;
|
||||
import com.ecep.contract.model.Inventory;
|
||||
import com.ecep.contract.model.SalesBillVoucher;
|
||||
import com.ecep.contract.model.SalesBillVoucherItem;
|
||||
import com.ecep.contract.model.SalesOrder;
|
||||
import com.ecep.contract.util.NumberUtils;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
public class SalesBillVoucherCtx extends AbstractYongYouU8Ctx {
|
||||
@Setter
|
||||
private ContractService contractService;
|
||||
@Setter
|
||||
private SaleOrdersService saleOrdersService;
|
||||
@Setter
|
||||
private SalesBillVoucherService salesBillVoucherService;
|
||||
|
||||
InventoryCtx inventoryCtx;
|
||||
|
||||
ContractService getContractService() {
|
||||
if (contractService == null) {
|
||||
contractService = getBean(ContractService.class);
|
||||
}
|
||||
return contractService;
|
||||
}
|
||||
|
||||
SaleOrdersService getSaleOrdersService() {
|
||||
if (saleOrdersService == null) {
|
||||
saleOrdersService = getBean(SaleOrdersService.class);
|
||||
}
|
||||
return saleOrdersService;
|
||||
}
|
||||
|
||||
SalesBillVoucherService getSalesBillVoucherService() {
|
||||
if (salesBillVoucherService == null) {
|
||||
salesBillVoucherService = SpringApp.getBean(SalesBillVoucherService.class);
|
||||
}
|
||||
return salesBillVoucherService;
|
||||
}
|
||||
|
||||
InventoryCtx getInventoryCtx() {
|
||||
if (inventoryCtx == null) {
|
||||
inventoryCtx = new InventoryCtx();
|
||||
inventoryCtx.from(this);
|
||||
}
|
||||
return inventoryCtx;
|
||||
}
|
||||
|
||||
public void syncByCompany(Company company, MessageHolder holder) {
|
||||
|
||||
List<SalesBillVoucher> vouchers = salesBillVoucherService.findAll((root, q, cb) -> {
|
||||
return cb.equal(root.get("company"), company);
|
||||
}, Sort.unsorted());
|
||||
holder.debug("查找到 " + vouchers.size() + " 条专用发票记录在数据库中");
|
||||
Map<Integer, SalesBillVoucher> voucherMap = vouchers.stream().collect(Collectors.toMap(SalesBillVoucher::getRefId, item -> item));
|
||||
|
||||
CompanyCustomer customer = getCompanyCustomerService().findByCompany(company);
|
||||
if (customer != null) {
|
||||
List<CompanyCustomerEntity> entities = getCompanyCustomerEntityService().findAllByCustomer(customer);
|
||||
for (CompanyCustomerEntity entity : entities) {
|
||||
// 查询 U8 数据库
|
||||
List<Map<String, Object>> ds = repository.findAllSalesBillVoucherByCustomerCode(entity.getCode());
|
||||
holder.debug("查找" + entity.getCode() + "到 " + ds.size() + " 条专用发票记录在 " + CloudServiceConstant.U8_NAME);
|
||||
|
||||
for (Map<String, Object> map : ds) {
|
||||
Integer sbvid = (Integer) map.get("SBVID");
|
||||
if (sbvid == 0) {
|
||||
holder.warn("跳过无效专用发票记录:缺少 SBVID");
|
||||
continue;
|
||||
}
|
||||
SalesBillVoucher voucher = voucherMap.get(sbvid);
|
||||
boolean voucherModified = false;
|
||||
if (voucher == null) {
|
||||
voucher = new SalesBillVoucher();
|
||||
voucher.setCompany(company);
|
||||
voucher.setRefId(sbvid);
|
||||
voucherMap.put(sbvid, voucher);
|
||||
voucherModified = true;
|
||||
|
||||
holder.info("新增专用发票记录 #" + sbvid);
|
||||
}
|
||||
if (applySalesBillVoucherDetail(voucher, map, holder)) {
|
||||
voucherModified = true;
|
||||
}
|
||||
|
||||
if (voucherModified) {
|
||||
voucher = salesBillVoucherService.save(voucher);
|
||||
voucherMap.put(sbvid, voucher);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<SalesBillVoucherItem> items = salesBillVoucherService.findAllItems((root, q, cb) -> {
|
||||
return cb.equal(root.get("voucher").get("company"), company);
|
||||
}, Sort.unsorted());
|
||||
|
||||
// 按 order 分组
|
||||
Map<SalesBillVoucher, Map<Integer, SalesBillVoucherItem>> itemMap = items.stream().collect(Collectors.groupingBy(SalesBillVoucherItem::getVoucher,
|
||||
Collectors.toMap(SalesBillVoucherItem::getRefId, item -> item)));
|
||||
for (SalesBillVoucher voucher : voucherMap.values()) {
|
||||
|
||||
// 查询 U8 数据库
|
||||
List<Map<String, Object>> ds = repository.findAllSalesBillVoucherItemBySBVID(voucher.getRefId());
|
||||
holder.debug("专用发票#" + voucher.getRefId() + "查找到 " + ds.size() + "条条目记录在 " + CloudServiceConstant.U8_NAME);
|
||||
Map<Integer, SalesBillVoucherItem> subItemMap = itemMap.computeIfAbsent(voucher, k -> new HashMap<>());
|
||||
for (Map<String, Object> map : ds) {
|
||||
Integer refId = (Integer) map.get("ID");
|
||||
if (refId == 0) {
|
||||
holder.warn("跳过无效专用发票记录:缺少 ID");
|
||||
continue;
|
||||
}
|
||||
SalesBillVoucherItem item = subItemMap.remove(refId);
|
||||
boolean itemModified = false;
|
||||
if (item == null) {
|
||||
item = new SalesBillVoucherItem();
|
||||
item.setVoucher(voucher);
|
||||
item.setRefId(refId);
|
||||
itemModified = true;
|
||||
|
||||
holder.info("新增专用发票条目 #" + refId);
|
||||
}
|
||||
MessageHolder subHolder = holder.sub("---| ");
|
||||
if (applySalesBillVoucherItemDetail(item, map, subHolder)) {
|
||||
itemModified = true;
|
||||
}
|
||||
if (itemModified) {
|
||||
item = salesBillVoucherService.save(item);
|
||||
}
|
||||
}
|
||||
for (SalesBillVoucherItem item : subItemMap.values()) {
|
||||
holder.info("删除无效专用发票条目");
|
||||
salesBillVoucherService.delete(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void syncBySalesOrder(SalesOrder order, MessageHolder holder) {
|
||||
SalesBillVoucherService voucherService = getSalesBillVoucherService();
|
||||
List<SalesBillVoucher> vouchers = voucherService.findAll((root, q, cb) -> {
|
||||
return cb.equal(root.get("order"), order);
|
||||
}, Sort.unsorted());
|
||||
holder.debug("查找到 " + vouchers.size() + " 条专用发票记录在数据库中");
|
||||
Map<Integer, SalesBillVoucher> voucherMap = vouchers.stream().collect(Collectors.toMap(SalesBillVoucher::getRefId, item -> item));
|
||||
{
|
||||
// 查询 U8 数据库
|
||||
List<Map<String, Object>> ds = repository.findAllSalesBillVoucherBySalesOrderCode(order.getCode());
|
||||
holder.debug("查找" + order.getCode() + "到 " + ds.size() + " 条专用发票记录在 " + CloudServiceConstant.U8_NAME);
|
||||
|
||||
for (Map<String, Object> map : ds) {
|
||||
Integer sbvid = (Integer) map.get("SBVID");
|
||||
if (sbvid == 0) {
|
||||
holder.warn("跳过无效专用发票记录:缺少 SBVID");
|
||||
continue;
|
||||
}
|
||||
SalesBillVoucher voucher = voucherMap.get(sbvid);
|
||||
boolean voucherModified = false;
|
||||
if (voucher == null) {
|
||||
voucher = new SalesBillVoucher();
|
||||
|
||||
Contract contract = order.getContract();
|
||||
if (!Hibernate.isInitialized(contract)) {
|
||||
contract = getContractService().findById(contract.getId());
|
||||
}
|
||||
voucher.setCompany(contract.getCompany());
|
||||
voucher.setRefId(sbvid);
|
||||
voucherMap.put(sbvid, voucher);
|
||||
voucherModified = true;
|
||||
|
||||
holder.info("新增专用发票记录 #" + sbvid);
|
||||
}
|
||||
if (applySalesBillVoucherDetail(voucher, map, holder)) {
|
||||
voucherModified = true;
|
||||
}
|
||||
|
||||
if (voucherModified) {
|
||||
voucher = voucherService.save(voucher);
|
||||
voucherMap.put(sbvid, voucher);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<SalesBillVoucherItem> items = voucherService.findAllItems((root, q, cb) -> {
|
||||
return cb.equal(root.get("voucher").get("order"), order);
|
||||
}, Sort.unsorted());
|
||||
|
||||
// 按 order 分组
|
||||
Map<SalesBillVoucher, Map<Integer, SalesBillVoucherItem>> itemMap = items.stream().collect(Collectors.groupingBy(SalesBillVoucherItem::getVoucher,
|
||||
Collectors.toMap(SalesBillVoucherItem::getRefId, item -> item)));
|
||||
for (SalesBillVoucher voucher : voucherMap.values()) {
|
||||
|
||||
// 查询 U8 数据库
|
||||
List<Map<String, Object>> ds = repository.findAllSalesBillVoucherItemBySBVID(voucher.getRefId());
|
||||
holder.debug("专用发票#" + voucher.getRefId() + "查找到 " + ds.size() + "条条目记录在 " + CloudServiceConstant.U8_NAME);
|
||||
Map<Integer, SalesBillVoucherItem> subItemMap = itemMap.computeIfAbsent(voucher, k -> new HashMap<>());
|
||||
for (Map<String, Object> map : ds) {
|
||||
Integer refId = (Integer) map.get("AutoID");
|
||||
if (refId == 0) {
|
||||
holder.warn("跳过无效专用发票记录:缺少 AutoID");
|
||||
continue;
|
||||
}
|
||||
SalesBillVoucherItem item = subItemMap.remove(refId);
|
||||
boolean itemModified = false;
|
||||
if (item == null) {
|
||||
item = new SalesBillVoucherItem();
|
||||
item.setVoucher(voucher);
|
||||
item.setRefId(refId);
|
||||
itemModified = true;
|
||||
|
||||
holder.info("新增专用发票条目 #" + refId);
|
||||
}
|
||||
MessageHolder subHolder = holder.sub("---| ");
|
||||
if (applySalesBillVoucherItemDetail(item, map, subHolder)) {
|
||||
itemModified = true;
|
||||
}
|
||||
if (itemModified) {
|
||||
item = voucherService.save(item);
|
||||
}
|
||||
}
|
||||
for (SalesBillVoucherItem item : subItemMap.values()) {
|
||||
holder.info("删除无效专用发票条目");
|
||||
voucherService.delete(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private boolean applySalesBillVoucherDetail(SalesBillVoucher voucher, Map<String, Object> map, MessageHolder holder) {
|
||||
String code = String.valueOf(map.get("SBVID"));
|
||||
String customerCode = (String) map.get("cCusCode");
|
||||
String salesOrderCode = (String) map.get("cSOCode");
|
||||
String personCode = (String) map.get("cPersonCode");
|
||||
String inCode = (String) map.get("cDLCode");
|
||||
Timestamp billDate = (Timestamp) map.get("dDate");
|
||||
|
||||
String maker = (String) map.get("cMaker");
|
||||
String checker = (String) map.get("cChecker");
|
||||
String modifier = (String) map.get("cmodifier");
|
||||
String verifier = (String) map.get("cVerifier");
|
||||
Timestamp makeTime = (Timestamp) map.get("cmaketime");
|
||||
Timestamp verifyTime = (Timestamp) map.get("dverifysystime");
|
||||
Timestamp modifyTime = (Timestamp) map.get("dmodifysystime");
|
||||
|
||||
String description = (String) map.get("cMemo");
|
||||
|
||||
|
||||
boolean modified = false;
|
||||
|
||||
holder.debug("条目:" + code + " x " + salesOrderCode);
|
||||
if (updateCompanyByCustomerCode(voucher::getCompany, voucher::setCompany, customerCode, holder, "客户")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
SalesOrder salesOrder = null;
|
||||
if (StringUtils.hasText(salesOrderCode)) {
|
||||
salesOrder = getSaleOrdersService().findByCode(salesOrderCode);
|
||||
}
|
||||
if (salesOrder == null) {
|
||||
voucher.setOrder(null);
|
||||
holder.warn("无效销售订单:" + salesOrderCode);
|
||||
modified = true;
|
||||
} else {
|
||||
if (!Objects.equals(voucher.getOrder(), salesOrder)) {
|
||||
voucher.setOrder(salesOrder);
|
||||
holder.info("销售订单修改为: " + salesOrder.getCode());
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (updateEmployeeByCode(voucher::getEmployee, voucher::setEmployee, personCode, holder, "业务员")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(voucher::getMaker, voucher::setMaker, maker, holder, "制单人")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(voucher::getVerifier, voucher::setVerifier, verifier, holder, "审核人")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (updateLocalDateTime(voucher::getMakerDate, voucher::setMakerDate, makeTime, holder, "制单时间")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDateTime(voucher::getVerifierDate, voucher::setVerifierDate, verifyTime, holder, "审核时间")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!Objects.equals(voucher.getDescription(), description)) {
|
||||
voucher.setDescription(description);
|
||||
holder.info("描述修改为: " + description);
|
||||
modified = true;
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
|
||||
private boolean applySalesBillVoucherItemDetail(SalesBillVoucherItem item, Map<String, Object> map, MessageHolder holder) {
|
||||
String code = String.valueOf(map.get("ID"));
|
||||
String inventoryCode = (String) map.get("cInvCode");
|
||||
String contractCode = (String) map.get("ContractCode");
|
||||
|
||||
String title = (String) map.get("cInvCode");
|
||||
double quantity = (double) map.get("iQuantity");
|
||||
double taxRate = (double) map.get("iTaxRate");
|
||||
double taxPrice = (double) map.get("iTaxUnitPrice");
|
||||
BigDecimal amount = (BigDecimal) map.get("iSum");
|
||||
|
||||
|
||||
Timestamp signDate = (Timestamp) map.get("dSignDate");
|
||||
|
||||
boolean modified = false;
|
||||
|
||||
holder.debug("条目:" + title + " x " + amount);
|
||||
|
||||
if (updateInventory(item::getInventory, item::setInventory, inventoryCode, holder, "商品")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!NumberUtils.equals(item.getPrice(), taxPrice)) {
|
||||
item.setPrice(taxPrice);
|
||||
holder.info("含税单价修改为: " + taxPrice);
|
||||
modified = true;
|
||||
}
|
||||
if (!NumberUtils.equals(item.getQuantity(), quantity)) {
|
||||
item.setQuantity(quantity);
|
||||
holder.info("数量修改为: " + quantity);
|
||||
modified = true;
|
||||
}
|
||||
if (!Objects.equals(item.getDescription(), contractCode)) {
|
||||
item.setDescription(contractCode);
|
||||
holder.info("描述修改为: " + contractCode);
|
||||
modified = true;
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
boolean updateInventory(Supplier<Inventory> getter, Consumer<Inventory> setter, String inventoryCode, MessageHolder holder, String topic) {
|
||||
return getInventoryCtx().syncInventoryDetailByCode(getter, setter, inventoryCode, holder, topic);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,264 @@
|
||||
package com.ecep.contract.cloud.u8.ctx;
|
||||
|
||||
import static com.ecep.contract.SpringApp.getBean;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.constant.CloudServiceConstant;
|
||||
import com.ecep.contract.ds.contract.service.SaleOrdersService;
|
||||
import com.ecep.contract.ds.contract.service.SalesOrderItemService;
|
||||
import com.ecep.contract.model.Contract;
|
||||
import com.ecep.contract.model.SalesOrder;
|
||||
import com.ecep.contract.model.SalesOrderItem;
|
||||
import com.ecep.contract.util.NumberUtils;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
public class SalesOrderCtx extends AbstractYongYouU8Ctx {
|
||||
|
||||
@Setter
|
||||
private SaleOrdersService saleOrdersService;
|
||||
@Setter
|
||||
private SalesOrderItemService orderItemService;
|
||||
|
||||
SaleOrdersService getSaleOrdersService() {
|
||||
if (saleOrdersService == null) {
|
||||
saleOrdersService = getBean(SaleOrdersService.class);
|
||||
}
|
||||
return saleOrdersService;
|
||||
}
|
||||
|
||||
SalesOrderItemService getOrderItemService() {
|
||||
if (orderItemService == null) {
|
||||
orderItemService = getBean(SalesOrderItemService.class);
|
||||
}
|
||||
return orderItemService;
|
||||
}
|
||||
|
||||
|
||||
public List<SalesOrder> syncByContract(Contract contract, MessageHolder holder) {
|
||||
SaleOrdersService saleOrdersService = getSaleOrdersService();
|
||||
SalesOrderItemService orderItemService = getOrderItemService();
|
||||
|
||||
List<SalesOrder> orders = saleOrdersService.findAll((root, q, cb) -> {
|
||||
return cb.equal(root.get("contract"), contract);
|
||||
}, Sort.unsorted());
|
||||
holder.debug("查找到 " + orders.size() + " 条销售订单记录在数据库中");
|
||||
Map<String, SalesOrder> ordersMap = orders.stream().collect(Collectors.toMap(SalesOrder::getCode, item -> item));
|
||||
|
||||
List<SalesOrderItem> items = orderItemService.findAll((root, q, cb) -> {
|
||||
return cb.equal(root.get("order").get("contract"), contract);
|
||||
}, Sort.unsorted());
|
||||
|
||||
// 按 order 分组
|
||||
Map<SalesOrder, Map<String, SalesOrderItem>> itemMap = items.stream().collect(Collectors.groupingBy(SalesOrderItem::getOrder,
|
||||
Collectors.toMap(SalesOrderItem::getCode, item -> item)));
|
||||
|
||||
// 查询 U8 数据库
|
||||
List<Map<String, Object>> ds = repository.findAllSalesOrderItemByContractCode(contract.getCode());
|
||||
holder.debug("查找到 " + ds.size() + " 条销售订单条目记录在 " + CloudServiceConstant.U8_NAME);
|
||||
|
||||
Map<SalesOrder, List<SalesOrderItem>> updateMap = new HashMap<>();
|
||||
for (Map<String, Object> map : ds) {
|
||||
String orderCode = Optional.ofNullable(map.get("cSOCode")).map(Object::toString).orElse("");
|
||||
if (orderCode.isEmpty()) {
|
||||
holder.warn("跳过无效销售订单记录:缺少 cSOCode");
|
||||
continue;
|
||||
}
|
||||
SalesOrder order = ordersMap.get(orderCode);
|
||||
if (order == null) {
|
||||
order = new SalesOrder();
|
||||
order.setContract(contract);
|
||||
order.setCode(orderCode);
|
||||
|
||||
order = saleOrdersService.save(order);
|
||||
ordersMap.put(orderCode, order);
|
||||
holder.info("新增销售订单 #" + orderCode);
|
||||
}
|
||||
}
|
||||
for (Map<String, Object> map : ds) {
|
||||
// holder.debug("条目:" + map.toString());
|
||||
String orderCode = Optional.ofNullable(map.get("cSOCode")).map(Object::toString).orElse("");
|
||||
if (orderCode.isEmpty()) {
|
||||
holder.warn("跳过无效销售订单记录:缺少 cSOCode");
|
||||
continue;
|
||||
}
|
||||
SalesOrder order = ordersMap.get(orderCode);
|
||||
|
||||
boolean itemModified = false;
|
||||
List<SalesOrderItem> updates = updateMap.computeIfAbsent(order, k -> new ArrayList<>());
|
||||
|
||||
// 获取条目标识并处理 null
|
||||
String refId = Optional.ofNullable(map.get("iSOsID")).map(Object::toString).orElse("");
|
||||
if (refId.isEmpty()) {
|
||||
holder.warn("跳过条目:订单 " + orderCode + " 缺少 iSOsID");
|
||||
continue;
|
||||
}
|
||||
Map<String, SalesOrderItem> subItemMap = itemMap.get(order);
|
||||
SalesOrderItem item = null;
|
||||
if (subItemMap != null) {
|
||||
item = subItemMap.remove(refId);
|
||||
}
|
||||
|
||||
if (item == null) {
|
||||
item = new SalesOrderItem();
|
||||
item.setOrder(order);
|
||||
item.setCode(refId);
|
||||
itemModified = true;
|
||||
|
||||
holder.info("新增销售订单条目 #" + refId);
|
||||
}
|
||||
MessageHolder subHolder = holder.sub("---| ");
|
||||
if (applySaleOrderItemDetail(item, map, subHolder)) {
|
||||
itemModified = true;
|
||||
}
|
||||
if (itemModified) {
|
||||
item = orderItemService.save(item);
|
||||
}
|
||||
updates.add(item);
|
||||
}
|
||||
|
||||
for (SalesOrder order : updateMap.keySet()) {
|
||||
holder.debug("销售订单 #" + order.getCode());
|
||||
if (applySalesOrderDetail(order, repository.querySalesOrderDetail(order.getCode()), holder)) {
|
||||
saleOrdersService.save(order);
|
||||
}
|
||||
}
|
||||
|
||||
if (!itemMap.isEmpty()) {
|
||||
for (Map<String, SalesOrderItem> subMap : itemMap.values()) {
|
||||
if (!subMap.isEmpty()) {
|
||||
for (SalesOrderItem item : subMap.values()) {
|
||||
holder.info("删除销售订单条目");
|
||||
orderItemService.delete(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (SalesOrder order : updateMap.keySet()) {
|
||||
itemMap.remove(order);
|
||||
}
|
||||
if (!itemMap.isEmpty()) {
|
||||
holder.info("剩余 " + itemMap.size() + " 个销售订单条目");
|
||||
}
|
||||
}
|
||||
|
||||
return new ArrayList<>(updateMap.keySet());
|
||||
|
||||
}
|
||||
|
||||
private boolean applySalesOrderDetail(SalesOrder order, Map<String, Object> map, MessageHolder holder) {
|
||||
String code = (String) map.get("cSOCode");
|
||||
|
||||
String cCloser = (String) map.get("cCloser");
|
||||
|
||||
String memo = (String) map.get("cMemo");
|
||||
|
||||
boolean modified = false;
|
||||
|
||||
if (!Objects.equals(order.getCode(), code)) {
|
||||
order.setCode(code);
|
||||
holder.info("订单编号更新为 " + code);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (updateEmployeeByCode(order::getEmployee, order::setEmployee, (String) map.get("cPersonCode"), holder, "业务员")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(order::getMaker, order::setMaker, (String) map.get("cMaker"), holder, "制单人")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(order::getVerifier, order::setVerifier, (String) map.get("cVerifier"), holder, "审核人")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (updateLocalDate(order::getMakerDate, order::setMakerDate, (Timestamp) map.get("dcreatesystime"), holder, "制单日期")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDate(order::getVerifierDate, order::setVerifierDate, (Timestamp) map.get("dverifysystime"), holder, "审核日期")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!Objects.equals(order.getDescription(), memo)) {
|
||||
order.setDescription(memo);
|
||||
holder.info("描述修改为: " + memo);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
return modified;
|
||||
}
|
||||
|
||||
|
||||
private boolean applySaleOrderItemDetail(SalesOrderItem item, Map<String, Object> map, MessageHolder holder) {
|
||||
String code = String.valueOf(map.get("iSOsID"));
|
||||
String spec = (String) map.get("cInvName");
|
||||
String title = (String) map.get("cInvName");
|
||||
double quantity = (double) map.get("iQuantity");
|
||||
double taxRate = (double) map.get("iTaxRate");
|
||||
double taxPrice = (double) map.get("iTaxUnitPrice");
|
||||
double exclusiveTaxPrice = (double) map.get("iUnitPrice");
|
||||
BigDecimal amount = (BigDecimal) map.get("iSum");
|
||||
|
||||
String memo = (String) map.get("cMemo");
|
||||
|
||||
boolean modified = false;
|
||||
|
||||
holder.debug("条目:" + title + " x " + amount);
|
||||
|
||||
|
||||
if (!Objects.equals(item.getCode(), code)) {
|
||||
item.setCode(code);
|
||||
holder.info("代码修改为: " + code);
|
||||
modified = true;
|
||||
}
|
||||
if (!Objects.equals(item.getName(), title)) {
|
||||
item.setName(title);
|
||||
holder.info("名称修改为: " + title);
|
||||
modified = true;
|
||||
}
|
||||
if (!NumberUtils.equals(item.getExclusiveTaxPrice(), exclusiveTaxPrice)) {
|
||||
item.setExclusiveTaxPrice(exclusiveTaxPrice);
|
||||
holder.info("不含税单价修改为: " + exclusiveTaxPrice);
|
||||
modified = true;
|
||||
}
|
||||
if (!NumberUtils.equals(item.getPrice(), taxPrice)) {
|
||||
item.setPrice(taxPrice);
|
||||
holder.info("含税单价修改为: " + taxPrice);
|
||||
modified = true;
|
||||
}
|
||||
if (!NumberUtils.equals(item.getTaxRate(), taxRate)) {
|
||||
item.setTaxRate(taxRate);
|
||||
holder.info("税率修改为: " + taxRate);
|
||||
modified = true;
|
||||
}
|
||||
if (!NumberUtils.equals(item.getQuantity(), quantity)) {
|
||||
item.setQuantity(quantity);
|
||||
holder.info("数量修改为: " + quantity);
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDate(item::getStartDate, item::setStartDate, (Timestamp) map.get("dPreDate"), holder, "开始日期")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDate(item::getEndDate, item::setEndDate, (Timestamp) map.get("dPreMoDate"), holder, "结束日期")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (!Objects.equals(item.getDescription(), memo)) {
|
||||
item.setDescription(memo);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
return modified;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,327 @@
|
||||
package com.ecep.contract.cloud.u8.ctx;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.MessageHolder;
|
||||
import com.ecep.contract.SpringApp;
|
||||
import com.ecep.contract.cloud.old.OldVersionService;
|
||||
import com.ecep.contract.ds.company.service.CompanyService;
|
||||
import com.ecep.contract.ds.vendor.service.CompanyVendorEntityService;
|
||||
import com.ecep.contract.ds.vendor.service.CompanyVendorService;
|
||||
import com.ecep.contract.model.Company;
|
||||
import com.ecep.contract.model.CompanyVendor;
|
||||
import com.ecep.contract.model.CompanyVendorEntity;
|
||||
import com.ecep.contract.model.VendorCatalog;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
public class VendorCtx extends AbstractYongYouU8Ctx {
|
||||
private static final String AUTO_CREATE_VENDOR_AFTER = "cloud.u8.auto-create-vendor-after";
|
||||
@Setter
|
||||
private CompanyVendorService companyVendorService;
|
||||
@Setter
|
||||
private CompanyCtx companyCtx;
|
||||
@Setter
|
||||
private ContractCtx contractCtx;
|
||||
@Setter
|
||||
private CompanyBankAccountCtx companyBankAccountCtx;
|
||||
|
||||
public CompanyCtx getCompanyCtx() {
|
||||
if (companyCtx == null) {
|
||||
companyCtx = new CompanyCtx();
|
||||
companyCtx.from(this);
|
||||
}
|
||||
return companyCtx;
|
||||
}
|
||||
|
||||
ContractCtx getContractCtx() {
|
||||
if (contractCtx == null) {
|
||||
contractCtx = new ContractCtx();
|
||||
contractCtx.from(this);
|
||||
}
|
||||
return contractCtx;
|
||||
}
|
||||
|
||||
CompanyBankAccountCtx getCompanyBankAccountCtx() {
|
||||
if (companyBankAccountCtx == null) {
|
||||
companyBankAccountCtx = new CompanyBankAccountCtx();
|
||||
companyBankAccountCtx.from(this);
|
||||
}
|
||||
return companyBankAccountCtx;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新供应商相关项(相关项是 U8 系统中的 Vendor 表数据,因为同一个公司有多个相关项,因此需要一个一对多的关系来处理)详情
|
||||
*
|
||||
* @param item 供应商相关项
|
||||
* @param unitCode 供应商相关项编码
|
||||
* @param holder 消息
|
||||
*/
|
||||
public CompanyVendorEntity updateVendorEntityDetailByCode(CompanyVendorEntity item, String unitCode, MessageHolder holder) {
|
||||
if (applyEntityDetail(item, repository.findVendorByVendCode(unitCode), holder)) {
|
||||
item = save(item);
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
public boolean applyEntityDetail(CompanyVendorEntity item, Map<String, Object> map, MessageHolder holder) {
|
||||
if (map == null || map.isEmpty()) {
|
||||
holder.warn("Vendor 中未检索到供应商数据");
|
||||
return false;
|
||||
}
|
||||
String name = (String) map.get("cVenName");
|
||||
String abbName = (String) map.get("cVenAbbName");
|
||||
String venCode = (String) map.get("cVenCode");
|
||||
String classCode = (String) map.get("cVCCode");
|
||||
|
||||
String createPerson = (String) map.get("cCreatePerson");
|
||||
String modifyPerson = (String) map.get("cModifyPerson");
|
||||
java.sql.Date devDate = (java.sql.Date) map.get("devDate");
|
||||
java.sql.Timestamp modifyDate = (java.sql.Timestamp) map.get("dModifyDate");
|
||||
java.sql.Timestamp createDatetime = (java.sql.Timestamp) map.get("dVenCreateDatetime");
|
||||
|
||||
String bank = (String) map.get("cVenBank");
|
||||
String bankAccount = (String) map.get("cVenAccount");
|
||||
String address = (String) map.get("cVenAddress");
|
||||
String phone = (String) map.get("cVenPhone");
|
||||
String person = (String) map.get("cVenPerson");
|
||||
|
||||
|
||||
boolean modified = false;
|
||||
if (updateText(item::getName, item::setName, name, holder, "名称")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(item::getAbbName, item::setAbbName, abbName, holder, "简称")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateText(item::getCode, item::setCode, venCode, holder, "供应商编号")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateVendorCatalog(item::getCatalog, item::setCatalog, classCode, holder, "分类")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(item::getCreator, item::setCreator, createPerson, holder, "创建人")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateEmployeeByName(item::getModifier, item::setModifier, modifyPerson, holder, "修改人")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDate(item::getDevelopDate, item::setDevelopDate, devDate, holder, "开发日期")) {
|
||||
modified = true;
|
||||
}
|
||||
if (updateLocalDate(item::getModifyDate, item::setModifyDate, modifyDate, holder, "修改日期")) {
|
||||
modified = true;
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
LocalDate today = LocalDate.now();
|
||||
if (item.getUpdatedDate() == null || item.getUpdatedDate().isBefore(today)) {
|
||||
item.setUpdatedDate(today);
|
||||
holder.info("更新日期更新为 " + today);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
CompanyVendor vendor = item.getVendor();
|
||||
if (vendor == null) {
|
||||
// 如果没有关联供应商,则根据供应商名称或别名查找公司
|
||||
Company company = findOrCreateCompanyByVendorEntity(item, holder);
|
||||
if (company != null) {
|
||||
vendor = getCompanyVendorService().findByCompany(company);
|
||||
if (vendor == null) {
|
||||
vendor = createVendorByVendorEntity(item, holder);
|
||||
if (vendor != null) {
|
||||
vendor.setCompany(company);
|
||||
vendor = getCompanyVendorService().save(vendor);
|
||||
}
|
||||
}
|
||||
if (vendor != null) {
|
||||
item.setVendor(vendor);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (vendor != null) {
|
||||
if (!Hibernate.isInitialized(vendor)) {
|
||||
vendor = getCompanyVendorService().findById(vendor.getId());
|
||||
}
|
||||
Company company = vendor.getCompany();
|
||||
if (company != null) {
|
||||
getCompanyBankAccountCtx().updateBankAccount(company, bank, bankAccount, holder.sub(item.getName()));
|
||||
}
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
private boolean updateVendorCatalog(Supplier<VendorCatalog> getter, Consumer<VendorCatalog> setter, String catalogCode, MessageHolder holder, String topic) {
|
||||
VendorCatalog catalog = null;
|
||||
if (StringUtils.hasText(catalogCode)) {
|
||||
catalog = getCompanyVendorService().findCatalogByCode(catalogCode);
|
||||
}
|
||||
if (catalog == null) {
|
||||
setter.accept(null);
|
||||
holder.warn("无效" + topic + ":" + catalogCode);
|
||||
return true;
|
||||
} else {
|
||||
if (!Objects.equals(getter.get(), catalog)) {
|
||||
if (!Hibernate.isInitialized(catalog)) {
|
||||
catalog = getCompanyVendorService().findCatalogByCode(catalogCode);
|
||||
}
|
||||
setter.accept(catalog);
|
||||
holder.info(topic + "修改为: " + catalog.getName());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步供应商
|
||||
*
|
||||
* @param company 要同步的供应商企业
|
||||
* @param holder 状态输出
|
||||
* @return 是否更新了供应商信息
|
||||
*/
|
||||
public boolean syncVendor(Company company, MessageHolder holder) {
|
||||
CompanyVendor companyVendor = getCompanyVendorService().findByCompany(company);
|
||||
if (companyVendor == null) {
|
||||
holder.warn("供应商未创建, 如需要请手动创建.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检索相关项
|
||||
List<CompanyVendorEntity> entities = getCompanyVendorEntityService().findAllByVendor(companyVendor);
|
||||
if (entities.isEmpty()) {
|
||||
holder.error("供应商关联任何相关项");
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean updated = false;
|
||||
boolean companyModified = false;
|
||||
|
||||
// 更新相关项
|
||||
for (CompanyVendorEntity entity : entities) {
|
||||
if (!StringUtils.hasText(entity.getCode())) {
|
||||
holder.warn("相关项:" + entity.getCode() + " 无效,跳过");
|
||||
continue;
|
||||
}
|
||||
if (applyEntityDetail(entity, repository.findVendorByVendCode(entity.getCode()), holder)) {
|
||||
entity = getCompanyVendorEntityService().save(entity);
|
||||
}
|
||||
if (updateCompanyNameAndAbbNameByVendorEntity(company, entity, holder)) {
|
||||
companyModified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (companyModified) {
|
||||
company = getCompanyService().save(company);
|
||||
updated = true;
|
||||
}
|
||||
|
||||
// 更新供应商的开发日期
|
||||
if (updateVendorDevelopDate(companyVendor, entities, holder)) {
|
||||
companyVendor = getCompanyVendorService().save(companyVendor);
|
||||
updated = true;
|
||||
}
|
||||
|
||||
// 同步供应商关联的合同
|
||||
for (CompanyVendorEntity entity : entities) {
|
||||
if (getContractCtx().syncByVendorEntity(companyVendor, entity, holder)) {
|
||||
updated = true;
|
||||
}
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
private boolean updateCompanyNameAndAbbNameByVendorEntity(Company company, CompanyVendorEntity entity, MessageHolder holder) {
|
||||
CompanyService companyService = getCompanyService();
|
||||
if (company == null) {
|
||||
return false;
|
||||
}
|
||||
if (!Hibernate.isInitialized(company)) {
|
||||
company = companyService.findById(company.getId());
|
||||
}
|
||||
boolean modified = false;
|
||||
CompanyCtx companyCtx = getCompanyCtx();
|
||||
if (companyCtx.updateCompanyNameIfAbsent(company, entity.getName(), holder)) {
|
||||
modified = true;
|
||||
}
|
||||
if (companyCtx.updateCompanyAbbNameIfAbsent(company, entity.getAbbName(), holder)) {
|
||||
modified = true;
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
private boolean updateVendorDevelopDate(CompanyVendor companyVendor, List<CompanyVendorEntity> entities, MessageHolder holder) {
|
||||
LocalDate developDate = null;
|
||||
for (CompanyVendorEntity entity : entities) {
|
||||
// 取最早的开发日期
|
||||
if (developDate == null || entity.getDevelopDate().isBefore(developDate)) {
|
||||
developDate = entity.getDevelopDate();
|
||||
}
|
||||
}
|
||||
return updateLocalDate(companyVendor::getDevelopDate, companyVendor::setDevelopDate, developDate, holder, "开发日期");
|
||||
}
|
||||
|
||||
public CompanyVendorEntity findOrCreateByCode(String venCode, MessageHolder subHolder) {
|
||||
CompanyVendorEntityService service = getCompanyVendorEntityService();
|
||||
CompanyVendorEntity entity = service.findByCode(venCode);
|
||||
if (entity == null) {
|
||||
entity = new CompanyVendorEntity();
|
||||
entity.setCode(venCode);
|
||||
entity = service.save(entity);
|
||||
subHolder.info("创建供应商相关项: " + venCode);
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
private CompanyVendor createVendorByVendorEntity(CompanyVendorEntity entity, MessageHolder holder) {
|
||||
LocalDate developDate = entity.getDevelopDate();
|
||||
if (developDate == null) {
|
||||
holder.warn(entity.getName() + " 没有设置开发日期,跳过");
|
||||
return null;
|
||||
}
|
||||
// 创建发展日期从2023年1月1日后的
|
||||
LocalDate start = LocalDate.of(2023, 1, 1);
|
||||
String autoCreateAfter = getConfService().getString(AUTO_CREATE_VENDOR_AFTER);
|
||||
if (StringUtils.hasText(autoCreateAfter)) {
|
||||
start = LocalDate.parse(autoCreateAfter);
|
||||
}
|
||||
if (developDate.isBefore(start)) {
|
||||
// start 之前的不自动创建
|
||||
holder.warn(entity.getName() + " 的发展日期 " + developDate + " 是 " + start + " 之前的, 按规定不自动创建供应商, 如有需要请手动创建");
|
||||
return null;
|
||||
}
|
||||
|
||||
CompanyVendor companyVendor = new CompanyVendor();
|
||||
int nextId = SpringApp.getBean(OldVersionService.class).newCompanyVendor(entity.getName());
|
||||
companyVendor.setId(nextId);
|
||||
companyVendor.setCatalog(entity.getCatalog());
|
||||
companyVendor.setDevelopDate(developDate);
|
||||
holder.info("新供应商:" + entity.getName() + "分配编号:" + nextId);
|
||||
companyVendor.setCreated(Instant.now());
|
||||
return companyVendor;
|
||||
}
|
||||
|
||||
|
||||
private Company findOrCreateCompanyByVendorEntity(CompanyVendorEntity entity, MessageHolder holder) {
|
||||
String name = entity.getName();
|
||||
String abbName = entity.getAbbName();
|
||||
return getCompanyCtx().findOrCreateByNameOrAbbName(name, abbName, entity.getDevelopDate(), holder);
|
||||
}
|
||||
|
||||
public CompanyVendorEntity save(CompanyVendorEntity entity) {
|
||||
return getCompanyVendorEntityService().save(entity);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user