feiyu02
2025-09-12 dc4f12f66685260ac357997680e5f3fe723c3c4a
src/main/kotlin/cn/flightfeather/supervision/lightshare/service/impl/SearchServiceImpl.kt
@@ -1,14 +1,19 @@
package cn.flightfeather.supervision.lightshare.service.impl
import cn.flightfeather.supervision.business.bgtask.ReportTaskCtrl
import cn.flightfeather.supervision.business.report.DataSource
import cn.flightfeather.supervision.business.report.DbMapper
import cn.flightfeather.supervision.business.report.file.ReportOne
import cn.flightfeather.supervision.business.report.file.ReportThree
import cn.flightfeather.supervision.business.report.file.ReportTwo
import cn.flightfeather.supervision.business.report.template.*
import cn.flightfeather.supervision.common.exception.BizException
import cn.flightfeather.supervision.common.utils.*
import cn.flightfeather.supervision.domain.ds1.entity.*
import cn.flightfeather.supervision.domain.ds1.mapper.*
import cn.flightfeather.supervision.domain.ds1.repository.EvaluationRep
import cn.flightfeather.supervision.domain.ds1.repository.EvaluationRuleRep
import cn.flightfeather.supervision.domain.ds1.repository.SceneRep
import cn.flightfeather.supervision.domain.ds2.mapper.LedgerRecordMapper
import cn.flightfeather.supervision.domain.ds2.mapper.LedgerSubTypeMapper
import cn.flightfeather.supervision.domain.ds2.mapper.UserMapMapper
@@ -17,14 +22,13 @@
import cn.flightfeather.supervision.lightshare.vo.*
import com.github.pagehelper.PageHelper
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import tk.mybatis.mapper.entity.Example
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.OutputStream
import java.net.URLEncoder
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.util.*
import javax.servlet.http.HttpServletResponse
@@ -35,80 +39,39 @@
 */
@Service
class SearchServiceImpl(
        val userinfoMapper: UserinfoMapper,
        val subtaskMapper: SubtaskMapper,
        val scenseMapper: ScenseMapper,
        val sceneConstructionSiteMapper: SceneConstructionSiteMapper,
        val sceneMixingPlantMapper: SceneMixingPlantMapper,
        val sceneStorageYardMapper: SceneStorageYardMapper,
        val sceneWharfMapper: SceneWharfMapper,
        val problemlistMapper: ProblemlistMapper,
        val problemtypeMapper: ProblemtypeMapper,
        val townMapper: TownMapper,
        val mediafileMapper: MediafileMapper,
        val scoreMapper: ScoreMapper,
        val inspectionMapper: InspectionMapper,
        val taskMapper: TaskMapper,
        val monitorobjectversionMapper: MonitorobjectversionMapper,
        val evaluationruleMapper: EvaluationruleMapper,
        val evaluationsubruleMapper: EvaluationsubruleMapper2,
        val evaluationMapper: EvaluationMapper,
        val itemevaluationMapper: ItemevaluationMapper,
        val ledgerSubTypeMapper: LedgerSubTypeMapper,
        val ledgerRecordMapper: LedgerRecordMapper,
        val userMapMapper: UserMapMapper,
        val taskService: TaskService
    val userinfoMapper: UserinfoMapper,
    val subtaskMapper: SubtaskMapper,
    val scenseMapper: ScenseMapper,
    val sceneConstructionSiteMapper: SceneConstructionSiteMapper,
    val sceneMixingPlantMapper: SceneMixingPlantMapper,
    val sceneStorageYardMapper: SceneStorageYardMapper,
    val sceneWharfMapper: SceneWharfMapper,
    val problemlistMapper: ProblemlistMapper,
    val problemtypeMapper: ProblemtypeMapper,
    val townMapper: TownMapper,
    val mediafileMapper: MediafileMapper,
    val scoreMapper: ScoreMapper,
    val inspectionMapper: InspectionMapper,
    val taskMapper: TaskMapper,
    val monitorobjectversionMapper: MonitorobjectversionMapper,
    val evaluationruleMapper: EvaluationruleMapper,
    val evaluationsubruleMapper: EvaluationsubruleMapper2,
    val evaluationMapper: EvaluationMapper,
    val itemevaluationMapper: ItemevaluationMapper,
    val ledgerSubTypeMapper: LedgerSubTypeMapper,
    val ledgerRecordMapper: LedgerRecordMapper,
    val userMapMapper: UserMapMapper,
    val taskService: TaskService,
    private val evaluationRep: EvaluationRep,
    private val evaluationRuleRep: EvaluationRuleRep,
    private val sceneRep: SceneRep,
    @Value("\${filePath}") var filePath: String,
    @Value("\${imgPath}") var imgPath: String,
    private val dbMapper: DbMapper,
    private val reportTaskCtrl: ReportTaskCtrl,
) : SearchService {
    private val dateUtil = DateUtil()
    override fun writeToFile(config: ExcelConfigVo, mode:Int) {
        val dbMapper = DbMapper(
            scenseMapper,
            problemlistMapper,
            problemtypeMapper,
            subtaskMapper,
            monitorobjectversionMapper,
            sceneConstructionSiteMapper,
            sceneMixingPlantMapper,
            sceneStorageYardMapper,
            sceneWharfMapper,
            taskMapper,
            evaluationruleMapper, evaluationsubruleMapper, evaluationMapper, itemevaluationMapper,
                ledgerSubTypeMapper, ledgerRecordMapper, userinfoMapper, userMapMapper, townMapper
        )
        val dataSource = DataSource(config, dbMapper)
        val t = when (mode) {
//            //问题与整改跟踪汇总表
//            0 -> ProDetailSummary(dataSource)
//            //分街镇问题整改分析汇总表
//            1 -> ProAnalysisSummary(dataSource)
//
//            //规范性评估详情表
//            2 -> ScoreDetailSummary(dataSource)
//            //分街镇规范性分析表
//            3 -> ScoreAnalysisSummary(dataSource)
//
//            //问题与整改分类统计表
//            4 -> ProTypeDetailSummary(dataSource)
//            //问题与整改分类排名
//            5 -> ProTypeRankSummary(dataSource)
//            //月度主要或典型问题分析表
//            6 -> ProTypeRankMainSummary(dataSource)
//            //工地施工阶段问题分类分析表
//            7 -> ProTypeStatusSummary(dataSource)
            8 -> ReportOne(dataSource)
            9 -> ReportTwo(dataSource)
            10 -> ReportThree(dataSource)
            else -> null
        }
//        t?.execute()
        t?.toFile("target/")
    }
    override fun getExcel(config: ExcelConfigVo, response: HttpServletResponse): Boolean {
    override fun writeToFile(config: ExcelConfigVo, mode: Int) {
        val dbMapper = DbMapper(
            scenseMapper,
            problemlistMapper,
@@ -124,6 +87,19 @@
            ledgerSubTypeMapper, ledgerRecordMapper, userinfoMapper, userMapMapper, townMapper
        )
        val dataSource = DataSource(config, dbMapper)
        val t = when (mode) {
            8 -> ReportOne(dataSource)
            9 -> ReportTwo(dataSource)
            10 -> ReportThree(dataSource)
            else -> null
        }
//        t?.execute()
        t?.toFile("target/")
    }
    override fun getExcel(config: ExcelConfigVo, response: HttpServletResponse): Boolean {
        val dataSource = DataSource(config, dbMapper)
        val t = when (config.mode) {
            1 -> ReportOne(dataSource)
            2 -> ReportTwo(dataSource)
@@ -132,25 +108,26 @@
            else -> ReportOne(dataSource)
        }
        val fileName = t.getReportName()
        val fName = URLEncoder.encode(fileName, "UTF-8")
        response.apply {
            setHeader("Content-Disposition", "attachment;filename=$fName")
            setHeader("fileName", fName)
            addHeader("Access-Control-Expose-Headers", "fileName")
            contentType = "application/vnd.ms-excel;charset=UTF-8"
            setHeader("Pragma", "no-cache")
            setHeader("Cache-Control", "no-cache")
            setDateHeader("Expires", 0)
        }
        val p = Constant.DEFAULT_FILE_PATH + "/files/autoscore/"
        val p = "$filePath/autoscore/"
        val file = File(p + fileName)
        if (config.forceUpdate || !file.exists()) {
            t.toFile(p)
            val downloadUrl = "/autoscore/${fileName}"
            reportTaskCtrl.startTask(t, downloadUrl)
            return false
        } else {
            val fName = Base64.getEncoder().encodeToString(fileName.toByteArray())
            response.apply {
                setHeader("Content-Disposition", "attachment;filename=$fName")
                setHeader("fileName", fName)
                addHeader("Access-Control-Expose-Headers", "fileName")
                contentType = "application/vnd.ms-excel;charset=UTF-8"
                setHeader("Pragma", "no-cache")
                setHeader("Cache-Control", "no-cache")
                setDateHeader("Expires", 0)
            }
            response.outputStream.write(file.readBytes())
            return true
        }
        response.outputStream.write(file.readBytes())
        return true
    }
    override fun getSubTaskDetail(config: ExcelConfigVo): SubTaskTableVo {
@@ -269,8 +246,9 @@
        }
        //建立第一层目录,包含所有的任务
        val time = dateUtil.DateToString(Date(), "yyyy-MM-dd_HH-mm-ss")
        var basePath = Constant.DEFAULT_FILE_PATH + File.separator + "images" + File.separator + "temp" + File.separator + time
        val time = DateUtil.DateToString(Date(), "yyyy-MM-dd_HH-mm-ss")
        var basePath =
            imgPath + File.separator + "temp" + File.separator + time
        var file = File(basePath)
        var i = 1
        while (file.exists() && i <= 100) {
@@ -321,7 +299,8 @@
                mediafileMapper.selectByExample(Example(Mediafile::class.java).apply {
                    createCriteria().andEqualTo("businessguid", p.guid)
                }).forEach { m ->
                    val picPath = Constant.DEFAULT_FILE_PATH + File.separator + "images" + File.separator + m.extension1 + m.guid + ".jpg"
                    val picPath =
                        imgPath + File.separator + m.extension1 + m.guid + ".jpg"
                    val fromFile = File(picPath)
                    val picName = p.problemname + "_" + p.location + "($y).jpg"
                    val toFile = File(pPath + File.separator + picName)
@@ -360,8 +339,9 @@
    override fun downloadPic2(sceneType: Int, topTaskId: String, response: HttpServletResponse): HttpServletResponse {
        //建立第一层目录,包含所有的任务
        val topTask = taskMapper.selectByPrimaryKey(topTaskId)
        val time = dateUtil.DateToString(Date(), "yyyy-MM-dd_HH-mm-ss")
        val basePath = Constant.DEFAULT_FILE_PATH + File.separator + "images" + File.separator + "temp" + File.separator + topTask.name
        val time = DateUtil.DateToString(Date(), "yyyy-MM-dd_HH-mm-ss")
        val basePath =
            imgPath + File.separator + "temp" + File.separator + topTask.name
        val file = File(basePath)
        if (!file.exists()) {
            file.mkdirs()
@@ -380,7 +360,7 @@
                //建立一个子任务文件夹
                var subTaskFilePath = "${basePath}${File.separator}(${
                    dateUtil.DateToString(
                    DateUtil.DateToString(
                        it.planstarttime,
                        DateUtil.DateStyle.YYYY_MM_DD_CN
                    )
@@ -430,7 +410,7 @@
                        u.forEach { f ->
                            val picPath =
                                Constant.DEFAULT_FILE_PATH + File.separator + "images" + File.separator + f.extension1 + f.guid + ".jpg"
                                imgPath + File.separator + f.extension1 + f.guid + ".jpg"
                            val fromFile = File(picPath)
                            val picName = f.description
                            val toFile = File(pPath + File.separator + picName)
@@ -470,7 +450,13 @@
        return response
    }
    override fun searchScore4JingAn(token: String, year: Int, month: Int, page: Int?, perPage: Int?): BaseResponse<List<ScoreVo>> {
    override fun searchScore4JingAn(
        token: String,
        year: Int,
        month: Int,
        page: Int?,
        perPage: Int?,
    ): BaseResponse<List<ScoreVo>> {
        if (token != "jingan") {
            return BaseResponse(false)
        }
@@ -493,7 +479,12 @@
        return BaseResponse(true, head = DataHead(p.pageNum, p.pages), data = result)
    }
    override fun searchSubTaskByKeyword(userId: String, keyword: String, page: Int, perPage: Int): BaseResponse<List<SubtaskVo>> {
    override fun searchSubTaskByKeyword(
        userId: String,
        keyword: String,
        page: Int,
        perPage: Int,
    ): BaseResponse<List<SubtaskVo>> {
        val userInfo = userinfoMapper.selectByPrimaryKey(userId)
        if (userInfo.usertypeid?.toInt() == 3) return BaseResponse(false, "企业用户无查询权限")//企业用户无法查询
        val result = mutableListOf<SubtaskVo>()
@@ -535,10 +526,12 @@
            evaluationruleMapper, evaluationsubruleMapper, evaluationMapper, itemevaluationMapper,
            ledgerSubTypeMapper, ledgerRecordMapper, userinfoMapper, userMapMapper, townMapper
        )
        val task = taskService.getByDistrictCode(config.districtCode, config.startTime)?.takeIf { it.isNotEmpty() }?.get(0)
        val task =
            taskService.getByDistrictCode(config.districtCode, config.startTime).takeIf { it.isNotEmpty() }?.get(0)
        config.topTaskGuid = task?.tguid ?: ""
        config.allScene = false
        val dataSource = mutableListOf<DataSource>()
        config.sceneType = Constant.ScenseType.TYPE1.value.toInt()
//        config.sceneType = Constant.SceneType.TYPE1.value.toInt()
        dataSource.add(DataSource(config, dbMapper))
//        val config2 = config.copy(sceneType = Constant.ScenseType.TYPE2.value.toInt())
@@ -556,4 +549,30 @@
        return BaseTableVo(result.first, result.second)
    }
    override fun getScoreDetail(subTaskId: String): ScoreDetail {
        //场景信息
        val scene = sceneRep.findBySubTask(subTaskId)
        //总分
        val evaluation = evaluationRep.findBySubtask(subTaskId)
        //子规则逐条得分
        val subRuleScores = evaluationRep.findItemEvaluation(subTaskId)
        if (subRuleScores.isEmpty()) throw BizException("无评估记录")
        //评分总规则
        val rule = evaluationRuleRep.findAutoEvaluationRule(subTaskId) ?: throw BizException("未找到相关自动评估规则")
        //总规则对应的评分子规则
        val subRules = evaluationRuleRep.findSubRule(rule.guid)
        //查询结果
        val result = ScoreDetail()
        result.status = if (scene?.extension1.equals("1")) "在建" else "完工"
        result.updateTime = evaluation?.updatedate
        subRules.forEach {
            it.ertype ?: return@forEach
            result.addDetail(result.details, it, it.ertype!! - 1, subRuleScores, true)
        }
        result.calScore()
        return result
    }
}