Browse Source

新品历史

master
常熟吴彦祖 4 days ago
parent
commit
5ebd9f5e6d
  1. 31
      src/main/java/com/xujie/modules/order/controller/SupplierNewProductHistoryController.java
  2. 95
      src/main/java/com/xujie/modules/order/service/Impl/SupplierNewProductHistoryQueryService.java
  3. 15
      src/main/java/com/xujie/modules/order/vo/SupplierNewProductHistoryQuery.java

31
src/main/java/com/xujie/modules/order/controller/SupplierNewProductHistoryController.java

@ -0,0 +1,31 @@
package com.xujie.modules.order.controller;
import com.xujie.common.utils.R;
import com.xujie.modules.order.service.Impl.SupplierNewProductHistoryQueryService;
import com.xujie.modules.order.vo.SupplierNewProductHistoryQuery;
import com.xujie.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/supplierNewProductHistory")
public class SupplierNewProductHistoryController {
private final SupplierNewProductHistoryQueryService queryService;
public SupplierNewProductHistoryController(SupplierNewProductHistoryQueryService queryService) {
this.queryService = queryService;
}
@PostMapping("/page")
public R page(@RequestBody(required = false) SupplierNewProductHistoryQuery query) {
SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
if (user == null) {
throw new IllegalStateException("请先登录后再查询供应商新品历史");
}
return R.ok().put("page", queryService.page(user.getSite(), query));
}
}

95
src/main/java/com/xujie/modules/order/service/Impl/SupplierNewProductHistoryQueryService.java

@ -0,0 +1,95 @@
package com.xujie.modules.order.service.Impl;
import com.xujie.common.utils.PageUtils;
import com.xujie.modules.order.vo.SupplierNewProductHistoryQuery;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class SupplierNewProductHistoryQueryService {
private final JdbcTemplate jdbcTemplate;
public SupplierNewProductHistoryQueryService(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public PageUtils page(String site, SupplierNewProductHistoryQuery query) {
if (!StringUtils.hasText(site)) {
throw new IllegalArgumentException("当前账号未设置 Site,无法查询供应商新品历史");
}
if (query == null) {
query = new SupplierNewProductHistoryQuery();
}
int page = query.getPage() == null ? 1 : Math.max(1, query.getPage());
int limit = query.getLimit() == null ? 20 : Math.max(1, Math.min(500, query.getLimit()));
LocalDate start = parseDate(query.getOrderDateStart(), "订单日期开始");
LocalDate end = parseDate(query.getOrderDateEnd(), "订单日期结束");
if (start != null && end != null && start.isAfter(end)) {
throw new IllegalArgumentException("订单日期开始不能晚于结束");
}
StringBuilder fromWhere = new StringBuilder(
" FROM dbo.supplier_new_product_history h WITH (NOLOCK) "
+ "LEFT JOIN dbo.part p WITH (NOLOCK) ON p.site = h.site AND p.part_no = h.part_no "
+ "LEFT JOIN dbo.srm_supplier s WITH (NOLOCK) "
+ "ON s.site = h.site AND s.supplier_no = h.supplier_no "
+ "WHERE h.site = ?");
List<Object> params = new ArrayList<>();
params.add(site.trim());
appendLike(fromWhere, params, "s.c_short_office", query.getShortOffice());
appendLike(fromWhere, params, "s.c_short_npc", query.getShortNpc());
appendLike(fromWhere, params, "h.base_part_no", query.getBasePartNo());
appendLike(fromWhere, params, "p.sku", query.getSku());
if (start != null) {
fromWhere.append(" AND h.ori_order_date >= CAST(? AS DATE)");
params.add(start.toString());
}
if (end != null) {
fromWhere.append(" AND h.ori_order_date < DATEADD(DAY, 1, CAST(? AS DATE))");
params.add(end.toString());
}
Integer total = jdbcTemplate.queryForObject(
"SELECT COUNT(1)" + fromWhere, Integer.class, params.toArray());
List<Object> pageParams = new ArrayList<>(params);
pageParams.add((long) (page - 1) * limit);
pageParams.add(limit);
String dataSql = "SELECT h.id AS id, s.c_short_office AS shortOffice, "
+ "s.c_short_npc AS shortNpc, h.base_part_no AS basePartNo, "
+ "p.sku AS sku, h.first_order_no AS firstOrderNo, "
+ "h.item_no AS itemNo, h.part_no AS partNo, "
+ "CONVERT(VARCHAR(10), h.ori_order_date, 23) AS oriOrderDate, "
+ "CONVERT(VARCHAR(19), h.create_time, 120) AS createTime"
+ fromWhere
+ " ORDER BY h.ori_order_date DESC, h.id DESC"
+ " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
List<Map<String, Object>> rows = jdbcTemplate.queryForList(dataSql, pageParams.toArray());
return new PageUtils(rows, total == null ? 0 : total, limit, page);
}
private static void appendLike(StringBuilder sql, List<Object> params, String column, String value) {
if (StringUtils.hasText(value)) {
sql.append(" AND ").append(column).append(" LIKE ?");
params.add("%" + value.trim() + "%");
}
}
private static LocalDate parseDate(String value, String label) {
if (!StringUtils.hasText(value)) {
return null;
}
try {
return LocalDate.parse(value.trim());
} catch (DateTimeParseException ex) {
throw new IllegalArgumentException(label + "格式应为 yyyy-MM-dd", ex);
}
}
}

15
src/main/java/com/xujie/modules/order/vo/SupplierNewProductHistoryQuery.java

@ -0,0 +1,15 @@
package com.xujie.modules.order.vo;
import lombok.Data;
@Data
public class SupplierNewProductHistoryQuery {
private String shortOffice;
private String shortNpc;
private String basePartNo;
private String sku;
private String orderDateStart;
private String orderDateEnd;
private Integer page = 1;
private Integer limit = 20;
}
Loading…
Cancel
Save