gongchunyi
2 天以前 fe4d8530e45569a8b17886f4bd050e96e187fb8d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.production.mapper.ProductionCostMapper">
 
    <sql id="baseCostQuery">
        SELECT
        ppi.quantity,
        pos.unit_price,
        (ppi.quantity * IFNULL(pos.unit_price, 0)) as calculated_cost,
        ppi.unit as unit,
        ppm.product_order_id,
        ppm.reporting_time as log_date,
        po.nps_no as order_no,
        pm_mat.product_name as material_name,
        pms_mat.model as material_model,
        po.strength as strength,
        sdd_type.dict_label as category_label
        FROM production_product_input ppi
        JOIN production_product_main ppm ON ppi.product_main_id = ppm.id
        JOIN product_order po ON ppm.product_order_id = po.id
        LEFT JOIN production_order_route pr ON po.route_id = pr.id
        LEFT JOIN sys_dict_data sdd_type ON pr.dict_code = sdd_type.dict_code
        LEFT JOIN production_order_structure pos ON ppm.product_order_id = pos.order_id
        AND ppi.product_id = pos.product_model_id
        AND ppi.bom_id = pos.bom_id
        LEFT JOIN product_material_sku pms_mat ON ppi.product_id = pms_mat.id
        LEFT JOIN product_material pm_mat ON pms_mat.product_id = pm_mat.id
        <where>
            <if test="dto.startDate != null">
                AND ppm.reporting_time &gt;= #{dto.startDate}
            </if>
            <if test="dto.endDate != null">
                AND ppm.reporting_time &lt; #{dto.endDate}
            </if>
            <if test="dto.dictCode != null">
                AND pr.dict_code = #{dto.dictCode}
            </if>
            <if test="dto.productOrderId != null">
                AND ppm.product_order_id = #{dto.productOrderId}
            </if>
        </where>
    </sql>
 
    <select id="selectCostSummary" resultType="com.ruoyi.production.vo.ProductionCostSummaryVo">
        SELECT
        ROUND(IFNULL(SUM(calculated_cost), 0), 2) as totalCost,
        COUNT(DISTINCT product_order_id) as orderCount,
        CASE
        WHEN COUNT(DISTINCT product_order_id) > 0
        THEN ROUND(SUM(calculated_cost) / COUNT(DISTINCT product_order_id), 2)
        ELSE 0
        END as averageOrderCost
        FROM (
        <include refid="baseCostQuery"/>
        ) t
    </select>
 
    <select id="selectCostAggregationByCategory" resultType="com.ruoyi.production.vo.ProductionCostAggregationVo">
        SELECT
        log_date as date,
        IFNULL(material_name, '未知产品') as name,
        material_model as model,
        unit,
        ROUND(IFNULL(SUM(quantity), 0), 6) as quantity,
        ROUND(IFNULL(SUM(calculated_cost), 0), 2) as totalCost
        FROM (
        <include refid="baseCostQuery"/>
        ) t
        GROUP BY log_date, material_name, material_model, unit
    </select>
 
    <select id="selectCostAggregationByOrder" resultType="com.ruoyi.production.vo.ProductionCostAggregationVo">
        SELECT
        log_date as date,
        IFNULL(order_no, '未知单号') as name,
        material_name as model,
        category_label as strength,
        unit,
        ROUND(IFNULL(SUM(quantity), 0), 6) as quantity,
        ROUND(IFNULL(SUM(calculated_cost), 0), 2) as totalCost
        FROM (
        <include refid="baseCostQuery"/>
        ) t
        GROUP BY log_date, order_no, material_name, category_label, unit
    </select>
 
</mapper>