feiyu02
2024-08-15 196bb14112448857a885e32dc4149e308e00b01a
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cn.flightfeather.supervision.lightshare.service.Impl
 
import cn.flightfeather.supervision.common.risk.DataSource
import cn.flightfeather.supervision.common.risk.DbMapper
import cn.flightfeather.supervision.common.risk.RiskAnalysis
import cn.flightfeather.supervision.domain.entity.Evaluation
import cn.flightfeather.supervision.domain.entity.RiskEvaluation
import cn.flightfeather.supervision.domain.mapper.RiskEvaluationMapper
import cn.flightfeather.supervision.domain.mapper.UserinfoMapper
import cn.flightfeather.supervision.domain.repository.UserConfigRep
import cn.flightfeather.supervision.lightshare.service.RiskService
import cn.flightfeather.supervision.lightshare.vo.*
import com.github.pagehelper.PageHelper
import org.springframework.stereotype.Service
import tk.mybatis.mapper.entity.Example
import java.util.*
import javax.servlet.http.HttpServletResponse
 
@Service
class RiskServiceImpl(
    private val userinfoMapper: UserinfoMapper,
    private val riskEvaluationMapper: RiskEvaluationMapper,
    private val userConfigRep: UserConfigRep,
    private val dbMapper: DbMapper,
) : RiskService {
 
    override fun getRiskCount(userId: String, condition: UserSearchCondition): BaseResponse<CountVo> {
        if (condition.sceneTypes.size != 1) return BaseResponse(false, "只支持单个场景类型的查询")
 
        val config = userConfigRep.getUserConfigBySubType(userId)
        val condition2 = UserSearchCondition.fromUserConfig(config, condition)
 
        // 低风险
        var level0 = 0
        // 中风险
        var level1 = 0
        // 高风险
        var level2 = 0
        condition2.period = condition2.period
            ?: riskEvaluationMapper.getLatestPeriod(condition2)
            ?: return BaseResponse(false, "无记录")
 
        riskEvaluationMapper.getRiskCount(condition2).forEach {
            when (it.reRiskLevel?.toInt()) {
                0 -> level0++
                1 -> level1++
                2 -> level2++
            }
        }
        val result = CountVo().apply {
            tag = condition2.period
            countList.addAll(listOf(level0, level1, level2))
        }
 
        return BaseResponse(true, data = result)
    }
 
    override fun searchRiskList(
        userId: String,
        condition: UserSearchCondition,
        page: Int,
        perPage: Int,
    ): BaseResponse<List<CreditInfoVo>> {
        if (condition.period == null) return BaseResponse(false, "必须选择周期")
        if (condition.sceneTypes.size > 2) return BaseResponse(false, "场景类型只支持一种")
        val config = userConfigRep.getUserConfigBySubType(userId)
        val condition2 = UserSearchCondition.fromUserConfig(config, condition)
        val p = PageHelper.startPage<Evaluation>(page, perPage)
        val result = riskEvaluationMapper.searchRiskList(condition2)
        return BaseResponse(true, head = DataHead(p.pageNum, p.pages, p.total), data = result)
    }
 
    override fun getRiskInfo(userId: String, period: String?): BaseResponse<RiskEvaluation> {
        PageHelper.startPage<RiskEvaluation>(1, 1)
        val result = riskEvaluationMapper.selectByExample(Example(RiskEvaluation::class.java).apply {
            createCriteria().andEqualTo("biGuid", userId)
                .apply {
                    period?.let {
                        andLessThanOrEqualTo("reStartTime", it)
                        andGreaterThanOrEqualTo("reEndTime", it)
                    }
                }
            orderBy("reStartTime").desc()
        })
        return if (result.isNotEmpty()) {
            BaseResponse(true, data = result[0])
        } else {
            BaseResponse(false, "无综合风险记录")
        }
    }
 
    override fun downloadRiskInfo(
        district: String,
        sceneType: String,
        year: Int,
        month: Int,
        response: HttpServletResponse,
    ) {
        val dataSource = DataSource(DataSource.Config(district, sceneType, year, month), dbMapper)
        val riskAnalysis = RiskAnalysis(dataSource, toDatabase = false, toFile = true)
 
//        val fName = Base64.getEncoder().encodeToString("risk.xlsx".toByteArray())
        val fName = "risk.xls"
        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)
        }
        riskAnalysis.executeToStream(response.outputStream)
 
        return
    }
}