<?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 >= #{dto.startDate}
|
</if>
|
<if test="dto.endDate != null">
|
AND ppm.reporting_time < #{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>
|