package cn.flightfeather.supervision.business.crosstimechange
|
|
import cn.flightfeather.supervision.common.utils.DateUtil
|
import cn.flightfeather.supervision.common.utils.ExcelUtil
|
import cn.flightfeather.supervision.domain.ds1.entity.Problemlist
|
import cn.flightfeather.supervision.domain.ds1.repository.ProblemRep
|
import cn.flightfeather.supervision.domain.ds1.repository.SubTaskRep
|
import cn.flightfeather.supervision.lightshare.vo.AreaVo
|
import org.springframework.stereotype.Component
|
import java.io.File
|
import java.io.FileOutputStream
|
import java.time.format.DateTimeFormatter
|
|
/**
|
* 跨时间跨月度整改
|
* @date 2024/12/20
|
* @author feiyu02
|
*/
|
@Component
|
class CrossTimeChangeManager(private val subTaskRep: SubTaskRep, private val problemRep: ProblemRep) {
|
|
/**
|
* 执行跨月度跨时间整改分析
|
* @param areaVo 筛选条件,包括区域、场景类型、时间范围
|
*/
|
fun execute(areaVo: AreaVo, fileName: String) {
|
val pbGroupList = searchProblem(areaVo)
|
pbGroupList.forEach { changeAnalysis(it) }
|
formatToExcel(areaVo, pbGroupList, fileName)
|
}
|
|
/**
|
* 查询范围内的场景及各月度问题
|
*/
|
fun searchProblem(areaVo: AreaVo): List<ScenePbGroup> {
|
// 获取范围内所有的巡查记录及具体问题
|
val summary = subTaskRep.findSummary(areaVo)
|
// 按照场景进行归类
|
val sceneMap = mutableMapOf<String?, ScenePbGroup>()
|
summary.forEach {
|
if (!sceneMap.containsKey(it.sceneId)) {
|
sceneMap[it.sceneId] = ScenePbGroup().apply {
|
scene = it.scene
|
}
|
}
|
sceneMap[it.sceneId]?.apply {
|
pbGroup.add(ProblemInfo().apply {
|
subtask = it.subtask
|
val pList = problemRep.find(Problemlist().apply {
|
stguid = it.stGuid
|
})
|
pbList.addAll(pList)
|
})
|
}
|
}
|
// 将归类好的各场景问题按照巡查时间升序排列并转换为数组结构
|
val res = mutableListOf<ScenePbGroup>()
|
sceneMap.forEach { (t, u) ->
|
u.pbGroup.sortBy { it?.subtask?.planstarttime }
|
res.add(u)
|
}
|
// 将场景按照唯一编号升序排列(非必要)
|
res.sortBy { it.scene?.index }
|
|
return res
|
}
|
|
/**
|
* 分析单个场景各月问题是否可以整改
|
*/
|
fun changeAnalysis(scenePbGroup: ScenePbGroup) {
|
scenePbGroup.pbGroup.forEachIndexed {i,it ->
|
it?.pbList?.forEach { p ->
|
// 已整改的问题略过
|
if (p?.ischanged == true) return@forEach
|
|
// 找到结果(指有一个月未出现此问题,或者出现了此问题但已整改),则允许将此历史问题作为已整改
|
var found = false
|
|
// 向后续月度查找是否有相同的问题出现
|
var index = i + 1
|
while (!found && index < scenePbGroup.pbGroup.size) {
|
val nextGroup = scenePbGroup.pbGroup[index]
|
val result = nextGroup?.pbList?.find { nP ->
|
nP?.ptguid == p?.ptguid
|
}
|
// 该月未出现此问题,或者出现了此问题但已整改
|
if (result == null || result.ischanged == true) {
|
found = true
|
}
|
index++
|
}
|
|
// 若该问题符合整改条件,则添加至可整改列表
|
if (found) {
|
it.pbChangeList.add(p)
|
}
|
}
|
}
|
}
|
|
/**
|
* 格式化输出至excel文件
|
*/
|
fun formatToExcel(areaVo: AreaVo, pbGroupList: List<ScenePbGroup>, fileName: String) {
|
if (pbGroupList.isEmpty()) return
|
|
val h = mutableListOf<MutableList<Any>>()
|
val c = mutableListOf<MutableList<Any>>()
|
|
// 生成表头
|
val h1 = mutableListOf<Any>("唯一编号", "场景")
|
val monHead = mutableListOf<String>()
|
var time = areaVo.starttime?.toLocalDate()
|
val end = areaVo.endtime?.toLocalDate()
|
// 此处开始时间应为月初,结束时间应为月末,则可以直接用isBefore比较
|
while (time?.isBefore(end) == true) {
|
monHead.add(time.format(DateTimeFormatter.ofPattern("YYYY-MM")))
|
time = time.plusMonths(1)
|
}
|
monHead.forEach {
|
val str = it.split("-")[1] + "月"
|
h1.addAll(listOf(str + "问题", str + "未整改", str + "未整改数"))
|
}
|
monHead.forEach {
|
val str = it.split("-")[1] + "月"
|
h1.addAll(listOf(str + "可整改", str + "原整改数", str + "新增可整改数"))
|
}
|
h.add(h1)
|
|
// 生成内容
|
pbGroupList.forEach {pbg ->
|
val index = pbg.scene?.index ?: ""
|
val name = pbg.scene?.name ?: ""
|
|
val problemContent = mutableListOf<Any>()
|
monHead.forEach {mH ->
|
val stp = pbg.pbGroup.find { pbi ->
|
DateUtil.DateToString(pbi?.subtask?.planstarttime, DateUtil.DateStyle.YYYY_MM) == mH
|
}
|
|
if (stp == null) {
|
problemContent.addAll(listOf("/", "/", "/"))
|
} else {
|
// 问题
|
problemContent.add(stp.pbList.mapIndexed { i, pb -> "${i+1}、${pb?.problemname}" }.joinToString
|
("\n"))
|
val unchanged = stp.pbList.filter { pb-> pb?.ischanged != true }
|
// 未整改
|
problemContent.add(unchanged.mapIndexed { i, pb -> "${i+1}、${pb?.problemname}" }.joinToString("\n"))
|
// 未整改数
|
problemContent.add(unchanged.size)
|
}
|
}
|
|
val willChangeContent = mutableListOf<Any>()
|
monHead.forEach {mH ->
|
val stp = pbg.pbGroup.find { pbi ->
|
DateUtil.DateToString(pbi?.subtask?.planstarttime, DateUtil.DateStyle.YYYY_MM) == mH
|
}
|
|
if (stp == null) {
|
willChangeContent.addAll(listOf("/", "/", "/"))
|
} else {
|
// 可整改
|
willChangeContent.add(stp.pbChangeList.mapIndexed { i, pb -> "${i+1}、${pb?.problemname}" }
|
.joinToString("\n"))
|
val changed = stp.pbList.filter { pb-> pb?.ischanged == true }
|
// 原整改数
|
willChangeContent.add(changed.size)
|
// 新增可整改数
|
willChangeContent.add(stp.pbChangeList.size)
|
}
|
}
|
|
val row = mutableListOf<Any>()
|
row.add(index)
|
row.add(name)
|
row.addAll(problemContent)
|
row.addAll(willChangeContent)
|
|
c.add(row)
|
}
|
|
// 生成文件
|
val file = File("target/${fileName}")
|
val out = FileOutputStream(file)
|
ExcelUtil.write2(out, h.map { it.toTypedArray() }, c.map { it.toTypedArray() }.toMutableList())
|
}
|
}
|