119 lines
3.7 KiB
TypeScript
119 lines
3.7 KiB
TypeScript
import { store } from '@/store'
|
|
import { defineStore } from 'pinia'
|
|
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
|
|
import { getReportManageListByType } from '@/api/submit/systemConfig/reportManage'
|
|
import type { ReportManageDTO } from '@/api/submit/systemConfig/reportManage/types'
|
|
|
|
const { wsCache } = useCache()
|
|
|
|
interface RewardsPunishReasonState {
|
|
// 存储奖惩原因列表
|
|
rewardsPunishReasonList: Array<{
|
|
id: string
|
|
name: string
|
|
}>
|
|
// 是否已设置奖惩原因
|
|
isSetRewardsPunishReason: boolean
|
|
}
|
|
|
|
export const useRewardsPunishReasonStore = defineStore('admin-rewardsPunishReason', {
|
|
state: (): RewardsPunishReasonState => ({
|
|
rewardsPunishReasonList: [],
|
|
isSetRewardsPunishReason: false
|
|
}),
|
|
getters: {
|
|
/**
|
|
* 获取奖惩原因列表
|
|
*/
|
|
getRewardsPunishReasonList(): Array<{ id: string; name: string }> {
|
|
return this.rewardsPunishReasonList
|
|
},
|
|
/**
|
|
* 是否已设置奖惩原因
|
|
*/
|
|
getIsSetRewardsPunishReason(): boolean {
|
|
return this.isSetRewardsPunishReason
|
|
}
|
|
},
|
|
actions: {
|
|
/**
|
|
* 设置奖惩原因列表信息
|
|
*/
|
|
async setRewardsPunishReasonAction() {
|
|
let rewardsPunishReasonInfo = wsCache.get(CACHE_KEY.REWARDS_PUNISH_REASON)
|
|
if (!rewardsPunishReasonInfo) {
|
|
// 无缓存时,从接口获取
|
|
const response = await getReportManageListByType('REWARDS_PUNISH_REASON')
|
|
const reasons = this.extractRewardsPunishReason(response)
|
|
rewardsPunishReasonInfo = {
|
|
rewardsPunishReasonList: reasons
|
|
}
|
|
wsCache.set(CACHE_KEY.REWARDS_PUNISH_REASON, rewardsPunishReasonInfo)
|
|
} else {
|
|
// 有缓存时,尝试更新数据
|
|
try {
|
|
const response = await getReportManageListByType('REWARDS_PUNISH_REASON')
|
|
const reasons = this.extractRewardsPunishReason(response)
|
|
rewardsPunishReasonInfo = {
|
|
rewardsPunishReasonList: reasons
|
|
}
|
|
} catch (error) {
|
|
// 更新失败时使用缓存数据
|
|
}
|
|
}
|
|
this.rewardsPunishReasonList = rewardsPunishReasonInfo.rewardsPunishReasonList
|
|
this.isSetRewardsPunishReason = true
|
|
wsCache.set(CACHE_KEY.REWARDS_PUNISH_REASON, rewardsPunishReasonInfo)
|
|
},
|
|
|
|
/**
|
|
* 从报表管理列表中提取奖惩原因
|
|
*/
|
|
extractRewardsPunishReason(reportList: ReportManageDTO[]): Array<{ id: string; name: string }> {
|
|
// 使用Map去重
|
|
const reasonMap = new Map<string, string>()
|
|
|
|
reportList.forEach(report => {
|
|
reasonMap.set(report.id, report.name)
|
|
})
|
|
|
|
// 转换为数组格式
|
|
return Array.from(reasonMap.entries()).map(([id, name]) => ({
|
|
id,
|
|
name
|
|
}))
|
|
},
|
|
|
|
/**
|
|
* 更新奖惩原因列表信息
|
|
*/
|
|
async updateRewardsPunishReasonAction() {
|
|
// 强制更新数据,不使用缓存
|
|
const response = await getReportManageListByType('REWARDS_PUNISH_REASON')
|
|
const reasons = this.extractRewardsPunishReason(response)
|
|
this.rewardsPunishReasonList = reasons
|
|
this.isSetRewardsPunishReason = true
|
|
const rewardsPunishReasonInfo = {
|
|
rewardsPunishReasonList: reasons
|
|
}
|
|
wsCache.set(CACHE_KEY.REWARDS_PUNISH_REASON, rewardsPunishReasonInfo)
|
|
},
|
|
|
|
/**
|
|
* 重置奖惩原因状态
|
|
*/
|
|
resetRewardsPunishReasonState() {
|
|
this.rewardsPunishReasonList = []
|
|
this.isSetRewardsPunishReason = false
|
|
wsCache.delete(CACHE_KEY.REWARDS_PUNISH_REASON)
|
|
}
|
|
},
|
|
persist: false
|
|
})
|
|
|
|
/**
|
|
* 在组件外部使用奖惩原因 store
|
|
*/
|
|
export const useRewardsPunishReasonStoreWithOut = () => {
|
|
return useRewardsPunishReasonStore(store)
|
|
} |