| | |
| | | package com.gkhy.system.service.impl; |
| | | |
| | | import cn.hutool.core.date.DateUtil; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import java.util.List; |
| | | 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 com.gkhy.system.mapper.SysSettingsMapper; |
| | | import com.gkhy.system.domain.SysSettings; |
| | | import com.gkhy.system.service.ISysSettingsService; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 系统配置Service业务层处理 |
| | |
| | | @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(); |
| | | } |
| | | |
| | | /** |
| | | * 查询系统配置 |
| | | * |
| | | * @param state 系统配置主键 |
| | | |
| | | * @return 系统配置 |
| | | */ |
| | | @Override |
| | | public SysSettings selectSysSettingsByState(String state) { |
| | | return sysSettingsMapper.selectSysSettingsByState(state); |
| | | 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(); |
| | | } |
| | | |
| | | /** |
| | | * 查询系统配置列表 |
| | | * |
| | | * @param sysSettings 系统配置 |
| | | * @return 系统配置 |
| | | */ |
| | | @Override |
| | | public List<SysSettings> selectSysSettingsList(SysSettings sysSettings) { |
| | | return sysSettingsMapper.selectSysSettingsList(sysSettings); |
| | | 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 |
| | | public int insertSysSettings(SysSettings sysSettings) { |
| | | return sysSettingsMapper.insertSysSettings(sysSettings); |
| | | } |
| | | |
| | | /** |
| | | * 修改系统配置 |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int updateSysSettings(SysSettings sysSettings) { |
| | | return sysSettingsMapper.updateSysSettings(sysSettings); |
| | | @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; |
| | | } |
| | | |
| | | /** |
| | | * 批量删除系统配置 |
| | | * 查询开启申请记录列表 |
| | | * |
| | | * @param states 需要删除的系统配置主键 |
| | | * @return 结果 |
| | | * @return 开启申请记录 |
| | | */ |
| | | @Override |
| | | public int deleteSysSettingsByStates(String[] states) { |
| | | return sysSettingsMapper.deleteSysSettingsByStates(states); |
| | | } |
| | | |
| | | /** |
| | | * 删除系统配置信息 |
| | | * |
| | | * @param state 系统配置主键 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int deleteSysSettingsByState(String state) { |
| | | return sysSettingsMapper.deleteSysSettingsByState(state); |
| | | public List<ApplyRecord> selectApplyRecordList() { |
| | | return applyRecordMapper.selectApplyRecordList(); |
| | | } |
| | | } |