package com.farriver.bwf.service.system; import com.farriver.bwf.common.model.ApiData; import com.farriver.bwf.common.model.ApiPageData; import com.farriver.bwf.common.options.ActionType; import com.farriver.bwf.common.options.MapDataType; import com.farriver.bwf.common.statics.LangConstants; import com.farriver.bwf.data.master.mapper.SystemConfigMapper; import com.farriver.bwf.data.master.model.SystemConfig; import com.farriver.bwf.data.master.model.SystemConfigExample; import com.farriver.bwf.data.transferobject.queryobject.system.SystemConfigQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.system.SystemConfigViewModel; import com.farriver.bwf.service.ServiceBase; import jakarta.annotation.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.*; @Service @CacheConfig(cacheNames = "erp_SystemConfigMapper_cache") public class SystemConfigService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(SystemConfigService.class); @Resource SystemConfigMapper systemConfigMapper; private String message; public Map GetList(SystemConfigQueryObject queryObject) { if (queryObject == null) ApiPageData.error(LangConstants.MSG_ERROR_PARAMETERS); SystemConfigExample example = new SystemConfigExample(); SystemConfigExample.Criteria criteria = example.createCriteria(); //查询条件 if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { criteria.andIdEqualTo(queryObject.getId()); } if (queryObject.getKey() != null && !queryObject.getKey().isEmpty()) { criteria.andCkeyLike("%" + queryObject.getKey() + "%"); } if (queryObject.getValue() != null && !queryObject.getValue().isEmpty()) { criteria.andCvalueLike("%" + queryObject.getValue() + "%"); } if (queryObject.getLevel() != null && !queryObject.getLevel().isEmpty()) { criteria.andClevelLike("%" + queryObject.getLevel() + "%"); } if (queryObject.getCategory() != null && !queryObject.getCategory().isEmpty()) { criteria.andCategoryLike("%" + queryObject.getCategory() + "%"); } if (queryObject.getStartDateTime() != null && queryObject.getEndDateTime() != null) { criteria.andCreatetimeBetween(queryObject.getStartDateTime(), queryObject.getEndDateTime()); } //查询总数 long total = systemConfigMapper.countByExample(example); //分页 int pageIndex = queryObject.getPageIndex(); int pageSize = queryObject.getPageSize(); example.setRows(pageSize); example.setOffset((pageIndex - 1) * pageSize); List list = systemConfigMapper.selectByExample(example); Map map = new HashMap<>(); map.put(MapDataType.DATA_LIST, list); map.put(MapDataType.TOTAL_COUNT, total); map.put(MapDataType.PAGE_INDEX, pageIndex); map.put(MapDataType.PAGE_SIZE, pageSize); return map; } public SystemConfig GetDetail(SystemConfigQueryObject queryObject) { Map map = GetList(queryObject); Object object = map.get(MapDataType.DATA_LIST); if (object != null) { List list = (List) object; if (list != null && list.size() == 1) return list.get(0); } return null; } @CacheEvict(allEntries = true) public ApiData Create(SystemConfigViewModel model) { SystemConfig entity = Check(ActionType.CREATE, model); if (entity != null) { int result = systemConfigMapper.insertSelective(entity); return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, entity); } else { return ApiData.error(message); } } @CacheEvict(allEntries = true) public ApiData Update(SystemConfigViewModel model) { SystemConfig entity = Check(ActionType.UPDATE, model); if (entity != null) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, systemConfigMapper.updateByPrimaryKeySelective(entity)); } else { return ApiData.error(message); } } @CacheEvict(allEntries = true) public ApiData Delete(String id) { return ApiData.ok("", systemConfigMapper.deleteByPrimaryKey(id)); } @CacheEvict(allEntries = true) public ApiData BatchDelete(SystemConfigQueryObject queryObject) { Map map = GetList(queryObject); Object object = map.get(MapDataType.DATA_LIST); int count = 0; if (object != null) { List list = (List) object; if (list != null && list.size() > 0) { List ids = list.stream().map(SystemConfig::getId).toList(); SystemConfigExample example = new SystemConfigExample(); SystemConfigExample.Criteria criteria = example.createCriteria(); criteria.andIdIn(ids); count += systemConfigMapper.deleteByExample(example); } } return ApiData.ok("", count); } @Cacheable public SystemConfigViewModel GetViewDetail(SystemConfigQueryObject queryObject) { SystemConfig entity = GetDetail(queryObject); if (entity == null) { return null; } return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(SystemConfigQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (SystemConfig entity : list) { SystemConfigViewModel viewModel = BuildViewModel(entity); viewModels.add(viewModel); } ApiPageData bean = ApiPageData.build(); bean.setData(viewModels); bean.setTotal((Long) map.get(MapDataType.TOTAL_COUNT)); return bean; } public String GetOrCreateSystemConfig(String key) { var data = GetViewPageList(new SystemConfigQueryObject()); if (data != null && data.getData() != null) { var list = (List) data.getData(); if (list != null && !list.isEmpty()) { var model = list.stream().filter(e -> key.equals(e.getKey())).findFirst(); if (model.isPresent()) { return model.get().getValue(); } } } return null; } //private methods private static SystemConfigViewModel BuildViewModel(SystemConfig entity) { if (entity == null) return null; SystemConfigViewModel model = new SystemConfigViewModel(); model.setId(entity.getId()); model.setKey(entity.getCkey()); model.setValue(entity.getCvalue()); model.setLevel(entity.getClevel()); model.setCategory(entity.getCategory()); model.setIsdeleted(entity.getIsdeleted()); model.setRemark(entity.getRemark()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); return model; } public SystemConfig Check(ActionType actionType, SystemConfigViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } SystemConfig entity = null; if (ActionType.CREATE == actionType) { if (model.getKey() != null && !model.getKey().isEmpty()) { var queryObject2 = new SystemConfigQueryObject(); queryObject2.setKey(model.getKey()); var map2 = GetList(queryObject2); var object2 = map2.get(MapDataType.DATA_LIST); if (object2 != null) { var list = (List) object2; if (list != null && list.size() > 0) { message = LangConstants.MSG_ERROR_CODE_EXISTED; return null; } } } entity = new SystemConfig(); entity.setId(UUID.randomUUID().toString()); entity.setCreatetime(new Date()); entity.setIsdeleted(false); } else { SystemConfigQueryObject queryObject = new SystemConfigQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); } entity.setCkey(model.getKey()); entity.setCvalue(model.getValue()); entity.setClevel(model.getLevel()); entity.setCategory(model.getCategory()); entity.setRemark(model.getRemark()); return entity; } }