|
@@ -0,0 +1,173 @@
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
+import com.sd.SdApplication;
|
|
|
+import com.sd.business.entity.apply.po.ApplyBuy;
|
|
|
+import com.sd.business.entity.apply.po.ApplyBuyBom;
|
|
|
+import com.sd.business.entity.bom.po.BomSpec;
|
|
|
+import com.sd.business.entity.purchase.dto.PurchaseBomDto;
|
|
|
+import com.sd.business.entity.purchase.dto.PurchaseDto;
|
|
|
+import com.sd.business.entity.purchase.po.Purchase;
|
|
|
+import com.sd.business.entity.supplier.po.Supplier;
|
|
|
+import com.sd.business.service.apply.ApplyBuyBomService;
|
|
|
+import com.sd.business.service.apply.ApplyBuyService;
|
|
|
+import com.sd.business.service.bom.BomSpecService;
|
|
|
+import com.sd.business.service.purchase.PurchaseBomService;
|
|
|
+import com.sd.business.service.purchase.PurchaseService;
|
|
|
+import com.sd.business.service.supplier.SupplierService;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+
|
|
|
+ * 同步采购表
|
|
|
+ */
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+@SpringBootTest(classes = SdApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
|
|
+public class A8_SyncPurchaseTest {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PurchaseService purchaseService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PurchaseBomService purchaseBomService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BomSpecService bomSpecService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SupplierService supplierService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ApplyBuyService applyBuyService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ApplyBuyBomService applyBuyBomService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DataSource dataSource;
|
|
|
+
|
|
|
+ @DSTransactional
|
|
|
+ @Test
|
|
|
+ public void test() {
|
|
|
+ DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource) dataSource;
|
|
|
+ DataSource sdDataSource = dynamicRoutingDataSource.getDataSource("shengde");
|
|
|
+
|
|
|
+ String sql = "SELECT\n" +
|
|
|
+ "\t id oldId,\n" +
|
|
|
+ "\t code,\n" +
|
|
|
+ "\t subscriber_id applyBuyId,\n" +
|
|
|
+ "\t supply_id supplierId,\n" +
|
|
|
+ "\t supply_province_name province,\n" +
|
|
|
+ "\t supply_city_name city,\n" +
|
|
|
+ "\t supply_address detailedAddress,\n" +
|
|
|
+ "\t supply_contact_name contactPerson,\n" +
|
|
|
+ "\t supply_contact_tel contactNumber,\n" +
|
|
|
+ "\t IF(receiving_addr_type = 0, 1, 2) receiveGoodsType,\n" +
|
|
|
+ "\t receiving_province_name receiveProvince,\n" +
|
|
|
+ "\t receiving_city_name receiveCity,\n" +
|
|
|
+ "\t receiving_address receiveDetailedAddress,\n" +
|
|
|
+ "\t receiving_zip_code receivePostcode,\n" +
|
|
|
+ "\t receiving_contact_name receiveContactPerson,\n" +
|
|
|
+ "\t receiving_contact_tel receiveContactNumber,\n" +
|
|
|
+ "\t pay_type settlementMethod,\n" +
|
|
|
+ "\t invoice_type invoiceType,\n" +
|
|
|
+ "\t delivery_time deliveryDate,\n" +
|
|
|
+ "\t currency currency,\n" +
|
|
|
+ "\t advance_charge advancePayment,\n" +
|
|
|
+ "\t purchase_price totalAmountIncludingTax,\n" +
|
|
|
+ "\t tax_purchase_price totalAmountExcludingTax,\n" +
|
|
|
+ "\t 0 returnAmount,\n" +
|
|
|
+ "\t 0 closedAccountAmount,\n" +
|
|
|
+ "\t 0 deductibleAmount,\n" +
|
|
|
+ "\t settlement_status paymentStatus,\n" +
|
|
|
+ "\t 0 storageStatus,\n" +
|
|
|
+ "\t 2 flowStatus,\n" +
|
|
|
+ "\t create_time createTime,\n" +
|
|
|
+ "\t update_time updateTime\n" +
|
|
|
+ "\t\n" +
|
|
|
+ "FROM\n" +
|
|
|
+ "\t t_pro_purchase_contract" +
|
|
|
+ "\t WHERE purchase_status = '20'";
|
|
|
+
|
|
|
+ List<Map<String, Object>> purchaseContractList = new JdbcTemplate(sdDataSource).queryForList(sql);
|
|
|
+
|
|
|
+ List<Map<String, Object>> list = purchaseContractList.stream().peek(p -> {
|
|
|
+ if (ObjectUtil.isNotEmpty(p.get("supplierId"))) {
|
|
|
+ String supplierId = p.get("supplierId").toString();
|
|
|
+ Supplier supplier = supplierService.getOne(q -> q.eq(Supplier::getOldId, supplierId));
|
|
|
+ p.put("supplierId", supplier.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ObjectUtil.isNotEmpty(p.get("applyBuyId"))) {
|
|
|
+ String applyBuyId = p.get("applyBuyId").toString();
|
|
|
+ ApplyBuy applyBuy = applyBuyService.getOne(q -> q.eq(ApplyBuy::getOldId, applyBuyId));
|
|
|
+ p.put("applyBuyId", applyBuy.getId());
|
|
|
+ }
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<PurchaseDto> purchaseDtoList = BeanUtil.copyToList(list, PurchaseDto.class);
|
|
|
+
|
|
|
+ for (PurchaseDto purchaseDto : purchaseDtoList) {
|
|
|
+ purchaseService.save(purchaseDto);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Purchase> purchaseList = purchaseService.list(q -> q.isNotNull(Purchase::getOldId));
|
|
|
+ for (Purchase purchase : purchaseList) {
|
|
|
+ String sql2 = "SELECT\n" +
|
|
|
+ "\t id oldId,\n" +
|
|
|
+ "\t " + purchase.getId() + " purchaseId,\n" +
|
|
|
+ "\t bom_color_id bomSpecId,\n" +
|
|
|
+ "\t price unitPrice,\n" +
|
|
|
+ "\t tax_amount taxRate,\n" +
|
|
|
+ "\t quantity purchaseQuantity,\n" +
|
|
|
+ "\t 0 returnQuantity,\n" +
|
|
|
+ "\t 0 paidAmount,\n" +
|
|
|
+ "\t create_time createTime,\n" +
|
|
|
+ "\t update_time updateTime\n" +
|
|
|
+ "\t\n" +
|
|
|
+ "FROM\n" +
|
|
|
+ "\t t_pro_purchase_contract_product" +
|
|
|
+ "\t WHERE purchase_contract_id = " + "'" + purchase.getOldId() + "'";
|
|
|
+
|
|
|
+ List<Map<String, Object>> productList = new JdbcTemplate(sdDataSource).queryForList(sql2);
|
|
|
+
|
|
|
+ if (!productList.isEmpty()) {
|
|
|
+ List<Map<String, Object>> bomList = productList.stream().peek(p -> {
|
|
|
+ String bomColorId = p.get("bomSpecId").toString();
|
|
|
+ BomSpec bomSpec = bomSpecService.getOne(q -> q.eq(BomSpec::getOldId, bomColorId));
|
|
|
+ p.put("bomSpecId", bomSpec.getId());
|
|
|
+
|
|
|
+ ApplyBuyBom applyBuyBom = applyBuyBomService.getOne(q -> q.eq(ApplyBuyBom::getBomSpecId, bomSpec.getId()).eq(ApplyBuyBom::getApplyBuyId, purchase.getApplyBuyId()));
|
|
|
+ if (ObjectUtil.isNotEmpty(applyBuyBom)) {
|
|
|
+ p.put("applyBuyBomId", applyBuyBom.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ String sql3 = "SELECT\n" +
|
|
|
+ "\t ifnull(sum(quantity), 0) arrivalQuantity \n" +
|
|
|
+ "\t\n" +
|
|
|
+ "FROM\n" +
|
|
|
+ "\t t_pro_wms_stock_registration_detail" +
|
|
|
+ "\t WHERE status = 1 and type = 1 and genre = 2 and purchase_contract_id = '" + purchase.getOldId() + "' and business_id = '" + bomColorId + "'";
|
|
|
+
|
|
|
+ List<Map<String, Object>> quantityList = new JdbcTemplate(sdDataSource).queryForList(sql3);
|
|
|
+ String arrivalQuantity = quantityList.get(0).get("arrivalQuantity").toString();
|
|
|
+ p.put("arrivalQuantity", arrivalQuantity);
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ List<PurchaseBomDto> purchaseBomDtoList = BeanUtil.copyToList(bomList, PurchaseBomDto.class);
|
|
|
+ for (PurchaseBomDto purchaseBomDto : purchaseBomDtoList) {
|
|
|
+ purchaseBomService.save(purchaseBomDto);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|