heheng
2024-11-20 2d27b24029adafdbfc5703b38a519d65beda6a68
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.gkhy.system.service.impl;
 
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.common.core.redis.RedisCache;
import com.gkhy.common.core.text.Convert;
import com.gkhy.common.enums.OpenFlagEnum;
import com.gkhy.common.exception.ServiceException;
import com.gkhy.common.utils.SecurityUtils;
import com.gkhy.common.utils.StringUtils;
import com.gkhy.system.domain.ApplyRecord;
import com.gkhy.system.domain.SysSettings;
import com.gkhy.system.mapper.ApplyRecordMapper;
import com.gkhy.system.mapper.SysSettingsMapper;
import com.gkhy.system.service.ISysSettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.PostConstruct;
import java.util.List;
 
/**
 * 系统配置Service业务层处理
 *
 * @author expert
 * @date 2024-11-13
 */
@Service
public class SysSettingsServiceImpl extends ServiceImpl<SysSettingsMapper, SysSettings> implements ISysSettingsService {
    @Autowired
    private SysSettingsMapper sysSettingsMapper;
 
    @Autowired
    private RedisCache redisCache;
 
    @Autowired
    private ApplyRecordMapper applyRecordMapper;
 
public static final String APPLY_SETTINGS = "APPLY_SETTINGS_KEY";
 
    @PostConstruct
    public void init()
    {
        loadingSettingsCache();
    }
 
    /**
     * 查询系统配置
     *
 
     * @return 系统配置
     */
    @Override
    public String selectSysSettings() {
 
        String state = Convert.toStr(redisCache.getCacheObject(APPLY_SETTINGS));
        if (StringUtils.isNotEmpty(state))
        {
            return state;
        }
 
        SysSettings sysSettings = sysSettingsMapper.selectSysSettingsByState();
        if (StringUtils.isNotNull(sysSettings)) {
            redisCache.setCacheObject(APPLY_SETTINGS, sysSettings.getState());
            return sysSettings.getState();
        }
 
        return OpenFlagEnum.CLOSE.getCode();
    }
 
    public void loadingSettingsCache(){
        SysSettings sysSettings = sysSettingsMapper.selectSysSettingsByState();
        if (sysSettings != null) {
            redisCache.setCacheObject(APPLY_SETTINGS, sysSettings.getState());
        }else {
            redisCache.setCacheObject(APPLY_SETTINGS, OpenFlagEnum.CLOSE.getCode());
        }
    }
 
 
    /**
     * 修改系统配置
     *
     * @param sysSettings 系统配置
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysSettings(String sysSettings) {
 
        int i = sysSettingsMapper.updateSysSettingsState(sysSettings);
        if (i > 0){
            String state = Convert.toStr(redisCache.getCacheObject(APPLY_SETTINGS));
 
            if (OpenFlagEnum.OPEN.getCode().equals(sysSettings)){
                if (StringUtils.isNotEmpty(state) && OpenFlagEnum.OPEN.getCode().equals(state)){
                    throw new ServiceException("当前状态已开启!");
                }
                ApplyRecord applyRecord = new ApplyRecord();
                applyRecord.setStartTime(DateUtil.date());
                applyRecord.setCreateBy(SecurityUtils.getUsername());
                int i1 = applyRecordMapper.insertApplyRecord(applyRecord);
                if (i1 < 1){
                    throw new ServiceException("开启失败,稍后再试!");
                }
            }else {
                if (StringUtils.isNotEmpty(state) && OpenFlagEnum.CLOSE.getCode().equals(state)){
                    throw new ServiceException("当前状态已关闭!");
                }
                List<ApplyRecord> list = applyRecordMapper.selectApplyRecordList();
                if (StringUtils.isNotEmpty(list)){
                    ApplyRecord applyRecord = list.get(0);
                    applyRecord.setEndTime(DateUtil.date());
                    applyRecord.setUpdateBy(SecurityUtils.getUsername());
                    int i1 = applyRecordMapper.updateApplyRecord(applyRecord);
                    if (i1 < 1){
                        throw new ServiceException("关闭失败,稍后再试!");
                    }
                }
            }
            redisCache.setCacheObject(APPLY_SETTINGS, sysSettings);
        }
        return i;
    }
 
    /**
     * 查询开启申请记录列表
     *
     * @return 开启申请记录
     */
    @Override
    public List<ApplyRecord> selectApplyRecordList() {
        return applyRecordMapper.selectApplyRecordList();
    }
}