package cn.flightfeather.supervision.business.storage
|
|
import cn.flightfeather.supervision.business.Info
|
import cn.flightfeather.supervision.business.ScoreItem
|
import cn.flightfeather.supervision.business.fume.AutoScore3
|
import cn.flightfeather.supervision.common.utils.Constant
|
import cn.flightfeather.supervision.common.utils.DateUtil
|
import cn.flightfeather.supervision.common.utils.ExcelUtil
|
import cn.flightfeather.supervision.common.utils.UUIDGenerator
|
import cn.flightfeather.supervision.domain.ds1.entity.*
|
import cn.flightfeather.supervision.domain.ds1.mapper.*
|
import cn.flightfeather.supervision.domain.ds2.entity.UserMap
|
import cn.flightfeather.supervision.domain.ds2.mapper.UserMapMapper
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook
|
import org.apache.poi.hssf.util.HSSFColor
|
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.stereotype.Component
|
import tk.mybatis.mapper.entity.Example
|
import java.io.File
|
import java.io.FileOutputStream
|
import java.time.LocalDate
|
import java.time.LocalDateTime
|
import java.util.*
|
import javax.annotation.PostConstruct
|
import kotlin.math.abs
|
|
/**
|
* Date 2022/1/21 13:44
|
* Created by feiyu
|
* 工地自动评分
|
*/
|
@Component
|
class StAutoScore(val stScoreItem_1: ScoreItem) {
|
companion object {
|
private lateinit var instance: StAutoScore
|
private val SCENE_TYPE = Constant.ScenseType.TYPE1
|
private const val DISTRICT = "金山区"
|
}
|
|
@PostConstruct
|
fun init() {
|
instance = this
|
}
|
|
@Autowired
|
lateinit var userinfoMapper: UserinfoMapper
|
|
@Autowired
|
lateinit var userMapMapper: UserMapMapper
|
|
@Autowired
|
lateinit var scenseMapper: ScenseMapper
|
|
@Autowired
|
lateinit var evaluationruleMapper: EvaluationruleMapper
|
|
@Autowired
|
lateinit var evaluationsubruleMapper: EvaluationsubruleMapper2
|
|
@Autowired
|
lateinit var evaluationMapper: EvaluationMapper
|
|
@Autowired
|
lateinit var itemevaluationMapper: ItemevaluationMapper
|
|
@Autowired
|
lateinit var problemlistMapper: ProblemlistMapper
|
|
@Autowired
|
lateinit var monitorobjectversionMapper: MonitorobjectversionMapper
|
|
@Autowired
|
lateinit var taskMapper: TaskMapper
|
|
//决定是否写h3
|
private val hasHead3 = false
|
|
private var totalScore = 100//满分
|
|
private val itemList = mutableListOf<ScoreItem>()
|
|
private val baseRules = mutableListOf<Evaluationrule>()
|
|
private val rules = mutableListOf<Pair<Evaluationsubrule2, MutableList<Evaluationsubrule2>>>()
|
|
private val topItems = mutableListOf<Evaluationsubrule2>()
|
|
private var workbook = HSSFWorkbook()
|
|
init {
|
itemList.add(stScoreItem_1)
|
}
|
|
fun go(_year: Int? = null, _month: Int? = null, _period: Int = 1) {
|
val now = LocalDate.now()
|
val year = _year ?: now.year
|
val month = _month ?: now.monthValue
|
val sMonth = DateUtil().getStartMonthByPeriod(month, _period) ?: 1
|
val eMonth = sMonth + _period - 1
|
val sTime = LocalDateTime.of(year, sMonth, 1, 0, 0)
|
val eTime = sTime.plusMonths(_period.toLong())
|
val period = "${year}/$sMonth-$eMonth"
|
|
getScoreItem()
|
|
val contents = mutableListOf<Array<Any>>()
|
|
val h1 = mutableListOf<ExcelUtil.MyCell>()
|
h1.add(ExcelUtil.MyCell(""))
|
val h2 = mutableListOf<ExcelUtil.MyCell>()
|
h2.add(ExcelUtil.MyCell(""))
|
val h3 = mutableListOf<String>()
|
h3.add("")
|
topItems.forEach {
|
h1.add(ExcelUtil.MyCell(it.itemname ?: "", 1, 0))
|
for (r in rules) {
|
if (r.first.fatherid == it.guid || r.first.guid == it.guid) {
|
h2.add(ExcelUtil.MyCell(r.first.itemname ?: "", 1, 0))
|
// FIXME: 2021/4/25 决定是否写h3
|
if (hasHead3) {
|
r.second.forEach { s ->
|
h3.add(s.itemname ?: "")
|
h2.last().colSpan++
|
h1.last().colSpan++
|
}
|
} else {
|
h2.last().colSpan++
|
h1.last().colSpan++
|
}
|
}
|
}
|
}
|
// FIXME: 2021/4/25 决定是否写h3
|
if (hasHead3) {
|
h1.add(ExcelUtil.MyCell("总分", 3, 1))
|
h1.add(ExcelUtil.MyCell("环信码", 3, 1))
|
} else {
|
h1.add(ExcelUtil.MyCell("总分", 2, 1))
|
h1.add(ExcelUtil.MyCell("环信码", 2, 1))
|
}
|
|
contents.add(h1.toTypedArray())
|
contents.add(h2.toTypedArray())
|
// FIXME: 2021/4/25 决定是否写h3
|
if (hasHead3) {
|
contents.add(h3.toTypedArray())
|
}
|
|
/** 2. 筛选打分场景*************************************************************************/
|
val tasks = taskMapper.selectByExample(Example(Task::class.java).apply {
|
createCriteria().andEqualTo("levelnum", 2)
|
.andEqualTo("districtname", DISTRICT)
|
.andGreaterThanOrEqualTo("starttime", sTime)
|
.andLessThanOrEqualTo("endtime", eTime)
|
})
|
val mList = if (tasks.isNotEmpty()) {
|
val taskId = tasks[0].tguid
|
monitorobjectversionMapper.selectByExample(Example(Monitorobjectversion::class.java).apply {
|
createCriteria().andEqualTo("tid", taskId)
|
})
|
} else {
|
emptyList()
|
}
|
// val sceneList = scenseMapper.selectByExample(Example(Scense::class.java).apply {
|
// createCriteria().andEqualTo("typeid", SCENE_TYPE.value.toByte())
|
// .andEqualTo("districtname", DISTRICT)
|
// .andNotEqualTo("extension1", '0')
|
//// createCriteria().andEqualTo("guid", "XcRAQtiD4tTU90sA")
|
// })
|
|
/** 3. 循环处理每个场景,给每个场景打分*************************************************************************/
|
val sceneList = listOf(
|
"q4qOX4kU0aa8J6W2",
|
"Syu177gGk1dkiFD1",
|
"7GQ53boTPxIPKFv5",
|
"zIAoNkridReTEFs9",
|
"q4qOX4kU0aa8J6W2",
|
"7vz7deflv1bXfVLq",
|
"F1f4FXrQ8Knd9UuS",
|
"5wufBlqzNPy5z9hs",
|
"y0t0XulzkNILtEr3",
|
"F7oulQiTCyVZAoJY"
|
)
|
mList.forEach {
|
if (!sceneList.contains(it.sguid)) return@forEach
|
|
//筛选所需要的场景类型
|
val scene = scenseMapper.selectByPrimaryKey(it.sguid)
|
if (scene.type != SCENE_TYPE.text) return@forEach
|
|
topItems.forEach { s -> s.extension1 = null }
|
rules.forEach { p ->
|
p.first.extension1 = null
|
p.second.forEach { e -> e.extension1 = null }
|
}
|
|
val userInfo = userinfoMapper.selectByExample(Example(Userinfo::class.java).apply {
|
createCriteria().andEqualTo("dGuid", it.sguid)
|
})?.takeIf { l -> l.isNotEmpty() }?.get(0)
|
|
val tzUserId = userMapMapper.selectByExample(Example(UserMap::class.java).apply {
|
createCriteria().andEqualTo("svUserId", userInfo?.guid)
|
})?.takeIf { m -> m.isNotEmpty() }?.get(0)?.tzUserId
|
|
val info =
|
Info(userInfo?.guid, tzUserId, it.sguid, it.sensename, SCENE_TYPE, year, sMonth, _period, sTime, eTime)
|
|
/** 1. 根据评分规则对应的问题自动判断是否扣分***************************************************************/
|
rules.forEach {r ->
|
// 评分条目
|
val rule = r.first
|
// 具体评分选项
|
val subRule = r.second
|
subRule.forEach {sr ->
|
sr.problemlist?.let {pId ->
|
problemlistMapper.selectByExample(Example(Problemlist::class.java).apply {
|
createCriteria().andEqualTo("sguid", info.sceneId)
|
.andEqualTo("ptguid", pId)
|
.andGreaterThanOrEqualTo("time", info.sTime)
|
.andLessThan("time", info.eTime)
|
})?.takeIf { p-> p.isNotEmpty() }?.run {
|
sr.extension1 = (0 - (sr.maxscore ?: 0)).toString()
|
}
|
}
|
}
|
var total: Int? = null
|
subRule.forEach {sr ->
|
if (!sr.extension1.isNullOrBlank()) {
|
total = (total ?: 0) + sr.extension1!!.toInt()
|
}
|
}
|
if (total == null) {
|
rule.extension1 = "0"
|
} else {
|
val s = if (abs(total!!) > rule.maxscore!!) {
|
0 - rule.maxscore!!
|
} else {
|
total
|
}
|
rule.extension1 = s.toString()
|
}
|
}
|
/** 2.部分有特殊评分逻辑的规则进行计算*************************************************************************/
|
itemList.forEach { item-> item.execute2(info, rules) }
|
|
/** 3. 补全各上级评分项的计分*************************************************************************/
|
this.rules.forEach { r ->
|
val fatherId = r.first.fatherid
|
for (t in topItems) {
|
if (t.guid == fatherId) {
|
var s = t.extension1?.toIntOrNull() ?: 0
|
s += r.first.extension1?.toIntOrNull() ?: 0
|
// 限制最高扣分
|
if (abs(s) > (t.maxscore ?: 0)) {
|
s = 0 - (t.maxscore ?: 0)
|
}
|
t.extension1 = s.toString()
|
break
|
}
|
}
|
}
|
|
/** 4. 计算总分*************************************************************************/
|
var total = 0//总扣除的分数
|
topItems.forEach top@{ top ->
|
total += top.extension1?.toIntOrNull() ?: 0
|
}
|
val evaluationList = mutableListOf<Evaluation>()
|
var evaluationId: String? = null
|
baseRules.forEach {
|
val evaluation = Evaluation().apply {
|
guid = UUIDGenerator.generate16ShortUUID()
|
iguid = info.userId
|
stguid = it.guid
|
scensetypeid = info.sceneType.value.toByte()
|
ertype = it.ruletype?.toByte()
|
scensename = period
|
evaluatorguid = "system"
|
evaluatorusername = "system"
|
evaluatorrealname = "2"
|
resultscorebef = (totalScore - abs(total)).toString()
|
createdate = Date()
|
updatedate = Date()
|
}
|
if (evaluation.ertype?.toInt() == 1) {
|
evaluationId = evaluation.guid
|
}
|
evaluationList.add(evaluation)
|
}
|
//子项具体得分
|
val itemevaluationList = mutableListOf<Itemevaluation>()
|
topItems.forEach { subRule ->
|
val item = newItemEvaluation(info, subRule, evaluationId)
|
itemevaluationList.add(item)
|
}
|
rules.forEach { p ->
|
if (p.first.ertype != 2) {
|
val item = newItemEvaluation(info, p.first, evaluationId)
|
itemevaluationList.add(item)
|
}
|
p.second.forEach { r ->
|
val item1 = newItemEvaluation(info, r, evaluationId)
|
itemevaluationList.add(item1)
|
}
|
}
|
// 添加至文档
|
addToFile(contents, info, evaluationList, topItems, this.rules)
|
// val r = evaluationMapper.selectByExample(Example(Evaluation::class.java).apply {
|
// createCriteria().andEqualTo("scensename", period)
|
// .andEqualTo("evaluatorguid", "system")
|
// .andEqualTo("iguid", info.userId)
|
//// .andEqualTo("ertype", 1)
|
// }).let { eList ->
|
// var guid: String? = null
|
// for (e in eList) {
|
// if (e.ertype?.toInt() == 1) {
|
// guid = e.guid
|
// break
|
// }
|
// }
|
// guid?.let {
|
// eList.forEach { e ->
|
// evaluationMapper.delete(e)
|
// }
|
// itemevaluationMapper.deleteByExample(Example(Itemevaluation::class.java).apply {
|
// createCriteria().andEqualTo("sguid", guid)
|
// })
|
// }
|
// } ?: 0
|
// 写入数据库
|
// if (r > 0) {
|
// evaluationList.forEach { el -> evaluationMapper.insert(el) }
|
// itemevaluationList.forEach { il -> itemevaluationMapper.insert(il) }
|
// for (e in evaluationList) {
|
// if (e.ertype?.toInt()?.equals(0) == true) {
|
// finalScore(e, year, eMonth, period)
|
// break
|
// }
|
// }
|
// }
|
}
|
|
// 写入文档
|
val fileName = "${year}年${if (_period == 1) "${sMonth}月" else "${sMonth}-${eMonth}月"}${SCENE_TYPE.text}自动评分-${DateUtil().DateToString(Date(), "yyyy-MM-ddhhmmss")}.xls"
|
// val filePath = "E:\\工作\\开发\\第三方监管app\\自动评分\\工地\\$fileName"
|
val filePath = "C:\\work\\工作\\第三方监管\\自动评分\\${SCENE_TYPE.text}\\$fileName"
|
val out = FileOutputStream(File(filePath))
|
ExcelUtil.write(emptyList(), contents, workbook)
|
workbook.write(out)
|
workbook.close()
|
out.flush()
|
out.close()
|
}
|
|
/**
|
* 获取评分规则
|
*/
|
private fun getScoreItem() {
|
this.rules.clear()
|
baseRules.clear()
|
|
val rule = evaluationruleMapper.selectByExample(Example(Evaluationrule::class.java).apply {
|
createCriteria()
|
.andEqualTo("tasktypeid", 99)
|
.andEqualTo("scensetypeid", SCENE_TYPE.value.toByte())
|
})
|
if (rule.isNotEmpty()) {
|
baseRules.addAll(rule)
|
|
val ruleId = rule[0].guid
|
val rules = evaluationsubruleMapper.selectByExample(Example(Evaluationsubrule2::class.java).apply {
|
createCriteria().andEqualTo("erguid", ruleId)
|
})
|
rules.forEach {
|
if (it.ertype == 2) {
|
topItems.add(it)
|
}
|
}
|
topItems.sortBy { it.displayid }
|
|
var t = 0
|
topItems.forEach {
|
t += it.maxscore ?: 0
|
val tempRules = mutableListOf<Evaluationsubrule2>()
|
for (i in rules) {
|
if (i.fatherid == it.guid && i.ertype == 3) {
|
tempRules.add(i)
|
}
|
}
|
//评分大项如果没有找到评分小项,则说明其直接对应最小评分项,其本身变成评分项
|
if (tempRules.isEmpty()) {
|
tempRules.add(it)
|
}
|
tempRules.sortBy { t-> t.displayid }
|
tempRules.forEach {temp ->
|
val tempSubRules = mutableListOf<Evaluationsubrule2>()
|
for (i in rules) {
|
if (i.fatherid == temp.guid && i.ertype == 4) {
|
tempSubRules.add(i)
|
}
|
}
|
tempSubRules.sortBy {ts-> ts.displayid }
|
this.rules.add(Pair(temp, tempSubRules))
|
}
|
}
|
// this.totalScore = t
|
}
|
}
|
|
/**
|
* 生成新的一条评分记录
|
*/
|
private fun newItemEvaluation(info: Info, itemRule: Evaluationsubrule2, evaluationId: String?) = cn.flightfeather.supervision.domain.ds1.entity.Itemevaluation()
|
.apply {
|
var rule: Evaluationrule? = null
|
for (r in baseRules) {
|
if (r.guid == itemRule.erguid) {
|
rule = r
|
break
|
}
|
}
|
ieguid = UUIDGenerator.generate16ShortUUID()
|
iguid = info.userId
|
sguid = evaluationId
|
erguid = rule?.guid
|
rulename = rule?.rulename
|
ruletype = rule?.ruletype?.toInt()
|
ertype = itemRule.ertype
|
esrguid = itemRule.guid
|
name = itemRule.itemname
|
value = itemRule.extension1 ?: "0"
|
extension1 = (itemRule.extension1 != null).toString()
|
}
|
|
private fun addToFile(contents: MutableList<Array<Any>>, info: Info, evaluations: List<Evaluation>, topItems: MutableList<Evaluationsubrule2>,
|
rules: MutableList<Pair<Evaluationsubrule2, MutableList<Evaluationsubrule2>>>) {
|
val cList = mutableListOf<Any>()
|
// FIXME: 2021/4/28 用户名是否添加账号
|
// cList.add("${info.userName}\t${info.userId}")
|
cList.add("${info.sceneName}")
|
//每一项具体得分
|
topItems.forEach {
|
for (r in rules) {
|
if (r.first.fatherid == it.guid || r.first.guid == it.guid) {
|
// FIXME: 2021/4/25 决定是否写h3
|
if (hasHead3) {
|
r.second.forEach {s ->
|
cList.add(s.extension1 ?: "")
|
}
|
} else {
|
cList.add(r.first.extension1?.toDoubleOrNull() ?: .0)
|
}
|
}
|
}
|
}
|
//总分和环信码
|
evaluations.forEach {
|
if ((it.ertype?.toInt() == 2)) {
|
cList.add(it.resultscorebef?.toDoubleOrNull() ?: .0)
|
val code = when (it.resultscorebef?.toIntOrNull() ?: 0) {
|
in 0..59 -> ExcelUtil.MyCell("红码", fontColor = HSSFColor.HSSFColorPredefined.RED.index)
|
in 60..89 -> ExcelUtil.MyCell("黄码", fontColor = HSSFColor.HSSFColorPredefined.GOLD.index)
|
in 90..100 -> ExcelUtil.MyCell("绿码", fontColor = HSSFColor.HSSFColorPredefined.BRIGHT_GREEN.index)
|
else -> ExcelUtil.MyCell("超出范围:${it.resultscoreaft}", fontColor = HSSFColor.HSSFColorPredefined.BLACK.index)
|
}
|
cList.add(code)
|
}
|
}
|
contents.add(cList.toTypedArray())
|
}
|
}
|