yn_digital_gadgets_system/bonus-business/src/main/resources/mapper/MonthPlanMapper.xml

530 lines
28 KiB
XML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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.bonus.digital.mapper.MonthlyPlanMapper">
<insert id="addMonthlyPlanList" useGeneratedKeys="true" keyProperty="monthlyPlanId">
insert into tb_monthly_plan (monthly_plan,plan_management_id,inspection_station_id,inspection_station_name,plan_major_id,business_type_id,
project_name,work_content,plan_category_id,tower_base_number,planned_start_time,
planned_end_time,plan_personnel,plan_car_num,plan_skilled_worker_num,plan_auxiliary_worker_num,
plan_sub_car_num,create_user,create_time,actual_working_day,risk_level)
values (#{monthlyPlan},#{planManagementId},#{inspectionStationId},#{inspectionStationName},#{planMajorId},#{businessTypeId},
#{projectName},#{workContent},#{planCategoryId},#{towerBaseNumber},#{plannedStartTime},#{plannedEndTime},
#{planPersonnel},#{planCarNum},#{planSkilledWorkerNum},#{planAuxiliaryWorkerNum},#{planSubCarNum},
#{createUser},#{createTime},#{actualWorkingDay},#{riskLevel})
</insert>
<insert id="addPersonnelArrangement">
insert into tb_personnel_arrangement (monthly_plan_id,day,personnel_names)
values (#{monthlyPlanId},#{day},#{personnelNames})
</insert>
<insert id="addWorkload">
insert into tb_workload (plan_id,workload_category_id,workload_category_name,unit_price,workload_num,data_source)
values (#{planId},#{workloadCategoryId},#{workloadCategoryName},#{unitPrice},#{workloadNum},#{dataSource})
</insert>
<update id="updateMonthlyPlan">
update tb_monthly_plan
<trim prefix="set" suffixOverrides=",">
<if test="monthlyPlan!= null " >
monthly_plan=#{monthlyPlan},
</if>
<if test="inspectionStationId!= null " >
inspection_station_id=#{inspectionStationId},
</if>
<if test="inspectionStationName!= null " >
inspection_station_name=#{inspectionStationName},
</if>
<if test="planMajorId!= null " >
plan_major_id=#{planMajorId},
</if>
<if test="businessTypeId!= null " >
business_type_id=#{businessTypeId},
</if>
<if test="projectName!= null " >
project_name=#{projectName},
</if>
<if test="workContent!= null " >
work_content=#{workContent},
</if>
<if test="planCategoryId!= null " >
plan_category_id=#{planCategoryId},
</if>
<if test="towerBaseNumber!= null " >
tower_base_number=#{towerBaseNumber},
</if>
<if test="plannedStartTime!= null " >
planned_start_time=#{plannedStartTime},
</if>
<if test="plannedEndTime!= null " >
planned_end_time=#{plannedEndTime},
</if>
<if test="planPersonnel!= null " >
plan_personnel=#{planPersonnel},
</if>
<if test="planCarNum!= null " >
plan_car_num=#{planCarNum},
</if>
<if test="planSkilledWorkerNum!= null " >
plan_skilled_worker_num=#{planSkilledWorkerNum},
</if>
<if test="planSkilledWorkerDay!= null " >
plan_skilled_worker_day=#{planSkilledWorkerDay},
</if>
<if test="planAuxiliaryWorkerNum!= null " >
plan_auxiliary_worker_num=#{planAuxiliaryWorkerNum},
</if>
<if test="planAuxiliaryWorkerDay!= null " >
plan_auxiliary_worker_day=#{planAuxiliaryWorkerDay},
</if>
<if test="planSubCarNum!= null " >
plan_sub_car_num=#{planSubCarNum},
</if>
<if test="updateTime!= null " >
update_time=#{updateTime},
</if>
<if test="updateUser!= null " >
update_user=#{updateUser},
</if>
<if test="actualWorkingDay!= null " >
actual_working_day=#{actualWorkingDay},
</if>
<if test="riskLevel!= null " >
risk_level=#{riskLevel},
</if>
</trim>
where monthly_plan_id = #{monthlyPlanId}
</update>
<delete id="delMonthlyPlanList">
update tb_monthly_plan set is_active ='0' where monthly_plan_id = #{monthlyPlanId}
</delete>
<delete id="delPersonnelArrangement">
delete from tb_personnel_arrangement where monthly_plan_id = #{monthlyPlanId}
</delete>
<delete id="delWorkload">
delete from tb_workload where plan_id= #{monthlyPlanId}
</delete>
<select id="getPlanMajorList" resultType="com.bonus.digital.dao.MonthlyPlanVo">
select tmp.monthly_plan_id,
tmp.monthly_plan,
tmp.inspection_station_id,
tmp.inspection_station_name,
tmp.plan_major_id,
tpm.plan_major_name as planMajorName,
tmp.business_type_id,
tpm2.plan_major_name as businessTypeName,
tmp.project_name,
tmp.work_content,
tmp.plan_category_id,
tpm3.plan_major_name as planCategoryName,
tmp.tower_base_number,
tmp.planned_start_time,
tmp.planned_end_time,
tmp.plan_personnel,
tmp.plan_car_num,
tmp.plan_skilled_worker_num,
tmp.plan_skilled_worker_day,
tmp.plan_auxiliary_worker_num,
tmp.plan_auxiliary_worker_day,
tmp.plan_sub_car_num,
tmp.create_user,
tmp.create_time,
tmp.risk_level,
tmp.actual_working_day
from tb_monthly_plan tmp
left join tb_plan_major tpm on tmp.plan_major_id = tpm.plan_major_id
left join tb_plan_major tpm2 on tmp.business_type_id = tpm2.plan_major_id
left join tb_plan_major tpm3 on tmp.plan_category_id = tpm3.plan_major_id
where tmp.is_active = '1'
<if test="inspectionStationId != null and inspectionStationId != ''">
AND tmp.inspection_station_id = #{inspectionStationId}
</if>
<if test="monthlyPlan != null and monthlyPlan != ''">
AND tmp.monthly_plan = #{monthlyPlan}
</if>
<if test="planMajorId != null and planMajorId != ''">
AND tmp.plan_major_id = #{planMajorId}
</if>
<if test="businessTypeId != null and businessTypeId != ''">
AND tmp.business_type_id = #{businessTypeId}
</if>
<if test="riskLevel != null and riskLevel != ''">
AND tmp.risk_level = #{riskLevel}
</if>
<if test="keyWord!= null " >
AND (tmp.project_name like concat('%', #{keyWord}, '%')
or tmp.work_content like concat('%', #{keyWord}, '%'))
</if>
</select>
<select id="getPersonnelArrangementList" resultType="com.bonus.digital.dao.PersonnelArrangementVo">
select personnel_arrangement_id,monthly_plan_id,day,personnel_names
from tb_personnel_arrangement where monthly_plan_id = #{monthlyPlanId}
</select>
<select id="getPlanMajorListByMonth" resultType="com.bonus.digital.dao.MonthlyPlanVo">
select tmp.inspection_station_name,
tmp.work_content,
tmp.planned_start_time,
tmp.planned_end_time,
tmp.plan_personnel
from tb_monthly_plan tmp
where tmp.is_active = '1' and tmp.monthly_plan = #{monthlyPlan}
</select>
<!-- 查询月度计划+人员安排表关联数据 -->
<select id="getPlanWithPersonArrangement" resultType="com.bonus.digital.dao.MonthlyPlanVo">
select
tmp.inspection_station_name,
tmp.work_content,
tmp.monthly_plan_id,
tpa.day as arrange_day,
tpa.personnel_names
from tb_monthly_plan tmp
left join tb_personnel_arrangement tpa on tmp.monthly_plan_id = tpa.monthly_plan_id
where tmp.is_active = '1'
and tmp.monthly_plan = #{monthlyPlan}
</select>
<select id="getWorkloadSummary" resultType="com.bonus.digital.dao.MonthlyPlanVo">
select
tmp.inspection_station_id,
tmp.inspection_station_name,
tw.workload_category_name,
IFNULL(tw.unit_price, 0) as unit_price,
IFNULL(SUM(tw.workload_num), 0) as total_workload,
IFNULL(SUM(tw.workload_num), 0) * IFNULL(tw.unit_price, 0) as total_amount
from tb_monthly_plan tmp
left join tb_workload tw
on tw.plan_id = tmp.monthly_plan_id
and tw.data_source = '0'
left join tb_plan_management pm
on pm.plan_management_id = tmp.plan_management_id
where tmp.is_active = '1'
<!-- 计划月份筛选 -->
<if test="monthlyPlan != null and monthlyPlan != ''">
AND tmp.monthly_plan = #{monthlyPlan}
</if>
GROUP BY
tmp.inspection_station_id,
tmp.inspection_station_name,
tw.workload_category_name,
tw.unit_price
ORDER BY tmp.inspection_station_name ASC;
</select>
<select id="getCarUseSummary" resultType="com.bonus.digital.dao.MonthlyPlanVo">
select
tmp.inspection_station_id,
tmp.inspection_station_name,
-- 汇总管理用车天数空则补0
IFNULL(SUM(tmp.plan_car_num), 0) as total_manage_car_days,
-- 汇总分包用车天数空则补0
IFNULL(SUM(tmp.plan_sub_car_num), 0) as total_sub_car_days
from tb_monthly_plan tmp
-- 关联计划管理表
left join tb_plan_management pm
on pm.plan_management_id = tmp.plan_management_id
where tmp.is_active = '1'
<!-- 计划月份筛选 -->
<if test="monthlyPlan != null and monthlyPlan != ''">
AND tmp.monthly_plan = #{monthlyPlan}
</if>
GROUP BY
tmp.inspection_station_id,
tmp.inspection_station_name
ORDER BY tmp.inspection_station_name ASC;
</select>
<select id="getResourceSummary" resultType="com.bonus.digital.dao.ResourceSummaryVo">
<![CDATA[
-- 外层:基础信息+未安排+分包用工 + 动态类型聚合结果(字符串拼接格式)
SELECT
base_info.inspection_station_id,
base_info.inspection_station_name,
-- 基础字段
IFNULL(base_info.compile_num, 0) AS compile_num,
IFNULL(base_info.secondment_num, 0) AS secondment_num,
IFNULL(base_info.actual_station_num, 0) AS actual_station_num,
IFNULL(base_info.total_workday, 0) AS total_workday,
-- 未安排
IFNULL(base_info.unarrange_workday, 0) AS unarrange_workday,
ROUND(IFNULL(base_info.unarrange_workday / NULLIF(base_info.total_workday, 0), 0), 4) AS unarrange_ratio,
ROUND(IFNULL(base_info.unarrange_workday / NULLIF(base_info.actual_station_num, 0), 0), 3) AS unarrange_avg,
-- 分包用工
IFNULL(base_info.subcontract_workday, 0) AS subcontract_workday,
-- 动态类型数据(左关联动态类型聚合结果)
IFNULL(type_data.type_str, '') AS type_str
FROM (
-- 子查询1运检站基础信息+未安排+分包用工(逻辑优化,统一参数)
SELECT
ista_inner.inspection_station_id,
ista_inner.inspection_station_name,
-- 1. 人员基础数据null转0
IFNULL((SELECT COUNT(p.id)
FROM tb_personnel p
WHERE p.inspection_station_id = ista_inner.inspection_station_id
AND p.is_active = '1'), 0) AS compile_num,
IFNULL((SELECT COUNT(p.id)
FROM tb_personnel p
WHERE p.inspection_station_id = ista_inner.inspection_station_id
AND p.long_term_secondment = '1'
AND p.is_active = '1'), 0) AS secondment_num,
-- 实际在站人数(减法结果兜底)
IFNULL((SELECT COUNT(p.id)
FROM tb_personnel p
WHERE p.inspection_station_id = ista_inner.inspection_station_id
AND p.is_active = '1'), 0) -
IFNULL((SELECT COUNT(p.id)
FROM tb_personnel p
WHERE p.inspection_station_id = ista_inner.inspection_station_id
AND p.long_term_secondment = '1'
AND p.is_active = '1'), 0) AS actual_station_num,
-- 总工日(先计算当月天数,避免重复查询)
IFNULL(
(SELECT DATEDIFF(
LAST_DAY(STR_TO_DATE(CONCAT(#{monthlyPlan}, '-01'), '%Y-%m-%d')),
DATE_FORMAT(STR_TO_DATE(CONCAT(#{monthlyPlan}, '-01'), '%Y-%m-%d'), '%Y-%m-01')
) + 1),
0
) AS month_days,
-- 总工日=当月天数*实际在站人数
IFNULL(
(SELECT DATEDIFF(
LAST_DAY(STR_TO_DATE(CONCAT(#{monthlyPlan}, '-01'), '%Y-%m-%d')),
DATE_FORMAT(STR_TO_DATE(CONCAT(#{monthlyPlan}, '-01'), '%Y-%m-%d'), '%Y-%m-01')
) + 1),
0
) *
IFNULL((
IFNULL((SELECT COUNT(p.id)
FROM tb_personnel p
WHERE p.inspection_station_id = ista_inner.inspection_station_id
AND p.is_active = '1'), 0) -
IFNULL((SELECT COUNT(p.id)
FROM tb_personnel p
WHERE p.inspection_station_id = ista_inner.inspection_station_id
AND p.long_term_secondment = '1'
AND p.is_active = '1'), 0)
), 0) AS total_workday,
-- 未安排(核心:扣除所有有效类型工日,不硬编码类型)
IFNULL(
(
-- 总工日
IFNULL(
(SELECT DATEDIFF(
LAST_DAY(STR_TO_DATE(CONCAT(#{monthlyPlan}, '-01'), '%Y-%m-%d')),
DATE_FORMAT(STR_TO_DATE(CONCAT(#{monthlyPlan}, '-01'), '%Y-%m-%d'), '%Y-%m-01')
) + 1),
0
) *
IFNULL((
IFNULL((SELECT COUNT(p.id)
FROM tb_personnel p
WHERE p.inspection_station_id = ista_inner.inspection_station_id
AND p.is_active = '1'), 0) -
IFNULL((SELECT COUNT(p.id)
FROM tb_personnel p
WHERE p.inspection_station_id = ista_inner.inspection_station_id
AND p.long_term_secondment = '1'
AND p.is_active = '1'), 0)
), 0) -
-- 扣除所有有效业务类型的总工日
IFNULL((
SELECT IFNULL(SUM(
IFNULL(DATEDIFF(tmp.planned_end_time, tmp.planned_start_time) + 1, 0) *
IFNULL(
CASE
WHEN tmp.plan_personnel IS NULL OR tmp.plan_personnel = '' THEN 0
ELSE LENGTH(tmp.plan_personnel) - LENGTH(REPLACE(tmp.plan_personnel, ',', '')) + 1
END,
0
)
), 0)
FROM tb_monthly_plan tmp
LEFT JOIN tb_plan_major pm ON tmp.plan_major_id = pm.plan_major_id
WHERE tmp.inspection_station_id = ista_inner.inspection_station_id
AND tmp.monthly_plan = #{monthlyPlan}
AND pm.category = '0'
AND pm.plan_major_name != '分包用工'
), 0)
), 0) AS unarrange_workday,
-- 分包用工
IFNULL((SELECT IFNULL(SUM(tmp.plan_skilled_worker_day + tmp.plan_auxiliary_worker_day), 0)
FROM tb_monthly_plan tmp
WHERE tmp.inspection_station_id = ista_inner.inspection_station_id
AND tmp.monthly_plan = #{monthlyPlan}), 0) AS subcontract_workday
FROM tb_inspection_station ista_inner
WHERE ista_inner.category = '0'
AND ista_inner.is_active = '1'
) AS base_info
-- 左关联动态类型聚合结果
LEFT JOIN (
-- 子查询2动态类型聚合按运检站分组拼接字符串
SELECT
tg.inspection_station_id,
-- 聚合多个类型为字符串,|分隔类型,,分隔字段
GROUP_CONCAT(
CONCAT(
REPLACE(tg.plan_major_name, ',', ''), -- 转义逗号,避免分隔符冲突
',', tg.total_days,
',', tg.person_count,
',', tg.workday,
',', bi.total_workday, -- 关联基础信息获取总工日
',', bi.actual_station_num -- 关联基础信息获取实际在站人数
)
SEPARATOR '|' -- 类型之间的分隔符
) AS type_str
FROM (
-- 子查询2.1:按运检站+专业分组,汇总单个类型数据
SELECT
tmp.inspection_station_id,
pm.plan_major_name,
-- 总天数(汇总单个计划的天数)
SUM(IFNULL(DATEDIFF(tmp.planned_end_time, tmp.planned_start_time) + 1, 0)) AS total_days,
-- 总人数(汇总单个计划的参与人数)
SUM(
IFNULL(
CASE
WHEN tmp.plan_personnel IS NULL OR tmp.plan_personnel = '' THEN 0
ELSE LENGTH(tmp.plan_personnel) - LENGTH(REPLACE(tmp.plan_personnel, ',', '')) + 1
END,
0
)
) AS person_count,
-- 总工日(汇总单个计划的工日)
SUM(
IFNULL(DATEDIFF(tmp.planned_end_time, tmp.planned_start_time) + 1, 0) *
IFNULL(
CASE
WHEN tmp.plan_personnel IS NULL OR tmp.plan_personnel = '' THEN 0
ELSE LENGTH(tmp.plan_personnel) - LENGTH(REPLACE(tmp.plan_personnel, ',', '')) + 1
END,
0
)
) AS workday
FROM tb_monthly_plan tmp
LEFT JOIN tb_plan_major pm ON tmp.plan_major_id = pm.plan_major_id
WHERE tmp.monthly_plan = #{monthlyPlan}
AND pm.category = '0'
AND pm.plan_major_name != '分包用工'
-- 过滤工日为0的无效数据
AND IFNULL(
IFNULL(DATEDIFF(tmp.planned_end_time, tmp.planned_start_time) + 1, 0) *
IFNULL(
CASE
WHEN tmp.plan_personnel IS NULL OR tmp.plan_personnel = '' THEN 0
ELSE LENGTH(tmp.plan_personnel) - LENGTH(REPLACE(tmp.plan_personnel, ',', '')) + 1
END,
0
), 0) > 0
GROUP BY tmp.inspection_station_id, pm.plan_major_name
HAVING SUM(
IFNULL(DATEDIFF(tmp.planned_end_time, tmp.planned_start_time) + 1, 0) *
IFNULL(
CASE
WHEN tmp.plan_personnel IS NULL OR tmp.plan_personnel = '' THEN 0
ELSE LENGTH(tmp.plan_personnel) - LENGTH(REPLACE(tmp.plan_personnel, ',', '')) + 1
END,
0
)
) > 0
) AS tg
-- 关联基础信息表,获取总工日和实际在站人数
INNER JOIN (
-- 复用基础信息查询,避免重复计算
SELECT
ista_inner.inspection_station_id,
IFNULL((SELECT COUNT(p.id)
FROM tb_personnel p
WHERE p.inspection_station_id = ista_inner.inspection_station_id
AND p.is_active = '1'), 0) -
IFNULL((SELECT COUNT(p.id)
FROM tb_personnel p
WHERE p.inspection_station_id = ista_inner.inspection_station_id
AND p.long_term_secondment = '1'
AND p.is_active = '1'), 0) AS actual_station_num,
IFNULL(
(SELECT DATEDIFF(
LAST_DAY(STR_TO_DATE(CONCAT(#{monthlyPlan}, '-01'), '%Y-%m-%d')),
DATE_FORMAT(STR_TO_DATE(CONCAT(#{monthlyPlan}, '-01'), '%Y-%m-%d'), '%Y-%m-01')
) + 1),
0
) *
IFNULL((
IFNULL((SELECT COUNT(p.id)
FROM tb_personnel p
WHERE p.inspection_station_id = ista_inner.inspection_station_id
AND p.is_active = '1'), 0) -
IFNULL((SELECT COUNT(p.id)
FROM tb_personnel p
WHERE p.inspection_station_id = ista_inner.inspection_station_id
AND p.long_term_secondment = '1'
AND p.is_active = '1'), 0)
), 0) AS total_workday
FROM tb_inspection_station ista_inner
WHERE ista_inner.category = '0'
AND ista_inner.is_active = '1'
) AS bi ON tg.inspection_station_id = bi.inspection_station_id
-- 按运检站分组,聚合类型字符串
GROUP BY tg.inspection_station_id
) AS type_data ON base_info.inspection_station_id = type_data.inspection_station_id
-- 按运检站名称排序
ORDER BY base_info.inspection_station_name ASC;
]]>
</select>
<select id="getMonthlyPlanById" resultType="com.bonus.digital.dao.MonthlyPlanVo">
select tmp.monthly_plan_id,
tmp.monthly_plan,
tmp.inspection_station_id,
tmp.inspection_station_name,
tmp.plan_major_id,
tpm.plan_major_name as planMajorName,
tmp.business_type_id,
tpm2.plan_major_name as businessTypeName,
tmp.project_name,
tmp.work_content,
tmp.plan_category_id,
tpm3.plan_major_name as planCategoryName,
tmp.tower_base_number,
tmp.planned_start_time,
tmp.planned_end_time,
tmp.plan_personnel,
tmp.plan_car_num,
tmp.plan_skilled_worker_num,
tmp.plan_skilled_worker_day,
tmp.plan_auxiliary_worker_num,
tmp.plan_auxiliary_worker_day,
tmp.plan_sub_car_num,
tmp.create_user,
tmp.create_time,
tmp.risk_level,
tmp.actual_working_day
from tb_monthly_plan tmp
left join tb_plan_major tpm on tmp.plan_major_id = tpm.plan_major_id
left join tb_plan_major tpm2 on tmp.business_type_id = tpm2.plan_major_id
left join tb_plan_major tpm3 on tmp.plan_category_id = tpm3.plan_major_id
where tmp.monthly_plan_id = #{monthlyPlanId}
</select>
<select id="getDayPlanByMonthPlanId" resultType="com.bonus.digital.dao.DayPlanVo">
SELECT
tdp.day_plan_id AS dayPlanId,
tdp.monthly_plan_id AS monthlyPlanId,
tdp.day_plan AS dayPlan,
tdp.planned_workload AS plannedWorkload,
-- 原有拟投入作业人员姓名通过子查询拼接替代主表存储的ID串
(SELECT GROUP_CONCAT(tp.NAME SEPARATOR ',')
FROM tb_personnel tp
WHERE FIND_IN_SET(tp.id, tdp.proposed_personnel)
AND tp.is_active = '1') AS proposedPersonnel,
-- 原有:拟投入作业人员数量
IF(tdp.proposed_personnel IS NULL OR tdp.proposed_personnel = '', 0,
LENGTH(tdp.proposed_personnel) - LENGTH(REPLACE(tdp.proposed_personnel, ',', '')) + 1)
AS proposedPersonnelNumber,
tdp.proposed_proficient_personnel AS proposedProficientPersonnel,
tdp.proposed_proficient_day AS proposedProficientDay,
tdp.proposed_assistance_personnel AS proposedAssistancePersonnel
from tb_day_plan tdp where monthly_plan_id=#{monthlyPlanId} and is_active ='1'
</select>
</mapper>