package com.farriver.bwf.service.organization; 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.OrganizationMajorMapper; import com.farriver.bwf.data.master.model.OrganizationMajor; import com.farriver.bwf.data.master.model.OrganizationMajorExample; import com.farriver.bwf.data.transferobject.queryobject.organization.OrganizationMajorQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.organization.OrganizationMajorViewModel; import com.farriver.bwf.service.ServiceBase; import org.jetbrains.annotations.Nullable; 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 org.springframework.transaction.annotation.Transactional; import jakarta.annotation.Resource; import java.util.*; @Service @CacheConfig(cacheNames = "bwf_OrganizationMajor_cache") public class OrganizationMajorService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(OrganizationMajorService.class); private String message; @Resource OrganizationMajorMapper mapper; public Map GetList(OrganizationMajorQueryObject queryObject) { if (queryObject == null) return null; OrganizationMajorExample example = new OrganizationMajorExample(); OrganizationMajorExample.Criteria criteria = example.createCriteria(); if (queryObject.getDepartmentid() != null && !queryObject.getDepartmentid().isEmpty()) { criteria.andDepartmentidEqualTo(queryObject.getDepartmentid()); } //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andNameLike("%" + queryObject.getFilter() + "%"); OrganizationMajorExample.Criteria criteria2 = example.createCriteria(); criteria2.andCodeLike("%" + queryObject.getFilter() + "%"); example.or(criteria2); } else { if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { criteria.andIdEqualTo(queryObject.getId()); } if (queryObject.getCode() != null && !queryObject.getCode().isEmpty()) { criteria.andCodeLike("%" + queryObject.getCode() + "%"); } if (queryObject.getName() != null && !queryObject.getName().isEmpty()) { criteria.andNameLike("%" + queryObject.getName() + "%"); } if (queryObject.getOrganizationid() != null && !queryObject.getOrganizationid().isEmpty()) { criteria.andOrganizationidEqualTo(queryObject.getOrganizationid()); } if (queryObject.getDepartmentid() != null && !queryObject.getDepartmentid().isEmpty()) { criteria.andDepartmentidEqualTo(queryObject.getDepartmentid()); } } //查询总数 long total = mapper.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 = mapper.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 OrganizationMajor GetDetail(OrganizationMajorQueryObject 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(OrganizationMajorViewModel model) { OrganizationMajor entity = Check(ActionType.CREATE, model); if (entity != null) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, mapper.insertSelective(entity)); } else { return ApiData.error(message); } } @CacheEvict(allEntries = true) public ApiData Update(OrganizationMajorViewModel model) { OrganizationMajor entity = Check(ActionType.UPDATE, model); if (entity != null) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, mapper.updateByPrimaryKeySelective(entity)); } else { return ApiData.error(message); } } @CacheEvict(allEntries = true) public ApiData Delete(String id) { return ApiData.ok("", mapper.deleteByPrimaryKey(id)); } @Transactional @CacheEvict(allEntries = true) public ApiData BatchDelete(OrganizationMajorQueryObject 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() == 1) { count += mapper.deleteByPrimaryKey(list.get(0).getId()); } } return ApiData.ok("", count); } @Cacheable public OrganizationMajorViewModel GetViewDetail(String id) { OrganizationMajorQueryObject queryObject = new OrganizationMajorQueryObject(); queryObject.setId(id); OrganizationMajor entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(OrganizationMajorQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (OrganizationMajor entity : list) { OrganizationMajorViewModel 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 OrganizationMajorViewModel BuildViewModel(OrganizationMajor entity) { if (entity == null) return null; OrganizationMajorViewModel model = new OrganizationMajorViewModel(); model.setId(entity.getId()); model.setIsdeleted(entity.getIsdeleted()); model.setRemark(entity.getRemark()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); model.setCode(entity.getCode()); model.setName(entity.getName()); model.setParentid(entity.getParentid()); model.setStatus(entity.getStatus()); model.setOrganizationid(entity.getOrganizationid()); model.setOrganizationname(entity.getOrganizationname()); model.setOrganizationcode(entity.getOrganizationcode()); model.setDepartmentid(entity.getDepartmentid()); model.setDepartmentname(entity.getDepartmentname()); model.setDepartmentcode(entity.getDepartmentcode()); return model; } public OrganizationMajor Check(ActionType actionType, OrganizationMajorViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } OrganizationMajor entity = null; if (ActionType.CREATE == actionType) { entity = new OrganizationMajor(); entity.setId(UUID.randomUUID().toString()); entity.setCreatetime(new Date()); entity.setIsdeleted(false); String oldCode = GetLatestCode(); String newCode = generateCode(oldCode); if (newCode == null) return null; entity.setCode(newCode); } else { OrganizationMajorQueryObject queryObject = new OrganizationMajorQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); } entity.setRemark(model.getRemark()); entity.setName(model.getName()); entity.setParentid(model.getParentid()); entity.setStatus(model.getStatus()); entity.setOrganizationid(model.getOrganizationid()); entity.setOrganizationname(model.getOrganizationname()); entity.setOrganizationcode(model.getOrganizationcode()); entity.setDepartmentid(model.getDepartmentid()); entity.setDepartmentname(model.getDepartmentname()); entity.setDepartmentcode(model.getDepartmentcode()); return entity; } @Nullable public String generateCode(String oldCode) { String preNo = "M"; int _newCodeNum = 1; try { int _oldCodeNum = 0; if (oldCode != null && !oldCode.equals("") && oldCode.length() > 2) { String _oldNoNumString = oldCode.substring(2); _oldCodeNum = Integer.parseInt(_oldNoNumString); } if (_oldCodeNum != 0) { int a = _oldCodeNum + 1; _newCodeNum = a; } } catch (NumberFormatException e) { e.printStackTrace(); message = LangConstants.MSG_ERROR_SYSTEM; return null; } return String.format("%s%03d", preNo, _newCodeNum); } public String GetLatestCode() { String oldCode = ""; OrganizationMajorExample example = new OrganizationMajorExample(); example.setOrderByClause("code desc"); example.setRows(1); example.setOffset(0); List list = mapper.selectByExample(example); if (list != null && list.size() > 0) { OrganizationMajor entity2 = list.get(0); if (entity2 != null) { oldCode = entity2.getCode(); } } return oldCode; } }