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.SystemMailLogMapper; import com.farriver.bwf.data.master.model.SystemMailLog; import com.farriver.bwf.data.master.model.SystemMailLogExample; import com.farriver.bwf.data.transferobject.queryobject.system.SystemMailLogQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.system.SystemMailLogViewModel; 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 SystemMailLogService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(SystemMailLogService.class); @Resource SystemMailLogMapper systemMailLogMapper; private String message; public Map GetList(SystemMailLogQueryObject queryObject) { if (queryObject == null) return null; SystemMailLogExample example = new SystemMailLogExample(); SystemMailLogExample.Criteria criteria = example.createCriteria(); //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andExchangeLike("%" + queryObject.getFilter() + "%"); } else { if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { criteria.andIdEqualTo(queryObject.getId()); } if (queryObject.getUid() != null && !queryObject.getUid().isEmpty()) { criteria.andIdEqualTo(queryObject.getUid()); } if (queryObject.getStatus() != null && queryObject.getStatus() >= 0) { criteria.andStatusEqualTo(queryObject.getStatus()); } if (queryObject.getExchange() != null && !queryObject.getExchange().isEmpty()) { criteria.andExchangeLike("%" + queryObject.getExchange() + "%"); } if (queryObject.getCount() != null) { criteria.andCountEqualTo(queryObject.getCount()); } if (queryObject.getStartDateTime() != null && queryObject.getEndDateTime() != null) { criteria.andCreatetimeBetween(queryObject.getStartDateTime(), queryObject.getEndDateTime()); } } //查询总数 long total = systemMailLogMapper.countByExample(example); //分页 int pageIndex = queryObject.getPageIndex(); int pageSize = queryObject.getPageSize(); if (pageIndex > 0 && pageSize > 0) { example.setRows(pageSize); example.setOffset((pageIndex - 1) * pageSize); } List list = systemMailLogMapper.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 SystemMailLog GetDetail(SystemMailLogQueryObject 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(SystemMailLogViewModel model) { SystemMailLog entity = Check(ActionType.CREATE, model); if (entity != null) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, systemMailLogMapper.insertSelective(entity)); } else { return ApiData.error(message); } } public ApiData Update(SystemMailLog model) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, systemMailLogMapper.updateByPrimaryKeySelective(model)); } public ApiData Update(SystemMailLogViewModel model) { SystemMailLog entity = Check(ActionType.UPDATE, model); if (entity != null) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, systemMailLogMapper.updateByPrimaryKeySelective(entity)); } else { return ApiData.error(message); } } public SystemMailLog Check(ActionType actionType, SystemMailLogViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } SystemMailLog entity = null; if (ActionType.CREATE == actionType) { entity = new SystemMailLog(); entity.setId(UUID.randomUUID().toString()); entity.setCreatetime(new Date()); } else { SystemMailLogQueryObject queryObject = new SystemMailLogQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); } entity.setUid(model.getUid()); entity.setStatus(model.getStatus()); entity.setRoutekey(model.getRoutekey()); entity.setExchange(model.getExchange()); entity.setCount(model.getCount()); entity.setTrytime(model.getTrytime()); entity.setUsername(model.getUsername()); entity.setAccount(model.getAccount()); return entity; } public ApiData Delete(String id) { return ApiData.ok("", systemMailLogMapper.deleteByPrimaryKey(id)); } public ApiData BatchDelete(SystemMailLogQueryObject 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(SystemMailLog::getId).toList(); SystemMailLogExample example = new SystemMailLogExample(); SystemMailLogExample.Criteria criteria = example.createCriteria(); criteria.andIdIn(ids); count += systemMailLogMapper.deleteByExample(example); } } return ApiData.ok("", count); } public SystemMailLogViewModel GetViewDetail(SystemMailLogQueryObject queryObject) { SystemMailLog entity = GetDetail(queryObject); if (entity == null) { return null; } return BuildViewModel(entity); } public ApiPageData GetViewPageList(SystemMailLogQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (SystemMailLog entity : list) { SystemMailLogViewModel 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 SystemMailLogViewModel BuildViewModel(SystemMailLog entity) { if (entity == null) return null; SystemMailLogViewModel model = new SystemMailLogViewModel(); model.setId(entity.getId()); model.setUid(entity.getUid()); model.setStatus(entity.getStatus()); model.setRoutekey(entity.getRoutekey()); model.setExchange(entity.getExchange()); model.setCount(entity.getCount()); model.setTrytime(entity.getTrytime()); model.setUsername(entity.getUsername()); model.setAccount(entity.getAccount()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); return model; } }