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.SystemNotificationMapper; import com.farriver.bwf.data.master.model.SystemNotification; import com.farriver.bwf.data.master.model.SystemNotificationExample; import com.farriver.bwf.data.transferobject.options.SystemNotificationStatusOptions; import com.farriver.bwf.data.transferobject.queryobject.system.SystemNotificationQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.system.SystemNotificationViewModel; import com.farriver.bwf.service.ServiceBase; import jakarta.annotation.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.util.*; @Service public class SystemNotificationService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(SystemNotificationService.class); @Resource SystemNotificationMapper systemNotificationMapper; private String message; public Map GetList(SystemNotificationQueryObject queryObject) { if (queryObject == null) return null; SystemNotificationExample example = new SystemNotificationExample(); SystemNotificationExample.Criteria criteria = example.createCriteria(); //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andTitleLike("%" + queryObject.getFilter() + "%"); } else { if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { criteria.andIdEqualTo(queryObject.getId()); } if (queryObject.getTitle() != null && !queryObject.getTitle().isEmpty()) { criteria.andTitleLike("%" + queryObject.getTitle() + "%"); } if (queryObject.getKeywords() != null && !queryObject.getKeywords().isEmpty()) { criteria.andKeywordsLike("%" + queryObject.getKeywords() + "%"); } if (queryObject.getDepartment() != null && !queryObject.getDepartment().isEmpty()) { criteria.andDepartmentLike("%" + queryObject.getDepartment() + "%"); } if (queryObject.getCategory() != null && !queryObject.getCategory().isEmpty()) { criteria.andCategoryLike("%" + queryObject.getCategory() + "%"); } if (queryObject.getActionby() != null && !queryObject.getActionby().isEmpty()) { criteria.andActionbyLike("%" + queryObject.getActionby() + "%"); } if (queryObject.getPublisher() != null && !queryObject.getPublisher().isEmpty()) { criteria.andPublisherLike("%" + queryObject.getPublisher() + "%"); } if (queryObject.getStatus() != null && queryObject.getStatus() >= 0) { criteria.andStatusEqualTo(queryObject.getStatus()); } if (queryObject.getStartDateTime() != null && queryObject.getEndDateTime() != null) { criteria.andCreatetimeBetween(queryObject.getStartDateTime(), queryObject.getEndDateTime()); } } //查询总数 long total = systemNotificationMapper.countByExample(example); //分页 int pageIndex = queryObject.getPageIndex(); int pageSize = queryObject.getPageSize(); if (pageIndex > 0 && pageSize > 0) { example.setRows(pageSize); example.setOffset((pageIndex - 1) * pageSize); } example.setOrderByClause("createtime desc"); List list = systemNotificationMapper.selectByExampleWithBLOBs(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 SystemNotification GetDetail(SystemNotificationQueryObject 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; } public ApiData Create(SystemNotificationViewModel model) { SystemNotification entity = Check(ActionType.CREATE, model); if (entity != null) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, systemNotificationMapper.insertSelective(entity)); } else { return ApiData.error(message); } } public ApiData Update(SystemNotificationViewModel model) { SystemNotification entity = Check(ActionType.UPDATE, model); if (entity != null) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, systemNotificationMapper.updateByPrimaryKeySelective(entity)); } else { return ApiData.error(message); } } public ApiData Delete(String id) { return ApiData.ok("", systemNotificationMapper.deleteByPrimaryKey(id)); } public ApiData BatchDelete(SystemNotificationQueryObject 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(SystemNotification::getId).toList(); SystemNotificationExample example = new SystemNotificationExample(); SystemNotificationExample.Criteria criteria = example.createCriteria(); criteria.andIdIn(ids); count += systemNotificationMapper.deleteByExample(example); } } return ApiData.ok("", count); } public SystemNotificationViewModel GetViewDetail(SystemNotificationQueryObject queryObject) { SystemNotification entity = GetDetail(queryObject); if (entity == null) { return null; } return BuildViewModel(entity); } public ApiPageData GetViewPageList(SystemNotificationQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); if (list != null && list.size() > 0) { for (SystemNotification entity : list) { SystemNotificationViewModel viewModel = BuildViewModel(entity); viewModels.add(viewModel); } } ApiPageData bean = ApiPageData.build(); bean.setData(viewModels); bean.setTotal((Long) map.get(MapDataType.TOTAL_COUNT)); return bean; } //private methods private static SystemNotificationViewModel BuildViewModel(SystemNotification entity) { if (entity == null) return null; SystemNotificationViewModel model = new SystemNotificationViewModel(); model.setId(entity.getId()); model.setTitle(entity.getTitle()); model.setContext(entity.getContext()); model.setDepartment(entity.getDepartment()); model.setCategory(entity.getCategory()); model.setNlevel(entity.getNlevel()); model.setKeywords(entity.getKeywords()); model.setActionby(entity.getActionby()); model.setStatus(entity.getStatus()); model.setPublishdatetime(entity.getPublishdatetime()); model.setPublisher(entity.getPublisher()); model.setContext(entity.getContext()); model.setIsdeleted(entity.getIsdeleted()); model.setRemark(entity.getRemark()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); return model; } public SystemNotification Check(ActionType actionType, SystemNotificationViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } SystemNotification entity = null; if (ActionType.CREATE == actionType) { entity = new SystemNotification(); entity.setId(UUID.randomUUID().toString()); entity.setCreatetime(new Date()); entity.setIsdeleted(false); entity.setStatus(SystemNotificationStatusOptions.Unpublished.ordinal()); } else { SystemNotificationQueryObject queryObject = new SystemNotificationQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); entity.setIsdeleted(model.getIsdeleted()); entity.setStatus(model.getStatus()); if (model.getStatus() == 2) { entity.setPublishdatetime(new Date()); } } entity.setTitle(model.getTitle()); entity.setContext(model.getContext()); entity.setDepartment(model.getDepartment()); entity.setCategory(model.getCategory()); entity.setNlevel(model.getNlevel()); entity.setKeywords(model.getKeywords()); entity.setActionby(model.getActionby()); entity.setPublishdatetime(model.getPublishdatetime()); entity.setPublisher(model.getPublisher()); entity.setContext(model.getContext()); entity.setRemark(model.getRemark()); return entity; } }