| | |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> productInOutAnalysis(Integer type) { |
| | | String targetName; |
| | | // 0-原材料 1-半成品 2-成品 |
| | | Integer modelType; |
| | | if (type == 1) { |
| | | targetName = "原材料"; |
| | | modelType = 0; |
| | | } else if (type == 2) { |
| | | targetName = "成品"; |
| | | modelType = 2; |
| | | } else if (type == 3) { |
| | | targetName = "半成品"; |
| | | modelType = 1; |
| | | } else { |
| | | return new ArrayList<>(); |
| | | } |
| | | |
| | | LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(Product::getProductName, targetName).isNull(Product::getParentId); |
| | | Product rootCategory = productMapper.selectOne(queryWrapper); |
| | | |
| | | if (rootCategory == null) { |
| | | log.warn("未找到名称为 {} 的顶级产品分类", targetName); |
| | | return new ArrayList<>(); |
| | | } |
| | | Long rootCategoryId = rootCategory.getId(); |
| | | |
| | | LocalDate now = LocalDate.now(); |
| | | DateTimeFormatter displayDtf = DateTimeFormatter.ofPattern("M/d"); |
| | |
| | | String startDate = now.minusDays(6).toString(); |
| | | String endDate = now.toString(); |
| | | |
| | | List<Map<String, Object>> inCounts = stockInventoryMapper.selectDailyStockInCounts(rootCategoryId, startDate, |
| | | List<Map<String, Object>> inCounts = stockInventoryMapper.selectDailyStockInCounts(modelType, startDate, |
| | | endDate + " 23:59:59"); |
| | | |
| | | List<Map<String, Object>> outCounts = stockInventoryMapper.selectDailyStockOutCounts(rootCategoryId, startDate, |
| | | List<Map<String, Object>> outCounts = stockInventoryMapper.selectDailyStockOutCounts(modelType, startDate, |
| | | endDate + " 23:59:59"); |
| | | |
| | | Map<LocalDate, BigDecimal> inMap = inCounts.stream() |
| | |
| | | |
| | | int selectStorageProductCountByDate(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate); |
| | | |
| | | List<Map<String, Object>> selectDailyStockInCounts(@Param("rootCategoryId") Long rootCategoryId, @Param("startDate") String startDate, @Param("endDate") String endDate); |
| | | List<Map<String, Object>> selectDailyStockInCounts(@Param("modelType") Integer modelType, @Param("startDate") String startDate, @Param("endDate") String endDate); |
| | | |
| | | List<Map<String, Object>> selectDailyStockOutCounts(@Param("rootCategoryId") Long rootCategoryId, @Param("startDate") String startDate, @Param("endDate") String endDate); |
| | | List<Map<String, Object>> selectDailyStockOutCounts(@Param("modelType") Integer modelType, @Param("startDate") String startDate, @Param("endDate") String endDate); |
| | | |
| | | BigDecimal selectTotalByDate(@Param("now") LocalDate now); |
| | | |
| | |
| | | </select> |
| | | |
| | | <select id="selectRawMaterialPurchaseAnalysis" resultType="java.util.Map"> |
| | | WITH RECURSIVE product_tree AS (SELECT id |
| | | FROM product |
| | | WHERE parent_id IS NULL |
| | | AND product_name LIKE '%原材料%' |
| | | UNION ALL |
| | | SELECT p.id |
| | | FROM product p |
| | | INNER JOIN product_tree pt ON p.parent_id = pt.id) |
| | | SELECT |
| | | pr.product_name AS name, |
| | | SUM( slp.tax_inclusive_total_price ) AS value |
| | |
| | | JOIN product pr ON slp.product_id = pr.id |
| | | WHERE |
| | | slp.type = 2 |
| | | AND pr.parent_id = ( SELECT id FROM product WHERE product_name = '原材料' ) |
| | | AND pr.id IN ( SELECT id FROM product_tree ) |
| | | GROUP BY |
| | | pr.id, |
| | | pr.product_name |
| | |
| | | |
| | | <select id="selectProductCountByTypeAndDate" resultType="int"> |
| | | SELECT IFNULL(COUNT(*), 0) |
| | | FROM sales_ledger_product |
| | | WHERE type = #{type} |
| | | FROM sales_ledger_product slp |
| | | LEFT JOIN sales_ledger sl |
| | | ON slp.type = 1 AND sl.id = slp.sales_ledger_id |
| | | LEFT JOIN purchase_ledger pl |
| | | ON slp.type = 2 AND pl.id = slp.sales_ledger_id |
| | | WHERE slp.type = #{type} |
| | | <if test="startDate != null"> |
| | | AND register_date >= #{startDate} |
| | | AND IFNULL(IF(slp.type = 1, sl.entry_date, pl.entry_date), slp.register_date) >= #{startDate} |
| | | </if> |
| | | <if test="endDate != null"> |
| | | AND register_date <= #{endDate} |
| | | AND IFNULL(IF(slp.type = 1, sl.entry_date, pl.entry_date), slp.register_date) <= #{endDate} |
| | | </if> |
| | | </select> |
| | | |
| | | <select id="selectRawMaterialExpense" resultType="java.math.BigDecimal"> |
| | | WITH RECURSIVE product_tree AS (SELECT id |
| | | FROM product |
| | | WHERE product_name = '原材料' |
| | | WHERE parent_id IS NULL |
| | | AND product_name LIKE '%原材料%' |
| | | |
| | | UNION ALL |
| | | |
| | |
| | | AND create_time <= #{endDate}) AS combined_counts |
| | | </select> |
| | | |
| | | <!-- 产品库根节点分类:0-原材料 1-半成品 2-成品(先匹配半成品,避免被成品包含) --> |
| | | <sql id="productTypeTree"> |
| | | WITH RECURSIVE product_tree AS (SELECT id |
| | | FROM product |
| | | WHERE parent_id IS NULL |
| | | AND ((#{modelType} = 0 AND product_name LIKE '%原材料%') |
| | | OR (#{modelType} = 1 AND product_name LIKE '%半成品%') |
| | | OR (#{modelType} = 2 AND product_name LIKE '%成品%' AND product_name NOT LIKE '%半成品%')) |
| | | UNION ALL |
| | | SELECT p.id |
| | | FROM product p |
| | | INNER JOIN product_tree pt ON p.parent_id = pt.id) |
| | | </sql> |
| | | |
| | | <select id="selectDailyStockInCounts" resultType="java.util.Map"> |
| | | <include refid="productTypeTree"/> |
| | | SELECT DATE(sir.create_time) AS date, |
| | | SUM(sir.stock_in_num) AS count |
| | | FROM stock_in_record sir |
| | | JOIN product_model pm ON sir.product_model_id = pm.id |
| | | JOIN product p ON pm.product_id = p.id |
| | | WHERE (p.parent_id = #{rootCategoryId} OR p.id = #{rootCategoryId}) |
| | | AND sir.create_time >= #{startDate} |
| | | JOIN product_tree pt ON pt.id = pm.product_id |
| | | WHERE sir.create_time >= #{startDate} |
| | | AND sir.create_time <= #{endDate} |
| | | GROUP BY DATE(sir.create_time) |
| | | ORDER BY DATE(sir.create_time) ASC |
| | | </select> |
| | | |
| | | <select id="selectDailyStockOutCounts" resultType="java.util.Map"> |
| | | <include refid="productTypeTree"/> |
| | | SELECT DATE(sor.create_time) AS date, |
| | | SUM(sor.stock_out_num) AS count |
| | | FROM stock_out_record sor |
| | | JOIN product_model pm ON sor.product_model_id = pm.id |
| | | JOIN product p ON pm.product_id = p.id |
| | | WHERE (p.parent_id = #{rootCategoryId} OR p.id = #{rootCategoryId}) |
| | | AND sor.create_time >= #{startDate} |
| | | JOIN product_tree pt ON pt.id = pm.product_id |
| | | WHERE sor.create_time >= #{startDate} |
| | | AND sor.create_time <= #{endDate} |
| | | GROUP BY DATE(sor.create_time) |
| | | ORDER BY DATE(sor.create_time) ASC |